BaseProgram.java revision 95961816a768da387f0b5523cf4363ace2044089
1/*
2 * Copyright (C) 2016 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.TvContentRating;
21import android.support.annotation.Nullable;
22import android.text.TextUtils;
23import com.android.tv.R;
24import java.util.Comparator;
25
26/**
27 * Base class for {@link com.android.tv.data.Program} and {@link
28 * com.android.tv.dvr.data.RecordedProgram}.
29 */
30public abstract class BaseProgram {
31    /**
32     * Comparator used to compare {@link BaseProgram} according to its season and episodes number.
33     * If a program's season or episode number is null, it will be consider "smaller" than programs
34     * with season or episode numbers.
35     */
36    public static final Comparator<BaseProgram> EPISODE_COMPARATOR = new EpisodeComparator(false);
37
38    /**
39     * Comparator used to compare {@link BaseProgram} according to its season and episodes number
40     * with season numbers in a reversed order. If a program's season or episode number is null, it
41     * will be consider "smaller" than programs with season or episode numbers.
42     */
43    public static final Comparator<BaseProgram> SEASON_REVERSED_EPISODE_COMPARATOR =
44            new EpisodeComparator(true);
45
46    private static class EpisodeComparator implements Comparator<BaseProgram> {
47        private final boolean mReversedSeason;
48
49        EpisodeComparator(boolean reversedSeason) {
50            mReversedSeason = reversedSeason;
51        }
52
53        @Override
54        public int compare(BaseProgram lhs, BaseProgram rhs) {
55            if (lhs == rhs) {
56                return 0;
57            }
58            int seasonNumberCompare = numberCompare(lhs.getSeasonNumber(), rhs.getSeasonNumber());
59            if (seasonNumberCompare != 0) {
60                return mReversedSeason ? -seasonNumberCompare : seasonNumberCompare;
61            } else {
62                return numberCompare(lhs.getEpisodeNumber(), rhs.getEpisodeNumber());
63            }
64        }
65    }
66
67    /** Compares two strings represent season numbers or episode numbers of programs. */
68    public static int numberCompare(String s1, String s2) {
69        if (s1 == s2) {
70            return 0;
71        } else if (s1 == null) {
72            return -1;
73        } else if (s2 == null) {
74            return 1;
75        } else if (s1.equals(s2)) {
76            return 0;
77        }
78        try {
79            return Integer.compare(Integer.parseInt(s1), Integer.parseInt(s2));
80        } catch (NumberFormatException e) {
81            return s1.compareTo(s2);
82        }
83    }
84
85    /** Returns ID of the program. */
86    public abstract long getId();
87
88    /** Returns the title of the program. */
89    public abstract String getTitle();
90
91    /** Returns the episode title. */
92    public abstract String getEpisodeTitle();
93
94    /** Returns the displayed title of the program episode. */
95    public String getEpisodeDisplayTitle(Context context) {
96        if (!TextUtils.isEmpty(getEpisodeNumber())) {
97            String episodeTitle = getEpisodeTitle() == null ? "" : getEpisodeTitle();
98            if (TextUtils.equals(getSeasonNumber(), "0")) {
99                // Do not show "S0: ".
100                return String.format(
101                        context.getResources()
102                                .getString(R.string.display_episode_title_format_no_season_number),
103                        getEpisodeNumber(),
104                        episodeTitle);
105            } else {
106                return String.format(
107                        context.getResources().getString(R.string.display_episode_title_format),
108                        getSeasonNumber(),
109                        getEpisodeNumber(),
110                        episodeTitle);
111            }
112        }
113        return getEpisodeTitle();
114    }
115
116    /** Returns the description of the program. */
117    public abstract String getDescription();
118
119    /** Returns the long description of the program. */
120    public abstract String getLongDescription();
121
122    /** Returns the start time of the program in Milliseconds. */
123    public abstract long getStartTimeUtcMillis();
124
125    /** Returns the end time of the program in Milliseconds. */
126    public abstract long getEndTimeUtcMillis();
127
128    /** Returns the duration of the program in Milliseconds. */
129    public abstract long getDurationMillis();
130
131    /** Returns the series ID. */
132    public abstract String getSeriesId();
133
134    /** Returns the season number. */
135    public abstract String getSeasonNumber();
136
137    /** Returns the episode number. */
138    public abstract String getEpisodeNumber();
139
140    /** Returns URI of the program's poster. */
141    public abstract String getPosterArtUri();
142
143    /** Returns URI of the program's thumbnail. */
144    public abstract String getThumbnailUri();
145
146    /** Returns the array of the ID's of the canonical genres. */
147    public abstract int[] getCanonicalGenreIds();
148
149    /** Returns the array of content ratings. */
150    @Nullable
151    public abstract TvContentRating[] getContentRatings();
152
153    /** Returns channel's ID of the program. */
154    public abstract long getChannelId();
155
156    /** Returns if the program is valid. */
157    public abstract boolean isValid();
158
159    /** Checks whether the program is episodic or not. */
160    public boolean isEpisodic() {
161        return getSeriesId() != null;
162    }
163
164    /** Generates the series ID for the other inputs than the tuner TV input. */
165    public static String generateSeriesId(String packageName, String title) {
166        return packageName + "/" + title;
167    }
168}
169