• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 // Assumes {@code null} language matches to any language since it means user hasn't
54                 // selected any track before or selected a track without language information.
55                 boolean rhsLangMatch = language == null || Utils.isEqualLanguage(rhs.getLanguage(),
56                         language);
57                 boolean lhsLangMatch = language == null || Utils.isEqualLanguage(lhs.getLanguage(),
58                         language);
59                 if (rhsLangMatch) {
60                     if (lhsLangMatch) {
61                         boolean rhsCountMatch = rhs.getAudioChannelCount() == channelCount;
62                         boolean lhsCountMatch = lhs.getAudioChannelCount() == channelCount;
63                         if (rhsCountMatch) {
64                             if (lhsCountMatch) {
65                                 boolean rhsIdMatch = rhs.getId().equals(id);
66                                 boolean lhsIdMatch = lhs.getId().equals(id);
67                                 if (rhsIdMatch) {
68                                     return lhsIdMatch ? 0 : -1;
69                                 } else {
70                                     return lhsIdMatch ? 1 : 0;
71                                 }
72 
73                             } else {
74                                 return -1;
75                             }
76                         } else {
77                             return lhsCountMatch ? 1 : 0;
78                         }
79                     } else {
80                         return -1;
81                     }
82                 } else {
83                     return lhsLangMatch ? 1 : 0;
84                 }
85             }
86         };
87     }
88 
89     /**
90      * Selects the  best TvTrackInfo available or the first if none matches.
91      *
92      * @param tracks       The tracks to choose from
93      * @param id           The track id to match.
94      * @param language     The language to match.
95      * @param channelCount The channel count to match.
96      * @return the best matching track or the first one if none matches.
97      */
98     public static TvTrackInfo getBestTrackInfo(List<TvTrackInfo> tracks, String id, String language,
99             int channelCount) {
100         if (tracks == null) {
101             return null;
102         }
103         Comparator<TvTrackInfo> comparator = createComparator(id, language, channelCount);
104         TvTrackInfo best = null;
105         for (TvTrackInfo track : tracks) {
106             if (comparator.compare(track, best) > 0) {
107                 best = track;
108             }
109         }
110         return best;
111     }
112 
113     private TvTrackInfoUtils() {
114     }
115 }
116