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