Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.wallpaper.model; |
| 17 | |
| 18 | import android.app.Activity; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 19 | import android.app.WallpaperColors; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.Color; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Clément Julliard | ea1638d | 2018-05-21 19:15:17 -0700 | [diff] [blame] | 24 | import android.net.Uri; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 25 | import android.os.Parcel; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 26 | import android.os.Parcelable; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 27 | |
Santiago Etchebehere | df3e116 | 2019-01-29 10:50:05 -0800 | [diff] [blame] | 28 | import androidx.annotation.DrawableRes; |
| 29 | import androidx.annotation.IntDef; |
| 30 | import androidx.annotation.StringRes; |
| 31 | |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 32 | import com.android.wallpaper.R; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 33 | import com.android.wallpaper.asset.Asset; |
| 34 | |
John Pan | 50fb312 | 2022-02-24 19:18:18 +0800 | [diff] [blame] | 35 | import java.util.Iterator; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 36 | import java.util.List; |
John Pan | 50fb312 | 2022-02-24 19:18:18 +0800 | [diff] [blame] | 37 | import java.util.PriorityQueue; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 38 | import java.util.concurrent.CompletableFuture; |
| 39 | import java.util.concurrent.ExecutorService; |
| 40 | import java.util.concurrent.Executors; |
| 41 | import java.util.concurrent.Future; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Interface for wallpaper info model. |
| 45 | */ |
| 46 | public abstract class WallpaperInfo implements Parcelable { |
| 47 | |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 48 | private static final ExecutorService sExecutor = Executors.newCachedThreadPool(); |
Kunhung Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 49 | private ColorInfo mColorInfo = new ColorInfo(); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 50 | |
John Pan | 50fb312 | 2022-02-24 19:18:18 +0800 | [diff] [blame] | 51 | private PriorityQueue<String> mEffectNames = new PriorityQueue<>(); |
| 52 | |
Kunhung Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 53 | public WallpaperInfo() { |
| 54 | } |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 55 | |
| 56 | protected WallpaperInfo(Parcel in) { |
Kunhung Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 57 | mColorInfo = new ColorInfo(in.readParcelable(WallpaperColors.class.getClassLoader()), |
| 58 | in.readInt()); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void writeToParcel(Parcel parcel, int flags) { |
Kunhung Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 63 | parcel.writeParcelable(mColorInfo.getWallpaperColors(), flags); |
| 64 | parcel.writeInt(mColorInfo.getPlaceholderColor()); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 67 | @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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 77 | 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 Etchebehere | df3e116 | 2019-01-29 10:50:05 -0800 | [diff] [blame] | 90 | * @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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 93 | */ |
| 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 Julliard | ea1638d | 2018-05-21 19:15:17 -0700 | [diff] [blame] | 110 | /** Returns the URI corresponding to the wallpaper, or null if none exists. */ |
| 111 | public Uri getUri() { |
| 112 | return null; |
| 113 | } |
| 114 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 115 | /** |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 116 | * Returns the icon to use to represent the action link corresponding to |
| 117 | * {@link #getActionUrl(Context)} |
| 118 | */ |
| 119 | @DrawableRes |
Santiago Etchebehere | e0810d0 | 2018-05-10 17:39:40 -0700 | [diff] [blame] | 120 | public int getActionIconRes(Context context) { |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 121 | 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 Etchebehere | e0810d0 | 2018-05-10 17:39:40 -0700 | [diff] [blame] | 129 | public int getActionLabelRes(Context context) { |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 130 | return getDefaultActionLabel(); |
| 131 | } |
| 132 | |
| 133 | /** |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 134 | * @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 | /** |
zonghuayang | 100ec96 | 2021-11-10 16:07:18 +0800 | [diff] [blame] | 193 | * 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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 203 | * 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 Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 222 | /** |
Kunhung Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 223 | * Returns a Future to obtain a wallpaper color and a placeholder color calculated in a |
| 224 | * background thread for this wallpaper's thumbnail. |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 225 | * 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 Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 229 | public Future<ColorInfo> computeColorInfo(Context context) { |
| 230 | if (mColorInfo.getWallpaperColors() != null |
| 231 | && mColorInfo.getPlaceholderColor() != Color.TRANSPARENT) { |
| 232 | return CompletableFuture.completedFuture(mColorInfo); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 233 | } |
| 234 | final Context appContext = context.getApplicationContext(); |
| 235 | return sExecutor.submit(() -> { |
| 236 | synchronized (WallpaperInfo.this) { |
Kunhung Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 237 | if (mColorInfo.getWallpaperColors() != null |
| 238 | && mColorInfo.getPlaceholderColor() != Color.TRANSPARENT) { |
| 239 | return mColorInfo; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 240 | } |
Kunhung Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 241 | |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 242 | Asset thumbAsset = getThumbAsset(appContext); |
| 243 | Bitmap lowResBitmap = thumbAsset.getLowResBitmap(appContext); |
| 244 | if (lowResBitmap == null) { |
Kunhung Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 245 | return new ColorInfo( |
| 246 | new WallpaperColors(Color.valueOf(Color.TRANSPARENT), null, null), |
| 247 | Color.TRANSPARENT); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 248 | } |
Kunhung Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 249 | mColorInfo = new ColorInfo(WallpaperColors.fromBitmap(lowResBitmap)); |
| 250 | return mColorInfo; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 251 | } |
| 252 | }); |
| 253 | } |
| 254 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 255 | /** |
John Pan | 50fb312 | 2022-02-24 19:18:18 +0800 | [diff] [blame] | 256 | * 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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 289 | * 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 Li | cbd4470 | 2021-12-23 14:35:24 +0800 | [diff] [blame] | 297 | |
| 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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 328 | } |