1 /* 2 * Copyright (C) 2023 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.drawable.Drawable; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import com.android.wallpaper.asset.Asset; 25 import com.android.wallpaper.model.InlinePreviewIntentFactory; 26 import com.android.wallpaper.model.LiveWallpaperInfo; 27 28 import java.util.Arrays; 29 import java.util.List; 30 import java.util.Objects; 31 32 /** 33 * Test model object for a wallpaper coming from a live wallpaper component. This is essentially a 34 * copy of {@link TestStaticWallpaperInfo} with the minimum changes required to show a preview. 35 */ 36 public class TestLiveWallpaperInfo extends LiveWallpaperInfo { 37 public static final int COLOR_DEFAULT = 0xff000000; 38 public static final Parcelable.Creator<TestLiveWallpaperInfo> CREATOR = 39 new Parcelable.Creator<TestLiveWallpaperInfo>() { 40 @Override 41 public TestLiveWallpaperInfo createFromParcel(Parcel in) { 42 return new TestLiveWallpaperInfo(in); 43 } 44 @Override 45 public TestLiveWallpaperInfo[] newArray(int size) { 46 return new TestLiveWallpaperInfo[size]; 47 } 48 }; 49 private int mPixelColor; 50 private TestAsset mAsset; 51 private TestAsset mThumbAsset; 52 private List<String> mAttributions; 53 private android.app.WallpaperInfo mWallpaperComponent; 54 private String mActionUrl; 55 private String mBaseImageUrl; 56 private String mCollectionId; 57 private String mWallpaperId; 58 private boolean mIsAssetCorrupt; 59 private int mBackupPermission; 60 61 /** Constructs a test WallpaperInfo object representing a 1x1 wallpaper of the given color. */ TestLiveWallpaperInfo(int pixelColor)62 public TestLiveWallpaperInfo(int pixelColor) { 63 this(pixelColor, null, "test-wallpaper"); 64 } 65 66 /** Constructs a test WallpaperInfo object representing a 1x1 wallpaper of the given color. */ TestLiveWallpaperInfo(int pixelColor, android.app.WallpaperInfo info, String id)67 public TestLiveWallpaperInfo(int pixelColor, android.app.WallpaperInfo info, String id) { 68 super(info); 69 mPixelColor = pixelColor; 70 mAttributions = Arrays.asList("Test wallpaper"); 71 mWallpaperComponent = info; 72 mIsAssetCorrupt = false; 73 mBackupPermission = BACKUP_ALLOWED; 74 mWallpaperId = id; 75 } 76 TestLiveWallpaperInfo(Parcel in)77 private TestLiveWallpaperInfo(Parcel in) { 78 super(in); 79 mPixelColor = in.readInt(); 80 mAttributions = in.createStringArrayList(); 81 mActionUrl = in.readString(); 82 mBaseImageUrl = in.readString(); 83 mCollectionId = in.readString(); 84 mWallpaperId = in.readString(); 85 mIsAssetCorrupt = in.readInt() == 1; 86 mBackupPermission = in.readInt(); 87 } 88 89 @Override getOverlayIcon(Context context)90 public Drawable getOverlayIcon(Context context) { 91 return null; 92 } 93 94 @Override getAttributions(Context context)95 public List<String> getAttributions(Context context) { 96 return mAttributions; 97 } 98 99 /** 100 * Override default "Test wallpaper" attributions for testing. 101 */ setAttributions(List<String> attributions)102 public void setAttributions(List<String> attributions) { 103 mAttributions = attributions; 104 } 105 106 @Override getActionDescription(Context context)107 public CharSequence getActionDescription(Context context) { 108 return null; 109 } 110 111 @Override getActionUrl(Context unused)112 public String getActionUrl(Context unused) { 113 return mActionUrl; 114 } 115 116 /** Sets the action URL for this wallpaper. */ setActionUrl(String actionUrl)117 public void setActionUrl(String actionUrl) { 118 mActionUrl = actionUrl; 119 } 120 121 @Override getBaseImageUrl()122 public String getBaseImageUrl() { 123 return mBaseImageUrl; 124 } 125 126 /** Sets the base image URL for this wallpaper. */ setBaseImageUrl(String baseImageUrl)127 public void setBaseImageUrl(String baseImageUrl) { 128 mBaseImageUrl = baseImageUrl; 129 } 130 131 @Override getCollectionId(Context unused)132 public String getCollectionId(Context unused) { 133 return mCollectionId; 134 } 135 136 /** Sets the collection ID for this wallpaper. */ setCollectionId(String collectionId)137 public void setCollectionId(String collectionId) { 138 mCollectionId = collectionId; 139 } 140 141 @Override getWallpaperId()142 public String getWallpaperId() { 143 return mWallpaperId; 144 145 } 146 /** Sets the ID for this wallpaper. */ setWallpaperId(String wallpaperId)147 public void setWallpaperId(String wallpaperId) { 148 mWallpaperId = wallpaperId; 149 } 150 151 @Override getAsset(Context context)152 public Asset getAsset(Context context) { 153 if (mAsset == null) { 154 mAsset = new TestAsset(mPixelColor, mIsAssetCorrupt); 155 } 156 return mAsset; 157 } 158 159 @Override getThumbAsset(Context context)160 public Asset getThumbAsset(Context context) { 161 if (mThumbAsset == null) { 162 mThumbAsset = new TestAsset(mPixelColor, mIsAssetCorrupt); 163 } 164 return mThumbAsset; 165 } 166 167 @Override showPreview(Activity srcActivity, InlinePreviewIntentFactory inlinePreviewIntentFactory, int requestCode, boolean isAssetIdPresent)168 public void showPreview(Activity srcActivity, 169 InlinePreviewIntentFactory inlinePreviewIntentFactory, int requestCode, 170 boolean isAssetIdPresent) { 171 srcActivity.startActivityForResult( 172 inlinePreviewIntentFactory.newIntent(srcActivity, this, isAssetIdPresent, 173 false), 174 requestCode); 175 } 176 177 @Override showPreview(Activity srcActivity, InlinePreviewIntentFactory inlinePreviewIntentFactory, int requestCode, boolean isAssetIdPresent, boolean shouldRefreshCategory)178 public void showPreview(Activity srcActivity, 179 InlinePreviewIntentFactory inlinePreviewIntentFactory, int requestCode, 180 boolean isAssetIdPresent, boolean shouldRefreshCategory) { 181 srcActivity.startActivityForResult( 182 inlinePreviewIntentFactory.newIntent(srcActivity, this, isAssetIdPresent, 183 shouldRefreshCategory), 184 requestCode); 185 } 186 187 @Override 188 @BackupPermission getBackupPermission()189 public int getBackupPermission() { 190 return mBackupPermission; 191 } 192 setBackupPermission(@ackupPermission int backupPermission)193 public void setBackupPermission(@BackupPermission int backupPermission) { 194 mBackupPermission = backupPermission; 195 } 196 197 @Override getWallpaperComponent()198 public android.app.WallpaperInfo getWallpaperComponent() { 199 return mWallpaperComponent; 200 } 201 setWallpaperComponent(android.app.WallpaperInfo wallpaperComponent)202 public void setWallpaperComponent(android.app.WallpaperInfo wallpaperComponent) { 203 mWallpaperComponent = wallpaperComponent; 204 } 205 206 @Override isSystemAppWallpaper()207 public boolean isSystemAppWallpaper() { 208 return true; 209 } 210 211 /** 212 * Simulates that the {@link Asset} instances returned by calls to #getAsset and #getThumbAsset 213 * on this object are "corrupt" and will fail to perform decode operations such as 214 * #decodeBitmap, #decodeBitmapRegion, #decodeRawDimensions, etc (these methods will call their 215 * callbacks with null instead of meaningful objects). 216 */ corruptAssets()217 public void corruptAssets() { 218 mIsAssetCorrupt = true; 219 } 220 221 @Override describeContents()222 public int describeContents() { 223 return 0; 224 } 225 226 @Override writeToParcel(Parcel parcel, int i)227 public void writeToParcel(Parcel parcel, int i) { 228 super.writeToParcel(parcel, i); 229 parcel.writeInt(mPixelColor); 230 parcel.writeStringList(mAttributions); 231 parcel.writeString(mActionUrl); 232 parcel.writeString(mBaseImageUrl); 233 parcel.writeString(mCollectionId); 234 parcel.writeString(mWallpaperId); 235 parcel.writeInt(mIsAssetCorrupt ? 1 : 0); 236 parcel.writeInt(mBackupPermission); 237 } 238 239 @Override equals(Object object)240 public boolean equals(Object object) { 241 if (object instanceof TestLiveWallpaperInfo other) { 242 if (this == other) return true; 243 if (!Objects.equals(getWallpaperId(), other.getWallpaperId())) return false; 244 return mPixelColor == other.mPixelColor; 245 } 246 return false; 247 } 248 249 @Override hashCode()250 public int hashCode() { 251 return mPixelColor; 252 } 253 } 254