aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/data/GenreItems.java
blob: b12fd1aaf06a8a12751b48ecf931fc4e866c5365 (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
/*
 * 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.data;

import android.content.Context;
import android.media.tv.TvContract.Programs.Genres;

import com.android.tv.R;

public class GenreItems {
    /**
     * Genre ID indicating all channels.
     */
    public static final int ID_ALL_CHANNELS = 0;

    private static final String[] CANONICAL_GENRES = {
        null, // All channels
        Genres.FAMILY_KIDS,
        Genres.SPORTS,
        Genres.SHOPPING,
        Genres.MOVIES,
        Genres.COMEDY,
        Genres.TRAVEL,
        Genres.DRAMA,
        Genres.EDUCATION,
        Genres.ANIMAL_WILDLIFE,
        Genres.NEWS,
        Genres.GAMING,
        Genres.ARTS,
        Genres.ENTERTAINMENT,
        Genres.LIFE_STYLE,
        Genres.MUSIC,
        Genres.PREMIER,
        Genres.TECH_SCIENCE
    };

    private GenreItems() { }

    /**
     * Returns array of all genre labels.
     */
    public static String[] getLabels(Context context) {
        String[] items = context.getResources().getStringArray(R.array.genre_labels);
        if (items.length != CANONICAL_GENRES.length) {
            throw new IllegalArgumentException("Genre data mismatch");
        }
        return items;
    }

    /**
     * Returns the number of genres including all channels.
     */
    public static int getGenreCount() {
        return CANONICAL_GENRES.length;
    }

    /**
     * Returns the canonical genre for the given id.
     * If the id is invalid, {@code null} will be returned instead.
     */
    public static String getCanonicalGenre(int id) {
        if (id < 0 || id >= CANONICAL_GENRES.length) {
            return null;
        }
        return CANONICAL_GENRES[id];
    }

    /**
     * Returns id for the given canonical genre.
     * If the genre is invalid, {@link #ID_ALL_CHANNELS} will be returned instead.
     */
    public static int getId(String canonicalGenre) {
        if (canonicalGenre == null) {
            return ID_ALL_CHANNELS;
        }
        for (int i = 1; i < CANONICAL_GENRES.length; ++i) {
            if (CANONICAL_GENRES[i].equals(canonicalGenre)) {
                return i;
            }
        }
        return ID_ALL_CHANNELS;
    }
}