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.WallpaperColors 19 import android.app.WallpaperManager.SetWallpaperFlags 20 import android.graphics.Bitmap 21 import android.graphics.Point 22 import android.graphics.Rect 23 import android.net.Uri 24 import android.text.TextUtils 25 import androidx.annotation.IntDef 26 import com.android.wallpaper.model.LiveWallpaperInfo 27 import com.android.wallpaper.model.LiveWallpaperPrefMetadata 28 import com.android.wallpaper.model.StaticWallpaperPrefMetadata 29 import com.android.wallpaper.model.WallpaperInfo 30 import com.android.wallpaper.picker.customization.shared.model.WallpaperDestination 31 import com.android.wallpaper.picker.data.WallpaperModel.LiveWallpaperModel 32 import com.android.wallpaper.picker.data.WallpaperModel.StaticWallpaperModel 33 34 /** Interface for persisting and retrieving wallpaper specific preferences. */ 35 interface WallpaperPreferences { 36 37 /** Returns the wallpaper presentation mode. */ getWallpaperPresentationModenull38 @PresentationMode fun getWallpaperPresentationMode(): Int 39 40 /** Sets the presentation mode of the current wallpaper. */ 41 fun setWallpaperPresentationMode(@PresentationMode presentationMode: Int) 42 43 /** Returns the home attributions as a list. */ 44 fun getHomeWallpaperAttributions(): List<String?>? 45 46 /** 47 * Sets the attributions for the current home wallpaper. Clears existing attributions if any 48 * exist. 49 */ 50 fun setHomeWallpaperAttributions(attributions: List<String?>?) 51 52 /** Returns the home wallpaper's action URL or null if there is none. */ 53 fun getHomeWallpaperActionUrl(): String? 54 55 /** Sets the home wallpaper's action URL. */ 56 fun setHomeWallpaperActionUrl(actionUrl: String?) 57 58 /** Returns the home wallpaper's collection ID or null if there is none. */ 59 fun getHomeWallpaperCollectionId(): String? 60 61 /** Sets the home wallpaper's collection ID. */ 62 fun setHomeWallpaperCollectionId(collectionId: String?) 63 64 /** Removes all home metadata from SharedPreferences. */ 65 fun clearHomeWallpaperMetadata() 66 67 /** Set homescreen static image wallpaper metadata to SharedPreferences. */ 68 fun setHomeStaticImageWallpaperMetadata(metadata: StaticWallpaperPrefMetadata) 69 70 /** Set homescreen live wallpaper metadata to SharedPreferences. */ 71 fun setHomeLiveWallpaperMetadata(metadata: LiveWallpaperPrefMetadata) 72 73 /** Returns the home wallpaper's bitmap hash code or 0 if there is none. */ 74 fun getHomeWallpaperHashCode(): Long 75 76 /** Sets the home wallpaper's bitmap hash code if it is an individual image. */ 77 fun setHomeWallpaperHashCode(hashCode: Long) 78 79 /** Gets the home wallpaper's service name, which is present for live wallpapers. */ 80 fun getHomeWallpaperServiceName(): String? 81 82 /** Sets the home wallpaper's service name, which is present for live wallpapers. */ 83 fun setHomeWallpaperServiceName(serviceName: String?) 84 85 /** 86 * Gets the home wallpaper's ID, which is provided by WallpaperManager for static wallpapers. 87 */ 88 fun getHomeWallpaperManagerId(): Int 89 90 /** 91 * Sets the home wallpaper's ID, which is provided by WallpaperManager for static wallpapers. 92 */ 93 fun setHomeWallpaperManagerId(homeWallpaperId: Int) 94 95 /** Gets the home wallpaper's remote identifier. */ 96 fun getHomeWallpaperRemoteId(): String? 97 98 /** 99 * Sets the home wallpaper's remote identifier to SharedPreferences. This should be a string 100 * which uniquely identifies the currently set home wallpaper in the context of a remote 101 * wallpaper collection. 102 */ 103 fun setHomeWallpaperRemoteId(wallpaperRemoteId: String?) 104 105 /** Gets the home wallpaper's identifier used to index into the list of recent wallpapers. */ 106 fun getHomeWallpaperRecentsKey(): String? 107 108 /** Sets the home wallpaper's identifier used to index into the list of recent wallpapers. */ 109 fun setHomeWallpaperRecentsKey(recentsKey: String?) 110 111 /** Gets the home wallpaper's effects. */ 112 fun getHomeWallpaperEffects(): String? 113 114 /** Sets the home wallpaper's effects to SharedPreferences. */ 115 fun setHomeWallpaperEffects(wallpaperEffects: String?) 116 117 /** Gets the home wallpaper's image uri. */ 118 fun getHomeWallpaperImageUri(): Uri? 119 120 /** Returns the lock screen attributions as a list. */ 121 fun getLockWallpaperAttributions(): List<String?>? 122 123 /** 124 * Sets the attributions for the current lock screen wallpaper. Clears existing attributions if 125 * any exist. 126 */ 127 fun setLockWallpaperAttributions(attributions: List<String?>?) 128 129 /** Returns the lock wallpaper's action URL or null if there is none. */ 130 fun getLockWallpaperActionUrl(): String? 131 132 /** Sets the lock wallpaper's action URL. */ 133 fun setLockWallpaperActionUrl(actionUrl: String?) 134 135 /** Returns the lock wallpaper's collection ID or null if there is none. */ 136 fun getLockWallpaperCollectionId(): String? 137 138 /** Sets the lock wallpaper's collection ID. */ 139 fun setLockWallpaperCollectionId(collectionId: String?) 140 141 /** Removes all lock screen metadata from SharedPreferences. */ 142 fun clearLockWallpaperMetadata() 143 144 /** Set lockscreen static image wallpaper metadata to SharedPreferences. */ 145 fun setLockStaticImageWallpaperMetadata(metadata: StaticWallpaperPrefMetadata) 146 147 /** Set lockscreen live wallpaper metadata to SharedPreferences. */ 148 fun setLockLiveWallpaperMetadata(metadata: LiveWallpaperPrefMetadata) 149 150 /** Returns the lock screen wallpaper's bitmap hash code or 0 if there is none. */ 151 fun getLockWallpaperHashCode(): Long 152 153 /** Sets the lock screen wallpaper's bitmap hash code if it is an individual image. */ 154 fun setLockWallpaperHashCode(hashCode: Long) 155 156 /** Gets the lock wallpaper's service name, which is present for live wallpapers. */ 157 fun getLockWallpaperServiceName(): String? 158 159 /** Sets the lock wallpaper's service name, which is present for live wallpapers. */ 160 fun setLockWallpaperServiceName(serviceName: String?) 161 162 /** 163 * Gets the lock wallpaper's ID, which is provided by WallpaperManager for static wallpapers. 164 */ 165 fun getLockWallpaperManagerId(): Int 166 167 /** 168 * Sets the lock wallpaper's ID, which is provided by WallpaperManager for static wallpapers. 169 */ 170 fun setLockWallpaperManagerId(lockWallpaperId: Int) 171 172 /** Gets the lock wallpaper's remote identifier. */ 173 fun getLockWallpaperRemoteId(): String? 174 175 /** 176 * Sets the lock wallpaper's remote identifier to SharedPreferences. This should be a string 177 * which uniquely identifies the currently set lock wallpaper in the context of a remote 178 * wallpaper collection. 179 */ 180 fun setLockWallpaperRemoteId(wallpaperRemoteId: String?) 181 182 /** Gets lock home wallpaper's identifier used to index into the list of recent wallpapers. */ 183 fun getLockWallpaperRecentsKey(): String? 184 185 /** Sets lock home wallpaper's identifier used to index into the list of recent wallpapers. */ 186 fun setLockWallpaperRecentsKey(recentsKey: String?) 187 188 /** Gets the lock wallpaper's effects. */ 189 // TODO (b/307939748): Log lock wallpaper effects. We need this function for snapshot logging 190 fun getLockWallpaperEffects(): String? 191 192 /** Sets the lock wallpaper's effects to SharedPreferences. */ 193 fun setLockWallpaperEffects(wallpaperEffects: String?) 194 195 /** Gets the lock wallpaper's image uri. */ 196 fun getLockWallpaperImageUri(): Uri? 197 198 /** Persists the timestamp of a daily wallpaper rotation that just occurred. */ 199 fun addDailyRotation(timestamp: Long) 200 201 /** 202 * Returns the timestamp of the last wallpaper daily rotation or -1 if there has never been a 203 * daily wallpaper rotation on the user's device. 204 */ 205 fun getLastDailyRotationTimestamp(): Long 206 207 /** 208 * Returns the daily wallpaper enabled timestamp in milliseconds since Unix epoch, or -1 if 209 * daily wallpaper is not currently enabled. 210 */ 211 fun getDailyWallpaperEnabledTimestamp(): Long 212 213 /** 214 * Persists the timestamp when daily wallpaper feature was last enabled. 215 * 216 * @param timestamp Milliseconds since Unix epoch. 217 */ 218 fun setDailyWallpaperEnabledTimestamp(timestamp: Long) 219 220 /** 221 * Clears the persisted daily rotation timestamps and the "daily wallpaper enabled" timestamp. 222 * Called if daily rotation is disabled. 223 */ 224 fun clearDailyRotations() 225 226 /** 227 * Returns the timestamp of the most recent daily logging event, in milliseconds since Unix 228 * epoch. Returns -1 if the very first daily logging event has not occurred yet. 229 */ 230 fun getLastDailyLogTimestamp(): Long 231 232 /** 233 * Sets the timestamp of the most recent daily logging event. 234 * 235 * @param timestamp Milliseconds since Unix epoch. 236 */ 237 fun setLastDailyLogTimestamp(timestamp: Long) 238 239 /** 240 * Returns the timestamp of the last time the app was noted to be active; i.e. the last time an 241 * activity entered the foreground (milliseconds since Unix epoch). 242 */ 243 fun getLastAppActiveTimestamp(): Long 244 245 /** 246 * Sets the timestamp of the last time the app was noted to be active; i.e. the last time an 247 * activity entered the foreground. 248 * 249 * @param timestamp Milliseconds since Unix epoch. 250 */ 251 fun setLastAppActiveTimestamp(timestamp: Long) 252 253 /** 254 * Sets the last rotation status for daily wallpapers with a timestamp. 255 * 256 * @param status Last status code of daily rotation. 257 * @param timestamp Milliseconds since Unix epoch. 258 */ 259 fun setDailyWallpaperRotationStatus(status: Int, timestamp: Long) 260 261 /** 262 * Sets the status of whether a wallpaper is currently pending being set (i.e., user tapped the 263 * UI to set a wallpaper but it has not yet been actually set on the device). Does so in a 264 * synchronous manner so a caller may be assured that the underlying store has been updated when 265 * this method returns. 266 */ 267 fun setPendingWallpaperSetStatusSync(@PendingWallpaperSetStatus setStatus: Int) 268 269 /** Gets the status of whether a wallpaper is currently pending being set. */ 270 @PendingWallpaperSetStatus fun getPendingWallpaperSetStatus(): Int 271 272 /** 273 * Sets the status of whether a wallpaper is currently pending being set (i.e., user tapped the 274 * UI to set a wallpaper but it has not yet been actually set on the device). Does so in an 275 * asynchronous manner so writing the preference to the underlying store doesn't block the 276 * calling thread. 277 */ 278 fun setPendingWallpaperSetStatus(@PendingWallpaperSetStatus setStatus: Int) 279 280 /** 281 * Sets whether a daily wallpaper update is pending. Writes status to memory and also to disk 282 * before returning. 283 */ 284 fun setPendingDailyWallpaperUpdateStatusSync( 285 @PendingDailyWallpaperUpdateStatus updateStatus: Int 286 ) 287 288 /** Returns whether a daily wallpaper update is pending. */ 289 @PendingDailyWallpaperUpdateStatus fun getPendingDailyWallpaperUpdateStatus(): Int 290 291 /** 292 * Sets whether a daily wallpaper update is pending. Writes status to memory immediately and to 293 * disk after returning. 294 */ 295 fun setPendingDailyWallpaperUpdateStatus(@PendingDailyWallpaperUpdateStatus updateStatus: Int) 296 297 /** Return the count of wallpaper picker launch. */ 298 fun getAppLaunchCount(): Int 299 300 /** Return the date for the first time to launch wallpaper picker. */ 301 fun getFirstLaunchDateSinceSetup(): Int 302 303 /** Increments the number of wallpaper picker launch. */ 304 fun incrementAppLaunched() 305 306 /** Returns the date for the first time to apply a wallpaper. */ 307 fun getFirstWallpaperApplyDateSinceSetup(): Int 308 309 /** 310 * Sets wallpapers colors of wallpaper's id. 311 * 312 * @param storedWallpaperId wallpaper id. 313 * @param wallpaperColors Colors extracted from an image via quantization. 314 */ 315 fun storeWallpaperColors(storedWallpaperId: String?, wallpaperColors: WallpaperColors?) 316 317 /** 318 * Returns the wallpaper colors from wallpaper's id. 319 * 320 * @param storedWallpaperId wallpaper id. 321 */ 322 fun getWallpaperColors(storedWallpaperId: String): WallpaperColors? 323 324 /** 325 * Update currently set daily wallpaper info. 326 * 327 * @param destination The wallpaper destination, 1: home, 2: lockscreen, 3: both. 328 * @param collectionId wallpaper category. 329 * @param wallpaperId wallpaper id. 330 */ 331 fun updateDailyWallpaperSet( 332 @WallpaperPersister.Destination destination: Int, 333 collectionId: String?, 334 wallpaperId: String?, 335 ) 336 337 /** 338 * Stores the given live wallpaper in the recent wallpapers list 339 * 340 * @param which flag indicating the wallpaper destination 341 * @param wallpaperId unique identifier for this wallpaper 342 * @param wallpaper [LiveWallpaperInfo] for the applied wallpaper 343 * @param colors WallpaperColors to be used as placeholder for quickswitching 344 */ 345 fun storeLatestWallpaper( 346 @SetWallpaperFlags which: Int, 347 wallpaperId: String, 348 wallpaper: LiveWallpaperInfo, 349 colors: WallpaperColors, 350 ) 351 352 /** 353 * Stores the given static wallpaper data in the recent wallpapers list. 354 * 355 * @param which flag indicating the wallpaper destination 356 * @param wallpaperId unique identifier for this wallpaper 357 * @param wallpaper [WallpaperInfo] for the applied wallpaper 358 * @param croppedWallpaperBitmap wallpaper bitmap exactly as applied to WallaperManager 359 * @param colors WallpaperColors to be used as placeholder for quickswitching 360 */ 361 fun storeLatestWallpaper( 362 @SetWallpaperFlags which: Int, 363 wallpaperId: String, 364 wallpaper: WallpaperInfo, 365 croppedWallpaperBitmap: Bitmap, 366 colors: WallpaperColors, 367 ) 368 369 /** 370 * Stores the given static wallpaper data in the recent wallpapers list. 371 * 372 * @param which flag indicating the wallpaper destination 373 * @param wallpaperId unique identifier for this wallpaper 374 * @param attributions List of attribution items. 375 * @param actionUrl The action or "explore" URL for the wallpaper. 376 * @param collectionId identifier of this wallpaper's collection. 377 * @param croppedWallpaperBitmap wallpaper bitmap exactly as applied to WallaperManager 378 * @param colors [WallpaperColors] to be used as placeholder for quickswitching 379 */ 380 fun storeLatestWallpaper( 381 @SetWallpaperFlags which: Int, 382 wallpaperId: String, 383 attributions: List<String>?, 384 actionUrl: String?, 385 collectionId: String?, 386 croppedWallpaperBitmap: Bitmap, 387 colors: WallpaperColors, 388 ) 389 390 /** 391 * Add a static wallpaper to recent wallpapers as json array, saved in preferences. 392 * 393 * @param destination destination where the wallpaper is set to 394 * @param wallpaperModel static wallpaper model 395 * @param bitmap full sie bitmap of the static wallpaper 396 * @param cropHints crop hints of the static wallpaper 397 */ 398 suspend fun addStaticWallpaperToRecentWallpapers( 399 destination: WallpaperDestination, 400 wallpaperModel: StaticWallpaperModel, 401 bitmap: Bitmap, 402 cropHints: Map<Point, Rect>?, 403 ) 404 405 /** 406 * Add a live wallpaper to recent wallpapers as json array, saved in preferences. 407 * 408 * @param destination destination where the wallpaper is set to 409 * @param wallpaperModel live wallpaper model 410 */ 411 suspend fun addLiveWallpaperToRecentWallpapers( 412 destination: WallpaperDestination, 413 wallpaperModel: LiveWallpaperModel, 414 ) 415 416 /** Sets whether the preview tooltip should be shown. */ 417 fun setHasSmallPreviewTooltipBeenShown(hasTooltipBeenShown: Boolean) 418 419 /** Gets whether the preview tooltip should be shown. */ 420 fun getHasSmallPreviewTooltipBeenShown(): Boolean 421 422 /** Sets whether the preview tooltip should be shown. */ 423 fun setHasFullPreviewTooltipBeenShown(hasTooltipBeenShown: Boolean) 424 425 /** Gets whether the preview tooltip should be shown. */ 426 fun getHasFullPreviewTooltipBeenShown(): Boolean 427 428 /** The possible wallpaper presentation modes, i.e., either "static" or "rotating". */ 429 @IntDef(PRESENTATION_MODE_STATIC, PRESENTATION_MODE_ROTATING) annotation class PresentationMode 430 431 /** Possible status of whether a wallpaper set operation is pending or not. */ 432 @IntDef(WALLPAPER_SET_NOT_PENDING, WALLPAPER_SET_PENDING) 433 annotation class PendingWallpaperSetStatus 434 435 /** Possible status of whether a wallpaper set operation is pending or not. */ 436 @IntDef(DAILY_WALLPAPER_UPDATE_NOT_PENDING, DAILY_WALLPAPER_UPDATE_PENDING) 437 annotation class PendingDailyWallpaperUpdateStatus 438 439 companion object { 440 /** 441 * Generates a default key to look up a wallpaper in the list of recent wallpapers. 442 * 443 * This key can be used as a fallback when [.getHomeWallpaperRecentsKey] or 444 * [.getLockWallpaperRecentsKey] return null. 445 * 446 * @param remoteId wallpaper's remote id 447 * @param hashCode wallpaper's hash code 448 * @return the recents key 449 */ 450 fun generateRecentsKey(remoteId: String?, hashCode: Long): String? { 451 return if (!TextUtils.isEmpty(remoteId)) { 452 remoteId 453 } else if (hashCode > 0) { 454 hashCode.toString() 455 } else { 456 null 457 } 458 } 459 460 const val PRESENTATION_MODE_STATIC = 1 461 const val PRESENTATION_MODE_ROTATING = 2 462 const val WALLPAPER_SET_NOT_PENDING = 0 463 const val WALLPAPER_SET_PENDING = 1 464 const val DAILY_WALLPAPER_UPDATE_NOT_PENDING = 0 465 const val DAILY_WALLPAPER_UPDATE_PENDING = 1 466 } 467 } 468