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