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.annotation.SuppressLint; 20 import android.content.Context; 21 import android.media.tv.TvContract.Programs.Genres; 22 import android.os.Build; 23 24 import com.android.tv.R; 25 26 public class GenreItems { 27 /** 28 * Genre ID indicating all channels. 29 */ 30 public static final int ID_ALL_CHANNELS = 0; 31 32 private static final String[] CANONICAL_GENRES_L = { 33 null, // All channels 34 Genres.FAMILY_KIDS, 35 Genres.SPORTS, 36 Genres.SHOPPING, 37 Genres.MOVIES, 38 Genres.COMEDY, 39 Genres.TRAVEL, 40 Genres.DRAMA, 41 Genres.EDUCATION, 42 Genres.ANIMAL_WILDLIFE, 43 Genres.NEWS, 44 Genres.GAMING 45 }; 46 47 @SuppressLint("InlinedApi") 48 private static final String[] CANONICAL_GENRES_L_MR1 = { 49 null, // All channels 50 Genres.FAMILY_KIDS, 51 Genres.SPORTS, 52 Genres.SHOPPING, 53 Genres.MOVIES, 54 Genres.COMEDY, 55 Genres.TRAVEL, 56 Genres.DRAMA, 57 Genres.EDUCATION, 58 Genres.ANIMAL_WILDLIFE, 59 Genres.NEWS, 60 Genres.GAMING, 61 Genres.ARTS, 62 Genres.ENTERTAINMENT, 63 Genres.LIFE_STYLE, 64 Genres.MUSIC, 65 Genres.PREMIER, 66 Genres.TECH_SCIENCE 67 }; 68 69 private static final String[] CANONICAL_GENRES = createGenres(); 70 createGenres()71 private static String[] createGenres() { 72 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) { 73 return CANONICAL_GENRES_L; 74 } else { 75 return CANONICAL_GENRES_L_MR1; 76 } 77 } 78 GenreItems()79 private GenreItems() { } 80 81 /** 82 * Returns array of all genre labels. 83 */ getLabels(Context context)84 public static String[] getLabels(Context context) { 85 String[] items = Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1 86 ? context.getResources().getStringArray(R.array.genre_labels_l) 87 : context.getResources().getStringArray(R.array.genre_labels_l_mr1); 88 if (items.length != CANONICAL_GENRES.length) { 89 throw new IllegalArgumentException("Genre data mismatch"); 90 } 91 return items; 92 } 93 94 /** 95 * Returns the number of genres including all channels. 96 */ 97 public static int getGenreCount() { 98 return CANONICAL_GENRES.length; 99 } 100 101 /** 102 * Returns the canonical genre for the given id. 103 * If the id is invalid, {@code null} will be returned instead. 104 */ 105 public static String getCanonicalGenre(int id) { 106 if (id < 0 || id >= CANONICAL_GENRES.length) { 107 return null; 108 } 109 return CANONICAL_GENRES[id]; 110 } 111 112 /** 113 * Returns id for the given canonical genre. 114 * If the genre is invalid, {@link #ID_ALL_CHANNELS} will be returned instead. 115 */ 116 public static int getId(String canonicalGenre) { 117 if (canonicalGenre == null) { 118 return ID_ALL_CHANNELS; 119 } 120 for (int i = 1; i < CANONICAL_GENRES.length; ++i) { 121 if (CANONICAL_GENRES[i].equals(canonicalGenre)) { 122 return i; 123 } 124 } 125 return ID_ALL_CHANNELS; 126 } 127 } 128