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