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