1 /* 2 * Copyright (C) 2009 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 17 package android.app; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.RawRes; 23 import android.annotation.RequiresPermission; 24 import android.annotation.SdkConstant; 25 import android.annotation.SdkConstant.SdkConstantType; 26 import android.annotation.SystemApi; 27 import android.annotation.SystemService; 28 import android.annotation.TestApi; 29 import android.annotation.UiContext; 30 import android.compat.annotation.UnsupportedAppUsage; 31 import android.content.ComponentName; 32 import android.content.ContentResolver; 33 import android.content.Context; 34 import android.content.Intent; 35 import android.content.pm.PackageManager; 36 import android.content.pm.ResolveInfo; 37 import android.content.res.Configuration; 38 import android.content.res.Resources; 39 import android.content.res.Resources.NotFoundException; 40 import android.graphics.Bitmap; 41 import android.graphics.BitmapFactory; 42 import android.graphics.BitmapRegionDecoder; 43 import android.graphics.Canvas; 44 import android.graphics.ColorFilter; 45 import android.graphics.ColorSpace; 46 import android.graphics.ImageDecoder; 47 import android.graphics.Matrix; 48 import android.graphics.Paint; 49 import android.graphics.PixelFormat; 50 import android.graphics.PorterDuff; 51 import android.graphics.PorterDuffXfermode; 52 import android.graphics.Rect; 53 import android.graphics.RectF; 54 import android.graphics.drawable.BitmapDrawable; 55 import android.graphics.drawable.Drawable; 56 import android.net.Uri; 57 import android.os.Build; 58 import android.os.Bundle; 59 import android.os.DeadSystemException; 60 import android.os.Environment; 61 import android.os.FileUtils; 62 import android.os.Handler; 63 import android.os.IBinder; 64 import android.os.Looper; 65 import android.os.ParcelFileDescriptor; 66 import android.os.RemoteException; 67 import android.os.StrictMode; 68 import android.os.SystemProperties; 69 import android.text.TextUtils; 70 import android.util.ArrayMap; 71 import android.util.ArraySet; 72 import android.util.Log; 73 import android.util.Pair; 74 import android.view.Display; 75 import android.view.WindowManagerGlobal; 76 77 import com.android.internal.R; 78 79 import libcore.io.IoUtils; 80 81 import java.io.BufferedInputStream; 82 import java.io.ByteArrayOutputStream; 83 import java.io.File; 84 import java.io.FileInputStream; 85 import java.io.FileOutputStream; 86 import java.io.IOException; 87 import java.io.InputStream; 88 import java.lang.annotation.Retention; 89 import java.lang.annotation.RetentionPolicy; 90 import java.util.ArrayList; 91 import java.util.Arrays; 92 import java.util.HashSet; 93 import java.util.List; 94 import java.util.Set; 95 import java.util.concurrent.CountDownLatch; 96 import java.util.concurrent.TimeUnit; 97 98 /** 99 * Provides access to the system wallpaper. With WallpaperManager, you can 100 * get the current wallpaper, get the desired dimensions for the wallpaper, set 101 * the wallpaper, and more. 102 * 103 * <p> An app can check whether wallpapers are supported for the current user, by calling 104 * {@link #isWallpaperSupported()}, and whether setting of wallpapers is allowed, by calling 105 * {@link #isSetWallpaperAllowed()}. 106 */ 107 @SystemService(Context.WALLPAPER_SERVICE) 108 public class WallpaperManager { 109 private static String TAG = "WallpaperManager"; 110 private static boolean DEBUG = false; 111 private float mWallpaperXStep = -1; 112 private float mWallpaperYStep = -1; 113 private static final @NonNull RectF LOCAL_COLOR_BOUNDS = 114 new RectF(0, 0, 1, 1); 115 116 /** {@hide} */ 117 private static final String PROP_WALLPAPER = "ro.config.wallpaper"; 118 /** {@hide} */ 119 private static final String PROP_LOCK_WALLPAPER = "ro.config.lock_wallpaper"; 120 /** {@hide} */ 121 private static final String PROP_WALLPAPER_COMPONENT = "ro.config.wallpaper_component"; 122 /** {@hide} */ 123 private static final String VALUE_CMF_COLOR = 124 android.os.SystemProperties.get("ro.boot.hardware.color"); 125 /** {@hide} */ 126 private static final String WALLPAPER_CMF_PATH = "/wallpaper/image/"; 127 128 /** 129 * Activity Action: Show settings for choosing wallpaper. Do not use directly to construct 130 * an intent; instead, use {@link #getCropAndSetWallpaperIntent}. 131 * <p>Input: {@link Intent#getData} is the URI of the image to crop and set as wallpaper. 132 * <p>Output: RESULT_OK if user decided to crop/set the wallpaper, RESULT_CANCEL otherwise 133 * Activities that support this intent should specify a MIME filter of "image/*" 134 */ 135 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 136 public static final String ACTION_CROP_AND_SET_WALLPAPER = 137 "android.service.wallpaper.CROP_AND_SET_WALLPAPER"; 138 139 /** 140 * Launch an activity for the user to pick the current global live 141 * wallpaper. 142 */ 143 public static final String ACTION_LIVE_WALLPAPER_CHOOSER 144 = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER"; 145 146 /** 147 * Directly launch live wallpaper preview, allowing the user to immediately 148 * confirm to switch to a specific live wallpaper. You must specify 149 * {@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of 150 * a live wallpaper component that is to be shown. 151 */ 152 public static final String ACTION_CHANGE_LIVE_WALLPAPER 153 = "android.service.wallpaper.CHANGE_LIVE_WALLPAPER"; 154 155 /** 156 * Extra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the 157 * ComponentName of a live wallpaper that should be shown as a preview, 158 * for the user to confirm. 159 */ 160 public static final String EXTRA_LIVE_WALLPAPER_COMPONENT 161 = "android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT"; 162 163 /** 164 * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER} 165 * which allows them to provide a custom large icon associated with this action. 166 */ 167 public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview"; 168 169 /** 170 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper 171 * host when the user taps on an empty area (not performing an action 172 * in the host). The x and y arguments are the location of the tap in 173 * screen coordinates. 174 */ 175 public static final String COMMAND_TAP = "android.wallpaper.tap"; 176 177 /** 178 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper 179 * host when the user releases a secondary pointer on an empty area 180 * (not performing an action in the host). The x and y arguments are 181 * the location of the secondary tap in screen coordinates. 182 */ 183 public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap"; 184 185 /** 186 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper 187 * host when the user drops an object into an area of the host. The x 188 * and y arguments are the location of the drop. 189 */ 190 public static final String COMMAND_DROP = "android.home.drop"; 191 192 /** 193 * Command for {@link #sendWallpaperCommand}: reported by System UI when the device is waking 194 * up. The x and y arguments are a location (possibly very roughly) corresponding to the action 195 * that caused the device to wake up. For example, if the power button was pressed, this will be 196 * the location on the screen nearest the power button. 197 * 198 * If the location is unknown or not applicable, x and y will be -1. 199 * 200 * @hide 201 */ 202 public static final String COMMAND_WAKING_UP = "android.wallpaper.wakingup"; 203 204 /** 205 * Command for {@link #sendWallpaperCommand}: reported by System UI when the device is going to 206 * sleep. The x and y arguments are a location (possibly very roughly) corresponding to the 207 * action that caused the device to go to sleep. For example, if the power button was pressed, 208 * this will be the location on the screen nearest the power button. 209 * 210 * If the location is unknown or not applicable, x and y will be -1. 211 * 212 * @hide 213 */ 214 public static final String COMMAND_GOING_TO_SLEEP = "android.wallpaper.goingtosleep"; 215 216 /** 217 * Command for {@link #sendWallpaperCommand}: reported when the wallpaper that was already 218 * set is re-applied by the user. 219 * @hide 220 */ 221 public static final String COMMAND_REAPPLY = "android.wallpaper.reapply"; 222 223 /** 224 * Extra passed back from setWallpaper() giving the new wallpaper's assigned ID. 225 * @hide 226 */ 227 public static final String EXTRA_NEW_WALLPAPER_ID = "android.service.wallpaper.extra.ID"; 228 229 /** 230 * Extra passed on {@link Intent.ACTION_WALLPAPER_CHANGED} indicating if wallpaper was set from 231 * a foreground app. 232 * @hide 233 */ 234 public static final String EXTRA_FROM_FOREGROUND_APP = 235 "android.service.wallpaper.extra.FROM_FOREGROUND_APP"; 236 237 // flags for which kind of wallpaper to act on 238 239 /** @hide */ 240 @IntDef(flag = true, prefix = { "FLAG_" }, value = { 241 FLAG_SYSTEM, 242 FLAG_LOCK 243 }) 244 @Retention(RetentionPolicy.SOURCE) 245 public @interface SetWallpaperFlags {} 246 247 /** 248 * Flag: set or retrieve the general system wallpaper. 249 */ 250 public static final int FLAG_SYSTEM = 1 << 0; 251 252 /** 253 * Flag: set or retrieve the lock-screen-specific wallpaper. 254 */ 255 public static final int FLAG_LOCK = 1 << 1; 256 257 private static final Object sSync = new Object[0]; 258 @UnsupportedAppUsage 259 private static Globals sGlobals; 260 private final Context mContext; 261 private final boolean mWcgEnabled; 262 private final ColorManagementProxy mCmProxy; 263 264 /** 265 * Special drawable that draws a wallpaper as fast as possible. Assumes 266 * no scaling or placement off (0,0) of the wallpaper (this should be done 267 * at the time the bitmap is loaded). 268 */ 269 static class FastBitmapDrawable extends Drawable { 270 private final Bitmap mBitmap; 271 private final int mWidth; 272 private final int mHeight; 273 private int mDrawLeft; 274 private int mDrawTop; 275 private final Paint mPaint; 276 FastBitmapDrawable(Bitmap bitmap)277 private FastBitmapDrawable(Bitmap bitmap) { 278 mBitmap = bitmap; 279 mWidth = bitmap.getWidth(); 280 mHeight = bitmap.getHeight(); 281 282 setBounds(0, 0, mWidth, mHeight); 283 284 mPaint = new Paint(); 285 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); 286 } 287 288 @Override draw(Canvas canvas)289 public void draw(Canvas canvas) { 290 canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, mPaint); 291 } 292 293 @Override getOpacity()294 public int getOpacity() { 295 return PixelFormat.OPAQUE; 296 } 297 298 @Override setBounds(int left, int top, int right, int bottom)299 public void setBounds(int left, int top, int right, int bottom) { 300 mDrawLeft = left + (right-left - mWidth) / 2; 301 mDrawTop = top + (bottom-top - mHeight) / 2; 302 } 303 304 @Override setAlpha(int alpha)305 public void setAlpha(int alpha) { 306 throw new UnsupportedOperationException("Not supported with this drawable"); 307 } 308 309 @Override setColorFilter(ColorFilter colorFilter)310 public void setColorFilter(ColorFilter colorFilter) { 311 throw new UnsupportedOperationException("Not supported with this drawable"); 312 } 313 314 @Override setDither(boolean dither)315 public void setDither(boolean dither) { 316 throw new UnsupportedOperationException("Not supported with this drawable"); 317 } 318 319 @Override setFilterBitmap(boolean filter)320 public void setFilterBitmap(boolean filter) { 321 throw new UnsupportedOperationException("Not supported with this drawable"); 322 } 323 324 @Override getIntrinsicWidth()325 public int getIntrinsicWidth() { 326 return mWidth; 327 } 328 329 @Override getIntrinsicHeight()330 public int getIntrinsicHeight() { 331 return mHeight; 332 } 333 334 @Override getMinimumWidth()335 public int getMinimumWidth() { 336 return mWidth; 337 } 338 339 @Override getMinimumHeight()340 public int getMinimumHeight() { 341 return mHeight; 342 } 343 } 344 345 private static class Globals extends IWallpaperManagerCallback.Stub { 346 private final IWallpaperManager mService; 347 private boolean mColorCallbackRegistered; 348 private final ArrayList<Pair<OnColorsChangedListener, Handler>> mColorListeners = 349 new ArrayList<>(); 350 private Bitmap mCachedWallpaper; 351 private int mCachedWallpaperUserId; 352 private Bitmap mDefaultWallpaper; 353 private Handler mMainLooperHandler; 354 private ArrayMap<RectF, ArraySet<LocalWallpaperColorConsumer>> mLocalColorAreas = 355 new ArrayMap<>(); 356 private ILocalWallpaperColorConsumer mLocalColorCallback = 357 new ILocalWallpaperColorConsumer.Stub() { 358 @Override 359 public void onColorsChanged(RectF area, WallpaperColors colors) { 360 ArraySet<LocalWallpaperColorConsumer> callbacks = 361 mLocalColorAreas.get(area); 362 if (callbacks == null) return; 363 for (LocalWallpaperColorConsumer callback: callbacks) { 364 callback.onColorsChanged(area, colors); 365 } 366 } 367 }; 368 Globals(IWallpaperManager service, Looper looper)369 Globals(IWallpaperManager service, Looper looper) { 370 mService = service; 371 mMainLooperHandler = new Handler(looper); 372 forgetLoadedWallpaper(); 373 } 374 onWallpaperChanged()375 public void onWallpaperChanged() { 376 /* The wallpaper has changed but we shouldn't eagerly load the 377 * wallpaper as that would be inefficient. Reset the cached wallpaper 378 * to null so if the user requests the wallpaper again then we'll 379 * fetch it. 380 */ 381 forgetLoadedWallpaper(); 382 } 383 384 /** 385 * Start listening to wallpaper color events. 386 * Will be called whenever someone changes their wallpaper or if a live wallpaper 387 * changes its colors. 388 * @param callback Listener 389 * @param handler Thread to call it from. Main thread if null. 390 * @param userId Owner of the wallpaper or UserHandle.USER_ALL 391 * @param displayId Caller comes from which display 392 */ addOnColorsChangedListener(@onNull OnColorsChangedListener callback, @Nullable Handler handler, int userId, int displayId)393 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener callback, 394 @Nullable Handler handler, int userId, int displayId) { 395 synchronized (this) { 396 if (!mColorCallbackRegistered) { 397 try { 398 mService.registerWallpaperColorsCallback(this, userId, displayId); 399 mColorCallbackRegistered = true; 400 } catch (RemoteException e) { 401 // Failed, service is gone 402 Log.w(TAG, "Can't register for color updates", e); 403 } 404 } 405 mColorListeners.add(new Pair<>(callback, handler)); 406 } 407 } 408 addOnColorsChangedListener(@onNull LocalWallpaperColorConsumer callback, @NonNull List<RectF> regions, int which, int userId, int displayId)409 public void addOnColorsChangedListener(@NonNull LocalWallpaperColorConsumer callback, 410 @NonNull List<RectF> regions, int which, int userId, int displayId) { 411 for (RectF area: regions) { 412 ArraySet<LocalWallpaperColorConsumer> callbacks = mLocalColorAreas.get(area); 413 if (callbacks == null) { 414 callbacks = new ArraySet<>(); 415 mLocalColorAreas.put(area, callbacks); 416 } 417 callbacks.add(callback); 418 } 419 try { 420 mService.addOnLocalColorsChangedListener(mLocalColorCallback , regions, which, 421 userId, displayId); 422 } catch (RemoteException e) { 423 // Can't get colors, connection lost. 424 Log.e(TAG, "Can't register for local color updates", e); 425 } 426 } 427 removeOnColorsChangedListener( @onNull LocalWallpaperColorConsumer callback, int which, int userId, int displayId)428 public void removeOnColorsChangedListener( 429 @NonNull LocalWallpaperColorConsumer callback, int which, int userId, 430 int displayId) { 431 final ArrayList<RectF> removeAreas = new ArrayList<>(); 432 for (RectF area : mLocalColorAreas.keySet()) { 433 ArraySet<LocalWallpaperColorConsumer> callbacks = mLocalColorAreas.get(area); 434 if (callbacks == null) continue; 435 callbacks.remove(callback); 436 if (callbacks.size() == 0) { 437 mLocalColorAreas.remove(area); 438 removeAreas.add(area); 439 } 440 } 441 try { 442 if (removeAreas.size() > 0) { 443 mService.removeOnLocalColorsChangedListener( 444 mLocalColorCallback, removeAreas, which, userId, displayId); 445 } 446 } catch (RemoteException e) { 447 // Can't get colors, connection lost. 448 Log.e(TAG, "Can't unregister for local color updates", e); 449 } 450 } 451 452 /** 453 * Stop listening to wallpaper color events. 454 * 455 * @param callback listener 456 * @param userId Owner of the wallpaper or UserHandle.USER_ALL 457 * @param displayId Which display is interested 458 */ removeOnColorsChangedListener(@onNull OnColorsChangedListener callback, int userId, int displayId)459 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback, 460 int userId, int displayId) { 461 synchronized (this) { 462 mColorListeners.removeIf(pair -> pair.first == callback); 463 464 if (mColorListeners.size() == 0 && mColorCallbackRegistered) { 465 mColorCallbackRegistered = false; 466 try { 467 mService.unregisterWallpaperColorsCallback(this, userId, displayId); 468 } catch (RemoteException e) { 469 // Failed, service is gone 470 Log.w(TAG, "Can't unregister color updates", e); 471 } 472 } 473 } 474 } 475 476 @Override onWallpaperColorsChanged(WallpaperColors colors, int which, int userId)477 public void onWallpaperColorsChanged(WallpaperColors colors, int which, int userId) { 478 synchronized (this) { 479 for (Pair<OnColorsChangedListener, Handler> listener : mColorListeners) { 480 Handler handler = listener.second; 481 if (listener.second == null) { 482 handler = mMainLooperHandler; 483 } 484 handler.post(() -> { 485 // Dealing with race conditions between posting a callback and 486 // removeOnColorsChangedListener being called. 487 boolean stillExists; 488 synchronized (sGlobals) { 489 stillExists = mColorListeners.contains(listener); 490 } 491 if (stillExists) { 492 listener.first.onColorsChanged(colors, which, userId); 493 } 494 }); 495 } 496 } 497 } 498 getWallpaperColors(int which, int userId, int displayId)499 WallpaperColors getWallpaperColors(int which, int userId, int displayId) { 500 if (which != FLAG_LOCK && which != FLAG_SYSTEM) { 501 throw new IllegalArgumentException( 502 "Must request colors for exactly one kind of wallpaper"); 503 } 504 505 try { 506 return mService.getWallpaperColors(which, userId, displayId); 507 } catch (RemoteException e) { 508 // Can't get colors, connection lost. 509 } 510 return null; 511 } 512 peekWallpaperBitmap(Context context, boolean returnDefault, @SetWallpaperFlags int which, ColorManagementProxy cmProxy)513 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault, 514 @SetWallpaperFlags int which, ColorManagementProxy cmProxy) { 515 return peekWallpaperBitmap(context, returnDefault, which, context.getUserId(), 516 false /* hardware */, cmProxy); 517 } 518 peekWallpaperBitmap(Context context, boolean returnDefault, @SetWallpaperFlags int which, int userId, boolean hardware, ColorManagementProxy cmProxy)519 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault, 520 @SetWallpaperFlags int which, int userId, boolean hardware, 521 ColorManagementProxy cmProxy) { 522 if (mService != null) { 523 try { 524 if (!mService.isWallpaperSupported(context.getOpPackageName())) { 525 return null; 526 } 527 } catch (RemoteException e) { 528 throw e.rethrowFromSystemServer(); 529 } 530 } 531 synchronized (this) { 532 if (mCachedWallpaper != null && mCachedWallpaperUserId == userId 533 && !mCachedWallpaper.isRecycled()) { 534 return mCachedWallpaper; 535 } 536 mCachedWallpaper = null; 537 mCachedWallpaperUserId = 0; 538 try { 539 mCachedWallpaper = getCurrentWallpaperLocked( 540 context, userId, hardware, cmProxy); 541 mCachedWallpaperUserId = userId; 542 } catch (OutOfMemoryError e) { 543 Log.w(TAG, "Out of memory loading the current wallpaper: " + e); 544 } catch (SecurityException e) { 545 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) { 546 Log.w(TAG, "No permission to access wallpaper, suppressing" 547 + " exception to avoid crashing legacy app."); 548 } else { 549 // Post-O apps really most sincerely need the permission. 550 throw e; 551 } 552 } 553 if (mCachedWallpaper != null) { 554 return mCachedWallpaper; 555 } 556 } 557 if (returnDefault) { 558 Bitmap defaultWallpaper = mDefaultWallpaper; 559 if (defaultWallpaper == null) { 560 defaultWallpaper = getDefaultWallpaper(context, which); 561 synchronized (this) { 562 mDefaultWallpaper = defaultWallpaper; 563 } 564 } 565 return defaultWallpaper; 566 } 567 return null; 568 } 569 forgetLoadedWallpaper()570 void forgetLoadedWallpaper() { 571 synchronized (this) { 572 mCachedWallpaper = null; 573 mCachedWallpaperUserId = 0; 574 mDefaultWallpaper = null; 575 } 576 } 577 getCurrentWallpaperLocked(Context context, int userId, boolean hardware, ColorManagementProxy cmProxy)578 private Bitmap getCurrentWallpaperLocked(Context context, int userId, boolean hardware, 579 ColorManagementProxy cmProxy) { 580 if (mService == null) { 581 Log.w(TAG, "WallpaperService not running"); 582 return null; 583 } 584 585 try { 586 Bundle params = new Bundle(); 587 ParcelFileDescriptor pfd = mService.getWallpaperWithFeature( 588 context.getOpPackageName(), context.getAttributionTag(), this, FLAG_SYSTEM, 589 params, userId); 590 591 if (pfd != null) { 592 try (BufferedInputStream bis = new BufferedInputStream( 593 new ParcelFileDescriptor.AutoCloseInputStream(pfd))) { 594 final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 595 int data; 596 while ((data = bis.read()) != -1) { 597 baos.write(data); 598 } 599 ImageDecoder.Source src = ImageDecoder.createSource(baos.toByteArray()); 600 return ImageDecoder.decodeBitmap(src, ((decoder, info, source) -> { 601 // Mutable and hardware config can't be set at the same time. 602 decoder.setMutableRequired(!hardware); 603 // Let's do color management 604 if (cmProxy != null) { 605 cmProxy.doColorManagement(decoder, info); 606 } 607 })); 608 } catch (OutOfMemoryError | IOException e) { 609 Log.w(TAG, "Can't decode file", e); 610 } 611 } 612 } catch (RemoteException e) { 613 throw e.rethrowFromSystemServer(); 614 } 615 return null; 616 } 617 getDefaultWallpaper(Context context, @SetWallpaperFlags int which)618 private Bitmap getDefaultWallpaper(Context context, @SetWallpaperFlags int which) { 619 InputStream is = openDefaultWallpaper(context, which); 620 if (is != null) { 621 try { 622 BitmapFactory.Options options = new BitmapFactory.Options(); 623 return BitmapFactory.decodeStream(is, null, options); 624 } catch (OutOfMemoryError e) { 625 Log.w(TAG, "Can't decode stream", e); 626 } finally { 627 IoUtils.closeQuietly(is); 628 } 629 } 630 return null; 631 } 632 } 633 initGlobals(IWallpaperManager service, Looper looper)634 static void initGlobals(IWallpaperManager service, Looper looper) { 635 synchronized (sSync) { 636 if (sGlobals == null) { 637 sGlobals = new Globals(service, looper); 638 } 639 } 640 } 641 WallpaperManager(IWallpaperManager service, @UiContext Context context, Handler handler)642 /*package*/ WallpaperManager(IWallpaperManager service, @UiContext Context context, 643 Handler handler) { 644 mContext = context; 645 if (service != null) { 646 initGlobals(service, context.getMainLooper()); 647 } 648 // Check if supports mixed color spaces composition in hardware. 649 mWcgEnabled = context.getResources().getConfiguration().isScreenWideColorGamut() 650 && context.getResources().getBoolean(R.bool.config_enableWcgMode); 651 mCmProxy = new ColorManagementProxy(context); 652 } 653 654 // no-op constructor called just by DisabledWallpaperManager WallpaperManager()655 /*package*/ WallpaperManager() { 656 mContext = null; 657 mCmProxy = null; 658 mWcgEnabled = false; 659 } 660 661 /** 662 * Retrieve a WallpaperManager associated with the given Context. 663 */ getInstance(Context context)664 public static WallpaperManager getInstance(Context context) { 665 return (WallpaperManager)context.getSystemService( 666 Context.WALLPAPER_SERVICE); 667 } 668 669 /** @hide */ 670 @UnsupportedAppUsage getIWallpaperManager()671 public IWallpaperManager getIWallpaperManager() { 672 return sGlobals.mService; 673 } 674 675 /** 676 * Indicate whether wcg (Wide Color Gamut) should be enabled. 677 * <p> 678 * Some devices lack of capability of mixed color spaces composition, 679 * enable wcg on such devices might cause memory or battery concern. 680 * <p> 681 * Therefore, in addition to {@link Configuration#isScreenWideColorGamut()}, 682 * we also take mixed color spaces composition (config_enableWcgMode) into account. 683 * 684 * @see Configuration#isScreenWideColorGamut() 685 * @return True if wcg should be enabled for this device. 686 * @hide 687 */ 688 @TestApi shouldEnableWideColorGamut()689 public boolean shouldEnableWideColorGamut() { 690 return mWcgEnabled; 691 } 692 693 /** 694 * Retrieve the current system wallpaper; if 695 * no wallpaper is set, the system built-in static wallpaper is returned. 696 * This is returned as an 697 * abstract Drawable that you can install in a View to display whatever 698 * wallpaper the user has currently set. 699 * <p> 700 * This method can return null if there is no system wallpaper available, if 701 * wallpapers are not supported in the current user, or if the calling app is not 702 * permitted to access the system wallpaper. 703 * 704 * @return Returns a Drawable object that will draw the system wallpaper, 705 * or {@code null} if no system wallpaper exists or if the calling application 706 * is not able to access the wallpaper. 707 */ 708 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) getDrawable()709 public Drawable getDrawable() { 710 final ColorManagementProxy cmProxy = getColorManagementProxy(); 711 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, cmProxy); 712 if (bm != null) { 713 Drawable dr = new BitmapDrawable(mContext.getResources(), bm); 714 dr.setDither(false); 715 return dr; 716 } 717 return null; 718 } 719 720 /** 721 * Obtain a drawable for the built-in static system wallpaper. 722 */ getBuiltInDrawable()723 public Drawable getBuiltInDrawable() { 724 return getBuiltInDrawable(0, 0, false, 0, 0, FLAG_SYSTEM); 725 } 726 727 /** 728 * Obtain a drawable for the specified built-in static system wallpaper. 729 * 730 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws 731 * IllegalArgumentException if an invalid wallpaper is requested. 732 * @return A Drawable presenting the specified wallpaper image, or {@code null} 733 * if no built-in default image for that wallpaper type exists. 734 */ getBuiltInDrawable(@etWallpaperFlags int which)735 public Drawable getBuiltInDrawable(@SetWallpaperFlags int which) { 736 return getBuiltInDrawable(0, 0, false, 0, 0, which); 737 } 738 739 /** 740 * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the 741 * drawable can be cropped and scaled 742 * 743 * @param outWidth The width of the returned drawable 744 * @param outWidth The height of the returned drawable 745 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it 746 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image; 747 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned 748 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image; 749 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned 750 * @return A Drawable presenting the built-in default system wallpaper image, 751 * or {@code null} if no such default image is defined on this device. 752 */ getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit, float horizontalAlignment, float verticalAlignment)753 public Drawable getBuiltInDrawable(int outWidth, int outHeight, 754 boolean scaleToFit, float horizontalAlignment, float verticalAlignment) { 755 return getBuiltInDrawable(outWidth, outHeight, scaleToFit, 756 horizontalAlignment, verticalAlignment, FLAG_SYSTEM); 757 } 758 759 /** 760 * Returns a drawable for the built-in static wallpaper of the specified type. Based on the 761 * parameters, the drawable can be cropped and scaled. 762 * 763 * @param outWidth The width of the returned drawable 764 * @param outWidth The height of the returned drawable 765 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it 766 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image; 767 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned 768 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image; 769 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned 770 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws 771 * IllegalArgumentException if an invalid wallpaper is requested. 772 * @return A Drawable presenting the built-in default wallpaper image of the given type, 773 * or {@code null} if no default image of that type is defined on this device. 774 */ getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit, float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which)775 public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit, 776 float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) { 777 if (sGlobals.mService == null) { 778 Log.w(TAG, "WallpaperService not running"); 779 throw new RuntimeException(new DeadSystemException()); 780 } 781 782 if (which != FLAG_SYSTEM && which != FLAG_LOCK) { 783 throw new IllegalArgumentException("Must request exactly one kind of wallpaper"); 784 } 785 786 Resources resources = mContext.getResources(); 787 horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment)); 788 verticalAlignment = Math.max(0, Math.min(1, verticalAlignment)); 789 790 InputStream wpStream = openDefaultWallpaper(mContext, which); 791 if (wpStream == null) { 792 if (DEBUG) { 793 Log.w(TAG, "default wallpaper stream " + which + " is null"); 794 } 795 return null; 796 } else { 797 InputStream is = new BufferedInputStream(wpStream); 798 if (outWidth <= 0 || outHeight <= 0) { 799 Bitmap fullSize = BitmapFactory.decodeStream(is, null, null); 800 return new BitmapDrawable(resources, fullSize); 801 } else { 802 int inWidth; 803 int inHeight; 804 // Just measure this time through... 805 { 806 BitmapFactory.Options options = new BitmapFactory.Options(); 807 options.inJustDecodeBounds = true; 808 BitmapFactory.decodeStream(is, null, options); 809 if (options.outWidth != 0 && options.outHeight != 0) { 810 inWidth = options.outWidth; 811 inHeight = options.outHeight; 812 } else { 813 Log.e(TAG, "default wallpaper dimensions are 0"); 814 return null; 815 } 816 } 817 818 // Reopen the stream to do the full decode. We know at this point 819 // that openDefaultWallpaper() will return non-null. 820 is = new BufferedInputStream(openDefaultWallpaper(mContext, which)); 821 822 RectF cropRectF; 823 824 outWidth = Math.min(inWidth, outWidth); 825 outHeight = Math.min(inHeight, outHeight); 826 if (scaleToFit) { 827 cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight, 828 horizontalAlignment, verticalAlignment); 829 } else { 830 float left = (inWidth - outWidth) * horizontalAlignment; 831 float right = left + outWidth; 832 float top = (inHeight - outHeight) * verticalAlignment; 833 float bottom = top + outHeight; 834 cropRectF = new RectF(left, top, right, bottom); 835 } 836 Rect roundedTrueCrop = new Rect(); 837 cropRectF.roundOut(roundedTrueCrop); 838 839 if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) { 840 Log.w(TAG, "crop has bad values for full size image"); 841 return null; 842 } 843 844 // See how much we're reducing the size of the image 845 int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth, 846 roundedTrueCrop.height() / outHeight); 847 848 // Attempt to open a region decoder 849 BitmapRegionDecoder decoder = null; 850 try { 851 decoder = BitmapRegionDecoder.newInstance(is, true); 852 } catch (IOException e) { 853 Log.w(TAG, "cannot open region decoder for default wallpaper"); 854 } 855 856 Bitmap crop = null; 857 if (decoder != null) { 858 // Do region decoding to get crop bitmap 859 BitmapFactory.Options options = new BitmapFactory.Options(); 860 if (scaleDownSampleSize > 1) { 861 options.inSampleSize = scaleDownSampleSize; 862 } 863 crop = decoder.decodeRegion(roundedTrueCrop, options); 864 decoder.recycle(); 865 } 866 867 if (crop == null) { 868 // BitmapRegionDecoder has failed, try to crop in-memory. We know at 869 // this point that openDefaultWallpaper() will return non-null. 870 is = new BufferedInputStream(openDefaultWallpaper(mContext, which)); 871 Bitmap fullSize = null; 872 BitmapFactory.Options options = new BitmapFactory.Options(); 873 if (scaleDownSampleSize > 1) { 874 options.inSampleSize = scaleDownSampleSize; 875 } 876 fullSize = BitmapFactory.decodeStream(is, null, options); 877 if (fullSize != null) { 878 crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left, 879 roundedTrueCrop.top, roundedTrueCrop.width(), 880 roundedTrueCrop.height()); 881 } 882 } 883 884 if (crop == null) { 885 Log.w(TAG, "cannot decode default wallpaper"); 886 return null; 887 } 888 889 // Scale down if necessary 890 if (outWidth > 0 && outHeight > 0 && 891 (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) { 892 Matrix m = new Matrix(); 893 RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight()); 894 RectF returnRect = new RectF(0, 0, outWidth, outHeight); 895 m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL); 896 Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(), 897 (int) returnRect.height(), Bitmap.Config.ARGB_8888); 898 if (tmp != null) { 899 Canvas c = new Canvas(tmp); 900 Paint p = new Paint(); 901 p.setFilterBitmap(true); 902 c.drawBitmap(crop, m, p); 903 crop = tmp; 904 } 905 } 906 907 return new BitmapDrawable(resources, crop); 908 } 909 } 910 } 911 getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight, float horizontalAlignment, float verticalAlignment)912 private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight, 913 float horizontalAlignment, float verticalAlignment) { 914 RectF cropRect = new RectF(); 915 // Get a crop rect that will fit this 916 if (inWidth / (float) inHeight > outWidth / (float) outHeight) { 917 cropRect.top = 0; 918 cropRect.bottom = inHeight; 919 float cropWidth = outWidth * (inHeight / (float) outHeight); 920 cropRect.left = (inWidth - cropWidth) * horizontalAlignment; 921 cropRect.right = cropRect.left + cropWidth; 922 } else { 923 cropRect.left = 0; 924 cropRect.right = inWidth; 925 float cropHeight = outHeight * (inWidth / (float) outWidth); 926 cropRect.top = (inHeight - cropHeight) * verticalAlignment; 927 cropRect.bottom = cropRect.top + cropHeight; 928 } 929 return cropRect; 930 } 931 932 /** 933 * Retrieve the current system wallpaper; if there is no wallpaper set, 934 * a null pointer is returned. This is returned as an 935 * abstract Drawable that you can install in a View to display whatever 936 * wallpaper the user has currently set. 937 * 938 * @return Returns a Drawable object that will draw the wallpaper or a 939 * null pointer if these is none. 940 */ peekDrawable()941 public Drawable peekDrawable() { 942 final ColorManagementProxy cmProxy = getColorManagementProxy(); 943 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy); 944 if (bm != null) { 945 Drawable dr = new BitmapDrawable(mContext.getResources(), bm); 946 dr.setDither(false); 947 return dr; 948 } 949 return null; 950 } 951 952 /** 953 * Like {@link #getDrawable()}, but the returned Drawable has a number 954 * of limitations to reduce its overhead as much as possible. It will 955 * never scale the wallpaper (only centering it if the requested bounds 956 * do match the bitmap bounds, which should not be typical), doesn't 957 * allow setting an alpha, color filter, or other attributes, etc. The 958 * bounds of the returned drawable will be initialized to the same bounds 959 * as the wallpaper, so normally you will not need to touch it. The 960 * drawable also assumes that it will be used in a context running in 961 * the same density as the screen (not in density compatibility mode). 962 * 963 * @return Returns a Drawable object that will draw the wallpaper. 964 */ 965 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) getFastDrawable()966 public Drawable getFastDrawable() { 967 final ColorManagementProxy cmProxy = getColorManagementProxy(); 968 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, cmProxy); 969 if (bm != null) { 970 return new FastBitmapDrawable(bm); 971 } 972 return null; 973 } 974 975 /** 976 * Like {@link #getFastDrawable()}, but if there is no wallpaper set, 977 * a null pointer is returned. 978 * 979 * @return Returns an optimized Drawable object that will draw the 980 * wallpaper or a null pointer if these is none. 981 */ 982 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) peekFastDrawable()983 public Drawable peekFastDrawable() { 984 final ColorManagementProxy cmProxy = getColorManagementProxy(); 985 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy); 986 if (bm != null) { 987 return new FastBitmapDrawable(bm); 988 } 989 return null; 990 } 991 992 /** 993 * Whether the wallpaper supports Wide Color Gamut or not. 994 * @param which The wallpaper whose image file is to be retrieved. Must be a single 995 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or {@link #FLAG_LOCK}. 996 * @return true when supported. 997 * 998 * @see #FLAG_LOCK 999 * @see #FLAG_SYSTEM 1000 * @hide 1001 */ 1002 @TestApi 1003 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) wallpaperSupportsWcg(int which)1004 public boolean wallpaperSupportsWcg(int which) { 1005 if (!shouldEnableWideColorGamut()) { 1006 return false; 1007 } 1008 final ColorManagementProxy cmProxy = getColorManagementProxy(); 1009 Bitmap bitmap = sGlobals.peekWallpaperBitmap(mContext, false, which, cmProxy); 1010 return bitmap != null && bitmap.getColorSpace() != null 1011 && bitmap.getColorSpace() != ColorSpace.get(ColorSpace.Named.SRGB) 1012 && cmProxy.isSupportedColorSpace(bitmap.getColorSpace()); 1013 } 1014 1015 /** 1016 * Like {@link #getDrawable()} but returns a Bitmap with default {@link Bitmap.Config}. 1017 * 1018 * @hide 1019 */ 1020 @TestApi 1021 @Nullable 1022 @UnsupportedAppUsage getBitmap()1023 public Bitmap getBitmap() { 1024 return getBitmap(false); 1025 } 1026 1027 /** 1028 * Like {@link #getDrawable()} but returns a Bitmap. 1029 * 1030 * @param hardware Asks for a hardware backed bitmap. 1031 * @see Bitmap.Config#HARDWARE 1032 * @hide 1033 */ 1034 @UnsupportedAppUsage getBitmap(boolean hardware)1035 public Bitmap getBitmap(boolean hardware) { 1036 return getBitmapAsUser(mContext.getUserId(), hardware); 1037 } 1038 1039 /** 1040 * Like {@link #getDrawable()} but returns a Bitmap for the provided user. 1041 * 1042 * @hide 1043 */ getBitmapAsUser(int userId, boolean hardware)1044 public Bitmap getBitmapAsUser(int userId, boolean hardware) { 1045 final ColorManagementProxy cmProxy = getColorManagementProxy(); 1046 return sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, userId, hardware, cmProxy); 1047 } 1048 1049 /** 1050 * Get an open, readable file descriptor to the given wallpaper image file. 1051 * The caller is responsible for closing the file descriptor when done ingesting the file. 1052 * 1053 * <p>If no lock-specific wallpaper has been configured for the given user, then 1054 * this method will return {@code null} when requesting {@link #FLAG_LOCK} rather than 1055 * returning the system wallpaper's image file. 1056 * 1057 * @param which The wallpaper whose image file is to be retrieved. Must be a single 1058 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or 1059 * {@link #FLAG_LOCK}. 1060 * @return An open, readable file desriptor to the requested wallpaper image file; 1061 * or {@code null} if no such wallpaper is configured or if the calling app does 1062 * not have permission to read the current wallpaper. 1063 * 1064 * @see #FLAG_LOCK 1065 * @see #FLAG_SYSTEM 1066 */ 1067 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) getWallpaperFile(@etWallpaperFlags int which)1068 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which) { 1069 return getWallpaperFile(which, mContext.getUserId()); 1070 } 1071 1072 /** 1073 * Registers a listener to get notified when the wallpaper colors change. 1074 * @param listener A listener to register 1075 * @param handler Where to call it from. Will be called from the main thread 1076 * if null. 1077 */ addOnColorsChangedListener(@onNull OnColorsChangedListener listener, @NonNull Handler handler)1078 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener, 1079 @NonNull Handler handler) { 1080 addOnColorsChangedListener(listener, handler, mContext.getUserId()); 1081 } 1082 1083 /** 1084 * Registers a listener to get notified when the wallpaper colors change 1085 * @param listener A listener to register 1086 * @param handler Where to call it from. Will be called from the main thread 1087 * if null. 1088 * @param userId Owner of the wallpaper or UserHandle.USER_ALL. 1089 * @hide 1090 */ 1091 @UnsupportedAppUsage addOnColorsChangedListener(@onNull OnColorsChangedListener listener, @NonNull Handler handler, int userId)1092 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener, 1093 @NonNull Handler handler, int userId) { 1094 sGlobals.addOnColorsChangedListener(listener, handler, userId, mContext.getDisplayId()); 1095 } 1096 1097 /** 1098 * Stop listening to color updates. 1099 * @param callback A callback to unsubscribe. 1100 */ removeOnColorsChangedListener(@onNull OnColorsChangedListener callback)1101 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback) { 1102 removeOnColorsChangedListener(callback, mContext.getUserId()); 1103 } 1104 1105 /** 1106 * Stop listening to color updates. 1107 * @param callback A callback to unsubscribe. 1108 * @param userId Owner of the wallpaper or UserHandle.USER_ALL. 1109 * @hide 1110 */ removeOnColorsChangedListener(@onNull OnColorsChangedListener callback, int userId)1111 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback, 1112 int userId) { 1113 sGlobals.removeOnColorsChangedListener(callback, userId, mContext.getDisplayId()); 1114 } 1115 1116 /** 1117 * Get the primary colors of a wallpaper. 1118 * 1119 * <p>This method can return {@code null} when: 1120 * <ul> 1121 * <li>Colors are still being processed by the system.</li> 1122 * <li>The user has chosen to use a live wallpaper: live wallpapers might not 1123 * implement 1124 * {@link android.service.wallpaper.WallpaperService.Engine#onComputeColors() 1125 * WallpaperService.Engine#onComputeColors()}.</li> 1126 * </ul> 1127 * <p>Please note that this API will go through IPC and may take some time to 1128 * calculate the wallpaper color, which could block the caller thread, so it is 1129 * not recommended to call this in the UI thread.</p> 1130 * 1131 * @param which Wallpaper type. Must be either {@link #FLAG_SYSTEM} or 1132 * {@link #FLAG_LOCK}. 1133 * @return Current {@link WallpaperColors} or null if colors are unknown. 1134 * @see #addOnColorsChangedListener(OnColorsChangedListener, Handler) 1135 */ getWallpaperColors(int which)1136 public @Nullable WallpaperColors getWallpaperColors(int which) { 1137 return getWallpaperColors(which, mContext.getUserId()); 1138 } 1139 1140 // TODO(b/181083333): add multiple root display area support on this API. 1141 /** 1142 * Get the primary colors of the wallpaper configured in the given user. 1143 * @param which wallpaper type. Must be either {@link #FLAG_SYSTEM} or 1144 * {@link #FLAG_LOCK} 1145 * @param userId Owner of the wallpaper. 1146 * @return {@link WallpaperColors} or null if colors are unknown. 1147 * @hide 1148 */ 1149 @UnsupportedAppUsage getWallpaperColors(int which, int userId)1150 public @Nullable WallpaperColors getWallpaperColors(int which, int userId) { 1151 StrictMode.assertUiContext(mContext, "getWallpaperColors"); 1152 return sGlobals.getWallpaperColors(which, userId, mContext.getDisplayId()); 1153 } 1154 1155 /** 1156 * @hide 1157 */ addOnColorsChangedListener(@onNull LocalWallpaperColorConsumer callback, List<RectF> regions)1158 public void addOnColorsChangedListener(@NonNull LocalWallpaperColorConsumer callback, 1159 List<RectF> regions) throws IllegalArgumentException { 1160 for (RectF region : regions) { 1161 if (!LOCAL_COLOR_BOUNDS.contains(region)) { 1162 throw new IllegalArgumentException("Regions must be within bounds " 1163 + LOCAL_COLOR_BOUNDS); 1164 } 1165 } 1166 sGlobals.addOnColorsChangedListener(callback, regions, FLAG_SYSTEM, 1167 mContext.getUserId(), mContext.getDisplayId()); 1168 } 1169 1170 /** 1171 * @hide 1172 */ removeOnColorsChangedListener(@onNull LocalWallpaperColorConsumer callback)1173 public void removeOnColorsChangedListener(@NonNull LocalWallpaperColorConsumer callback) { 1174 sGlobals.removeOnColorsChangedListener(callback, FLAG_SYSTEM, mContext.getUserId(), 1175 mContext.getDisplayId()); 1176 } 1177 1178 /** 1179 * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data 1180 * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL 1181 * permission to access another user's wallpaper data. 1182 * 1183 * @param which The wallpaper whose image file is to be retrieved. Must be a single 1184 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or 1185 * {@link #FLAG_LOCK}. 1186 * @param userId The user or profile whose imagery is to be retrieved 1187 * 1188 * @see #FLAG_LOCK 1189 * @see #FLAG_SYSTEM 1190 * 1191 * @hide 1192 */ 1193 @UnsupportedAppUsage getWallpaperFile(@etWallpaperFlags int which, int userId)1194 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which, int userId) { 1195 if (which != FLAG_SYSTEM && which != FLAG_LOCK) { 1196 throw new IllegalArgumentException("Must request exactly one kind of wallpaper"); 1197 } 1198 1199 if (sGlobals.mService == null) { 1200 Log.w(TAG, "WallpaperService not running"); 1201 throw new RuntimeException(new DeadSystemException()); 1202 } else { 1203 try { 1204 Bundle outParams = new Bundle(); 1205 return sGlobals.mService.getWallpaperWithFeature(mContext.getOpPackageName(), 1206 mContext.getAttributionTag(), null, which, outParams, userId); 1207 } catch (RemoteException e) { 1208 throw e.rethrowFromSystemServer(); 1209 } catch (SecurityException e) { 1210 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) { 1211 Log.w(TAG, "No permission to access wallpaper, suppressing" 1212 + " exception to avoid crashing legacy app."); 1213 return null; 1214 } else { 1215 throw e; 1216 } 1217 } 1218 } 1219 } 1220 1221 /** 1222 * Remove all internal references to the last loaded wallpaper. Useful 1223 * for apps that want to reduce memory usage when they only temporarily 1224 * need to have the wallpaper. After calling, the next request for the 1225 * wallpaper will require reloading it again from disk. 1226 */ forgetLoadedWallpaper()1227 public void forgetLoadedWallpaper() { 1228 sGlobals.forgetLoadedWallpaper(); 1229 } 1230 1231 /** 1232 * Returns the information about the wallpaper if the current wallpaper is 1233 * a live wallpaper component. Otherwise, if the wallpaper is a static image, 1234 * this returns null. 1235 */ getWallpaperInfo()1236 public WallpaperInfo getWallpaperInfo() { 1237 return getWallpaperInfo(mContext.getUserId()); 1238 } 1239 1240 /** 1241 * Returns the information about the wallpaper if the current wallpaper is 1242 * a live wallpaper component. Otherwise, if the wallpaper is a static image, 1243 * this returns null. 1244 * 1245 * @param userId Owner of the wallpaper. 1246 * @hide 1247 */ getWallpaperInfo(int userId)1248 public WallpaperInfo getWallpaperInfo(int userId) { 1249 try { 1250 if (sGlobals.mService == null) { 1251 Log.w(TAG, "WallpaperService not running"); 1252 throw new RuntimeException(new DeadSystemException()); 1253 } else { 1254 return sGlobals.mService.getWallpaperInfo(userId); 1255 } 1256 } catch (RemoteException e) { 1257 throw e.rethrowFromSystemServer(); 1258 } 1259 } 1260 1261 /** 1262 * Get the ID of the current wallpaper of the given kind. If there is no 1263 * such wallpaper configured, returns a negative number. 1264 * 1265 * <p>Every time the wallpaper image is set, a new ID is assigned to it. 1266 * This method allows the caller to determine whether the wallpaper imagery 1267 * has changed, regardless of how that change happened. 1268 * 1269 * @param which The wallpaper whose ID is to be returned. Must be a single 1270 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or 1271 * {@link #FLAG_LOCK}. 1272 * @return The positive numeric ID of the current wallpaper of the given kind, 1273 * or a negative value if no such wallpaper is configured. 1274 */ getWallpaperId(@etWallpaperFlags int which)1275 public int getWallpaperId(@SetWallpaperFlags int which) { 1276 return getWallpaperIdForUser(which, mContext.getUserId()); 1277 } 1278 1279 /** 1280 * Get the ID of the given user's current wallpaper of the given kind. If there 1281 * is no such wallpaper configured, returns a negative number. 1282 * @hide 1283 */ getWallpaperIdForUser(@etWallpaperFlags int which, int userId)1284 public int getWallpaperIdForUser(@SetWallpaperFlags int which, int userId) { 1285 try { 1286 if (sGlobals.mService == null) { 1287 Log.w(TAG, "WallpaperService not running"); 1288 throw new RuntimeException(new DeadSystemException()); 1289 } else { 1290 return sGlobals.mService.getWallpaperIdForUser(which, userId); 1291 } 1292 } catch (RemoteException e) { 1293 throw e.rethrowFromSystemServer(); 1294 } 1295 } 1296 1297 /** 1298 * Gets an Intent that will launch an activity that crops the given 1299 * image and sets the device's wallpaper. If there is a default HOME activity 1300 * that supports cropping wallpapers, it will be preferred as the default. 1301 * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER} 1302 * intent. 1303 * 1304 * @param imageUri The image URI that will be set in the intent. The must be a content 1305 * URI and its provider must resolve its type to "image/*" 1306 * 1307 * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is 1308 * not "image/*" 1309 */ getCropAndSetWallpaperIntent(Uri imageUri)1310 public Intent getCropAndSetWallpaperIntent(Uri imageUri) { 1311 if (imageUri == null) { 1312 throw new IllegalArgumentException("Image URI must not be null"); 1313 } 1314 1315 if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) { 1316 throw new IllegalArgumentException("Image URI must be of the " 1317 + ContentResolver.SCHEME_CONTENT + " scheme type"); 1318 } 1319 1320 final PackageManager packageManager = mContext.getPackageManager(); 1321 Intent cropAndSetWallpaperIntent = 1322 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri); 1323 cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 1324 1325 // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER 1326 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME); 1327 ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent, 1328 PackageManager.MATCH_DEFAULT_ONLY); 1329 if (resolvedHome != null) { 1330 cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName); 1331 1332 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities( 1333 cropAndSetWallpaperIntent, 0); 1334 if (cropAppList.size() > 0) { 1335 return cropAndSetWallpaperIntent; 1336 } 1337 } 1338 1339 // fallback crop activity 1340 final String cropperPackage = mContext.getString( 1341 com.android.internal.R.string.config_wallpaperCropperPackage); 1342 cropAndSetWallpaperIntent.setPackage(cropperPackage); 1343 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities( 1344 cropAndSetWallpaperIntent, 0); 1345 if (cropAppList.size() > 0) { 1346 return cropAndSetWallpaperIntent; 1347 } 1348 // If the URI is not of the right type, or for some reason the system wallpaper 1349 // cropper doesn't exist, return null 1350 throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " + 1351 "check that the type returned by ContentProvider matches image/*"); 1352 } 1353 1354 /** 1355 * Change the current system wallpaper to the bitmap in the given resource. 1356 * The resource is opened as a raw data stream and copied into the 1357 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent 1358 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. 1359 * 1360 * <p>This method requires the caller to hold the permission 1361 * {@link android.Manifest.permission#SET_WALLPAPER}. 1362 * 1363 * @param resid The resource ID of the bitmap to be used as the wallpaper image 1364 * 1365 * @throws IOException If an error occurs reverting to the built-in 1366 * wallpaper. 1367 */ 1368 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) setResource(@awRes int resid)1369 public void setResource(@RawRes int resid) throws IOException { 1370 setResource(resid, FLAG_SYSTEM | FLAG_LOCK); 1371 } 1372 1373 /** 1374 * Version of {@link #setResource(int)} that allows the caller to specify which 1375 * of the supported wallpaper categories to set. 1376 * 1377 * @param resid The resource ID of the bitmap to be used as the wallpaper image 1378 * @param which Flags indicating which wallpaper(s) to configure with the new imagery 1379 * 1380 * @see #FLAG_LOCK 1381 * @see #FLAG_SYSTEM 1382 * 1383 * @return An integer ID assigned to the newly active wallpaper; or zero on failure. 1384 * 1385 * @throws IOException 1386 */ 1387 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) setResource(@awRes int resid, @SetWallpaperFlags int which)1388 public int setResource(@RawRes int resid, @SetWallpaperFlags int which) 1389 throws IOException { 1390 if (sGlobals.mService == null) { 1391 Log.w(TAG, "WallpaperService not running"); 1392 throw new RuntimeException(new DeadSystemException()); 1393 } 1394 final Bundle result = new Bundle(); 1395 final WallpaperSetCompletion completion = new WallpaperSetCompletion(); 1396 try { 1397 Resources resources = mContext.getResources(); 1398 /* Set the wallpaper to the default values */ 1399 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper( 1400 "res:" + resources.getResourceName(resid), 1401 mContext.getOpPackageName(), null, false, result, which, completion, 1402 mContext.getUserId()); 1403 if (fd != null) { 1404 FileOutputStream fos = null; 1405 boolean ok = false; 1406 try { 1407 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); 1408 copyStreamToWallpaperFile(resources.openRawResource(resid), fos); 1409 // The 'close()' is the trigger for any server-side image manipulation, 1410 // so we must do that before waiting for completion. 1411 fos.close(); 1412 completion.waitForCompletion(); 1413 } finally { 1414 // Might be redundant but completion shouldn't wait unless the write 1415 // succeeded; this is a fallback if it threw past the close+wait. 1416 IoUtils.closeQuietly(fos); 1417 } 1418 } 1419 } catch (RemoteException e) { 1420 throw e.rethrowFromSystemServer(); 1421 } 1422 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0); 1423 } 1424 1425 /** 1426 * Change the current system wallpaper to a bitmap. The given bitmap is 1427 * converted to a PNG and stored as the wallpaper. On success, the intent 1428 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. 1429 * 1430 * <p>This method is equivalent to calling 1431 * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the 1432 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup} 1433 * parameter. 1434 * 1435 * <p>This method requires the caller to hold the permission 1436 * {@link android.Manifest.permission#SET_WALLPAPER}. 1437 * 1438 * @param bitmap The bitmap to be used as the new system wallpaper. 1439 * 1440 * @throws IOException If an error occurs when attempting to set the wallpaper 1441 * to the provided image. 1442 */ 1443 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) setBitmap(Bitmap bitmap)1444 public void setBitmap(Bitmap bitmap) throws IOException { 1445 setBitmap(bitmap, null, true); 1446 } 1447 1448 /** 1449 * Change the current system wallpaper to a bitmap, specifying a hint about 1450 * which subrectangle of the full image is to be visible. The OS will then 1451 * try to best present the given portion of the full image as the static system 1452 * wallpaper image. On success, the intent 1453 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. 1454 * 1455 * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to 1456 * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}). 1457 * 1458 * <p>This method requires the caller to hold the permission 1459 * {@link android.Manifest.permission#SET_WALLPAPER}. 1460 * 1461 * @param fullImage A bitmap that will supply the wallpaper imagery. 1462 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be 1463 * displayed as wallpaper. Passing {@code null} for this parameter means that 1464 * the full image should be displayed if possible given the image's and device's 1465 * aspect ratios, etc. 1466 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper 1467 * image for restore to a future device; {@code false} otherwise. 1468 * 1469 * @return An integer ID assigned to the newly active wallpaper; or zero on failure. 1470 * 1471 * @throws IOException If an error occurs when attempting to set the wallpaper 1472 * to the provided image. 1473 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is 1474 * empty or invalid. 1475 */ 1476 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)1477 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup) 1478 throws IOException { 1479 return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK); 1480 } 1481 1482 /** 1483 * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller 1484 * to specify which of the supported wallpaper categories to set. 1485 * 1486 * @param fullImage A bitmap that will supply the wallpaper imagery. 1487 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be 1488 * displayed as wallpaper. Passing {@code null} for this parameter means that 1489 * the full image should be displayed if possible given the image's and device's 1490 * aspect ratios, etc. 1491 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper 1492 * image for restore to a future device; {@code false} otherwise. 1493 * @param which Flags indicating which wallpaper(s) to configure with the new imagery. 1494 * 1495 * @see #FLAG_LOCK 1496 * @see #FLAG_SYSTEM 1497 * 1498 * @return An integer ID assigned to the newly active wallpaper; or zero on failure. 1499 * 1500 * @throws IOException 1501 */ 1502 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, @SetWallpaperFlags int which)1503 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, 1504 boolean allowBackup, @SetWallpaperFlags int which) 1505 throws IOException { 1506 return setBitmap(fullImage, visibleCropHint, allowBackup, which, 1507 mContext.getUserId()); 1508 } 1509 1510 /** 1511 * Like {@link #setBitmap(Bitmap, Rect, boolean, int)}, but allows to pass in an explicit user 1512 * id. If the user id doesn't match the user id the process is running under, calling this 1513 * requires permission {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}. 1514 * @hide 1515 */ 1516 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, @SetWallpaperFlags int which, int userId)1517 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, 1518 boolean allowBackup, @SetWallpaperFlags int which, int userId) 1519 throws IOException { 1520 validateRect(visibleCropHint); 1521 if (sGlobals.mService == null) { 1522 Log.w(TAG, "WallpaperService not running"); 1523 throw new RuntimeException(new DeadSystemException()); 1524 } 1525 final Bundle result = new Bundle(); 1526 final WallpaperSetCompletion completion = new WallpaperSetCompletion(); 1527 try { 1528 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null, 1529 mContext.getOpPackageName(), visibleCropHint, allowBackup, 1530 result, which, completion, userId); 1531 if (fd != null) { 1532 FileOutputStream fos = null; 1533 try { 1534 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); 1535 fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos); 1536 fos.close(); 1537 completion.waitForCompletion(); 1538 } finally { 1539 IoUtils.closeQuietly(fos); 1540 } 1541 } 1542 } catch (RemoteException e) { 1543 throw e.rethrowFromSystemServer(); 1544 } 1545 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0); 1546 } 1547 validateRect(Rect rect)1548 private final void validateRect(Rect rect) { 1549 if (rect != null && rect.isEmpty()) { 1550 throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty"); 1551 } 1552 } 1553 1554 /** 1555 * Change the current system wallpaper to a specific byte stream. The 1556 * give InputStream is copied into persistent storage and will now be 1557 * used as the wallpaper. Currently it must be either a JPEG or PNG 1558 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} 1559 * is broadcast. 1560 * 1561 * <p>This method is equivalent to calling 1562 * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the 1563 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup} 1564 * parameter. 1565 * 1566 * <p>This method requires the caller to hold the permission 1567 * {@link android.Manifest.permission#SET_WALLPAPER}. 1568 * 1569 * @param bitmapData A stream containing the raw data to install as a wallpaper. This 1570 * data can be in any format handled by {@link BitmapRegionDecoder}. 1571 * 1572 * @throws IOException If an error occurs when attempting to set the wallpaper 1573 * based on the provided image data. 1574 */ 1575 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) setStream(InputStream bitmapData)1576 public void setStream(InputStream bitmapData) throws IOException { 1577 setStream(bitmapData, null, true); 1578 } 1579 copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)1580 private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos) 1581 throws IOException { 1582 FileUtils.copy(data, fos); 1583 } 1584 1585 /** 1586 * Change the current system wallpaper to a specific byte stream, specifying a 1587 * hint about which subrectangle of the full image is to be visible. The OS will 1588 * then try to best present the given portion of the full image as the static system 1589 * wallpaper image. The data from the given InputStream is copied into persistent 1590 * storage and will then be used as the system wallpaper. Currently the data must 1591 * be either a JPEG or PNG image. On success, the intent 1592 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. 1593 * 1594 * <p>This method requires the caller to hold the permission 1595 * {@link android.Manifest.permission#SET_WALLPAPER}. 1596 * 1597 * @param bitmapData A stream containing the raw data to install as a wallpaper. This 1598 * data can be in any format handled by {@link BitmapRegionDecoder}. 1599 * @param visibleCropHint The rectangular subregion of the streamed image that should be 1600 * displayed as wallpaper. Passing {@code null} for this parameter means that 1601 * the full image should be displayed if possible given the image's and device's 1602 * aspect ratios, etc. 1603 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper 1604 * image for restore to a future device; {@code false} otherwise. 1605 * @return An integer ID assigned to the newly active wallpaper; or zero on failure. 1606 * 1607 * @see #getWallpaperId(int) 1608 * 1609 * @throws IOException If an error occurs when attempting to set the wallpaper 1610 * based on the provided image data. 1611 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is 1612 * empty or invalid. 1613 */ 1614 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)1615 public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup) 1616 throws IOException { 1617 return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK); 1618 } 1619 1620 /** 1621 * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller 1622 * to specify which of the supported wallpaper categories to set. 1623 * 1624 * @param bitmapData A stream containing the raw data to install as a wallpaper. This 1625 * data can be in any format handled by {@link BitmapRegionDecoder}. 1626 * @param visibleCropHint The rectangular subregion of the streamed image that should be 1627 * displayed as wallpaper. Passing {@code null} for this parameter means that 1628 * the full image should be displayed if possible given the image's and device's 1629 * aspect ratios, etc. 1630 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper 1631 * image for restore to a future device; {@code false} otherwise. 1632 * @param which Flags indicating which wallpaper(s) to configure with the new imagery. 1633 * @return An integer ID assigned to the newly active wallpaper; or zero on failure. 1634 * 1635 * @see #getWallpaperId(int) 1636 * @see #FLAG_LOCK 1637 * @see #FLAG_SYSTEM 1638 * 1639 * @throws IOException 1640 */ 1641 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup, @SetWallpaperFlags int which)1642 public int setStream(InputStream bitmapData, Rect visibleCropHint, 1643 boolean allowBackup, @SetWallpaperFlags int which) 1644 throws IOException { 1645 validateRect(visibleCropHint); 1646 if (sGlobals.mService == null) { 1647 Log.w(TAG, "WallpaperService not running"); 1648 throw new RuntimeException(new DeadSystemException()); 1649 } 1650 final Bundle result = new Bundle(); 1651 final WallpaperSetCompletion completion = new WallpaperSetCompletion(); 1652 try { 1653 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null, 1654 mContext.getOpPackageName(), visibleCropHint, allowBackup, 1655 result, which, completion, mContext.getUserId()); 1656 if (fd != null) { 1657 FileOutputStream fos = null; 1658 try { 1659 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); 1660 copyStreamToWallpaperFile(bitmapData, fos); 1661 fos.close(); 1662 completion.waitForCompletion(); 1663 } finally { 1664 IoUtils.closeQuietly(fos); 1665 } 1666 } 1667 } catch (RemoteException e) { 1668 throw e.rethrowFromSystemServer(); 1669 } 1670 1671 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0); 1672 } 1673 1674 /** 1675 * Return whether any users are currently set to use the wallpaper 1676 * with the given resource ID. That is, their wallpaper has been 1677 * set through {@link #setResource(int)} with the same resource id. 1678 */ hasResourceWallpaper(@awRes int resid)1679 public boolean hasResourceWallpaper(@RawRes int resid) { 1680 if (sGlobals.mService == null) { 1681 Log.w(TAG, "WallpaperService not running"); 1682 throw new RuntimeException(new DeadSystemException()); 1683 } 1684 try { 1685 Resources resources = mContext.getResources(); 1686 String name = "res:" + resources.getResourceName(resid); 1687 return sGlobals.mService.hasNamedWallpaper(name); 1688 } catch (RemoteException e) { 1689 throw e.rethrowFromSystemServer(); 1690 } 1691 } 1692 1693 // TODO(b/181083333): add multiple root display area support on this API. 1694 /** 1695 * Returns the desired minimum width for the wallpaper. Callers of 1696 * {@link #setBitmap(android.graphics.Bitmap)} or 1697 * {@link #setStream(java.io.InputStream)} should check this value 1698 * beforehand to make sure the supplied wallpaper respects the desired 1699 * minimum width. 1700 * 1701 * If the returned value is <= 0, the caller should use the width of 1702 * the default display instead. 1703 * 1704 * @return The desired minimum width for the wallpaper. This value should 1705 * be honored by applications that set the wallpaper but it is not 1706 * mandatory. 1707 * 1708 * @see #getDesiredMinimumHeight() 1709 */ getDesiredMinimumWidth()1710 public int getDesiredMinimumWidth() { 1711 StrictMode.assertUiContext(mContext, "getDesiredMinimumWidth"); 1712 if (sGlobals.mService == null) { 1713 Log.w(TAG, "WallpaperService not running"); 1714 throw new RuntimeException(new DeadSystemException()); 1715 } 1716 try { 1717 return sGlobals.mService.getWidthHint(mContext.getDisplayId()); 1718 } catch (RemoteException e) { 1719 throw e.rethrowFromSystemServer(); 1720 } 1721 } 1722 1723 // TODO(b/181083333): add multiple root display area support on this API. 1724 /** 1725 * Returns the desired minimum height for the wallpaper. Callers of 1726 * {@link #setBitmap(android.graphics.Bitmap)} or 1727 * {@link #setStream(java.io.InputStream)} should check this value 1728 * beforehand to make sure the supplied wallpaper respects the desired 1729 * minimum height. 1730 * 1731 * If the returned value is <= 0, the caller should use the height of 1732 * the default display instead. 1733 * 1734 * @return The desired minimum height for the wallpaper. This value should 1735 * be honored by applications that set the wallpaper but it is not 1736 * mandatory. 1737 * 1738 * @see #getDesiredMinimumWidth() 1739 */ getDesiredMinimumHeight()1740 public int getDesiredMinimumHeight() { 1741 StrictMode.assertUiContext(mContext, "getDesiredMinimumHeight"); 1742 if (sGlobals.mService == null) { 1743 Log.w(TAG, "WallpaperService not running"); 1744 throw new RuntimeException(new DeadSystemException()); 1745 } 1746 try { 1747 return sGlobals.mService.getHeightHint(mContext.getDisplayId()); 1748 } catch (RemoteException e) { 1749 throw e.rethrowFromSystemServer(); 1750 } 1751 } 1752 1753 // TODO(b/181083333): add multiple root display area support on this API. 1754 /** 1755 * For use only by the current home application, to specify the size of 1756 * wallpaper it would like to use. This allows such applications to have 1757 * a virtual wallpaper that is larger than the physical screen, matching 1758 * the size of their workspace. 1759 * 1760 * <p class="note">Calling this method from apps other than the active 1761 * home app is not guaranteed to work properly. Other apps that supply 1762 * wallpaper imagery should use {@link #getDesiredMinimumWidth()} and 1763 * {@link #getDesiredMinimumHeight()} and construct a wallpaper that 1764 * matches those dimensions. 1765 * 1766 * <p>This method requires the caller to hold the permission 1767 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}. 1768 * 1769 * @param minimumWidth Desired minimum width 1770 * @param minimumHeight Desired minimum height 1771 */ suggestDesiredDimensions(int minimumWidth, int minimumHeight)1772 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) { 1773 StrictMode.assertUiContext(mContext, "suggestDesiredDimensions"); 1774 try { 1775 /** 1776 * The framework makes no attempt to limit the window size 1777 * to the maximum texture size. Any window larger than this 1778 * cannot be composited. 1779 * 1780 * Read maximum texture size from system property and scale down 1781 * minimumWidth and minimumHeight accordingly. 1782 */ 1783 int maximumTextureSize; 1784 try { 1785 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0); 1786 } catch (Exception e) { 1787 maximumTextureSize = 0; 1788 } 1789 1790 if (maximumTextureSize > 0) { 1791 if ((minimumWidth > maximumTextureSize) || 1792 (minimumHeight > maximumTextureSize)) { 1793 float aspect = (float)minimumHeight / (float)minimumWidth; 1794 if (minimumWidth > minimumHeight) { 1795 minimumWidth = maximumTextureSize; 1796 minimumHeight = (int)((minimumWidth * aspect) + 0.5); 1797 } else { 1798 minimumHeight = maximumTextureSize; 1799 minimumWidth = (int)((minimumHeight / aspect) + 0.5); 1800 } 1801 } 1802 } 1803 1804 if (sGlobals.mService == null) { 1805 Log.w(TAG, "WallpaperService not running"); 1806 throw new RuntimeException(new DeadSystemException()); 1807 } else { 1808 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight, 1809 mContext.getOpPackageName(), mContext.getDisplayId()); 1810 } 1811 } catch (RemoteException e) { 1812 throw e.rethrowFromSystemServer(); 1813 } 1814 } 1815 1816 // TODO(b/181083333): add multiple root display area support on this API. 1817 /** 1818 * Specify extra padding that the wallpaper should have outside of the display. 1819 * That is, the given padding supplies additional pixels the wallpaper should extend 1820 * outside of the display itself. 1821 * 1822 * <p>This method requires the caller to hold the permission 1823 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}. 1824 * 1825 * @param padding The number of pixels the wallpaper should extend beyond the display, 1826 * on its left, top, right, and bottom sides. 1827 */ 1828 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_HINTS) setDisplayPadding(Rect padding)1829 public void setDisplayPadding(Rect padding) { 1830 StrictMode.assertUiContext(mContext, "setDisplayPadding"); 1831 try { 1832 if (sGlobals.mService == null) { 1833 Log.w(TAG, "WallpaperService not running"); 1834 throw new RuntimeException(new DeadSystemException()); 1835 } else { 1836 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName(), 1837 mContext.getDisplayId()); 1838 } 1839 } catch (RemoteException e) { 1840 throw e.rethrowFromSystemServer(); 1841 } 1842 } 1843 1844 /** 1845 * Apply a raw offset to the wallpaper window. Should only be used in 1846 * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you 1847 * have ensured that the wallpaper will extend outside of the display area so that 1848 * it can be moved without leaving part of the display uncovered. 1849 * @param x The offset, in pixels, to apply to the left edge. 1850 * @param y The offset, in pixels, to apply to the top edge. 1851 * @hide 1852 */ 1853 @SystemApi setDisplayOffset(IBinder windowToken, int x, int y)1854 public void setDisplayOffset(IBinder windowToken, int x, int y) { 1855 try { 1856 //Log.v(TAG, "Sending new wallpaper display offsets from app..."); 1857 WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset( 1858 windowToken, x, y); 1859 //Log.v(TAG, "...app returning after sending display offset!"); 1860 } catch (RemoteException e) { 1861 throw e.rethrowFromSystemServer(); 1862 } 1863 } 1864 1865 /** 1866 * Reset all wallpaper to the factory default. 1867 * 1868 * <p>This method requires the caller to hold the permission 1869 * {@link android.Manifest.permission#SET_WALLPAPER}. 1870 */ 1871 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) clearWallpaper()1872 public void clearWallpaper() { 1873 clearWallpaper(FLAG_LOCK, mContext.getUserId()); 1874 clearWallpaper(FLAG_SYSTEM, mContext.getUserId()); 1875 } 1876 1877 /** 1878 * Clear the wallpaper for a specific user. The caller must hold the 1879 * INTERACT_ACROSS_USERS_FULL permission to clear another user's 1880 * wallpaper, and must hold the SET_WALLPAPER permission in all 1881 * circumstances. 1882 * @hide 1883 */ 1884 @SystemApi 1885 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) clearWallpaper(@etWallpaperFlags int which, int userId)1886 public void clearWallpaper(@SetWallpaperFlags int which, int userId) { 1887 if (sGlobals.mService == null) { 1888 Log.w(TAG, "WallpaperService not running"); 1889 throw new RuntimeException(new DeadSystemException()); 1890 } 1891 try { 1892 sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId); 1893 } catch (RemoteException e) { 1894 throw e.rethrowFromSystemServer(); 1895 } 1896 } 1897 1898 /** 1899 * Set the live wallpaper. 1900 * 1901 * @hide 1902 */ 1903 @SystemApi 1904 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT) setWallpaperComponent(ComponentName name)1905 public boolean setWallpaperComponent(ComponentName name) { 1906 return setWallpaperComponent(name, mContext.getUserId()); 1907 } 1908 1909 /** 1910 * Set the live wallpaper. 1911 * 1912 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT 1913 * permission. The caller must hold the INTERACT_ACROSS_USERS_FULL permission to change 1914 * another user's wallpaper. 1915 * 1916 * @hide 1917 */ 1918 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT) 1919 @UnsupportedAppUsage setWallpaperComponent(ComponentName name, int userId)1920 public boolean setWallpaperComponent(ComponentName name, int userId) { 1921 if (sGlobals.mService == null) { 1922 Log.w(TAG, "WallpaperService not running"); 1923 throw new RuntimeException(new DeadSystemException()); 1924 } 1925 try { 1926 sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName(), 1927 userId); 1928 return true; 1929 } catch (RemoteException e) { 1930 throw e.rethrowFromSystemServer(); 1931 } 1932 } 1933 1934 /** 1935 * Set the display position of the current wallpaper within any larger space, when 1936 * that wallpaper is visible behind the given window. The X and Y offsets 1937 * are floating point numbers ranging from 0 to 1, representing where the 1938 * wallpaper should be positioned within the screen space. These only 1939 * make sense when the wallpaper is larger than the display. 1940 * 1941 * @param windowToken The window who these offsets should be associated 1942 * with, as returned by {@link android.view.View#getWindowToken() 1943 * View.getWindowToken()}. 1944 * @param xOffset The offset along the X dimension, from 0 to 1. 1945 * @param yOffset The offset along the Y dimension, from 0 to 1. 1946 */ setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset)1947 public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) { 1948 try { 1949 //Log.v(TAG, "Sending new wallpaper offsets from app..."); 1950 WindowManagerGlobal.getWindowSession().setWallpaperPosition( 1951 windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep); 1952 //Log.v(TAG, "...app returning after sending offsets!"); 1953 } catch (RemoteException e) { 1954 throw e.rethrowFromSystemServer(); 1955 } 1956 } 1957 1958 /** 1959 * For applications that use multiple virtual screens showing a wallpaper, 1960 * specify the step size between virtual screens. For example, if the 1961 * launcher has 3 virtual screens, it would specify an xStep of 0.5, 1962 * since the X offset for those screens are 0.0, 0.5 and 1.0 1963 * @param xStep The X offset delta from one screen to the next one 1964 * @param yStep The Y offset delta from one screen to the next one 1965 */ setWallpaperOffsetSteps(float xStep, float yStep)1966 public void setWallpaperOffsetSteps(float xStep, float yStep) { 1967 mWallpaperXStep = xStep; 1968 mWallpaperYStep = yStep; 1969 } 1970 1971 /** 1972 * Send an arbitrary command to the current active wallpaper. 1973 * 1974 * @param windowToken The window who these offsets should be associated 1975 * with, as returned by {@link android.view.View#getWindowToken() 1976 * View.getWindowToken()}. 1977 * @param action Name of the command to perform. This must be a scoped 1978 * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT". 1979 * @param x Arbitrary integer argument based on command. 1980 * @param y Arbitrary integer argument based on command. 1981 * @param z Arbitrary integer argument based on command. 1982 * @param extras Optional additional information for the command, or null. 1983 */ sendWallpaperCommand(IBinder windowToken, String action, int x, int y, int z, Bundle extras)1984 public void sendWallpaperCommand(IBinder windowToken, String action, 1985 int x, int y, int z, Bundle extras) { 1986 try { 1987 //Log.v(TAG, "Sending new wallpaper offsets from app..."); 1988 WindowManagerGlobal.getWindowSession().sendWallpaperCommand( 1989 windowToken, action, x, y, z, extras, false); 1990 //Log.v(TAG, "...app returning after sending offsets!"); 1991 } catch (RemoteException e) { 1992 throw e.rethrowFromSystemServer(); 1993 } 1994 } 1995 1996 /** 1997 * Set the current zoom out level of the wallpaper. 1998 * 1999 * @param windowToken window requesting wallpaper zoom. Zoom level will only be applier while 2000 * such window is visible. 2001 * @param zoom from 0 to 1 (inclusive) where 1 means fully zoomed out, 0 means fully zoomed in 2002 * 2003 * @hide 2004 */ setWallpaperZoomOut(@onNull IBinder windowToken, float zoom)2005 public void setWallpaperZoomOut(@NonNull IBinder windowToken, float zoom) { 2006 if (zoom < 0 || zoom > 1f) { 2007 throw new IllegalArgumentException("zoom must be between 0 and 1: " + zoom); 2008 } 2009 if (windowToken == null) { 2010 throw new IllegalArgumentException("windowToken must not be null"); 2011 } 2012 try { 2013 WindowManagerGlobal.getWindowSession().setWallpaperZoomOut(windowToken, zoom); 2014 } catch (RemoteException e) { 2015 throw e.rethrowFromSystemServer(); 2016 } 2017 } 2018 2019 /** 2020 * Returns whether wallpapers are supported for the calling user. If this function returns 2021 * {@code false}, any attempts to changing the wallpaper will have no effect, 2022 * and any attempt to obtain of the wallpaper will return {@code null}. 2023 */ isWallpaperSupported()2024 public boolean isWallpaperSupported() { 2025 if (sGlobals.mService == null) { 2026 Log.w(TAG, "WallpaperService not running"); 2027 throw new RuntimeException(new DeadSystemException()); 2028 } else { 2029 try { 2030 return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName()); 2031 } catch (RemoteException e) { 2032 throw e.rethrowFromSystemServer(); 2033 } 2034 } 2035 } 2036 2037 /** 2038 * Returns whether the calling package is allowed to set the wallpaper for the calling user. 2039 * If this function returns {@code false}, any attempts to change the wallpaper will have 2040 * no effect. Always returns {@code true} for device owner and profile owner. 2041 * 2042 * @see android.os.UserManager#DISALLOW_SET_WALLPAPER 2043 */ isSetWallpaperAllowed()2044 public boolean isSetWallpaperAllowed() { 2045 if (sGlobals.mService == null) { 2046 Log.w(TAG, "WallpaperService not running"); 2047 throw new RuntimeException(new DeadSystemException()); 2048 } else { 2049 try { 2050 return sGlobals.mService.isSetWallpaperAllowed(mContext.getOpPackageName()); 2051 } catch (RemoteException e) { 2052 throw e.rethrowFromSystemServer(); 2053 } 2054 } 2055 } 2056 2057 /** 2058 * Clear the offsets previously associated with this window through 2059 * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts 2060 * the window to its default state, where it does not cause the wallpaper 2061 * to scroll from whatever its last offsets were. 2062 * 2063 * @param windowToken The window who these offsets should be associated 2064 * with, as returned by {@link android.view.View#getWindowToken() 2065 * View.getWindowToken()}. 2066 */ clearWallpaperOffsets(IBinder windowToken)2067 public void clearWallpaperOffsets(IBinder windowToken) { 2068 try { 2069 WindowManagerGlobal.getWindowSession().setWallpaperPosition( 2070 windowToken, -1, -1, -1, -1); 2071 } catch (RemoteException e) { 2072 throw e.rethrowFromSystemServer(); 2073 } 2074 } 2075 2076 /** 2077 * Remove any currently set system wallpaper, reverting to the system's built-in 2078 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} 2079 * is broadcast. 2080 * 2081 * <p>This method requires the caller to hold the permission 2082 * {@link android.Manifest.permission#SET_WALLPAPER}. 2083 * 2084 * @throws IOException If an error occurs reverting to the built-in 2085 * wallpaper. 2086 */ 2087 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) clear()2088 public void clear() throws IOException { 2089 setStream(openDefaultWallpaper(mContext, FLAG_SYSTEM), null, false); 2090 } 2091 2092 /** 2093 * Remove one or more currently set wallpapers, reverting to the system default 2094 * display for each one. If {@link #FLAG_SYSTEM} is set in the {@code which} 2095 * parameter, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} will be broadcast 2096 * upon success. 2097 * 2098 * @param which A bitwise combination of {@link #FLAG_SYSTEM} or 2099 * {@link #FLAG_LOCK} 2100 * @throws IOException If an error occurs reverting to the built-in wallpaper. 2101 */ 2102 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER) clear(@etWallpaperFlags int which)2103 public void clear(@SetWallpaperFlags int which) throws IOException { 2104 if ((which & FLAG_SYSTEM) != 0) { 2105 clear(); 2106 } 2107 if ((which & FLAG_LOCK) != 0) { 2108 clearWallpaper(FLAG_LOCK, mContext.getUserId()); 2109 } 2110 } 2111 2112 /** 2113 * Open stream representing the default static image wallpaper. 2114 * 2115 * If the device defines no default wallpaper of the requested kind, 2116 * {@code null} is returned. 2117 * 2118 * @hide 2119 */ 2120 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) openDefaultWallpaper(Context context, @SetWallpaperFlags int which)2121 public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) { 2122 final String whichProp; 2123 final int defaultResId; 2124 if (which == FLAG_LOCK) { 2125 /* Factory-default lock wallpapers are not yet supported 2126 whichProp = PROP_LOCK_WALLPAPER; 2127 defaultResId = com.android.internal.R.drawable.default_lock_wallpaper; 2128 */ 2129 return null; 2130 } else { 2131 whichProp = PROP_WALLPAPER; 2132 defaultResId = com.android.internal.R.drawable.default_wallpaper; 2133 } 2134 final String path = SystemProperties.get(whichProp); 2135 final InputStream wallpaperInputStream = getWallpaperInputStream(path); 2136 if (wallpaperInputStream != null) { 2137 return wallpaperInputStream; 2138 } 2139 final String cmfPath = getCmfWallpaperPath(); 2140 final InputStream cmfWallpaperInputStream = getWallpaperInputStream(cmfPath); 2141 if (cmfWallpaperInputStream != null) { 2142 return cmfWallpaperInputStream; 2143 } 2144 try { 2145 return context.getResources().openRawResource(defaultResId); 2146 } catch (NotFoundException e) { 2147 // no default defined for this device; this is not a failure 2148 } 2149 return null; 2150 } 2151 getWallpaperInputStream(String path)2152 private static InputStream getWallpaperInputStream(String path) { 2153 if (!TextUtils.isEmpty(path)) { 2154 final File file = new File(path); 2155 if (file.exists()) { 2156 try { 2157 return new FileInputStream(file); 2158 } catch (IOException e) { 2159 // Ignored, fall back to platform default 2160 } 2161 } 2162 } 2163 return null; 2164 } 2165 getCmfWallpaperPath()2166 private static String getCmfWallpaperPath() { 2167 return Environment.getProductDirectory() + WALLPAPER_CMF_PATH + "default_wallpaper_" 2168 + VALUE_CMF_COLOR; 2169 } 2170 2171 /** 2172 * Return {@link ComponentName} of the default live wallpaper, or 2173 * {@code null} if none is defined. 2174 * 2175 * @hide 2176 */ getDefaultWallpaperComponent(Context context)2177 public static ComponentName getDefaultWallpaperComponent(Context context) { 2178 ComponentName cn = null; 2179 2180 String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT); 2181 if (!TextUtils.isEmpty(flat)) { 2182 cn = ComponentName.unflattenFromString(flat); 2183 } 2184 2185 if (cn == null) { 2186 flat = context.getString(com.android.internal.R.string.default_wallpaper_component); 2187 if (!TextUtils.isEmpty(flat)) { 2188 cn = ComponentName.unflattenFromString(flat); 2189 } 2190 } 2191 2192 // Check if the package exists 2193 if (cn != null) { 2194 try { 2195 final PackageManager packageManager = context.getPackageManager(); 2196 packageManager.getPackageInfo(cn.getPackageName(), 2197 PackageManager.MATCH_DIRECT_BOOT_AWARE 2198 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); 2199 } catch (PackageManager.NameNotFoundException e) { 2200 cn = null; 2201 } 2202 } 2203 2204 return cn; 2205 } 2206 2207 /** 2208 * Register a callback for lock wallpaper observation. Only the OS may use this. 2209 * 2210 * @return true on success; false on error. 2211 * @hide 2212 */ setLockWallpaperCallback(IWallpaperManagerCallback callback)2213 public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) { 2214 if (sGlobals.mService == null) { 2215 Log.w(TAG, "WallpaperService not running"); 2216 throw new RuntimeException(new DeadSystemException()); 2217 } 2218 2219 try { 2220 return sGlobals.mService.setLockWallpaperCallback(callback); 2221 } catch (RemoteException e) { 2222 throw e.rethrowFromSystemServer(); 2223 } 2224 } 2225 2226 /** 2227 * Is the current system wallpaper eligible for backup? 2228 * 2229 * Only the OS itself may use this method. 2230 * @hide 2231 */ isWallpaperBackupEligible(int which)2232 public boolean isWallpaperBackupEligible(int which) { 2233 if (sGlobals.mService == null) { 2234 Log.w(TAG, "WallpaperService not running"); 2235 throw new RuntimeException(new DeadSystemException()); 2236 } 2237 try { 2238 return sGlobals.mService.isWallpaperBackupEligible(which, mContext.getUserId()); 2239 } catch (RemoteException e) { 2240 Log.e(TAG, "Exception querying wallpaper backup eligibility: " + e.getMessage()); 2241 } 2242 return false; 2243 } 2244 2245 /** 2246 * Get the instance of {@link ColorManagementProxy}. 2247 * 2248 * @return instance of {@link ColorManagementProxy}. 2249 * @hide 2250 */ getColorManagementProxy()2251 public ColorManagementProxy getColorManagementProxy() { 2252 return mCmProxy; 2253 } 2254 2255 /** 2256 * A hidden class to help {@link Globals#getCurrentWallpaperLocked} handle color management. 2257 * @hide 2258 */ 2259 public static class ColorManagementProxy { 2260 private final Set<ColorSpace> mSupportedColorSpaces = new HashSet<>(); 2261 ColorManagementProxy(@onNull Context context)2262 public ColorManagementProxy(@NonNull Context context) { 2263 // Get a list of supported wide gamut color spaces. 2264 Display display = context.getDisplayNoVerify(); 2265 if (display != null) { 2266 mSupportedColorSpaces.addAll(Arrays.asList(display.getSupportedWideColorGamut())); 2267 } 2268 } 2269 2270 @NonNull getSupportedColorSpaces()2271 public Set<ColorSpace> getSupportedColorSpaces() { 2272 return mSupportedColorSpaces; 2273 } 2274 isSupportedColorSpace(ColorSpace colorSpace)2275 boolean isSupportedColorSpace(ColorSpace colorSpace) { 2276 return colorSpace != null && (colorSpace == ColorSpace.get(ColorSpace.Named.SRGB) 2277 || getSupportedColorSpaces().contains(colorSpace)); 2278 } 2279 doColorManagement(ImageDecoder decoder, ImageDecoder.ImageInfo info)2280 void doColorManagement(ImageDecoder decoder, ImageDecoder.ImageInfo info) { 2281 if (!isSupportedColorSpace(info.getColorSpace())) { 2282 decoder.setTargetColorSpace(ColorSpace.get(ColorSpace.Named.SRGB)); 2283 Log.w(TAG, "Not supported color space: " + info.getColorSpace()); 2284 } 2285 } 2286 } 2287 2288 // Private completion callback for setWallpaper() synchronization 2289 private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub { 2290 final CountDownLatch mLatch; 2291 WallpaperSetCompletion()2292 public WallpaperSetCompletion() { 2293 mLatch = new CountDownLatch(1); 2294 } 2295 waitForCompletion()2296 public void waitForCompletion() { 2297 try { 2298 mLatch.await(30, TimeUnit.SECONDS); 2299 } catch (InterruptedException e) { 2300 // This might be legit: the crop may take a very long time. Don't sweat 2301 // it in that case; we are okay with display lagging behind in order to 2302 // keep the caller from locking up indeterminately. 2303 } 2304 } 2305 2306 @Override onWallpaperChanged()2307 public void onWallpaperChanged() throws RemoteException { 2308 mLatch.countDown(); 2309 } 2310 2311 @Override onWallpaperColorsChanged(WallpaperColors colors, int which, int userId)2312 public void onWallpaperColorsChanged(WallpaperColors colors, int which, int userId) 2313 throws RemoteException { 2314 sGlobals.onWallpaperColorsChanged(colors, which, userId); 2315 } 2316 } 2317 2318 /** 2319 * Interface definition for a callback to be invoked when colors change on a wallpaper. 2320 */ 2321 public interface OnColorsChangedListener { 2322 /** 2323 * Called when colors change. 2324 * A {@link android.app.WallpaperColors} object containing a simplified 2325 * color histogram will be given. 2326 * 2327 * @param colors Wallpaper color info 2328 * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM} 2329 */ onColorsChanged(WallpaperColors colors, int which)2330 void onColorsChanged(WallpaperColors colors, int which); 2331 2332 /** 2333 * Called when colors change. 2334 * A {@link android.app.WallpaperColors} object containing a simplified 2335 * color histogram will be given. 2336 * 2337 * @param colors Wallpaper color info 2338 * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM} 2339 * @param userId Owner of the wallpaper 2340 * @hide 2341 */ onColorsChanged(WallpaperColors colors, int which, int userId)2342 default void onColorsChanged(WallpaperColors colors, int which, int userId) { 2343 onColorsChanged(colors, which); 2344 } 2345 } 2346 2347 /** 2348 * Callback to update a consumer with a local color change 2349 * @hide 2350 */ 2351 public interface LocalWallpaperColorConsumer { 2352 2353 /** 2354 * Gets called when a color of an area gets updated 2355 * @param area 2356 * @param colors 2357 */ onColorsChanged(RectF area, WallpaperColors colors)2358 void onColorsChanged(RectF area, WallpaperColors colors); 2359 } 2360 } 2361