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.Activity; 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Rect; 22 23 import androidx.annotation.Nullable; 24 25 import com.android.wallpaper.asset.Asset; 26 import com.android.wallpaper.asset.Asset.BitmapReceiver; 27 import com.android.wallpaper.model.WallpaperInfo; 28 import com.android.wallpaper.module.InjectorProvider; 29 import com.android.wallpaper.module.WallpaperChangedNotifier; 30 import com.android.wallpaper.module.WallpaperPersister; 31 import com.android.wallpaper.module.WallpaperPreferences; 32 33 import java.util.List; 34 35 /** 36 * Test double for {@link WallpaperPersister}. 37 */ 38 public class TestWallpaperPersister implements WallpaperPersister { 39 40 private Context mAppContext; 41 private WallpaperPreferences mPrefs; 42 private WallpaperChangedNotifier mWallpaperChangedNotifier; 43 private Bitmap mCurrentHomeWallpaper; 44 private Bitmap mCurrentLockWallpaper; 45 private Bitmap mPendingHomeWallpaper; 46 private Bitmap mPendingLockWallpaper; 47 private List<String> mHomeAttributions; 48 private String mHomeActionUrl; 49 @Destination 50 private int mDestination; 51 private WallpaperPersister.SetWallpaperCallback mCallback; 52 private boolean mFailNextCall; 53 private Rect mCropRect; 54 private float mScale; 55 @WallpaperPosition 56 private int mWallpaperPosition; 57 private WallpaperInfo mWallpaperInfo; 58 TestWallpaperPersister(Context appContext)59 public TestWallpaperPersister(Context appContext) { 60 mAppContext = appContext; 61 mPrefs = InjectorProvider.getInjector().getPreferences(appContext); 62 mWallpaperChangedNotifier = WallpaperChangedNotifier.getInstance(); 63 64 mCurrentHomeWallpaper = null; 65 mCurrentLockWallpaper = null; 66 mPendingHomeWallpaper = null; 67 mPendingLockWallpaper = null; 68 mWallpaperInfo = null; 69 mFailNextCall = false; 70 mScale = -1.0f; 71 } 72 73 @Override setIndividualWallpaper(final WallpaperInfo wallpaperInfo, Asset asset, @Nullable final Rect cropRect, final float scale, final @Destination int destination, final WallpaperPersister.SetWallpaperCallback callback)74 public void setIndividualWallpaper(final WallpaperInfo wallpaperInfo, Asset asset, 75 @Nullable final Rect cropRect, final float scale, final @Destination int destination, 76 final WallpaperPersister.SetWallpaperCallback callback) { 77 asset.decodeBitmap(50, 50, bitmap -> { 78 if (destination == DEST_HOME_SCREEN || destination == DEST_BOTH) { 79 mPendingHomeWallpaper = bitmap; 80 mPrefs.setHomeWallpaperAttributions(wallpaperInfo.getAttributions(mAppContext)); 81 mPrefs.setWallpaperPresentationMode( 82 WallpaperPreferences.PRESENTATION_MODE_STATIC); 83 mPrefs.setHomeWallpaperRemoteId(wallpaperInfo.getWallpaperId()); 84 } 85 if (destination == DEST_LOCK_SCREEN || destination == DEST_BOTH) { 86 mPendingLockWallpaper = bitmap; 87 mPrefs.setLockWallpaperAttributions(wallpaperInfo.getAttributions(mAppContext)); 88 } 89 mDestination = destination; 90 mCallback = callback; 91 mCropRect = cropRect; 92 mScale = scale; 93 mWallpaperInfo = wallpaperInfo; 94 }); 95 } 96 97 @Override setIndividualWallpaperWithPosition(Activity activity, WallpaperInfo wallpaper, @WallpaperPosition int wallpaperPosition, SetWallpaperCallback callback)98 public void setIndividualWallpaperWithPosition(Activity activity, WallpaperInfo wallpaper, 99 @WallpaperPosition int wallpaperPosition, SetWallpaperCallback callback) { 100 wallpaper.getAsset(activity).decodeBitmap(50, 50, new BitmapReceiver() { 101 @Override 102 public void onBitmapDecoded(@Nullable Bitmap bitmap) { 103 mPendingHomeWallpaper = bitmap; 104 mPrefs.setHomeWallpaperAttributions(wallpaper.getAttributions(mAppContext)); 105 mPrefs.setHomeWallpaperBaseImageUrl(wallpaper.getBaseImageUrl()); 106 mPrefs.setHomeWallpaperActionUrl(wallpaper.getActionUrl(mAppContext)); 107 mPrefs.setHomeWallpaperCollectionId(wallpaper.getCollectionId(mAppContext)); 108 mPrefs.setHomeWallpaperRemoteId(wallpaper.getWallpaperId()); 109 mPrefs.setWallpaperPresentationMode(WallpaperPreferences.PRESENTATION_MODE_STATIC); 110 mPendingLockWallpaper = bitmap; 111 mPrefs.setLockWallpaperAttributions(wallpaper.getAttributions(mAppContext)); 112 113 mDestination = WallpaperPersister.DEST_BOTH; 114 mCallback = callback; 115 mWallpaperPosition = wallpaperPosition; 116 mWallpaperInfo = wallpaper; 117 } 118 }); 119 } 120 121 @Override setWallpaperInRotation(Bitmap wallpaperBitmap, List<String> attributions, int actionLabelRes, int actionIconRes, String actionUrl, String collectionId)122 public boolean setWallpaperInRotation(Bitmap wallpaperBitmap, List<String> attributions, 123 int actionLabelRes, int actionIconRes, String actionUrl, String collectionId) { 124 if (mFailNextCall) { 125 return false; 126 } 127 128 mCurrentHomeWallpaper = wallpaperBitmap; 129 mCurrentLockWallpaper = wallpaperBitmap; 130 mHomeAttributions = attributions; 131 mHomeActionUrl = actionUrl; 132 return true; 133 } 134 135 @Override setWallpaperBitmapInNextRotation(Bitmap wallpaperBitmap)136 public int setWallpaperBitmapInNextRotation(Bitmap wallpaperBitmap) { 137 mCurrentHomeWallpaper = wallpaperBitmap; 138 mCurrentLockWallpaper = wallpaperBitmap; 139 return 1; 140 } 141 142 @Override finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl, int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId)143 public boolean finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl, 144 int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId) { 145 mHomeAttributions = attributions; 146 mHomeActionUrl = actionUrl; 147 return true; 148 } 149 150 /** Returns mock system wallpaper bitmap. */ getCurrentHomeWallpaper()151 public Bitmap getCurrentHomeWallpaper() { 152 return mCurrentHomeWallpaper; 153 } 154 155 /** Returns mock lock screen wallpaper bitmap. */ getCurrentLockWallpaper()156 public Bitmap getCurrentLockWallpaper() { 157 return mCurrentLockWallpaper; 158 } 159 160 /** Returns mock home attributions. */ getHomeAttributions()161 public List<String> getHomeAttributions() { 162 return mHomeAttributions; 163 } 164 165 /** Returns the home wallpaper action URL. */ getHomeActionUrl()166 public String getHomeActionUrl() { 167 return mHomeActionUrl; 168 } 169 170 /** Returns the Destination a wallpaper was most recently set on. */ 171 @Destination getLastDestination()172 public int getLastDestination() { 173 return mDestination; 174 } 175 176 /** 177 * Sets whether the next "set wallpaper" operation should fail or succeed. 178 */ setFailNextCall(Boolean failNextCall)179 public void setFailNextCall(Boolean failNextCall) { 180 mFailNextCall = failNextCall; 181 } 182 183 /** 184 * Implemented so synchronous test methods can control the completion of what would otherwise be 185 * an asynchronous operation. 186 */ finishSettingWallpaper()187 public void finishSettingWallpaper() { 188 if (mFailNextCall) { 189 mCallback.onError(null /* throwable */); 190 } else { 191 if (mDestination == DEST_HOME_SCREEN || mDestination == DEST_BOTH) { 192 mCurrentHomeWallpaper = mPendingHomeWallpaper; 193 mPendingHomeWallpaper = null; 194 } 195 if (mDestination == DEST_LOCK_SCREEN || mDestination == DEST_BOTH) { 196 mCurrentLockWallpaper = mPendingLockWallpaper; 197 mPendingLockWallpaper = null; 198 } 199 mCallback.onSuccess(mWallpaperInfo); 200 mWallpaperChangedNotifier.notifyWallpaperChanged(); 201 } 202 } 203 204 @Override setWallpaperInfoInPreview(WallpaperInfo wallpaperInfo)205 public void setWallpaperInfoInPreview(WallpaperInfo wallpaperInfo) { 206 } 207 208 @Override onLiveWallpaperSet()209 public void onLiveWallpaperSet() { 210 } 211 212 /** Returns the last requested wallpaper bitmap scale. */ getScale()213 public float getScale() { 214 return mScale; 215 } 216 217 /** Returns the last requested wallpaper crop. */ getCropRect()218 public Rect getCropRect() { 219 return mCropRect; 220 } 221 222 /** Returns the last selected wallpaper position option. */ 223 @WallpaperPosition getWallpaperPosition()224 public int getWallpaperPosition() { 225 return mWallpaperPosition; 226 } 227 } 228