• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.tv.data;
18 
19 import android.content.Context;
20 import android.media.tv.TvContentRating;
21 import android.support.annotation.Nullable;
22 import android.text.TextUtils;
23 import com.android.tv.R;
24 import 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  */
30 public 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 
EpisodeComparator(boolean reversedSeason)49         EpisodeComparator(boolean reversedSeason) {
50             mReversedSeason = reversedSeason;
51         }
52 
53         @Override
compare(BaseProgram lhs, BaseProgram rhs)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. */
numberCompare(String s1, String s2)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. */
getId()86     public abstract long getId();
87 
88     /** Returns the title of the program. */
getTitle()89     public abstract String getTitle();
90 
91     /** Returns the episode title. */
getEpisodeTitle()92     public abstract String getEpisodeTitle();
93 
94     /** Returns the displayed title of the program episode. */
getEpisodeDisplayTitle(Context context)95     public String getEpisodeDisplayTitle(Context context) {
96         String episodeNumber = getEpisodeNumber();
97         String episodeTitle = getEpisodeTitle();
98         if (!TextUtils.isEmpty(episodeNumber)) {
99             episodeTitle = episodeTitle == null ? "" : episodeTitle;
100             String seasonNumber = getSeasonNumber();
101             if (TextUtils.isEmpty(seasonNumber) || TextUtils.equals(seasonNumber, "0")) {
102                 // Do not show "S0: ".
103                 return context.getResources()
104                         .getString(
105                                 R.string.display_episode_title_format_no_season_number,
106                                 episodeNumber,
107                                 episodeTitle);
108             } else {
109                 return context.getResources()
110                         .getString(
111                                 R.string.display_episode_title_format,
112                                 seasonNumber,
113                                 episodeNumber,
114                                 episodeTitle);
115             }
116         }
117         return episodeTitle;
118     }
119 
120     /**
121      * Returns the content description of the program episode, suitable for being spoken by an
122      * accessibility service.
123      */
getEpisodeContentDescription(Context context)124     public String getEpisodeContentDescription(Context context) {
125         String episodeNumber = getEpisodeNumber();
126         String episodeTitle = getEpisodeTitle();
127         if (!TextUtils.isEmpty(episodeNumber)) {
128             episodeTitle = episodeTitle == null ? "" : episodeTitle;
129             String seasonNumber = getSeasonNumber();
130             if (TextUtils.isEmpty(seasonNumber) || TextUtils.equals(seasonNumber, "0")) {
131                 // Do not list season if it is empty or 0
132                 return context.getResources()
133                         .getString(
134                                 R.string.content_description_episode_format_no_season_number,
135                                 episodeNumber,
136                                 episodeTitle);
137             } else {
138                 return context.getResources()
139                         .getString(
140                                 R.string.content_description_episode_format,
141                                 seasonNumber,
142                                 episodeNumber,
143                                 episodeTitle);
144             }
145         }
146         return episodeTitle;
147     }
148 
149     /** Returns the description of the program. */
getDescription()150     public abstract String getDescription();
151 
152     /** Returns the long description of the program. */
getLongDescription()153     public abstract String getLongDescription();
154 
155     /** Returns the start time of the program in Milliseconds. */
getStartTimeUtcMillis()156     public abstract long getStartTimeUtcMillis();
157 
158     /** Returns the end time of the program in Milliseconds. */
getEndTimeUtcMillis()159     public abstract long getEndTimeUtcMillis();
160 
161     /** Returns the duration of the program in Milliseconds. */
getDurationMillis()162     public abstract long getDurationMillis();
163 
164     /** Returns the series ID. */
getSeriesId()165     public abstract String getSeriesId();
166 
167     /** Returns the season number. */
getSeasonNumber()168     public abstract String getSeasonNumber();
169 
170     /** Returns the episode number. */
getEpisodeNumber()171     public abstract String getEpisodeNumber();
172 
173     /** Returns URI of the program's poster. */
getPosterArtUri()174     public abstract String getPosterArtUri();
175 
176     /** Returns URI of the program's thumbnail. */
getThumbnailUri()177     public abstract String getThumbnailUri();
178 
179     /** Returns the array of the ID's of the canonical genres. */
getCanonicalGenreIds()180     public abstract int[] getCanonicalGenreIds();
181 
182     /** Returns the array of content ratings. */
183     @Nullable
getContentRatings()184     public abstract TvContentRating[] getContentRatings();
185 
186     /** Returns channel's ID of the program. */
getChannelId()187     public abstract long getChannelId();
188 
189     /** Returns if the program is valid. */
isValid()190     public abstract boolean isValid();
191 
192     /** Checks whether the program is episodic or not. */
isEpisodic()193     public boolean isEpisodic() {
194         return getSeriesId() != null;
195     }
196 
197     /** Generates the series ID for the other inputs than the tuner TV input. */
generateSeriesId(String packageName, String title)198     public static String generateSeriesId(String packageName, String title) {
199         return packageName + "/" + title;
200     }
201 }
202