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