1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package com.example.android.tv.channelsprograms.model; 15 16 import android.content.Context; 17 import android.content.SharedPreferences; 18 import android.support.annotation.DrawableRes; 19 import android.support.annotation.Nullable; 20 import android.support.annotation.StringRes; 21 22 import com.example.android.tv.channelsprograms.R; 23 import com.example.android.tv.channelsprograms.util.AppLinkHelper; 24 import com.example.android.tv.channelsprograms.util.SharedPreferencesHelper; 25 26 import java.util.Collections; 27 import java.util.List; 28 29 /** Mock database stores data in {@link SharedPreferences}. */ 30 public final class MockDatabase { 31 MockDatabase()32 private MockDatabase() { 33 // Do nothing. 34 } 35 36 /** 37 * Returns a subscription to mock content representing tv shows. 38 * 39 * @param context used for accessing shared preferences. 40 * @return a subscription with tv show data. 41 */ getTvShowSubscription(Context context)42 public static Subscription getTvShowSubscription(Context context) { 43 44 return findOrCreateSubscription( 45 context, 46 R.string.title_tv_shows, 47 R.string.tv_shows_description, 48 R.drawable.ic_video_library_blue_80dp); 49 } 50 51 /** 52 * Returns a subscription to mock content representing your videos. 53 * 54 * @param context used for accessing shared preferences. 55 * @return a subscription with your video data. 56 */ getVideoSubscription(Context context)57 public static Subscription getVideoSubscription(Context context) { 58 59 return findOrCreateSubscription( 60 context, 61 R.string.your_videos, 62 R.string.your_videos_description, 63 R.drawable.ic_video_library_blue_80dp); 64 } 65 66 /** 67 * Returns a subscription to mock content representing cat videos. 68 * 69 * @param context used for accessing shared preferences. 70 * @return a subscription with cat videos. 71 */ getCatVideosSubscription(Context context)72 public static Subscription getCatVideosSubscription(Context context) { 73 74 return findOrCreateSubscription( 75 context, 76 R.string.cat_videos, 77 R.string.cat_videos_description, 78 R.drawable.ic_movie_blue_80dp); 79 } 80 findOrCreateSubscription( Context context, @StringRes int titleResource, @StringRes int descriptionResource, @DrawableRes int logoResource)81 private static Subscription findOrCreateSubscription( 82 Context context, 83 @StringRes int titleResource, 84 @StringRes int descriptionResource, 85 @DrawableRes int logoResource) { 86 // See if we have already created the channel in the TV Provider. 87 String title = context.getString(titleResource); 88 89 Subscription subscription = findSubscriptionByTitle(context, title); 90 if (subscription != null) { 91 return subscription; 92 } 93 94 return Subscription.createSubscription( 95 title, 96 context.getString(descriptionResource), 97 AppLinkHelper.buildBrowseUri(title).toString(), 98 logoResource); 99 } 100 101 @Nullable findSubscriptionByTitle(Context context, String title)102 private static Subscription findSubscriptionByTitle(Context context, String title) { 103 for (Subscription subscription : getSubscriptions(context)) { 104 if (subscription.getName().equals(title)) { 105 return subscription; 106 } 107 } 108 return null; 109 } 110 111 /** 112 * Overrides the subscriptions stored in {@link SharedPreferences}. 113 * 114 * @param context used for accessing shared preferences. 115 * @param subscriptions stored in shared preferences. 116 */ saveSubscriptions(Context context, List<Subscription> subscriptions)117 public static void saveSubscriptions(Context context, List<Subscription> subscriptions) { 118 SharedPreferencesHelper.storeSubscriptions(context, subscriptions); 119 } 120 121 /** 122 * Adds the subscription to the list of persisted subscriptions in {@link SharedPreferences}. 123 * Will update the persisted subscription if it already exists. 124 * 125 * @param context used for accessing shared preferences. 126 * @param subscription to be saved. 127 */ saveSubscription(Context context, Subscription subscription)128 public static void saveSubscription(Context context, Subscription subscription) { 129 List<Subscription> subscriptions = getSubscriptions(context); 130 int index = findSubscription(subscriptions, subscription); 131 if (index == -1) { 132 subscriptions.add(subscription); 133 } else { 134 subscriptions.set(index, subscription); 135 } 136 saveSubscriptions(context, subscriptions); 137 } 138 findSubscription( List<Subscription> subscriptions, Subscription subscription)139 private static int findSubscription( 140 List<Subscription> subscriptions, Subscription subscription) { 141 for (int index = 0; index < subscriptions.size(); ++index) { 142 Subscription current = subscriptions.get(index); 143 if (current.getName().equals(subscription.getName())) { 144 return index; 145 } 146 } 147 return -1; 148 } 149 150 /** 151 * Returns subscriptions stored in {@link SharedPreferences}. 152 * 153 * @param context used for accessing shared preferences. 154 * @return a list of subscriptions or empty list if none exist. 155 */ getSubscriptions(Context context)156 public static List<Subscription> getSubscriptions(Context context) { 157 return SharedPreferencesHelper.readSubscriptions(context); 158 } 159 160 /** 161 * Finds a subscription given a channel id that the subscription is associated with. 162 * 163 * @param context used for accessing shared preferences. 164 * @param channelId of the channel that the subscription is associated with. 165 * @return a subscription or null if none exist. 166 */ 167 @Nullable findSubscriptionByChannelId(Context context, long channelId)168 public static Subscription findSubscriptionByChannelId(Context context, long channelId) { 169 for (Subscription subscription : getSubscriptions(context)) { 170 if (subscription.getChannelId() == channelId) { 171 return subscription; 172 } 173 } 174 return null; 175 } 176 177 /** 178 * Finds a subscription with the given name. 179 * 180 * @param context used for accessing shared preferences. 181 * @param name of the subscription. 182 * @return a subscription or null if none exist. 183 */ 184 @Nullable findSubscriptionByName(Context context, String name)185 public static Subscription findSubscriptionByName(Context context, String name) { 186 for (Subscription subscription : getSubscriptions(context)) { 187 if (subscription.getName().equals(name)) { 188 return subscription; 189 } 190 } 191 return null; 192 } 193 194 /** 195 * Overrides the movies stored in {@link SharedPreferences} for a given subscription. 196 * 197 * @param context used for accessing shared preferences. 198 * @param channelId of the channel that the movies are associated with. 199 * @param movies to be stored. 200 */ saveMovies(Context context, long channelId, List<Movie> movies)201 public static void saveMovies(Context context, long channelId, List<Movie> movies) { 202 SharedPreferencesHelper.storeMovies(context, channelId, movies); 203 } 204 205 /** 206 * Removes the list of movies associated with a channel. Overrides the current list with an 207 * empty list in {@link SharedPreferences}. 208 * 209 * @param context used for accessing shared preferences. 210 * @param channelId of the channel that the movies are associated with. 211 */ removeMovies(Context context, long channelId)212 public static void removeMovies(Context context, long channelId) { 213 saveMovies(context, channelId, Collections.<Movie>emptyList()); 214 } 215 216 /** 217 * Finds movie in subscriptions with channel id and updates it. Otherwise will add the new movie 218 * to the subscription. 219 * 220 * @param context to access shared preferences. 221 * @param channelId of the subscription that the movie is associated with. 222 * @param movie to be persisted or updated. 223 */ saveMovie(Context context, long channelId, Movie movie)224 public static void saveMovie(Context context, long channelId, Movie movie) { 225 List<Movie> movies = getMovies(context, channelId); 226 int index = findMovie(movies, movie); 227 if (index == -1) { 228 movies.add(movie); 229 } else { 230 movies.set(index, movie); 231 } 232 saveMovies(context, channelId, movies); 233 } 234 findMovie(List<Movie> movies, Movie movie)235 private static int findMovie(List<Movie> movies, Movie movie) { 236 for (int index = 0; index < movies.size(); ++index) { 237 Movie current = movies.get(index); 238 if (current.getId() == movie.getId()) { 239 return index; 240 } 241 } 242 return -1; 243 } 244 245 /** 246 * Returns movies stored in {@link SharedPreferences} for a given subscription. 247 * 248 * @param context used for accessing shared preferences. 249 * @param channelId of the subscription that the movie is associated with. 250 * @return a list of movies for a subscription 251 */ getMovies(Context context, long channelId)252 public static List<Movie> getMovies(Context context, long channelId) { 253 return SharedPreferencesHelper.readMovies(context, channelId); 254 } 255 256 /** 257 * Finds a movie in a subscription by its id. 258 * 259 * @param context to access shared preferences. 260 * @param channelId of the subscription that the movie is associated with. 261 * @param movieId of the movie. 262 * @return a movie or null if none exist. 263 */ 264 @Nullable findMovieById(Context context, long channelId, long movieId)265 public static Movie findMovieById(Context context, long channelId, long movieId) { 266 for (Movie movie : getMovies(context, channelId)) { 267 if (movie.getId() == movieId) { 268 return movie; 269 } 270 } 271 return null; 272 } 273 } 274