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