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