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