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 
     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      */
     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 lhsLangMatch = language == null || Utils.isEqualLanguage(lhs.getLanguage(),
     56                         language);
     57                 boolean rhsLangMatch = language == null || Utils.isEqualLanguage(rhs.getLanguage(),
     58                         language);
     59                 if (lhsLangMatch && rhsLangMatch) {
     60                     boolean lhsCountMatch = lhs.getType() != TvTrackInfo.TYPE_AUDIO
     61                             || lhs.getAudioChannelCount() == channelCount;
     62                     boolean rhsCountMatch = rhs.getType() != TvTrackInfo.TYPE_AUDIO
     63                             || rhs.getAudioChannelCount() == channelCount;
     64                     if (lhsCountMatch && rhsCountMatch) {
     65                         return Boolean.compare(lhs.getId().equals(id), rhs.getId().equals(id));
     66                     } else {
     67                         return Boolean.compare(lhsCountMatch, rhsCountMatch);
     68                     }
     69                 } else {
     70                     return Boolean.compare(lhsLangMatch, rhsLangMatch);
     71                 }
     72             }
     73         };
     74     }
     75 
     76     /**
     77      * Selects the  best TvTrackInfo available or the first if none matches.
     78      *
     79      * @param tracks       The tracks to choose from
     80      * @param id           The track id to match.
     81      * @param language     The language to match.
     82      * @param channelCount The channel count to match.
     83      * @return the best matching track or the first one if none matches.
     84      */
     85     public static TvTrackInfo getBestTrackInfo(List<TvTrackInfo> tracks, String id, String language,
     86             int channelCount) {
     87         if (tracks == null) {
     88             return null;
     89         }
     90         Comparator<TvTrackInfo> comparator = createComparator(id, language, channelCount);
     91         TvTrackInfo best = null;
     92         for (TvTrackInfo track : tracks) {
     93             if (comparator.compare(track, best) > 0) {
     94                 best = track;
     95             }
     96         }
     97         return best;
     98     }
     99 
    100     private TvTrackInfoUtils() {
    101     }
    102 }