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