aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/util/TvTrackInfoUtils.java
blob: c004f00191ad7dd442b0ee7758c753232285f956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tv.util;

import android.media.tv.TvTrackInfo;

import java.util.Comparator;
import java.util.List;

/**
 * Static utilities for {@link TvTrackInfo}.
 */
public class TvTrackInfoUtils {

    /**
     * Compares how closely two {@link android.media.tv.TvTrackInfo}s match {@code language}, {@code
     * channelCount} and {@code id} in that precedence.
     *
     * @param id           The track id to match.
     * @param language     The language to match.
     * @param channelCount The channel count to match.
     * @return -1 if lhs is a worse match, 0 if lhs and rhs match equally and 1 if lhs is a better
     * match.
     */
    public static Comparator<TvTrackInfo> createComparator(final String id, final String language,
            final int channelCount) {
        return new Comparator<TvTrackInfo>() {

            @Override
            public int compare(TvTrackInfo lhs, TvTrackInfo rhs) {
                if (lhs == rhs) {
                    return 0;
                }
                if (lhs == null) {
                    return -1;
                }
                if (rhs == null) {
                    return 1;
                }
                // Assumes {@code null} language matches to any language since it means user hasn't
                // selected any track before or selected a track without language information.
                boolean rhsLangMatch = language == null || Utils.isEqualLanguage(rhs.getLanguage(),
                        language);
                boolean lhsLangMatch = language == null || Utils.isEqualLanguage(lhs.getLanguage(),
                        language);
                if (rhsLangMatch) {
                    if (lhsLangMatch) {
                        boolean rhsCountMatch = rhs.getAudioChannelCount() == channelCount;
                        boolean lhsCountMatch = lhs.getAudioChannelCount() == channelCount;
                        if (rhsCountMatch) {
                            if (lhsCountMatch) {
                                boolean rhsIdMatch = rhs.getId().equals(id);
                                boolean lhsIdMatch = lhs.getId().equals(id);
                                if (rhsIdMatch) {
                                    return lhsIdMatch ? 0 : -1;
                                } else {
                                    return lhsIdMatch ? 1 : 0;
                                }

                            } else {
                                return -1;
                            }
                        } else {
                            return lhsCountMatch ? 1 : 0;
                        }
                    } else {
                        return -1;
                    }
                } else {
                    return lhsLangMatch ? 1 : 0;
                }
            }
        };
    }

    /**
     * Selects the  best TvTrackInfo available or the first if none matches.
     *
     * @param tracks       The tracks to choose from
     * @param id           The track id to match.
     * @param language     The language to match.
     * @param channelCount The channel count to match.
     * @return the best matching track or the first one if none matches.
     */
    public static TvTrackInfo getBestTrackInfo(List<TvTrackInfo> tracks, String id, String language,
            int channelCount) {
        if (tracks == null) {
            return null;
        }
        Comparator<TvTrackInfo> comparator = createComparator(id, language, channelCount);
        TvTrackInfo best = null;
        for (TvTrackInfo track : tracks) {
            if (comparator.compare(track, best) > 0) {
                best = track;
            }
        }
        return best;
    }

    private TvTrackInfoUtils() {
    }
}