1 /*
2  * Copyright (C) 2019 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.testing
17 
18 import android.app.WallpaperColors
19 import android.graphics.Bitmap
20 import android.graphics.Color
21 import android.graphics.Point
22 import android.graphics.Rect
23 import com.android.wallpaper.model.LiveWallpaperInfo
24 import com.android.wallpaper.model.LiveWallpaperPrefMetadata
25 import com.android.wallpaper.model.StaticWallpaperPrefMetadata
26 import com.android.wallpaper.model.WallpaperInfo
27 import com.android.wallpaper.module.WallpaperPersister
28 import com.android.wallpaper.module.WallpaperPreferences
29 import com.android.wallpaper.module.WallpaperPreferences.PendingDailyWallpaperUpdateStatus
30 import com.android.wallpaper.module.WallpaperPreferences.PendingWallpaperSetStatus
31 import com.android.wallpaper.module.WallpaperPreferences.PresentationMode
32 import com.android.wallpaper.picker.customization.shared.model.WallpaperDestination
33 import com.android.wallpaper.picker.data.WallpaperModel
34 import java.text.SimpleDateFormat
35 import java.util.Calendar
36 import java.util.Locale
37 import java.util.TimeZone
38 import javax.inject.Inject
39 import javax.inject.Singleton
40 
41 /** Test implementation of the WallpaperPreferences interface. Just keeps prefs in memory. */
42 @Singleton
43 open class TestWallpaperPreferences @Inject constructor() : WallpaperPreferences {
44 
45     private var appLaunchCount = 0
46     private var firstLaunchDate = 0
47     private var firstWallpaperApplyDate = 0
48     @PresentationMode private var wallpaperPresentationMode: Int
49     private var homeScreenAttributions: List<String?>?
50     private var homeScreenBitmapHashCode: Long = 0
51     private var homeWallpaperManagerId = 0
52     private var homeScreenServiceName: String? = null
53     private var lockScreenServiceName: String? = null
54     private var homeActionUrl: String? = null
55     private val homeBaseImageUrl: String? = null
56     private var homeCollectionId: String? = null
57     private var homeWallpaperRemoteId: String? = null
58     private var homeWallpaperRecentsKey: String? = null
59     private var lockScreenAttributions: List<String?>?
60     private var lockScreenBitmapHashCode: Long = 0
61     private var lockWallpaperManagerId = 0
62     private var lockActionUrl: String? = null
63     private var lockCollectionId: String? = null
64     private var lockWallpaperRemoteId: String? = null
65     private var lockWallpaperRecentsKey: String? = null
66     private val dailyRotations: MutableList<Long>
67     private var dailyWallpaperEnabledTimestamp: Long
68     private var lastDailyLogTimestamp: Long
69     private var lastAppActiveTimestamp: Long = 0
70     private var lastDailyWallpaperRotationStatus: Int
71     private var lastDailyWallpaperRotationStatusTimestamp: Long = 0
72     private val lastSyncTimestamp: Long = 0
73 
74     @PendingWallpaperSetStatus private var mPendingWallpaperSetStatus: Int
75 
76     @PendingDailyWallpaperUpdateStatus private var mPendingDailyWallpaperUpdateStatus = 0
77     private var mNumDaysDailyRotationFailed = 0
78     private var mNumDaysDailyRotationNotAttempted = 0
79     private var mHomeWallpaperEffects: String? = null
80     private var mLockWallpaperEffects: String? = null
81     private var mHomeStaticWallpaperPrefMetadata: StaticWallpaperPrefMetadata? = null
82     private var mLockStaticWallpaperPrefMetadata: StaticWallpaperPrefMetadata? = null
83     private var mHomeLiveWallpaperPrefMetadata: LiveWallpaperPrefMetadata? = null
84     private var mLockLiveWallpaperPrefMetadata: LiveWallpaperPrefMetadata? = null
85     private val mWallStoredColor: HashMap<String, String> = HashMap()
86 
87     private var hasSmallPreviewTooltipBeenShown = false
88     private var hasFullPreviewTooltipBeenShown = false
89 
90     init {
91         wallpaperPresentationMode = WallpaperPreferences.PRESENTATION_MODE_STATIC
92         homeScreenAttributions = mutableListOf<String?>("Android wallpaper")
93         lockScreenAttributions = mutableListOf("Android wallpaper")
94         dailyRotations = ArrayList()
95         dailyWallpaperEnabledTimestamp = -1
96         lastDailyLogTimestamp = -1
97         lastDailyWallpaperRotationStatus = -1
98         mPendingWallpaperSetStatus = WallpaperPreferences.WALLPAPER_SET_NOT_PENDING
99     }
100 
getWallpaperPresentationModenull101     override fun getWallpaperPresentationMode(): Int {
102         return wallpaperPresentationMode
103     }
104 
setWallpaperPresentationModenull105     override fun setWallpaperPresentationMode(@PresentationMode presentationMode: Int) {
106         wallpaperPresentationMode = presentationMode
107     }
108 
getHomeWallpaperAttributionsnull109     override fun getHomeWallpaperAttributions(): List<String?>? {
110         return homeScreenAttributions
111     }
112 
setHomeWallpaperAttributionsnull113     override fun setHomeWallpaperAttributions(attributions: List<String?>?) {
114         homeScreenAttributions = attributions
115     }
116 
getHomeWallpaperActionUrlnull117     override fun getHomeWallpaperActionUrl(): String? {
118         return homeActionUrl
119     }
120 
setHomeWallpaperActionUrlnull121     override fun setHomeWallpaperActionUrl(actionUrl: String?) {
122         homeActionUrl = actionUrl
123     }
124 
getHomeWallpaperCollectionIdnull125     override fun getHomeWallpaperCollectionId(): String? {
126         return homeCollectionId
127     }
128 
setHomeWallpaperCollectionIdnull129     override fun setHomeWallpaperCollectionId(collectionId: String?) {
130         homeCollectionId = collectionId
131     }
132 
clearHomeWallpaperMetadatanull133     override fun clearHomeWallpaperMetadata() {
134         homeScreenAttributions = null
135         wallpaperPresentationMode = WallpaperPreferences.PRESENTATION_MODE_STATIC
136         homeScreenBitmapHashCode = 0
137         homeScreenServiceName = null
138         homeWallpaperManagerId = 0
139     }
140 
setHomeStaticImageWallpaperMetadatanull141     override fun setHomeStaticImageWallpaperMetadata(metadata: StaticWallpaperPrefMetadata) {
142         mHomeStaticWallpaperPrefMetadata = metadata
143     }
144 
setHomeLiveWallpaperMetadatanull145     override fun setHomeLiveWallpaperMetadata(metadata: LiveWallpaperPrefMetadata) {
146         mHomeLiveWallpaperPrefMetadata = metadata
147     }
148 
getHomeWallpaperHashCodenull149     override fun getHomeWallpaperHashCode(): Long {
150         return homeScreenBitmapHashCode
151     }
152 
setHomeWallpaperHashCodenull153     override fun setHomeWallpaperHashCode(hashCode: Long) {
154         homeScreenBitmapHashCode = hashCode
155     }
156 
getHomeWallpaperServiceNamenull157     override fun getHomeWallpaperServiceName(): String? {
158         return homeScreenServiceName
159     }
160 
setHomeWallpaperServiceNamenull161     override fun setHomeWallpaperServiceName(serviceName: String?) {
162         homeScreenServiceName = serviceName
163         setFirstWallpaperApplyDateIfNeeded()
164     }
165 
getHomeWallpaperManagerIdnull166     override fun getHomeWallpaperManagerId(): Int {
167         return homeWallpaperManagerId
168     }
169 
setHomeWallpaperManagerIdnull170     override fun setHomeWallpaperManagerId(homeWallpaperId: Int) {
171         homeWallpaperManagerId = homeWallpaperId
172     }
173 
getHomeWallpaperRemoteIdnull174     override fun getHomeWallpaperRemoteId(): String? {
175         return homeWallpaperRemoteId
176     }
177 
setHomeWallpaperRemoteIdnull178     override fun setHomeWallpaperRemoteId(wallpaperRemoteId: String?) {
179         homeWallpaperRemoteId = wallpaperRemoteId
180         setFirstWallpaperApplyDateIfNeeded()
181     }
182 
getHomeWallpaperRecentsKeynull183     override fun getHomeWallpaperRecentsKey(): String? {
184         return homeWallpaperRecentsKey
185     }
186 
setHomeWallpaperRecentsKeynull187     override fun setHomeWallpaperRecentsKey(recentsKey: String?) {
188         homeWallpaperRecentsKey = recentsKey
189     }
190 
getHomeWallpaperEffectsnull191     override fun getHomeWallpaperEffects(): String? {
192         return mHomeWallpaperEffects
193     }
194 
setHomeWallpaperEffectsnull195     override fun setHomeWallpaperEffects(wallpaperEffects: String?) {
196         mHomeWallpaperEffects = wallpaperEffects
197     }
198 
getLockWallpaperAttributionsnull199     override fun getLockWallpaperAttributions(): List<String?>? {
200         return lockScreenAttributions
201     }
202 
setLockWallpaperAttributionsnull203     override fun setLockWallpaperAttributions(attributions: List<String?>?) {
204         lockScreenAttributions = attributions
205     }
206 
getLockWallpaperActionUrlnull207     override fun getLockWallpaperActionUrl(): String? {
208         return lockActionUrl
209     }
210 
setLockWallpaperActionUrlnull211     override fun setLockWallpaperActionUrl(actionUrl: String?) {
212         lockActionUrl = actionUrl
213     }
214 
getLockWallpaperCollectionIdnull215     override fun getLockWallpaperCollectionId(): String? {
216         return lockCollectionId
217     }
218 
setLockWallpaperCollectionIdnull219     override fun setLockWallpaperCollectionId(collectionId: String?) {
220         lockCollectionId = collectionId
221     }
222 
clearLockWallpaperMetadatanull223     override fun clearLockWallpaperMetadata() {
224         lockScreenAttributions = null
225         lockScreenBitmapHashCode = 0
226         lockWallpaperManagerId = 0
227     }
228 
setLockStaticImageWallpaperMetadatanull229     override fun setLockStaticImageWallpaperMetadata(metadata: StaticWallpaperPrefMetadata) {
230         mLockStaticWallpaperPrefMetadata = metadata
231     }
232 
setLockLiveWallpaperMetadatanull233     override fun setLockLiveWallpaperMetadata(metadata: LiveWallpaperPrefMetadata) {
234         mLockLiveWallpaperPrefMetadata = metadata
235     }
236 
getLockWallpaperHashCodenull237     override fun getLockWallpaperHashCode(): Long {
238         return lockScreenBitmapHashCode
239     }
240 
setLockWallpaperHashCodenull241     override fun setLockWallpaperHashCode(hashCode: Long) {
242         lockScreenBitmapHashCode = hashCode
243     }
244 
getLockWallpaperServiceNamenull245     override fun getLockWallpaperServiceName(): String? {
246         return lockScreenServiceName
247     }
248 
setLockWallpaperServiceNamenull249     override fun setLockWallpaperServiceName(serviceName: String?) {
250         lockScreenServiceName = serviceName
251     }
252 
getLockWallpaperManagerIdnull253     override fun getLockWallpaperManagerId(): Int {
254         return lockWallpaperManagerId
255     }
256 
setLockWallpaperManagerIdnull257     override fun setLockWallpaperManagerId(lockWallpaperId: Int) {
258         lockWallpaperManagerId = lockWallpaperId
259     }
260 
getLockWallpaperRemoteIdnull261     override fun getLockWallpaperRemoteId(): String? {
262         return lockWallpaperRemoteId
263     }
264 
setLockWallpaperRemoteIdnull265     override fun setLockWallpaperRemoteId(wallpaperRemoteId: String?) {
266         lockWallpaperRemoteId = wallpaperRemoteId
267         setFirstWallpaperApplyDateIfNeeded()
268     }
269 
getLockWallpaperRecentsKeynull270     override fun getLockWallpaperRecentsKey(): String? {
271         return lockWallpaperRecentsKey
272     }
273 
setLockWallpaperRecentsKeynull274     override fun setLockWallpaperRecentsKey(recentsKey: String?) {
275         lockWallpaperRecentsKey = recentsKey
276     }
277 
getLockWallpaperEffectsnull278     override fun getLockWallpaperEffects(): String? {
279         return mLockWallpaperEffects
280     }
281 
setLockWallpaperEffectsnull282     override fun setLockWallpaperEffects(wallpaperEffects: String?) {
283         mLockWallpaperEffects = wallpaperEffects
284     }
285 
addDailyRotationnull286     override fun addDailyRotation(timestamp: Long) {
287         dailyRotations.add(timestamp)
288     }
289 
getLastDailyRotationTimestampnull290     override fun getLastDailyRotationTimestamp(): Long {
291         return if (dailyRotations.size == 0) {
292             -1
293         } else dailyRotations[dailyRotations.size - 1]
294     }
295 
getDailyWallpaperEnabledTimestampnull296     override fun getDailyWallpaperEnabledTimestamp(): Long {
297         return dailyWallpaperEnabledTimestamp
298     }
299 
setDailyWallpaperEnabledTimestampnull300     override fun setDailyWallpaperEnabledTimestamp(timestamp: Long) {
301         dailyWallpaperEnabledTimestamp = timestamp
302     }
303 
clearDailyRotationsnull304     override fun clearDailyRotations() {
305         dailyRotations.clear()
306     }
307 
getLastDailyLogTimestampnull308     override fun getLastDailyLogTimestamp(): Long {
309         return lastDailyLogTimestamp
310     }
311 
setLastDailyLogTimestampnull312     override fun setLastDailyLogTimestamp(timestamp: Long) {
313         lastDailyLogTimestamp = timestamp
314     }
315 
getLastAppActiveTimestampnull316     override fun getLastAppActiveTimestamp(): Long {
317         return lastAppActiveTimestamp
318     }
319 
setLastAppActiveTimestampnull320     override fun setLastAppActiveTimestamp(timestamp: Long) {
321         lastAppActiveTimestamp = timestamp
322     }
323 
setDailyWallpaperRotationStatusnull324     override fun setDailyWallpaperRotationStatus(status: Int, timestamp: Long) {
325         lastDailyWallpaperRotationStatus = status
326         lastDailyWallpaperRotationStatusTimestamp = timestamp
327     }
328 
setPendingWallpaperSetStatusSyncnull329     override fun setPendingWallpaperSetStatusSync(@PendingWallpaperSetStatus setStatus: Int) {
330         mPendingWallpaperSetStatus = setStatus
331     }
332 
333     @PendingWallpaperSetStatus
getPendingWallpaperSetStatusnull334     override fun getPendingWallpaperSetStatus(): Int {
335         return mPendingWallpaperSetStatus
336     }
337 
setPendingWallpaperSetStatusnull338     override fun setPendingWallpaperSetStatus(@PendingWallpaperSetStatus setStatus: Int) {
339         mPendingWallpaperSetStatus = setStatus
340     }
341 
setPendingDailyWallpaperUpdateStatusSyncnull342     override fun setPendingDailyWallpaperUpdateStatusSync(
343         @PendingDailyWallpaperUpdateStatus updateStatus: Int
344     ) {
345         mPendingDailyWallpaperUpdateStatus = updateStatus
346     }
347 
348     @PendingDailyWallpaperUpdateStatus
getPendingDailyWallpaperUpdateStatusnull349     override fun getPendingDailyWallpaperUpdateStatus(): Int {
350         return mPendingDailyWallpaperUpdateStatus
351     }
352 
setPendingDailyWallpaperUpdateStatusnull353     override fun setPendingDailyWallpaperUpdateStatus(
354         @PendingDailyWallpaperUpdateStatus updateStatus: Int
355     ) {
356         mPendingDailyWallpaperUpdateStatus = updateStatus
357     }
358 
getAppLaunchCountnull359     override fun getAppLaunchCount(): Int {
360         return appLaunchCount
361     }
362 
getFirstLaunchDateSinceSetupnull363     override fun getFirstLaunchDateSinceSetup(): Int {
364         return firstLaunchDate
365     }
366 
incrementAppLaunchednull367     override fun incrementAppLaunched() {
368         if (getFirstLaunchDateSinceSetup() == 0) {
369             setFirstLaunchDateSinceSetup(getCurrentDate())
370         }
371         val appLaunchCount = getAppLaunchCount()
372         if (appLaunchCount < Int.MAX_VALUE) {
373             setAppLaunchCount(appLaunchCount + 1)
374         }
375     }
376 
getFirstWallpaperApplyDateSinceSetupnull377     override fun getFirstWallpaperApplyDateSinceSetup(): Int {
378         return firstWallpaperApplyDate
379     }
380 
storeWallpaperColorsnull381     override fun storeWallpaperColors(
382         storedWallpaperId: String?,
383         wallpaperColors: WallpaperColors?
384     ) {
385         if (storedWallpaperId == null || wallpaperColors == null) {
386             return
387         }
388         val primaryColor = wallpaperColors.primaryColor
389         var value = java.lang.String(primaryColor.toArgb().toString()) as String
390         val secondaryColor = wallpaperColors.secondaryColor
391         if (secondaryColor != null) {
392             value += "," + secondaryColor.toArgb()
393         }
394         val tertiaryColor = wallpaperColors.tertiaryColor
395         if (tertiaryColor != null) {
396             value += "," + tertiaryColor.toArgb()
397         }
398         mWallStoredColor[storedWallpaperId] = value
399     }
400 
getWallpaperColorsnull401     override fun getWallpaperColors(storedWallpaperId: String): WallpaperColors? {
402         if (mWallStoredColor.isEmpty()) {
403             return null
404         }
405         val value = mWallStoredColor[storedWallpaperId]
406         if (value == "") {
407             return null
408         }
409         val colorStrings =
410             value!!.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
411         val colorPrimary = Color.valueOf(colorStrings[0].toInt())
412         var colorSecondary: Color? = null
413         if (colorStrings.size >= 2) {
414             colorSecondary = Color.valueOf(colorStrings[1].toInt())
415         }
416         var colorTerTiary: Color? = null
417         if (colorStrings.size >= 3) {
418             colorTerTiary = Color.valueOf(colorStrings[2].toInt())
419         }
420         return WallpaperColors(
421             colorPrimary,
422             colorSecondary,
423             colorTerTiary,
424             WallpaperColors.HINT_FROM_BITMAP
425         )
426     }
427 
updateDailyWallpaperSetnull428     override fun updateDailyWallpaperSet(
429         @WallpaperPersister.Destination destination: Int,
430         collectionId: String?,
431         wallpaperId: String?
432     ) {
433         // Assign wallpaper info by destination.
434         when (destination) {
435             WallpaperPersister.DEST_HOME_SCREEN -> {
436                 setHomeWallpaperCollectionId(collectionId!!)
437                 setHomeWallpaperRemoteId(wallpaperId)
438             }
439             WallpaperPersister.DEST_LOCK_SCREEN -> {
440                 setLockWallpaperCollectionId(collectionId!!)
441                 setLockWallpaperRemoteId(wallpaperId!!)
442             }
443             WallpaperPersister.DEST_BOTH -> {
444                 setHomeWallpaperCollectionId(collectionId!!)
445                 setHomeWallpaperRemoteId(wallpaperId)
446                 setLockWallpaperCollectionId(collectionId)
447                 setLockWallpaperRemoteId(wallpaperId!!)
448             }
449         }
450     }
451 
storeLatestWallpapernull452     override fun storeLatestWallpaper(
453         which: Int,
454         wallpaperId: String,
455         wallpaper: LiveWallpaperInfo,
456         colors: WallpaperColors
457     ) {}
458 
storeLatestWallpapernull459     override fun storeLatestWallpaper(
460         which: Int,
461         wallpaperId: String,
462         wallpaper: WallpaperInfo,
463         croppedWallpaperBitmap: Bitmap,
464         colors: WallpaperColors
465     ) {}
466 
storeLatestWallpapernull467     override fun storeLatestWallpaper(
468         which: Int,
469         wallpaperId: String,
470         attributions: List<String>?,
471         actionUrl: String?,
472         collectionId: String?,
473         croppedWallpaperBitmap: Bitmap,
474         colors: WallpaperColors
475     ) {}
476 
addStaticWallpaperToRecentWallpapersnull477     override suspend fun addStaticWallpaperToRecentWallpapers(
478         destination: WallpaperDestination,
479         wallpaperModel: WallpaperModel.StaticWallpaperModel,
480         bitmap: Bitmap,
481         cropHints: Map<Point, Rect>?,
482     ) {}
483 
addLiveWallpaperToRecentWallpapersnull484     override suspend fun addLiveWallpaperToRecentWallpapers(
485         destination: WallpaperDestination,
486         wallpaperModel: WallpaperModel.LiveWallpaperModel
487     ) {}
488 
setHasSmallPreviewTooltipBeenShownnull489     override fun setHasSmallPreviewTooltipBeenShown(hasTooltipBeenShown: Boolean) {
490         this.hasSmallPreviewTooltipBeenShown = hasTooltipBeenShown
491     }
492 
getHasSmallPreviewTooltipBeenShownnull493     override fun getHasSmallPreviewTooltipBeenShown(): Boolean {
494         return hasSmallPreviewTooltipBeenShown
495     }
496 
setHasFullPreviewTooltipBeenShownnull497     override fun setHasFullPreviewTooltipBeenShown(hasTooltipBeenShown: Boolean) {
498         this.hasFullPreviewTooltipBeenShown = hasTooltipBeenShown
499     }
500 
getHasFullPreviewTooltipBeenShownnull501     override fun getHasFullPreviewTooltipBeenShown(): Boolean {
502         return hasFullPreviewTooltipBeenShown
503     }
504 
setAppLaunchCountnull505     private fun setAppLaunchCount(count: Int) {
506         appLaunchCount = count
507     }
508 
setFirstLaunchDateSinceSetupnull509     private fun setFirstLaunchDateSinceSetup(firstLaunchDate: Int) {
510         this.firstLaunchDate = firstLaunchDate
511     }
512 
setFirstWallpaperApplyDateSinceSetupnull513     private fun setFirstWallpaperApplyDateSinceSetup(firstWallpaperApplyDate: Int) {
514         this.firstWallpaperApplyDate = firstWallpaperApplyDate
515     }
516 
setFirstWallpaperApplyDateIfNeedednull517     private fun setFirstWallpaperApplyDateIfNeeded() {
518         if (getFirstWallpaperApplyDateSinceSetup() == 0) {
519             setFirstWallpaperApplyDateSinceSetup(getCurrentDate())
520         }
521     }
522 
getCurrentDatenull523     private fun getCurrentDate(): Int {
524         val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
525         val format = SimpleDateFormat("yyyyMMdd", Locale.US)
526         return format.format(calendar.time).toInt()
527     }
528 }
529