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.content.Context;
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 import com.android.wallpaper.model.WallpaperRotationInitializer;
23 import com.android.wallpaper.module.InjectorProvider;
24 import com.android.wallpaper.module.WallpaperPreferences;
25 
26 /**
27  * Test implementation of {@link WallpaperRotationInitializer}.
28  */
29 public class TestWallpaperRotationInitializer implements WallpaperRotationInitializer {
30 
31     private boolean mIsRotationInitialized;
32     @NetworkPreference
33     private int mNetworkPreference;
34     private Listener mListener;
35     @RotationInitializationState
36     private int mRotationInitializationState;
37 
TestWallpaperRotationInitializer()38     TestWallpaperRotationInitializer() {
39         mIsRotationInitialized = false;
40         mNetworkPreference = NETWORK_PREFERENCE_WIFI_ONLY;
41         mRotationInitializationState = WallpaperRotationInitializer.ROTATION_NOT_INITIALIZED;
42     }
43 
TestWallpaperRotationInitializer(@otationInitializationState int rotationState)44     TestWallpaperRotationInitializer(@RotationInitializationState int rotationState) {
45         mIsRotationInitialized = false;
46         mNetworkPreference = NETWORK_PREFERENCE_WIFI_ONLY;
47         mRotationInitializationState = rotationState;
48     }
49 
TestWallpaperRotationInitializer(Parcel unused)50     private TestWallpaperRotationInitializer(Parcel unused) {
51         mIsRotationInitialized = false;
52         mNetworkPreference = NETWORK_PREFERENCE_WIFI_ONLY;
53     }
54 
55     @Override
setFirstWallpaperInRotation(Context context, @NetworkPreference int networkPreference, Listener listener)56     public void setFirstWallpaperInRotation(Context context,
57             @NetworkPreference int networkPreference,
58             Listener listener) {
59         mListener = listener;
60         mNetworkPreference = networkPreference;
61     }
62 
63     @Override
startRotation(Context appContext)64     public boolean startRotation(Context appContext) {
65         WallpaperPreferences wallpaperPreferences =
66                 InjectorProvider.getInjector().getPreferences(appContext);
67         wallpaperPreferences.setWallpaperPresentationMode(
68                 WallpaperPreferences.PRESENTATION_MODE_ROTATING);
69         return true;
70     }
71 
72     @Override
fetchRotationInitializationState(Context context, RotationStateListener listener)73     public void fetchRotationInitializationState(Context context, RotationStateListener listener) {
74         listener.onRotationStateReceived(mRotationInitializationState);
75     }
76 
77     @Override
getRotationInitializationStateDirty(Context context)78     public int getRotationInitializationStateDirty(Context context) {
79         return mRotationInitializationState;
80     }
81 
82     /** Sets the mocked rotation initialization state. */
setRotationInitializationState(@otationInitializationState int rotationState)83     public void setRotationInitializationState(@RotationInitializationState int rotationState) {
84         mRotationInitializationState = rotationState;
85     }
86 
87     /**
88      * Simulates completion of the asynchronous task to download the first wallpaper in a rotation..
89      *
90      * @param isSuccessful Whether the first wallpaper downloaded successfully.
91      */
finishDownloadingFirstWallpaper(boolean isSuccessful)92     public void finishDownloadingFirstWallpaper(boolean isSuccessful) {
93         if (isSuccessful) {
94             mIsRotationInitialized = true;
95             mListener.onFirstWallpaperInRotationSet();
96         } else {
97             mIsRotationInitialized = false;
98             mListener.onError();
99         }
100     }
101 
102     /** Returns whether a wallpaper rotation is initialized. */
isRotationInitialized()103     public boolean isRotationInitialized() {
104         return mIsRotationInitialized;
105     }
106 
107     /** Returns whether a wallpaper rotation is enabled for WiFi-only. */
isWifiOnly()108     public boolean isWifiOnly() {
109         return mNetworkPreference == NETWORK_PREFERENCE_WIFI_ONLY;
110     }
111 
112     @Override
writeToParcel(Parcel dest, int flags)113     public void writeToParcel(Parcel dest, int flags) {
114     }
115 
116     @Override
describeContents()117     public int describeContents() {
118         return 0;
119     }
120 
121     public static final Parcelable.Creator<TestWallpaperRotationInitializer> CREATOR =
122             new Parcelable.Creator<TestWallpaperRotationInitializer>() {
123                 @Override
124                 public TestWallpaperRotationInitializer createFromParcel(Parcel in) {
125                     return new TestWallpaperRotationInitializer(in);
126                 }
127 
128                 @Override
129                 public TestWallpaperRotationInitializer[] newArray(int size) {
130                     return new TestWallpaperRotationInitializer[size];
131                 }
132             };
133 }
134