GenreItems.java revision ba5845f23b8fbc985890f892961abc8b39886611
120c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee/*
2816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * Copyright (C) 2015 The Android Open Source Project
320c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee *
420c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee * Licensed under the Apache License, Version 2.0 (the "License");
520c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee * you may not use this file except in compliance with the License.
620c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee * You may obtain a copy of the License at
720c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee *
820c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee *      http://www.apache.org/licenses/LICENSE-2.0
920c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee *
1020c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee * Unless required by applicable law or agreed to in writing, software
1120c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee * distributed under the License is distributed on an "AS IS" BASIS,
1220c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1320c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee * See the License for the specific language governing permissions and
1420c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee * limitations under the License.
1520c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee */
1620c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
1720c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Leepackage com.android.tv.data;
1820c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
19ba5845f23b8fbc985890f892961abc8b39886611Nick Chalkoimport android.annotation.SuppressLint;
20ba5845f23b8fbc985890f892961abc8b39886611Nick Chalkoimport android.annotation.TargetApi;
2120c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Leeimport android.content.Context;
2220c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Leeimport android.media.tv.TvContract.Programs.Genres;
23ba5845f23b8fbc985890f892961abc8b39886611Nick Chalkoimport android.os.Build;
2420c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
2520c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Leeimport com.android.tv.R;
26ba5845f23b8fbc985890f892961abc8b39886611Nick Chalkoimport com.android.tv.common.CollectionUtils;
2720c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
2820c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Leepublic class GenreItems {
29816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    /**
30816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     * Genre ID indicating all channels.
31816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     */
32816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    public static final int ID_ALL_CHANNELS = 0;
3320c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
34ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko    private static final String[] CANONICAL_GENRES_BASE = {
3520c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        null, // All channels
3620c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.FAMILY_KIDS,
3720c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.SPORTS,
3820c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.SHOPPING,
3920c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.MOVIES,
4020c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.COMEDY,
4120c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.TRAVEL,
4220c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.DRAMA,
4320c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.EDUCATION,
4420c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.ANIMAL_WILDLIFE,
4520c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        Genres.NEWS,
46ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko        Genres.GAMING
4720c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee    };
4820c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
49ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko    @SuppressLint("InlinedApi")
50ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko    private static final String[] CANONICAL_GENRES_ADDED_IN_L_MR1 = {
51ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko            Genres.ARTS,
52ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko            Genres.ENTERTAINMENT,
53ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko            Genres.LIFE_STYLE,
54ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko            Genres.MUSIC,
55ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko            Genres.PREMIER,
56ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko            Genres.TECH_SCIENCE
57ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko    };
58ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko
59ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko    private static final String[] CANONICAL_GENRES = createGenres();
60ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko
61ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko    private static String[] createGenres() {
62ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
63ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko            return CANONICAL_GENRES_BASE;
64ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko        } else {
65ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko            return CollectionUtils
66ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko                    .concatAll(CANONICAL_GENRES_BASE, CANONICAL_GENRES_ADDED_IN_L_MR1);
67ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko        }
68ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko    }
69ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko
7020c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee    private GenreItems() { }
7120c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
72816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    /**
73816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     * Returns array of all genre labels.
74816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     */
75816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    public static String[] getLabels(Context context) {
76816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        String[] items = context.getResources().getStringArray(R.array.genre_labels);
77816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        if (items.length != CANONICAL_GENRES.length) {
78816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko            throw new IllegalArgumentException("Genre data mismatch");
7920c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        }
80816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        return items;
8120c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee    }
8220c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
83816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    /**
84816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     * Returns the number of genres including all channels.
85816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     */
86816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    public static int getGenreCount() {
87816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        return CANONICAL_GENRES.length;
8820c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee    }
8920c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
90816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    /**
91816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     * Returns the canonical genre for the given id.
92816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     * If the id is invalid, {@code null} will be returned instead.
93816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     */
94816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    public static String getCanonicalGenre(int id) {
95816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        if (id < 0 || id >= CANONICAL_GENRES.length) {
96816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko            return null;
9720c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        }
98816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        return CANONICAL_GENRES[id];
9920c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee    }
10020c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee
101816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    /**
102816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     * Returns id for the given canonical genre.
103816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     * If the genre is invalid, {@link #ID_ALL_CHANNELS} will be returned instead.
104816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko     */
105816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    public static int getId(String canonicalGenre) {
10620c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        if (canonicalGenre == null) {
107816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko            return ID_ALL_CHANNELS;
10820c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        }
10920c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        for (int i = 1; i < CANONICAL_GENRES.length; ++i) {
11020c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee            if (CANONICAL_GENRES[i].equals(canonicalGenre)) {
11120c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee                return i;
11220c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee            }
11320c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee        }
114816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        return ID_ALL_CHANNELS;
11520c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee    }
11620c5411cd0e3f26ea78543f9166cecb5d08b9220Chulwoo Lee}
117