GenreItems.java revision 919e1ed7e914029a1a0054237d86dc7b19ced898
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
17package com.android.tv.data;
18
19import android.content.Context;
20import android.media.tv.TvContract.Programs.Genres;
21
22import com.android.tv.R;
23
24public class GenreItems {
25    /**
26     * Genre ID indicating all channels.
27     */
28    public static final int ID_ALL_CHANNELS = 0;
29
30    private static final String[] CANONICAL_GENRES = {
31        null, // All channels
32        Genres.FAMILY_KIDS,
33        Genres.SPORTS,
34        Genres.SHOPPING,
35        Genres.MOVIES,
36        Genres.COMEDY,
37        Genres.TRAVEL,
38        Genres.DRAMA,
39        Genres.EDUCATION,
40        Genres.ANIMAL_WILDLIFE,
41        Genres.NEWS,
42        Genres.GAMING,
43        Genres.ARTS,
44        Genres.ENTERTAINMENT,
45        Genres.LIFE_STYLE,
46        Genres.MUSIC,
47        Genres.PREMIER,
48        Genres.TECH_SCIENCE
49    };
50
51    private GenreItems() { }
52
53    /**
54     * Returns array of all genre labels.
55     */
56    public static String[] getLabels(Context context) {
57        String[] items = context.getResources().getStringArray(R.array.genre_labels);
58        if (items.length != CANONICAL_GENRES.length) {
59            throw new IllegalArgumentException("Genre data mismatch");
60        }
61        return items;
62    }
63
64    /**
65     * Returns the number of genres including all channels.
66     */
67    public static int getGenreCount() {
68        return CANONICAL_GENRES.length;
69    }
70
71    /**
72     * Returns the canonical genre for the given id.
73     * If the id is invalid, {@code null} will be returned instead.
74     */
75    public static String getCanonicalGenre(int id) {
76        if (id < 0 || id >= CANONICAL_GENRES.length) {
77            return null;
78        }
79        return CANONICAL_GENRES[id];
80    }
81
82    /**
83     * Returns id for the given canonical genre.
84     * If the genre is invalid, {@link #ID_ALL_CHANNELS} will be returned instead.
85     */
86    public static int getId(String canonicalGenre) {
87        if (canonicalGenre == null) {
88            return ID_ALL_CHANNELS;
89        }
90        for (int i = 1; i < CANONICAL_GENRES.length; ++i) {
91            if (CANONICAL_GENRES[i].equals(canonicalGenre)) {
92                return i;
93            }
94        }
95        return ID_ALL_CHANNELS;
96    }
97}
98