Home | History | Annotate | Download | only in util
      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 import java.util.Comparator;
     20 import java.util.List;
     21 
     22 /** Static utilities for {@link TvTrackInfo}. */
     23 public class TvTrackInfoUtils {
     24 
     25     /**
     26      * Compares how closely two {@link android.media.tv.TvTrackInfo}s match {@code language}, {@code
     27      * channelCount} and {@code id} in that precedence.
     28      *
     29      * @param id The track id to match.
     30      * @param language The language to match.
     31      * @param channelCount The channel count to match.
     32      * @return -1 if lhs is a worse match, 0 if lhs and rhs match equally and 1 if lhs is a better
     33      *     match.
     34      */
     35     public static Comparator<TvTrackInfo> createComparator(
     36             final String id, final String language, final int channelCount) {
     37         return new Comparator<TvTrackInfo>() {
     38 
     39             @Override
     40             public int compare(TvTrackInfo lhs, TvTrackInfo rhs) {
     41                 if (lhs == rhs) {
     42                     return 0;
     43                 }
     44                 if (lhs == null) {
     45                     return -1;
     46                 }
     47                 if (rhs == null) {
     48                     return 1;
     49                 }
     50                 // Assumes {@code null} language matches to any language since it means user hasn't
     51                 // selected any track before or selected a track without language information.
     52                 boolean lhsLangMatch =
     53                         language == null || Utils.isEqualLanguage(lhs.getLanguage(), language);
     54                 boolean rhsLangMatch =
     55                         language == null || Utils.isEqualLanguage(rhs.getLanguage(), language);
     56                 if (lhsLangMatch && rhsLangMatch) {
     57                     boolean lhsCountMatch =
     58                             lhs.getType() != TvTrackInfo.TYPE_AUDIO
     59                                     || lhs.getAudioChannelCount() == channelCount;
     60                     boolean rhsCountMatch =
     61                             rhs.getType() != TvTrackInfo.TYPE_AUDIO
     62                                     || rhs.getAudioChannelCount() == channelCount;
     63                     if (lhsCountMatch && rhsCountMatch) {
     64                         return Boolean.compare(lhs.getId().equals(id), rhs.getId().equals(id));
     65                     } else {
     66                         return Boolean.compare(lhsCountMatch, rhsCountMatch);
     67                     }
     68                 } else {
     69                     return Boolean.compare(lhsLangMatch, rhsLangMatch);
     70                 }
     71             }
     72         };
     73     }
     74 
     75     /**
     76      * Selects the best TvTrackInfo available or the first if none matches.
     77      *
     78      * @param tracks The tracks to choose from
     79      * @param id The track id to match.
     80      * @param language The language to match.
     81      * @param channelCount The channel count to match.
     82      * @return the best matching track or the first one if none matches.
     83      */
     84     public static TvTrackInfo getBestTrackInfo(
     85             List<TvTrackInfo> tracks, String id, String language, int channelCount) {
     86         if (tracks == null) {
     87             return null;
     88         }
     89         Comparator<TvTrackInfo> comparator = createComparator(id, language, channelCount);
     90         TvTrackInfo best = null;
     91         for (TvTrackInfo track : tracks) {
     92             if (comparator.compare(track, best) > 0) {
     93                 best = track;
     94             }
     95         }
     96         return best;
     97     }
     98 
     99     private TvTrackInfoUtils() {}
    100 }
    101