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 static com.android.wallpaper.module.WallpaperPersister.DEST_BOTH;
19 import static com.android.wallpaper.module.WallpaperPersister.DEST_HOME_SCREEN;
20 import static com.android.wallpaper.module.WallpaperPersister.DEST_LOCK_SCREEN;
21 
22 import androidx.annotation.Nullable;
23 
24 import com.android.wallpaper.module.WallpaperPersister.Destination;
25 import com.android.wallpaper.module.WallpaperPreferences;
26 
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Calendar;
31 import java.util.List;
32 import java.util.Locale;
33 import java.util.TimeZone;
34 
35 /**
36  * Test implementation of the WallpaperPreferences interface. Just keeps prefs in memory.
37  */
38 public class TestWallpaperPreferences implements WallpaperPreferences {
39 
40     private int mAppLaunchCount;
41     private int mFirstLaunchDate;
42     private int mFirstWallpaperApplyDate;
43     @PresentationMode
44     private int mWallpaperPresentationMode;
45 
46     private List<String> mHomeScreenAttributions;
47     private long mHomeScreenBitmapHashCode;
48     private int mHomeWallpaperManagerId;
49     private String mHomeScreenPackageName;
50     private String mHomeScreenServiceName;
51     private String mHomeActionUrl;
52     private String mHomeBaseImageUrl;
53     private String mHomeCollectionId;
54     private String mHomeWallpaperRemoteId;
55 
56     private List<String> mLockScreenAttributions;
57     private long mLockScreenBitmapHashCode;
58     private int mLockWallpaperManagerId;
59     private String mLockActionUrl;
60     private String mLockCollectionId;
61     private String mLockWallpaperRemoteId;
62 
63     private List<Long> mDailyRotations;
64     private long mDailyWallpaperEnabledTimestamp;
65     private long mLastDailyLogTimestamp;
66     private long mLastAppActiveTimestamp;
67     private int mLastDailyWallpaperRotationStatus;
68     private long mLastDailyWallpaperRotationStatusTimestamp;
69     private long mLastSyncTimestamp;
70     @PendingWallpaperSetStatus
71     private int mPendingWallpaperSetStatus;
72     @PendingDailyWallpaperUpdateStatus
73     private int mPendingDailyWallpaperUpdateStatus;
74     private int mNumDaysDailyRotationFailed;
75     private int mNumDaysDailyRotationNotAttempted;
76     private int mHomeWallpaperActionLabelRes;
77     private int mHomeWallpaperActionIconRes;
78     private int mLockWallpaperActionLabelRes;
79     private int mLockWallpaperActionIconRes;
80 
TestWallpaperPreferences()81     public TestWallpaperPreferences() {
82         mWallpaperPresentationMode = WallpaperPreferences.PRESENTATION_MODE_STATIC;
83         mHomeScreenAttributions = Arrays.asList("Android wallpaper");
84         mHomeScreenBitmapHashCode = 0;
85         mHomeWallpaperManagerId = 0;
86 
87         mLockScreenAttributions = Arrays.asList("Android wallpaper");
88         mLockScreenBitmapHashCode = 0;
89         mLockWallpaperManagerId = 0;
90 
91         mDailyRotations = new ArrayList<>();
92         mDailyWallpaperEnabledTimestamp = -1;
93         mLastDailyLogTimestamp = -1;
94         mLastDailyWallpaperRotationStatus = -1;
95         mLastDailyWallpaperRotationStatusTimestamp = 0;
96         mLastSyncTimestamp = 0;
97         mPendingWallpaperSetStatus = WALLPAPER_SET_NOT_PENDING;
98     }
99 
100     @Override
getWallpaperPresentationMode()101     public int getWallpaperPresentationMode() {
102         return mWallpaperPresentationMode;
103     }
104 
105     @Override
setWallpaperPresentationMode(@resentationMode int presentationMode)106     public void setWallpaperPresentationMode(@PresentationMode int presentationMode) {
107         mWallpaperPresentationMode = presentationMode;
108     }
109 
110     @Override
getHomeWallpaperAttributions()111     public List<String> getHomeWallpaperAttributions() {
112         return mHomeScreenAttributions;
113     }
114 
115     @Override
setHomeWallpaperAttributions(List<String> attributions)116     public void setHomeWallpaperAttributions(List<String> attributions) {
117         mHomeScreenAttributions = attributions;
118     }
119 
120     @Override
getHomeWallpaperActionUrl()121     public String getHomeWallpaperActionUrl() {
122         return mHomeActionUrl;
123     }
124 
125     @Override
setHomeWallpaperActionUrl(String actionUrl)126     public void setHomeWallpaperActionUrl(String actionUrl) {
127         mHomeActionUrl = actionUrl;
128     }
129 
130     @Override
getHomeWallpaperActionLabelRes()131     public int getHomeWallpaperActionLabelRes() {
132         return mHomeWallpaperActionLabelRes;
133     }
134 
135     @Override
setHomeWallpaperActionLabelRes(int resId)136     public void setHomeWallpaperActionLabelRes(int resId) {
137         mHomeWallpaperActionLabelRes = resId;
138     }
139 
140     @Override
getHomeWallpaperActionIconRes()141     public int getHomeWallpaperActionIconRes() {
142         return mHomeWallpaperActionIconRes;
143     }
144 
145     @Override
setHomeWallpaperActionIconRes(int resId)146     public void setHomeWallpaperActionIconRes(int resId) {
147         mHomeWallpaperActionIconRes = resId;
148     }
149 
150     @Override
getHomeWallpaperBaseImageUrl()151     public String getHomeWallpaperBaseImageUrl() {
152         return mHomeBaseImageUrl;
153     }
154 
155     @Override
setHomeWallpaperBaseImageUrl(String baseImageUrl)156     public void setHomeWallpaperBaseImageUrl(String baseImageUrl) {
157         mHomeBaseImageUrl = baseImageUrl;
158     }
159 
160     @Override
getHomeWallpaperCollectionId()161     public String getHomeWallpaperCollectionId() {
162         return mHomeCollectionId;
163     }
164 
165     @Override
setHomeWallpaperCollectionId(String collectionId)166     public void setHomeWallpaperCollectionId(String collectionId) {
167         mHomeCollectionId = collectionId;
168     }
169 
170     @Override
getHomeWallpaperBackingFileName()171     public String getHomeWallpaperBackingFileName() {
172         return null;
173     }
174 
175     @Override
setHomeWallpaperBackingFileName(String fileName)176     public void setHomeWallpaperBackingFileName(String fileName) {
177 
178     }
179 
180     @Override
clearHomeWallpaperMetadata()181     public void clearHomeWallpaperMetadata() {
182         mHomeScreenAttributions = null;
183         mWallpaperPresentationMode = WallpaperPreferences.PRESENTATION_MODE_STATIC;
184         mHomeScreenBitmapHashCode = 0;
185         mHomeScreenPackageName = null;
186         mHomeWallpaperManagerId = 0;
187     }
188 
189     @Override
getHomeWallpaperHashCode()190     public long getHomeWallpaperHashCode() {
191         return mHomeScreenBitmapHashCode;
192     }
193 
194     @Override
setHomeWallpaperHashCode(long hashCode)195     public void setHomeWallpaperHashCode(long hashCode) {
196         mHomeScreenBitmapHashCode = hashCode;
197     }
198 
199     @Override
getHomeWallpaperPackageName()200     public String getHomeWallpaperPackageName() {
201         return mHomeScreenPackageName;
202     }
203 
204     @Override
setHomeWallpaperPackageName(String packageName)205     public void setHomeWallpaperPackageName(String packageName) {
206         mHomeScreenPackageName = packageName;
207     }
208 
209     @Override
getHomeWallpaperServiceName()210     public String getHomeWallpaperServiceName() {
211         return mHomeScreenServiceName;
212     }
213 
214     @Override
setHomeWallpaperServiceName(String serviceName)215     public void setHomeWallpaperServiceName(String serviceName) {
216         mHomeScreenServiceName = serviceName;
217         setFirstWallpaperApplyDateIfNeeded();
218     }
219 
220     @Override
getHomeWallpaperManagerId()221     public int getHomeWallpaperManagerId() {
222         return mHomeWallpaperManagerId;
223     }
224 
225     @Override
setHomeWallpaperManagerId(int homeWallpaperId)226     public void setHomeWallpaperManagerId(int homeWallpaperId) {
227         mHomeWallpaperManagerId = homeWallpaperId;
228     }
229 
230     @Override
getHomeWallpaperRemoteId()231     public String getHomeWallpaperRemoteId() {
232         return mHomeWallpaperRemoteId;
233     }
234 
235     @Override
setHomeWallpaperRemoteId(String wallpaperRemoteId)236     public void setHomeWallpaperRemoteId(String wallpaperRemoteId) {
237         mHomeWallpaperRemoteId = wallpaperRemoteId;
238         setFirstWallpaperApplyDateIfNeeded();
239     }
240 
241     @Override
getLockWallpaperAttributions()242     public List<String> getLockWallpaperAttributions() {
243         return mLockScreenAttributions;
244     }
245 
246     @Override
setLockWallpaperAttributions(List<String> attributions)247     public void setLockWallpaperAttributions(List<String> attributions) {
248         mLockScreenAttributions = attributions;
249     }
250 
251     @Override
getLockWallpaperActionUrl()252     public String getLockWallpaperActionUrl() {
253         return mLockActionUrl;
254     }
255 
256     @Override
setLockWallpaperActionUrl(String actionUrl)257     public void setLockWallpaperActionUrl(String actionUrl) {
258         mLockActionUrl = actionUrl;
259     }
260 
261     @Override
getLockWallpaperActionLabelRes()262     public int getLockWallpaperActionLabelRes() {
263         return mLockWallpaperActionLabelRes;
264     }
265 
266     @Override
setLockWallpaperActionLabelRes(int resId)267     public void setLockWallpaperActionLabelRes(int resId) {
268         mLockWallpaperActionLabelRes = resId;
269     }
270 
271     @Override
getLockWallpaperActionIconRes()272     public int getLockWallpaperActionIconRes() {
273         return mLockWallpaperActionIconRes;
274     }
275 
276     @Override
setLockWallpaperActionIconRes(int resId)277     public void setLockWallpaperActionIconRes(int resId) {
278         mLockWallpaperActionIconRes = resId;
279     }
280 
281     @Override
getLockWallpaperCollectionId()282     public String getLockWallpaperCollectionId() {
283         return mLockCollectionId;
284     }
285 
286     @Override
setLockWallpaperCollectionId(String collectionId)287     public void setLockWallpaperCollectionId(String collectionId) {
288         mLockCollectionId = collectionId;
289     }
290 
291     @Override
getLockWallpaperBackingFileName()292     public String getLockWallpaperBackingFileName() {
293         return null;
294     }
295 
296     @Override
setLockWallpaperBackingFileName(String fileName)297     public void setLockWallpaperBackingFileName(String fileName) {
298 
299     }
300 
301     @Override
clearLockWallpaperMetadata()302     public void clearLockWallpaperMetadata() {
303         mLockScreenAttributions = null;
304         mLockScreenBitmapHashCode = 0;
305         mLockWallpaperManagerId = 0;
306     }
307 
308     @Override
getLockWallpaperHashCode()309     public long getLockWallpaperHashCode() {
310         return mLockScreenBitmapHashCode;
311     }
312 
313     @Override
setLockWallpaperHashCode(long hashCode)314     public void setLockWallpaperHashCode(long hashCode) {
315         mLockScreenBitmapHashCode = hashCode;
316     }
317 
318     @Override
getLockWallpaperId()319     public int getLockWallpaperId() {
320         return mLockWallpaperManagerId;
321     }
322 
323     @Override
setLockWallpaperId(int lockWallpaperId)324     public void setLockWallpaperId(int lockWallpaperId) {
325         mLockWallpaperManagerId = lockWallpaperId;
326     }
327 
328     @Override
getLockWallpaperRemoteId()329     public String getLockWallpaperRemoteId() {
330         return mLockWallpaperRemoteId;
331     }
332 
333     @Override
setLockWallpaperRemoteId(String wallpaperRemoteId)334     public void setLockWallpaperRemoteId(String wallpaperRemoteId) {
335         mLockWallpaperRemoteId = wallpaperRemoteId;
336         setFirstWallpaperApplyDateIfNeeded();
337     }
338 
339     @Override
addDailyRotation(long timestamp)340     public void addDailyRotation(long timestamp) {
341         mDailyRotations.add(timestamp);
342     }
343 
344     @Override
getLastDailyRotationTimestamp()345     public long getLastDailyRotationTimestamp() {
346         if (mDailyRotations.size() == 0) {
347             return -1;
348         }
349 
350         return mDailyRotations.get(mDailyRotations.size() - 1);
351     }
352 
353     @Override
getDailyRotationsInLastWeek()354     public List<Long> getDailyRotationsInLastWeek() {
355         return mDailyRotations;
356     }
357 
358     @Nullable
359     @Override
getDailyRotationsPreviousDay()360     public List<Long> getDailyRotationsPreviousDay() {
361         return null;
362     }
363 
364     @Override
getDailyWallpaperEnabledTimestamp()365     public long getDailyWallpaperEnabledTimestamp() {
366         return mDailyWallpaperEnabledTimestamp;
367     }
368 
369     @Override
setDailyWallpaperEnabledTimestamp(long timestamp)370     public void setDailyWallpaperEnabledTimestamp(long timestamp) {
371         mDailyWallpaperEnabledTimestamp = timestamp;
372     }
373 
374     @Override
clearDailyRotations()375     public void clearDailyRotations() {
376         mDailyRotations.clear();
377     }
378 
379     @Override
getLastDailyLogTimestamp()380     public long getLastDailyLogTimestamp() {
381         return mLastDailyLogTimestamp;
382     }
383 
384     @Override
setLastDailyLogTimestamp(long timestamp)385     public void setLastDailyLogTimestamp(long timestamp) {
386         mLastDailyLogTimestamp = timestamp;
387     }
388 
389     @Override
getLastAppActiveTimestamp()390     public long getLastAppActiveTimestamp() {
391         return mLastAppActiveTimestamp;
392     }
393 
394     @Override
setLastAppActiveTimestamp(long timestamp)395     public void setLastAppActiveTimestamp(long timestamp) {
396         mLastAppActiveTimestamp = timestamp;
397     }
398 
399     @Override
setDailyWallpaperRotationStatus(int status, long timestamp)400     public void setDailyWallpaperRotationStatus(int status, long timestamp) {
401         mLastDailyWallpaperRotationStatus = status;
402         mLastDailyWallpaperRotationStatusTimestamp = timestamp;
403     }
404 
405     @Override
getDailyWallpaperLastRotationStatus()406     public int getDailyWallpaperLastRotationStatus() {
407         return mLastDailyWallpaperRotationStatus;
408     }
409 
410     @Override
getDailyWallpaperLastRotationStatusTimestamp()411     public long getDailyWallpaperLastRotationStatusTimestamp() {
412         return mLastDailyWallpaperRotationStatusTimestamp;
413     }
414 
415     @Override
getLastSyncTimestamp()416     public long getLastSyncTimestamp() {
417         return mLastSyncTimestamp;
418     }
419 
420     @Override
setLastSyncTimestamp(long timestamp)421     public void setLastSyncTimestamp(long timestamp) {
422         mLastSyncTimestamp = timestamp;
423     }
424 
425     @Override
setPendingWallpaperSetStatusSync(@endingWallpaperSetStatus int setStatus)426     public void setPendingWallpaperSetStatusSync(@PendingWallpaperSetStatus int setStatus) {
427         mPendingWallpaperSetStatus = setStatus;
428     }
429 
430     @Override
getPendingWallpaperSetStatus()431     public int getPendingWallpaperSetStatus() {
432         return mPendingWallpaperSetStatus;
433     }
434 
435     @Override
setPendingWallpaperSetStatus(@endingWallpaperSetStatus int setStatus)436     public void setPendingWallpaperSetStatus(@PendingWallpaperSetStatus int setStatus) {
437         mPendingWallpaperSetStatus = setStatus;
438     }
439 
440     @Override
setPendingDailyWallpaperUpdateStatusSync( @endingDailyWallpaperUpdateStatus int updateStatus)441     public void setPendingDailyWallpaperUpdateStatusSync(
442             @PendingDailyWallpaperUpdateStatus int updateStatus) {
443         mPendingDailyWallpaperUpdateStatus = updateStatus;
444     }
445 
446     @Override
getPendingDailyWallpaperUpdateStatus()447     public int getPendingDailyWallpaperUpdateStatus() {
448         return mPendingDailyWallpaperUpdateStatus;
449     }
450 
451     @Override
setPendingDailyWallpaperUpdateStatus( @endingDailyWallpaperUpdateStatus int updateStatus)452     public void setPendingDailyWallpaperUpdateStatus(
453             @PendingDailyWallpaperUpdateStatus int updateStatus) {
454         mPendingDailyWallpaperUpdateStatus = updateStatus;
455     }
456 
457     @Override
incrementNumDaysDailyRotationFailed()458     public void incrementNumDaysDailyRotationFailed() {
459         mNumDaysDailyRotationFailed++;
460     }
461 
462     @Override
getNumDaysDailyRotationFailed()463     public int getNumDaysDailyRotationFailed() {
464         return mNumDaysDailyRotationFailed;
465     }
466 
setNumDaysDailyRotationFailed(int days)467     public void setNumDaysDailyRotationFailed(int days) {
468         mNumDaysDailyRotationFailed = days;
469     }
470 
471     @Override
resetNumDaysDailyRotationFailed()472     public void resetNumDaysDailyRotationFailed() {
473         mNumDaysDailyRotationFailed = 0;
474     }
475 
476     @Override
incrementNumDaysDailyRotationNotAttempted()477     public void incrementNumDaysDailyRotationNotAttempted() {
478         mNumDaysDailyRotationNotAttempted++;
479     }
480 
481     @Override
getNumDaysDailyRotationNotAttempted()482     public int getNumDaysDailyRotationNotAttempted() {
483         return mNumDaysDailyRotationNotAttempted;
484     }
485 
setNumDaysDailyRotationNotAttempted(int days)486     public void setNumDaysDailyRotationNotAttempted(int days) {
487         mNumDaysDailyRotationNotAttempted = days;
488     }
489 
490     @Override
resetNumDaysDailyRotationNotAttempted()491     public void resetNumDaysDailyRotationNotAttempted() {
492         mNumDaysDailyRotationNotAttempted = 0;
493     }
494 
495 
496     @Override
getAppLaunchCount()497     public int getAppLaunchCount() {
498         return mAppLaunchCount;
499     }
500 
setAppLaunchCount(int count)501     private void setAppLaunchCount(int count) {
502         mAppLaunchCount = count;
503     }
504 
505     @Override
getFirstLaunchDateSinceSetup()506     public int getFirstLaunchDateSinceSetup() {
507         return mFirstLaunchDate;
508     }
509 
setFirstLaunchDateSinceSetup(int firstLaunchDate)510     private void setFirstLaunchDateSinceSetup(int firstLaunchDate) {
511         mFirstLaunchDate = firstLaunchDate;
512     }
513 
514     @Override
getFirstWallpaperApplyDateSinceSetup()515     public int getFirstWallpaperApplyDateSinceSetup() {
516         return mFirstWallpaperApplyDate;
517     }
518 
setFirstWallpaperApplyDateSinceSetup(int firstWallpaperApplyDate)519     private void setFirstWallpaperApplyDateSinceSetup(int firstWallpaperApplyDate) {
520         mFirstWallpaperApplyDate = firstWallpaperApplyDate;
521     }
522 
523     @Override
incrementAppLaunched()524     public void incrementAppLaunched() {
525         if (getFirstLaunchDateSinceSetup() == 0) {
526             setFirstLaunchDateSinceSetup(getCurrentDate());
527         }
528 
529         int appLaunchCount = getAppLaunchCount();
530         if (appLaunchCount < Integer.MAX_VALUE) {
531             setAppLaunchCount(appLaunchCount + 1);
532         }
533     }
534 
setFirstWallpaperApplyDateIfNeeded()535     private void setFirstWallpaperApplyDateIfNeeded() {
536         if (getFirstWallpaperApplyDateSinceSetup() == 0) {
537             setFirstWallpaperApplyDateSinceSetup(getCurrentDate());
538         }
539     }
540 
541     @Override
updateDailyWallpaperSet(@estination int destination, String collectionId, String wallpaperId)542     public void updateDailyWallpaperSet(@Destination int destination, String collectionId,
543             String wallpaperId) {
544         // Assign wallpaper info by destination.
545         if (destination == DEST_HOME_SCREEN) {
546             setHomeWallpaperCollectionId(collectionId);
547             setHomeWallpaperRemoteId(wallpaperId);
548         } else if (destination == DEST_LOCK_SCREEN) {
549             setLockWallpaperCollectionId(collectionId);
550             setLockWallpaperRemoteId(wallpaperId);
551         } else if (destination == DEST_BOTH) {
552             setHomeWallpaperCollectionId(collectionId);
553             setHomeWallpaperRemoteId(wallpaperId);
554             setLockWallpaperCollectionId(collectionId);
555             setLockWallpaperRemoteId(wallpaperId);
556         }
557     }
558 
getCurrentDate()559     private int getCurrentDate() {
560         Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
561         SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.US);
562         return Integer.parseInt(format.format(calendar.getTime()));
563     }
564 }
565