blob: 28c2f432ff624f286169851d7010e4f37752e010 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
2 * Copyright (C) 2017 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 */
16package com.android.wallpaper.model;
17
18import android.app.Activity;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070019import android.app.WallpaperColors;
Jon Miranda16ea1b12017-12-12 14:52:48 -080020import android.content.Context;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070021import android.graphics.Bitmap;
22import android.graphics.Color;
Jon Miranda16ea1b12017-12-12 14:52:48 -080023import android.graphics.drawable.Drawable;
Clément Julliardea1638d2018-05-21 19:15:17 -070024import android.net.Uri;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070025import android.os.Parcel;
Jon Miranda16ea1b12017-12-12 14:52:48 -080026import android.os.Parcelable;
Jon Miranda16ea1b12017-12-12 14:52:48 -080027
Santiago Etchebeheredf3e1162019-01-29 10:50:05 -080028import androidx.annotation.DrawableRes;
29import androidx.annotation.IntDef;
30import androidx.annotation.StringRes;
31
Santiago Etchebehered1bd5092018-04-18 16:03:30 -070032import com.android.wallpaper.R;
Jon Miranda16ea1b12017-12-12 14:52:48 -080033import com.android.wallpaper.asset.Asset;
34
John Pan50fb3122022-02-24 19:18:18 +080035import java.util.Iterator;
Jon Miranda16ea1b12017-12-12 14:52:48 -080036import java.util.List;
John Pan50fb3122022-02-24 19:18:18 +080037import java.util.PriorityQueue;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070038import java.util.concurrent.CompletableFuture;
39import java.util.concurrent.ExecutorService;
40import java.util.concurrent.Executors;
41import java.util.concurrent.Future;
Jon Miranda16ea1b12017-12-12 14:52:48 -080042
43/**
44 * Interface for wallpaper info model.
45 */
46public abstract class WallpaperInfo implements Parcelable {
47
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070048 private static final ExecutorService sExecutor = Executors.newCachedThreadPool();
Kunhung Licbd44702021-12-23 14:35:24 +080049 private ColorInfo mColorInfo = new ColorInfo();
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070050
John Pan50fb3122022-02-24 19:18:18 +080051 private PriorityQueue<String> mEffectNames = new PriorityQueue<>();
52
Kunhung Licbd44702021-12-23 14:35:24 +080053 public WallpaperInfo() {
54 }
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070055
56 protected WallpaperInfo(Parcel in) {
Kunhung Licbd44702021-12-23 14:35:24 +080057 mColorInfo = new ColorInfo(in.readParcelable(WallpaperColors.class.getClassLoader()),
58 in.readInt());
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070059 }
60
61 @Override
62 public void writeToParcel(Parcel parcel, int flags) {
Kunhung Licbd44702021-12-23 14:35:24 +080063 parcel.writeParcelable(mColorInfo.getWallpaperColors(), flags);
64 parcel.writeInt(mColorInfo.getPlaceholderColor());
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070065 }
66
Santiago Etchebehered1bd5092018-04-18 16:03:30 -070067 @DrawableRes
68 public static int getDefaultActionIcon() {
69 return R.drawable.ic_explore_24px;
70 }
71
72 @StringRes
73 public static int getDefaultActionLabel() {
74 return R.string.explore;
75 }
76
Jon Miranda16ea1b12017-12-12 14:52:48 -080077 public static final int BACKUP_NOT_ALLOWED = 0;
78 public static final int BACKUP_ALLOWED = 1;
79
80 /**
81 * @param context
82 * @return The title for this wallpaper, if applicable (as in a wallpaper "app" or live
83 * wallpaper), or null if not applicable.
84 */
85 public String getTitle(Context context) {
86 return null;
87 }
88
89 /**
Santiago Etchebeheredf3e1162019-01-29 10:50:05 -080090 * @return The available attributions for this wallpaper, as a list of strings. These represent
91 * the author / website or any other attribution required to be displayed for this wallpaper
92 * regarding authorship, ownership, etc.
Jon Miranda16ea1b12017-12-12 14:52:48 -080093 */
94 public abstract List<String> getAttributions(Context context);
95
96 /**
97 * Returns the base (remote) image URL for this wallpaper, or null if none exists.
98 */
99 public String getBaseImageUrl() {
100 return null;
101 }
102
103 /**
104 * Returns the action or "explore" URL for the wallpaper, or null if none exists.
105 */
106 public String getActionUrl(Context unused) {
107 return null;
108 }
109
Clément Julliardea1638d2018-05-21 19:15:17 -0700110 /** Returns the URI corresponding to the wallpaper, or null if none exists. */
111 public Uri getUri() {
112 return null;
113 }
114
Jon Miranda16ea1b12017-12-12 14:52:48 -0800115 /**
Santiago Etchebehered1bd5092018-04-18 16:03:30 -0700116 * Returns the icon to use to represent the action link corresponding to
117 * {@link #getActionUrl(Context)}
118 */
119 @DrawableRes
Santiago Etchebeheree0810d02018-05-10 17:39:40 -0700120 public int getActionIconRes(Context context) {
Santiago Etchebehered1bd5092018-04-18 16:03:30 -0700121 return getDefaultActionIcon();
122 }
123
124 /**
125 * Returns the label to use for the action link corresponding to
126 * {@link #getActionUrl(Context)}
127 */
128 @StringRes
Santiago Etchebeheree0810d02018-05-10 17:39:40 -0700129 public int getActionLabelRes(Context context) {
Santiago Etchebehered1bd5092018-04-18 16:03:30 -0700130 return getDefaultActionLabel();
131 }
132
133 /**
Jon Miranda16ea1b12017-12-12 14:52:48 -0800134 * @param context
135 * @return An overlay icon to be used instead of a thumbnail, if appropriate, or null if not
136 * applicable.
137 */
138 public Drawable getOverlayIcon(Context context) {
139 return null;
140 }
141
142 ;
143
144 @Override
145 public int describeContents() {
146 return 0;
147 }
148
149 /**
150 * @param context The client application's context.
151 * @return The {@link Asset} representing the wallpaper image.
152 */
153 public abstract Asset getAsset(Context context);
154
155 /**
156 * @param context The client application's context.
157 * @return The {@link Asset} representing the wallpaper's thumbnail.
158 */
159 public abstract Asset getThumbAsset(Context context);
160
161 /**
162 * @param context The client application's context.
163 * @return An {@link Asset} that is appropriately sized to be directly set to the desktop. By
164 * default, this just the full wallpaper image asset (#getAsset) but subclasses may provide an
165 * Asset sized exactly for the device's primary display (i.e., cropped prior to providing a
166 * bitmap or input stream).
167 */
168 public Asset getDesktopAsset(Context context) {
169 return getAsset(context);
170 }
171
172 /**
173 * @return the {@link android.app.WallpaperInfo} associated with this wallpaper, which is
174 * generally present for live wallpapers, or null if there is none.
175 */
176 public android.app.WallpaperInfo getWallpaperComponent() {
177 return null;
178 }
179
180 /**
181 * Returns the ID of the collection this image is associated with, if any.
182 */
183 public abstract String getCollectionId(Context context);
184
185 /**
186 * Returns the ID of this wallpaper or null if there is no ID.
187 */
188 public String getWallpaperId() {
189 return null;
190 }
191
192 /**
zonghuayang100ec962021-11-10 16:07:18 +0800193 * Returns the distinct ID of the stored wallpaper or null if there is no ID.
194 */
195 public String getStoredWallpaperId(Context context) {
196 if (getWallpaperId() == null) {
197 return null;
198 }
199 return getCollectionId(context) + "-" + getWallpaperId();
200 }
201
202 /**
Jon Miranda16ea1b12017-12-12 14:52:48 -0800203 * Returns whether backup is allowed for this wallpaper.
204 */
205 @BackupPermission
206 public int getBackupPermission() {
207 return BACKUP_ALLOWED;
208 }
209
210 /**
211 * Shows the appropriate preview activity for this WallpaperInfo.
212 *
213 * @param srcActivity
214 * @param factory A factory for showing the inline preview activity for within this app.
215 * Only used for certain WallpaperInfo implementations that require an inline preview
216 * (as opposed to some external preview activity).
217 * @param requestCode Request code to pass in when starting the inline preview activity.
218 */
219 public abstract void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory,
220 int requestCode);
221
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700222 /**
Kunhung Licbd44702021-12-23 14:35:24 +0800223 * Returns a Future to obtain a wallpaper color and a placeholder color calculated in a
224 * background thread for this wallpaper's thumbnail.
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700225 * If it's already available, the Future will return the color immediately.
226 * This is intended to be a "best effort" attempt and might not obtain a color if no low res
227 * thumbnail is available.
228 */
Kunhung Licbd44702021-12-23 14:35:24 +0800229 public Future<ColorInfo> computeColorInfo(Context context) {
230 if (mColorInfo.getWallpaperColors() != null
231 && mColorInfo.getPlaceholderColor() != Color.TRANSPARENT) {
232 return CompletableFuture.completedFuture(mColorInfo);
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700233 }
234 final Context appContext = context.getApplicationContext();
235 return sExecutor.submit(() -> {
236 synchronized (WallpaperInfo.this) {
Kunhung Licbd44702021-12-23 14:35:24 +0800237 if (mColorInfo.getWallpaperColors() != null
238 && mColorInfo.getPlaceholderColor() != Color.TRANSPARENT) {
239 return mColorInfo;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700240 }
Kunhung Licbd44702021-12-23 14:35:24 +0800241
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700242 Asset thumbAsset = getThumbAsset(appContext);
243 Bitmap lowResBitmap = thumbAsset.getLowResBitmap(appContext);
244 if (lowResBitmap == null) {
Kunhung Licbd44702021-12-23 14:35:24 +0800245 return new ColorInfo(
246 new WallpaperColors(Color.valueOf(Color.TRANSPARENT), null, null),
247 Color.TRANSPARENT);
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700248 }
Kunhung Licbd44702021-12-23 14:35:24 +0800249 mColorInfo = new ColorInfo(WallpaperColors.fromBitmap(lowResBitmap));
250 return mColorInfo;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700251 }
252 });
253 }
254
Jon Miranda16ea1b12017-12-12 14:52:48 -0800255 /**
John Pan50fb3122022-02-24 19:18:18 +0800256 * Remove the effect name from this wallpaper, only use it for logging.
257 */
258 public void removeEffectName(String effect) {
259 mEffectNames.remove(effect);
260 }
261
262 /**
263 * Add the effect name apply with this wallpaper, only use it for logging.
264 */
265 public void addEffectName(String effect) {
266 mEffectNames.add(effect);
267 }
268
269 /**
270 * Returns the effects apply with this wallpaper.
271 */
272 public String getEffectNames() {
273 if (mEffectNames.isEmpty()) {
274 return null;
275 }
276 String effectNames = "";
277 Iterator value = mEffectNames.iterator();
278 while (value.hasNext()) {
279 if (!effectNames.isEmpty()) {
280 effectNames += ",";
281 }
282 effectNames += value.next();
283 }
284
285 return effectNames;
286 }
287
288 /**
Jon Miranda16ea1b12017-12-12 14:52:48 -0800289 * Whether backup is allowed for this type of wallpaper.
290 */
291 @IntDef({
292 BACKUP_NOT_ALLOWED,
293 BACKUP_ALLOWED
294 })
295 public @interface BackupPermission {
296 }
Kunhung Licbd44702021-12-23 14:35:24 +0800297
298 /**
299 * Inner class to keep wallpaper colors and placeholder color.
300 */
301 public static class ColorInfo {
302 private WallpaperColors mWallpaperColors;
303 private Integer mPlaceholderColor = Color.TRANSPARENT;
304
305 public ColorInfo() {
306 }
307
308 public ColorInfo(WallpaperColors wallpaperColors) {
309 mWallpaperColors = wallpaperColors;
310 if (mWallpaperColors != null) {
311 mPlaceholderColor = mWallpaperColors.getPrimaryColor().toArgb();
312 }
313 }
314
315 public ColorInfo(WallpaperColors wallpaperColors, Integer placeholderColor) {
316 mWallpaperColors = wallpaperColors;
317 mPlaceholderColor = placeholderColor;
318 }
319
320 public WallpaperColors getWallpaperColors() {
321 return mWallpaperColors;
322 }
323
324 public Integer getPlaceholderColor() {
325 return mPlaceholderColor;
326 }
327 }
Jon Miranda16ea1b12017-12-12 14:52:48 -0800328}