1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tv.util;
17 
18 import android.media.tv.TvTrackInfo;
19 
20 import java.util.Comparator;
21 import java.util.List;
22 
23 /**
24  * Static utilities for {@link TvTrackInfo}.
25  */
26 public class TvTrackInfoUtils {
27 
28     /**
29      * Compares how closely two {@link android.media.tv.TvTrackInfo}s match {@code language}, {@code
30      * channelCount} and {@code id} in that precedence.
31      *
32      * @param id           The track id to match.
33      * @param language     The language to match.
34      * @param channelCount The channel count to match.
35      * @return -1 if lhs is a worse match, 0 if lhs and rhs match equally and 1 if lhs is a better
36      * match.
37      */
createComparator(final String id, final String language, final int channelCount)38     public static Comparator<TvTrackInfo> createComparator(final String id, final String language,
39             final int channelCount) {
40         return new Comparator<TvTrackInfo>() {
41 
42             @Override
43             public int compare(TvTrackInfo lhs, TvTrackInfo rhs) {
44                 if (lhs == rhs) {
45                     return 0;
46                 }
47                 if (lhs == null) {
48                     return -1;
49                 }
50                 if (rhs == null) {
51                     return 1;
52                 }
53                 boolean rhsLangMatch = Utils.isEqualLanguage(rhs.getLanguage(), language);
54                 boolean lhsLangMatch = Utils.isEqualLanguage(lhs.getLanguage(), language);
55                 if (rhsLangMatch) {
56                     if (lhsLangMatch) {
57                         boolean rhsCountMatch = rhs.getAudioChannelCount() == channelCount;
58                         boolean lhsCountMatch = lhs.getAudioChannelCount() == channelCount;
59                         if (rhsCountMatch) {
60                             if (lhsCountMatch) {
61                                 boolean rhsIdMatch = rhs.getId().equals(id);
62                                 boolean lhsIdMatch = lhs.getId().equals(id);
63                                 if (rhsIdMatch) {
64                                     return lhsIdMatch ? 0 : -1;
65                                 } else {
66                                     return lhsIdMatch ? 1 : 0;
67                                 }
68 
69                             } else {
70                                 return -1;
71                             }
72                         } else {
73                             return lhsCountMatch ? 1 : 0;
74                         }
75                     } else {
76                         return -1;
77                     }
78                 } else {
79                     return lhsLangMatch ? 1 : 0;
80                 }
81             }
82         };
83     }
84 
85     /**
86      * Selects the  best TvTrackInfo available or the first if none matches.
87      *
88      * @param tracks       The tracks to choose from
89      * @param id           The track id to match.
90      * @param language     The language to match.
91      * @param channelCount The channel count to match.
92      * @return the best matching track or the first one if none matches.
93      */
94     public static TvTrackInfo getBestTrackInfo(List<TvTrackInfo> tracks, String id, String language,
95             int channelCount) {
96         if (tracks == null) {
97             return null;
98         }
99         Comparator<TvTrackInfo> comparator = createComparator(id, language, channelCount);
100         TvTrackInfo best = null;
101         for (TvTrackInfo track : tracks) {
102             if (comparator.compare(track, best) > 0) {
103                 best = track;
104             }
105         }
106         return best;
107     }
108 
109     private TvTrackInfoUtils() {
110     }
111 }
112