Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package com.android.wallpaper.module; |
| 17 | |
| 18 | import android.app.backup.BackupManager; |
| 19 | import android.content.Context; |
| 20 | import android.content.SharedPreferences; |
| 21 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
| 22 | import android.support.annotation.Nullable; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import org.json.JSONArray; |
| 26 | import org.json.JSONException; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Arrays; |
| 30 | import java.util.Calendar; |
| 31 | import java.util.Date; |
| 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * Default implementation that writes to and reads from SharedPreferences. |
| 36 | */ |
| 37 | public class DefaultWallpaperPreferences implements WallpaperPreferences { |
| 38 | public static final String PREFS_NAME = "wallpaper"; |
| 39 | |
| 40 | private static final String TAG = "DefaultWPPreferences"; |
| 41 | |
| 42 | private SharedPreferences mSharedPrefs; |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 43 | private Context mContext; |
| 44 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 45 | // Keep a strong reference to this OnSharedPreferenceChangeListener to prevent the listener from |
| 46 | // being garbage collected because SharedPreferences only holds a weak reference. |
| 47 | private OnSharedPreferenceChangeListener mSharedPrefsChangedListener; |
| 48 | |
| 49 | public DefaultWallpaperPreferences(Context context) { |
| 50 | mSharedPrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 51 | mContext = context.getApplicationContext(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 52 | |
| 53 | // Register a prefs changed listener so that all prefs changes trigger a backup event. |
| 54 | final BackupManager backupManager = new BackupManager(context); |
| 55 | mSharedPrefsChangedListener = new OnSharedPreferenceChangeListener() { |
| 56 | @Override |
| 57 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { |
| 58 | backupManager.dataChanged(); |
| 59 | } |
| 60 | }; |
| 61 | mSharedPrefs.registerOnSharedPreferenceChangeListener(mSharedPrefsChangedListener); |
| 62 | } |
| 63 | |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 64 | private int getResIdPersistedByName(String key, String type) { |
| 65 | String resName = mSharedPrefs.getString(key, null); |
| 66 | if (resName == null) { |
| 67 | return 0; |
| 68 | } |
| 69 | return mContext.getResources().getIdentifier(resName, type, |
| 70 | mContext.getPackageName()); |
| 71 | } |
| 72 | |
| 73 | private void persistResIdByName(String key, int resId) { |
| 74 | String resName = mContext.getResources().getResourceName(resId); |
| 75 | mSharedPrefs.edit().putString(key, resName).apply(); |
| 76 | } |
| 77 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 78 | @Override |
| 79 | public int getWallpaperPresentationMode() { |
| 80 | @PresentationMode |
| 81 | int homeWallpaperPresentationMode = mSharedPrefs.getInt( |
| 82 | WallpaperPreferenceKeys.KEY_WALLPAPER_PRESENTATION_MODE, |
| 83 | WallpaperPreferences.PRESENTATION_MODE_STATIC); |
| 84 | return homeWallpaperPresentationMode; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void setWallpaperPresentationMode(@PresentationMode int presentationMode) { |
| 89 | mSharedPrefs.edit().putInt( |
| 90 | WallpaperPreferenceKeys.KEY_WALLPAPER_PRESENTATION_MODE, presentationMode).apply(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public List<String> getHomeWallpaperAttributions() { |
| 95 | return Arrays.asList( |
| 96 | mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_1, null), |
| 97 | mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_2, null), |
| 98 | mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_3, null)); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void setHomeWallpaperAttributions(List<String> attributions) { |
| 104 | SharedPreferences.Editor editor = mSharedPrefs.edit(); |
| 105 | if (attributions.size() > 0) { |
| 106 | editor.putString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_1, attributions.get(0)); |
| 107 | } |
| 108 | if (attributions.size() > 1) { |
| 109 | editor.putString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_2, attributions.get(1)); |
| 110 | } |
| 111 | if (attributions.size() > 2) { |
| 112 | editor.putString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_3, attributions.get(2)); |
| 113 | } |
| 114 | editor.apply(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | @Nullable |
| 119 | public String getHomeWallpaperActionUrl() { |
| 120 | return mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_URL, null); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void setHomeWallpaperActionUrl(String actionUrl) { |
| 125 | mSharedPrefs.edit().putString( |
| 126 | WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_URL, actionUrl).apply(); |
| 127 | } |
| 128 | |
| 129 | @Override |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 130 | public int getHomeWallpaperActionLabelRes() { |
| 131 | // We need to store and read the resource names as their ids could change from build to |
| 132 | // build and we might end up reading the wrong id |
| 133 | return getResIdPersistedByName(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_LABEL_RES, |
| 134 | "string"); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setHomeWallpaperActionLabelRes(int resId) { |
| 139 | persistResIdByName(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_LABEL_RES, resId); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int getHomeWallpaperActionIconRes() { |
| 144 | return getResIdPersistedByName(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_ICON_RES, |
| 145 | "drawable"); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void setHomeWallpaperActionIconRes(int resId) { |
| 150 | persistResIdByName(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_ICON_RES, resId); |
| 151 | } |
| 152 | |
| 153 | @Override |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 154 | public String getHomeWallpaperBaseImageUrl() { |
| 155 | return mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_BASE_IMAGE_URL, null); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void setHomeWallpaperBaseImageUrl(String baseImageUrl) { |
| 160 | mSharedPrefs.edit().putString( |
| 161 | WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_BASE_IMAGE_URL, baseImageUrl).apply(); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | @Nullable |
| 166 | public String getHomeWallpaperCollectionId() { |
| 167 | return mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_COLLECTION_ID, null); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void setHomeWallpaperCollectionId(String collectionId) { |
| 172 | mSharedPrefs.edit().putString( |
| 173 | WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_COLLECTION_ID, collectionId).apply(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public long getHomeWallpaperHashCode() { |
| 178 | return mSharedPrefs.getLong(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_HASH_CODE, 0); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void setHomeWallpaperHashCode(long hashCode) { |
| 183 | mSharedPrefs.edit().putLong( |
| 184 | WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_HASH_CODE, hashCode).apply(); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void clearHomeWallpaperMetadata() { |
| 189 | mSharedPrefs.edit() |
| 190 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_1) |
| 191 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_2) |
| 192 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_3) |
| 193 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_URL) |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 194 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_LABEL_RES) |
| 195 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_ICON_RES) |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 196 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_BASE_IMAGE_URL) |
| 197 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_HASH_CODE) |
| 198 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_MANAGER_ID) |
| 199 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_PACKAGE_NAME) |
| 200 | .remove(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_REMOTE_ID) |
| 201 | .apply(); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public String getHomeWallpaperPackageName() { |
| 206 | return mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_PACKAGE_NAME, null); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void setHomeWallpaperPackageName(String packageName) { |
| 211 | mSharedPrefs.edit().putString( |
| 212 | WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_PACKAGE_NAME, packageName).apply(); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public int getHomeWallpaperManagerId() { |
| 217 | return mSharedPrefs.getInt(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_MANAGER_ID, 0); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void setHomeWallpaperManagerId(int homeWallpaperId) { |
| 222 | mSharedPrefs.edit().putInt( |
| 223 | WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_MANAGER_ID, homeWallpaperId).apply(); |
| 224 | } |
| 225 | |
| 226 | @Nullable |
| 227 | @Override |
| 228 | public String getHomeWallpaperRemoteId() { |
| 229 | return mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_REMOTE_ID, null); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void setHomeWallpaperRemoteId(@Nullable String wallpaperRemoteId) { |
| 234 | mSharedPrefs.edit().putString( |
| 235 | WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_REMOTE_ID, wallpaperRemoteId).apply(); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public List<String> getLockWallpaperAttributions() { |
| 240 | return Arrays.asList( |
| 241 | mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_1, null), |
| 242 | mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_2, null), |
| 243 | mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_3, null)); |
| 244 | |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void setLockWallpaperAttributions(List<String> attributions) { |
| 249 | SharedPreferences.Editor editor = mSharedPrefs.edit(); |
| 250 | if (attributions.size() > 0) { |
| 251 | editor.putString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_1, attributions.get(0)); |
| 252 | } |
| 253 | if (attributions.size() > 1) { |
| 254 | editor.putString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_2, attributions.get(1)); |
| 255 | } |
| 256 | if (attributions.size() > 2) { |
| 257 | editor.putString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_3, attributions.get(2)); |
| 258 | } |
| 259 | editor.apply(); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | @Nullable |
| 264 | public String getLockWallpaperActionUrl() { |
| 265 | return mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_URL, null); |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void setLockWallpaperActionUrl(String actionUrl) { |
| 270 | mSharedPrefs.edit().putString( |
| 271 | WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_URL, actionUrl).apply(); |
| 272 | } |
| 273 | |
| 274 | @Override |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 275 | public int getLockWallpaperActionLabelRes() { |
| 276 | // We need to store and read the resource names as their ids could change from build to |
| 277 | // build and we might end up reading the wrong id |
| 278 | return getResIdPersistedByName(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_LABEL_RES, |
| 279 | "string"); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void setLockWallpaperActionLabelRes(int resId) { |
| 284 | persistResIdByName(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_LABEL_RES, resId); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public int getLockWallpaperActionIconRes() { |
| 289 | return getResIdPersistedByName(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_ICON_RES, |
| 290 | "drawable"); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public void setLockWallpaperActionIconRes(int resId) { |
| 295 | persistResIdByName(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_ICON_RES, resId); |
| 296 | } |
| 297 | |
| 298 | @Override |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 299 | @Nullable |
| 300 | public String getLockWallpaperCollectionId() { |
| 301 | return mSharedPrefs.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_COLLECTION_ID, null); |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public void setLockWallpaperCollectionId(String collectionId) { |
| 306 | mSharedPrefs.edit().putString( |
| 307 | WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_COLLECTION_ID, collectionId).apply(); |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public int getLockWallpaperId() { |
| 312 | return mSharedPrefs.getInt(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_MANAGER_ID, 0); |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public void setLockWallpaperId(int lockWallpaperId) { |
| 317 | mSharedPrefs.edit().putInt( |
| 318 | WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_MANAGER_ID, lockWallpaperId).apply(); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public long getLockWallpaperHashCode() { |
| 323 | return mSharedPrefs.getLong(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_HASH_CODE, 0); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void setLockWallpaperHashCode(long hashCode) { |
| 328 | mSharedPrefs.edit() |
| 329 | .putLong(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_HASH_CODE, hashCode) |
| 330 | .apply(); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void clearLockWallpaperMetadata() { |
| 335 | mSharedPrefs.edit() |
| 336 | .remove(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_1) |
| 337 | .remove(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_2) |
| 338 | .remove(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_3) |
| 339 | .remove(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_URL) |
Santiago Etchebehere | 1ab7548 | 2018-06-15 15:05:25 -0700 | [diff] [blame] | 340 | .remove(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_LABEL_RES) |
| 341 | .remove(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_ICON_RES) |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 342 | .remove(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_HASH_CODE) |
| 343 | .remove(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_MANAGER_ID) |
| 344 | .apply(); |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void addDailyRotation(long timestamp) { |
| 349 | String jsonString = mSharedPrefs.getString( |
| 350 | WallpaperPreferenceKeys.KEY_DAILY_ROTATION_TIMESTAMPS, "[]"); |
| 351 | try { |
| 352 | JSONArray jsonArray = new JSONArray(jsonString); |
| 353 | jsonArray.put(timestamp); |
| 354 | |
| 355 | mSharedPrefs.edit() |
| 356 | .putString(WallpaperPreferenceKeys.KEY_DAILY_ROTATION_TIMESTAMPS, jsonArray.toString()) |
| 357 | .apply(); |
| 358 | } catch (JSONException e) { |
| 359 | Log.e(TAG, "Failed to add a daily rotation timestamp due to a JSON parse exception"); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public long getLastDailyRotationTimestamp() { |
| 365 | String jsonString = mSharedPrefs.getString( |
| 366 | WallpaperPreferenceKeys.KEY_DAILY_ROTATION_TIMESTAMPS, "[]"); |
| 367 | |
| 368 | try { |
| 369 | JSONArray jsonArray = new JSONArray(jsonString); |
| 370 | |
| 371 | if (jsonArray.length() == 0) { |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | return jsonArray.getLong(jsonArray.length() - 1); |
| 376 | } catch (JSONException e) { |
| 377 | Log.e(TAG, "Failed to find a daily rotation timestamp due to a JSON parse exception"); |
| 378 | return -1; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | @Nullable |
| 384 | public List<Long> getDailyRotationsInLastWeek() { |
| 385 | long enabledTimestamp = getDailyWallpaperEnabledTimestamp(); |
| 386 | |
| 387 | Calendar oneWeekAgo = Calendar.getInstance(); |
| 388 | oneWeekAgo.setTime(new Date()); |
| 389 | oneWeekAgo.add(Calendar.WEEK_OF_YEAR, -1); |
| 390 | long oneWeekAgoTimestamp = oneWeekAgo.getTimeInMillis(); |
| 391 | |
| 392 | // Return null if daily rotation wasn't enabled (timestamp value of -1) or was enabled earlier |
| 393 | // than one week ago. |
| 394 | if (enabledTimestamp == -1 || enabledTimestamp > oneWeekAgoTimestamp) { |
| 395 | return null; |
| 396 | } |
| 397 | |
| 398 | List<Long> timestamps = new ArrayList<>(); |
| 399 | String jsonString = mSharedPrefs.getString( |
| 400 | WallpaperPreferenceKeys.KEY_DAILY_ROTATION_TIMESTAMPS, "[]"); |
| 401 | |
| 402 | try { |
| 403 | JSONArray jsonArray = new JSONArray(jsonString); |
| 404 | |
| 405 | // Before recording the new daily rotation timestamp, filter out any that are older than |
| 406 | // 1 week old. |
| 407 | for (int i = 0; i < jsonArray.length(); i++) { |
| 408 | long existingTimestamp = jsonArray.getLong(i); |
| 409 | if (existingTimestamp >= oneWeekAgoTimestamp) { |
| 410 | timestamps.add(existingTimestamp); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | jsonArray = new JSONArray(timestamps); |
| 415 | mSharedPrefs.edit() |
| 416 | .putString(WallpaperPreferenceKeys.KEY_DAILY_ROTATION_TIMESTAMPS, jsonArray.toString()) |
| 417 | .apply(); |
| 418 | } catch (JSONException e) { |
| 419 | Log.e(TAG, "Failed to get daily rotation timestamps due to a JSON parse exception"); |
| 420 | } |
| 421 | |
| 422 | return timestamps; |
| 423 | } |
| 424 | |
| 425 | @Nullable |
| 426 | @Override |
| 427 | public List<Long> getDailyRotationsPreviousDay() { |
| 428 | long enabledTimestamp = getDailyWallpaperEnabledTimestamp(); |
| 429 | |
| 430 | Calendar midnightYesterday = Calendar.getInstance(); |
| 431 | midnightYesterday.set(Calendar.AM_PM, Calendar.AM); |
| 432 | midnightYesterday.set(Calendar.HOUR, 0); |
| 433 | midnightYesterday.set(Calendar.MINUTE, 0); |
| 434 | midnightYesterday.add(Calendar.DATE, -1); |
| 435 | long midnightYesterdayTimestamp = midnightYesterday.getTimeInMillis(); |
| 436 | |
| 437 | Calendar midnightToday = Calendar.getInstance(); |
| 438 | midnightToday.set(Calendar.AM_PM, Calendar.AM); |
| 439 | midnightToday.set(Calendar.HOUR, 0); |
| 440 | midnightToday.set(Calendar.MINUTE, 0); |
| 441 | long midnightTodayTimestamp = midnightToday.getTimeInMillis(); |
| 442 | |
| 443 | // Return null if daily rotation wasn't enabled (timestamp value of -1) or was enabled earlier |
| 444 | // than midnight yesterday. |
| 445 | if (enabledTimestamp == -1 || enabledTimestamp > midnightYesterdayTimestamp) { |
| 446 | return null; |
| 447 | } |
| 448 | |
| 449 | List<Long> timestamps = new ArrayList<>(); |
| 450 | String jsonString = mSharedPrefs.getString( |
| 451 | WallpaperPreferenceKeys.KEY_DAILY_ROTATION_TIMESTAMPS, "[]"); |
| 452 | |
| 453 | try { |
| 454 | JSONArray jsonArray = new JSONArray(jsonString); |
| 455 | |
| 456 | // Filter the timestamps (which cover up to one week of data) to only include those between |
| 457 | // midnight yesterday and midnight today. |
| 458 | for (int i = 0; i < jsonArray.length(); i++) { |
| 459 | long timestamp = jsonArray.getLong(i); |
| 460 | if (timestamp >= midnightYesterdayTimestamp && timestamp < midnightTodayTimestamp) { |
| 461 | timestamps.add(timestamp); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | } catch (JSONException e) { |
| 466 | Log.e(TAG, "Failed to get daily rotation timestamps due to a JSON parse exception"); |
| 467 | } |
| 468 | |
| 469 | return timestamps; |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public long getDailyWallpaperEnabledTimestamp() { |
| 474 | return mSharedPrefs.getLong(WallpaperPreferenceKeys.KEY_DAILY_WALLPAPER_ENABLED_TIMESTAMP, -1); |
| 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public void setDailyWallpaperEnabledTimestamp(long timestamp) { |
| 479 | mSharedPrefs.edit() |
| 480 | .putLong(WallpaperPreferenceKeys.KEY_DAILY_WALLPAPER_ENABLED_TIMESTAMP, timestamp) |
| 481 | .apply(); |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public void clearDailyRotations() { |
| 486 | mSharedPrefs.edit() |
| 487 | .remove(WallpaperPreferenceKeys.KEY_DAILY_ROTATION_TIMESTAMPS) |
| 488 | .remove(WallpaperPreferenceKeys.KEY_DAILY_WALLPAPER_ENABLED_TIMESTAMP) |
| 489 | .apply(); |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public long getLastDailyLogTimestamp() { |
| 494 | return mSharedPrefs.getLong(WallpaperPreferenceKeys.KEY_LAST_DAILY_LOG_TIMESTAMP, 0); |
| 495 | } |
| 496 | |
| 497 | @Override |
| 498 | public void setLastDailyLogTimestamp(long timestamp) { |
| 499 | mSharedPrefs.edit() |
| 500 | .putLong(WallpaperPreferenceKeys.KEY_LAST_DAILY_LOG_TIMESTAMP, timestamp) |
| 501 | .apply(); |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public long getLastAppActiveTimestamp() { |
| 506 | return mSharedPrefs.getLong(WallpaperPreferenceKeys.KEY_LAST_APP_ACTIVE_TIMESTAMP, 0); |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public void setLastAppActiveTimestamp(long timestamp) { |
| 511 | mSharedPrefs.edit() |
| 512 | .putLong(WallpaperPreferenceKeys.KEY_LAST_APP_ACTIVE_TIMESTAMP, timestamp) |
| 513 | .apply(); |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public void setDailyWallpaperRotationStatus(int status, long timestamp) { |
| 518 | mSharedPrefs.edit() |
| 519 | .putInt(WallpaperPreferenceKeys.KEY_LAST_ROTATION_STATUS, status) |
| 520 | .putLong(WallpaperPreferenceKeys.KEY_LAST_ROTATION_STATUS_TIMESTAMP, timestamp) |
| 521 | .apply(); |
| 522 | } |
| 523 | |
| 524 | @Override |
| 525 | public int getDailyWallpaperLastRotationStatus() { |
| 526 | return mSharedPrefs.getInt(WallpaperPreferenceKeys.KEY_LAST_ROTATION_STATUS, -1); |
| 527 | } |
| 528 | |
| 529 | @Override |
| 530 | public long getDailyWallpaperLastRotationStatusTimestamp() { |
| 531 | return mSharedPrefs.getLong(WallpaperPreferenceKeys.KEY_LAST_ROTATION_STATUS_TIMESTAMP, 0); |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public long getLastSyncTimestamp() { |
| 536 | return mSharedPrefs.getLong(WallpaperPreferenceKeys.KEY_LAST_SYNC_TIMESTAMP, 0); |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public void setLastSyncTimestamp(long timestamp) { |
| 541 | // Write synchronously via commit() to ensure this timetsamp gets written to disk immediately. |
| 542 | mSharedPrefs.edit() |
| 543 | .putLong(WallpaperPreferenceKeys.KEY_LAST_SYNC_TIMESTAMP, timestamp) |
| 544 | .commit(); |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public void setPendingWallpaperSetStatusSync(@PendingWallpaperSetStatus int setStatus) { |
| 549 | mSharedPrefs.edit() |
| 550 | .putInt(WallpaperPreferenceKeys.KEY_PENDING_WALLPAPER_SET_STATUS, setStatus) |
| 551 | .commit(); |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public int getPendingWallpaperSetStatus() { |
| 556 | //noinspection ResourceType |
| 557 | return mSharedPrefs.getInt( |
| 558 | WallpaperPreferenceKeys.KEY_PENDING_WALLPAPER_SET_STATUS, WALLPAPER_SET_NOT_PENDING); |
| 559 | } |
| 560 | |
| 561 | @Override |
| 562 | public void setPendingWallpaperSetStatus(@PendingWallpaperSetStatus int setStatus) { |
| 563 | mSharedPrefs.edit() |
| 564 | .putInt(WallpaperPreferenceKeys.KEY_PENDING_WALLPAPER_SET_STATUS, setStatus) |
| 565 | .apply(); |
| 566 | } |
| 567 | |
| 568 | @Override |
| 569 | public void setPendingDailyWallpaperUpdateStatusSync( |
| 570 | @PendingDailyWallpaperUpdateStatus int updateStatus) { |
| 571 | mSharedPrefs.edit() |
| 572 | .putInt(WallpaperPreferenceKeys.KEY_PENDING_DAILY_WALLPAPER_UPDATE_STATUS, updateStatus) |
| 573 | .commit(); |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public int getPendingDailyWallpaperUpdateStatus() { |
| 578 | //noinspection ResourceType |
| 579 | return mSharedPrefs.getInt(WallpaperPreferenceKeys.KEY_PENDING_DAILY_WALLPAPER_UPDATE_STATUS, |
| 580 | DAILY_WALLPAPER_UPDATE_NOT_PENDING); |
| 581 | } |
| 582 | |
| 583 | @Override |
| 584 | public void setPendingDailyWallpaperUpdateStatus( |
| 585 | @PendingDailyWallpaperUpdateStatus int updateStatus) { |
| 586 | mSharedPrefs.edit() |
| 587 | .putInt(WallpaperPreferenceKeys.KEY_PENDING_DAILY_WALLPAPER_UPDATE_STATUS, updateStatus) |
| 588 | .apply(); |
| 589 | } |
| 590 | |
| 591 | @Override |
| 592 | public void incrementNumDaysDailyRotationFailed() { |
| 593 | mSharedPrefs.edit() |
| 594 | .putInt(WallpaperPreferenceKeys.KEY_NUM_DAYS_DAILY_ROTATION_FAILED, |
| 595 | getNumDaysDailyRotationFailed() + 1) |
| 596 | .apply(); |
| 597 | } |
| 598 | |
| 599 | @Override |
| 600 | public int getNumDaysDailyRotationFailed() { |
| 601 | return mSharedPrefs.getInt(WallpaperPreferenceKeys.KEY_NUM_DAYS_DAILY_ROTATION_FAILED, 0); |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public void resetNumDaysDailyRotationFailed() { |
| 606 | mSharedPrefs.edit() |
| 607 | .putInt(WallpaperPreferenceKeys.KEY_NUM_DAYS_DAILY_ROTATION_FAILED, 0) |
| 608 | .apply(); |
| 609 | } |
| 610 | |
| 611 | @Override |
| 612 | public void incrementNumDaysDailyRotationNotAttempted() { |
| 613 | mSharedPrefs.edit() |
| 614 | .putInt(WallpaperPreferenceKeys.KEY_NUM_DAYS_DAILY_ROTATION_NOT_ATTEMPTED, |
| 615 | getNumDaysDailyRotationNotAttempted() + 1) |
| 616 | .apply(); |
| 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public int getNumDaysDailyRotationNotAttempted() { |
| 621 | return mSharedPrefs.getInt(WallpaperPreferenceKeys.KEY_NUM_DAYS_DAILY_ROTATION_NOT_ATTEMPTED, 0); |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public void resetNumDaysDailyRotationNotAttempted() { |
| 626 | mSharedPrefs.edit() |
| 627 | .putInt(WallpaperPreferenceKeys.KEY_NUM_DAYS_DAILY_ROTATION_NOT_ATTEMPTED, 0) |
| 628 | .apply(); |
| 629 | } |
| 630 | } |