WallpaperManager.java revision 038b737c7561e6a92edcd6267f1299d2602f1920
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 17package android.app; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.res.Resources; 22import android.graphics.Bitmap; 23import android.graphics.BitmapFactory; 24import android.graphics.Canvas; 25import android.graphics.ColorFilter; 26import android.graphics.Paint; 27import android.graphics.PixelFormat; 28import android.graphics.PorterDuff; 29import android.graphics.PorterDuffXfermode; 30import android.graphics.Rect; 31import android.graphics.drawable.BitmapDrawable; 32import android.graphics.drawable.Drawable; 33import android.os.Bundle; 34import android.os.Handler; 35import android.os.IBinder; 36import android.os.Looper; 37import android.os.Message; 38import android.os.ParcelFileDescriptor; 39import android.os.RemoteException; 40import android.os.ServiceManager; 41import android.util.DisplayMetrics; 42import android.util.Log; 43import android.view.ViewAncestor; 44 45import java.io.FileOutputStream; 46import java.io.IOException; 47import java.io.InputStream; 48 49/** 50 * Provides access to the system wallpaper. With WallpaperManager, you can 51 * get the current wallpaper, get the desired dimensions for the wallpaper, set 52 * the wallpaper, and more. Get an instance of WallpaperManager with 53 * {@link #getInstance(android.content.Context) getInstance()}. 54 */ 55public class WallpaperManager { 56 private static String TAG = "WallpaperManager"; 57 private static boolean DEBUG = false; 58 private float mWallpaperXStep = -1; 59 private float mWallpaperYStep = -1; 60 61 /** 62 * Launch an activity for the user to pick the current global live 63 * wallpaper. 64 */ 65 public static final String ACTION_LIVE_WALLPAPER_CHOOSER 66 = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER"; 67 68 /** 69 * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER} 70 * which allows them to provide a custom large icon associated with this action. 71 */ 72 public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview"; 73 74 /** 75 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper 76 * host when the user taps on an empty area (not performing an action 77 * in the host). The x and y arguments are the location of the tap in 78 * screen coordinates. 79 */ 80 public static final String COMMAND_TAP = "android.wallpaper.tap"; 81 82 /** 83 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper 84 * host when the user releases a secondary pointer on an empty area 85 * (not performing an action in the host). The x and y arguments are 86 * the location of the secondary tap in screen coordinates. 87 */ 88 public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap"; 89 90 /** 91 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper 92 * host when the user drops an object into an area of the host. The x 93 * and y arguments are the location of the drop. 94 */ 95 public static final String COMMAND_DROP = "android.home.drop"; 96 97 private final Context mContext; 98 99 /** 100 * Special drawable that draws a wallpaper as fast as possible. Assumes 101 * no scaling or placement off (0,0) of the wallpaper (this should be done 102 * at the time the bitmap is loaded). 103 */ 104 static class FastBitmapDrawable extends Drawable { 105 private final Bitmap mBitmap; 106 private final int mWidth; 107 private final int mHeight; 108 private int mDrawLeft; 109 private int mDrawTop; 110 111 private FastBitmapDrawable(Bitmap bitmap) { 112 mBitmap = bitmap; 113 mWidth = bitmap.getWidth(); 114 mHeight = bitmap.getHeight(); 115 setBounds(0, 0, mWidth, mHeight); 116 } 117 118 @Override 119 public void draw(Canvas canvas) { 120 Paint paint = new Paint(); 121 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); 122 canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, paint); 123 } 124 125 @Override 126 public int getOpacity() { 127 return PixelFormat.OPAQUE; 128 } 129 130 @Override 131 public void setBounds(int left, int top, int right, int bottom) { 132 mDrawLeft = left + (right-left - mWidth) / 2; 133 mDrawTop = top + (bottom-top - mHeight) / 2; 134 } 135 136 @Override 137 public void setBounds(Rect bounds) { 138 // TODO Auto-generated method stub 139 super.setBounds(bounds); 140 } 141 142 @Override 143 public void setAlpha(int alpha) { 144 throw new UnsupportedOperationException( 145 "Not supported with this drawable"); 146 } 147 148 @Override 149 public void setColorFilter(ColorFilter cf) { 150 throw new UnsupportedOperationException( 151 "Not supported with this drawable"); 152 } 153 154 @Override 155 public void setDither(boolean dither) { 156 throw new UnsupportedOperationException( 157 "Not supported with this drawable"); 158 } 159 160 @Override 161 public void setFilterBitmap(boolean filter) { 162 throw new UnsupportedOperationException( 163 "Not supported with this drawable"); 164 } 165 166 @Override 167 public int getIntrinsicWidth() { 168 return mWidth; 169 } 170 171 @Override 172 public int getIntrinsicHeight() { 173 return mHeight; 174 } 175 176 @Override 177 public int getMinimumWidth() { 178 return mWidth; 179 } 180 181 @Override 182 public int getMinimumHeight() { 183 return mHeight; 184 } 185 } 186 187 static class Globals extends IWallpaperManagerCallback.Stub { 188 private IWallpaperManager mService; 189 private Bitmap mWallpaper; 190 private Bitmap mDefaultWallpaper; 191 192 private static final int MSG_CLEAR_WALLPAPER = 1; 193 194 private final Handler mHandler; 195 196 Globals(Looper looper) { 197 IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE); 198 mService = IWallpaperManager.Stub.asInterface(b); 199 mHandler = new Handler(looper) { 200 @Override 201 public void handleMessage(Message msg) { 202 switch (msg.what) { 203 case MSG_CLEAR_WALLPAPER: 204 synchronized (this) { 205 mWallpaper = null; 206 mDefaultWallpaper = null; 207 } 208 break; 209 } 210 } 211 }; 212 } 213 214 public void onWallpaperChanged() { 215 /* The wallpaper has changed but we shouldn't eagerly load the 216 * wallpaper as that would be inefficient. Reset the cached wallpaper 217 * to null so if the user requests the wallpaper again then we'll 218 * fetch it. 219 */ 220 mHandler.sendEmptyMessage(MSG_CLEAR_WALLPAPER); 221 } 222 223 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault) { 224 synchronized (this) { 225 if (mWallpaper != null) { 226 return mWallpaper; 227 } 228 if (mDefaultWallpaper != null) { 229 return mDefaultWallpaper; 230 } 231 mWallpaper = null; 232 try { 233 mWallpaper = getCurrentWallpaperLocked(context); 234 } catch (OutOfMemoryError e) { 235 Log.w(TAG, "No memory load current wallpaper", e); 236 } 237 if (mWallpaper == null && returnDefault) { 238 mDefaultWallpaper = getDefaultWallpaperLocked(context); 239 return mDefaultWallpaper; 240 } 241 return mWallpaper; 242 } 243 } 244 245 private Bitmap getCurrentWallpaperLocked(Context context) { 246 try { 247 Bundle params = new Bundle(); 248 ParcelFileDescriptor fd = mService.getWallpaper(this, params); 249 if (fd != null) { 250 int width = params.getInt("width", 0); 251 int height = params.getInt("height", 0); 252 253 try { 254 BitmapFactory.Options options = new BitmapFactory.Options(); 255 Bitmap bm = BitmapFactory.decodeFileDescriptor( 256 fd.getFileDescriptor(), null, options); 257 return generateBitmap(context, bm, width, height); 258 } catch (OutOfMemoryError e) { 259 Log.w(TAG, "Can't decode file", e); 260 } finally { 261 try { 262 fd.close(); 263 } catch (IOException e) { 264 } 265 } 266 } 267 } catch (RemoteException e) { 268 } 269 return null; 270 } 271 272 private Bitmap getDefaultWallpaperLocked(Context context) { 273 try { 274 InputStream is = context.getResources().openRawResource( 275 com.android.internal.R.drawable.default_wallpaper); 276 if (is != null) { 277 int width = mService.getWidthHint(); 278 int height = mService.getHeightHint(); 279 280 try { 281 BitmapFactory.Options options = new BitmapFactory.Options(); 282 Bitmap bm = BitmapFactory.decodeStream(is, null, options); 283 return generateBitmap(context, bm, width, height); 284 } catch (OutOfMemoryError e) { 285 Log.w(TAG, "Can't decode stream", e); 286 } finally { 287 try { 288 is.close(); 289 } catch (IOException e) { 290 } 291 } 292 } 293 } catch (RemoteException e) { 294 } 295 return null; 296 } 297 } 298 299 private static Object mSync = new Object(); 300 private static Globals sGlobals; 301 302 static void initGlobals(Looper looper) { 303 synchronized (mSync) { 304 if (sGlobals == null) { 305 sGlobals = new Globals(looper); 306 } 307 } 308 } 309 310 /*package*/ WallpaperManager(Context context, Handler handler) { 311 mContext = context; 312 initGlobals(context.getMainLooper()); 313 } 314 315 /** 316 * Retrieve a WallpaperManager associated with the given Context. 317 */ 318 public static WallpaperManager getInstance(Context context) { 319 return (WallpaperManager)context.getSystemService( 320 Context.WALLPAPER_SERVICE); 321 } 322 323 /** @hide */ 324 public IWallpaperManager getIWallpaperManager() { 325 return sGlobals.mService; 326 } 327 328 /** 329 * Retrieve the current system wallpaper; if 330 * no wallpaper is set, the system default wallpaper is returned. 331 * This is returned as an 332 * abstract Drawable that you can install in a View to display whatever 333 * wallpaper the user has currently set. 334 * 335 * @return Returns a Drawable object that will draw the wallpaper. 336 */ 337 public Drawable getDrawable() { 338 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true); 339 if (bm != null) { 340 Drawable dr = new BitmapDrawable(mContext.getResources(), bm); 341 dr.setDither(false); 342 return dr; 343 } 344 return null; 345 } 346 347 /** 348 * Retrieve the current system wallpaper; if there is no wallpaper set, 349 * a null pointer is returned. This is returned as an 350 * abstract Drawable that you can install in a View to display whatever 351 * wallpaper the user has currently set. 352 * 353 * @return Returns a Drawable object that will draw the wallpaper or a 354 * null pointer if these is none. 355 */ 356 public Drawable peekDrawable() { 357 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false); 358 if (bm != null) { 359 Drawable dr = new BitmapDrawable(mContext.getResources(), bm); 360 dr.setDither(false); 361 return dr; 362 } 363 return null; 364 } 365 366 /** 367 * Like {@link #getDrawable()}, but the returned Drawable has a number 368 * of limitations to reduce its overhead as much as possible. It will 369 * never scale the wallpaper (only centering it if the requested bounds 370 * do match the bitmap bounds, which should not be typical), doesn't 371 * allow setting an alpha, color filter, or other attributes, etc. The 372 * bounds of the returned drawable will be initialized to the same bounds 373 * as the wallpaper, so normally you will not need to touch it. The 374 * drawable also assumes that it will be used in a context running in 375 * the same density as the screen (not in density compatibility mode). 376 * 377 * @return Returns a Drawable object that will draw the wallpaper. 378 */ 379 public Drawable getFastDrawable() { 380 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true); 381 if (bm != null) { 382 Drawable dr = new FastBitmapDrawable(bm); 383 return dr; 384 } 385 return null; 386 } 387 388 /** 389 * Like {@link #getFastDrawable()}, but if there is no wallpaper set, 390 * a null pointer is returned. 391 * 392 * @return Returns an optimized Drawable object that will draw the 393 * wallpaper or a null pointer if these is none. 394 */ 395 public Drawable peekFastDrawable() { 396 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false); 397 if (bm != null) { 398 Drawable dr = new FastBitmapDrawable(bm); 399 return dr; 400 } 401 return null; 402 } 403 404 /** 405 * If the current wallpaper is a live wallpaper component, return the 406 * information about that wallpaper. Otherwise, if it is a static image, 407 * simply return null. 408 */ 409 public WallpaperInfo getWallpaperInfo() { 410 try { 411 return sGlobals.mService.getWallpaperInfo(); 412 } catch (RemoteException e) { 413 return null; 414 } 415 } 416 417 /** 418 * Change the current system wallpaper to the bitmap in the given resource. 419 * The resource is opened as a raw data stream and copied into the 420 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent 421 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. 422 * 423 * @param resid The bitmap to save. 424 * 425 * @throws IOException If an error occurs reverting to the default 426 * wallpaper. 427 */ 428 public void setResource(int resid) throws IOException { 429 try { 430 Resources resources = mContext.getResources(); 431 /* Set the wallpaper to the default values */ 432 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper( 433 "res:" + resources.getResourceName(resid)); 434 if (fd != null) { 435 FileOutputStream fos = null; 436 try { 437 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); 438 setWallpaper(resources.openRawResource(resid), fos); 439 } finally { 440 if (fos != null) { 441 fos.close(); 442 } 443 } 444 } 445 } catch (RemoteException e) { 446 } 447 } 448 449 /** 450 * Change the current system wallpaper to a bitmap. The given bitmap is 451 * converted to a PNG and stored as the wallpaper. On success, the intent 452 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. 453 * 454 * @param bitmap The bitmap to save. 455 * 456 * @throws IOException If an error occurs reverting to the default 457 * wallpaper. 458 */ 459 public void setBitmap(Bitmap bitmap) throws IOException { 460 try { 461 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null); 462 if (fd == null) { 463 return; 464 } 465 FileOutputStream fos = null; 466 try { 467 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); 468 bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); 469 } finally { 470 if (fos != null) { 471 fos.close(); 472 } 473 } 474 } catch (RemoteException e) { 475 } 476 } 477 478 /** 479 * Change the current system wallpaper to a specific byte stream. The 480 * give InputStream is copied into persistent storage and will now be 481 * used as the wallpaper. Currently it must be either a JPEG or PNG 482 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} 483 * is broadcast. 484 * 485 * @param data A stream containing the raw data to install as a wallpaper. 486 * 487 * @throws IOException If an error occurs reverting to the default 488 * wallpaper. 489 */ 490 public void setStream(InputStream data) throws IOException { 491 try { 492 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null); 493 if (fd == null) { 494 return; 495 } 496 FileOutputStream fos = null; 497 try { 498 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); 499 setWallpaper(data, fos); 500 } finally { 501 if (fos != null) { 502 fos.close(); 503 } 504 } 505 } catch (RemoteException e) { 506 } 507 } 508 509 private void setWallpaper(InputStream data, FileOutputStream fos) 510 throws IOException { 511 byte[] buffer = new byte[32768]; 512 int amt; 513 while ((amt=data.read(buffer)) > 0) { 514 fos.write(buffer, 0, amt); 515 } 516 } 517 518 /** 519 * Returns the desired minimum width for the wallpaper. Callers of 520 * {@link #setBitmap(android.graphics.Bitmap)} or 521 * {@link #setStream(java.io.InputStream)} should check this value 522 * beforehand to make sure the supplied wallpaper respects the desired 523 * minimum width. 524 * 525 * If the returned value is <= 0, the caller should use the width of 526 * the default display instead. 527 * 528 * @return The desired minimum width for the wallpaper. This value should 529 * be honored by applications that set the wallpaper but it is not 530 * mandatory. 531 */ 532 public int getDesiredMinimumWidth() { 533 try { 534 return sGlobals.mService.getWidthHint(); 535 } catch (RemoteException e) { 536 // Shouldn't happen! 537 return 0; 538 } 539 } 540 541 /** 542 * Returns the desired minimum height for the wallpaper. Callers of 543 * {@link #setBitmap(android.graphics.Bitmap)} or 544 * {@link #setStream(java.io.InputStream)} should check this value 545 * beforehand to make sure the supplied wallpaper respects the desired 546 * minimum height. 547 * 548 * If the returned value is <= 0, the caller should use the height of 549 * the default display instead. 550 * 551 * @return The desired minimum height for the wallpaper. This value should 552 * be honored by applications that set the wallpaper but it is not 553 * mandatory. 554 */ 555 public int getDesiredMinimumHeight() { 556 try { 557 return sGlobals.mService.getHeightHint(); 558 } catch (RemoteException e) { 559 // Shouldn't happen! 560 return 0; 561 } 562 } 563 564 /** 565 * For use only by the current home application, to specify the size of 566 * wallpaper it would like to use. This allows such applications to have 567 * a virtual wallpaper that is larger than the physical screen, matching 568 * the size of their workspace. 569 * @param minimumWidth Desired minimum width 570 * @param minimumHeight Desired minimum height 571 */ 572 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) { 573 try { 574 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight); 575 } catch (RemoteException e) { 576 } 577 } 578 579 /** 580 * Set the position of the current wallpaper within any larger space, when 581 * that wallpaper is visible behind the given window. The X and Y offsets 582 * are floating point numbers ranging from 0 to 1, representing where the 583 * wallpaper should be positioned within the screen space. These only 584 * make sense when the wallpaper is larger than the screen. 585 * 586 * @param windowToken The window who these offsets should be associated 587 * with, as returned by {@link android.view.View#getWindowToken() 588 * View.getWindowToken()}. 589 * @param xOffset The offset along the X dimension, from 0 to 1. 590 * @param yOffset The offset along the Y dimension, from 0 to 1. 591 */ 592 public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) { 593 try { 594 //Log.v(TAG, "Sending new wallpaper offsets from app..."); 595 ViewAncestor.getWindowSession(mContext.getMainLooper()).setWallpaperPosition( 596 windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep); 597 //Log.v(TAG, "...app returning after sending offsets!"); 598 } catch (RemoteException e) { 599 // Ignore. 600 } 601 } 602 603 /** 604 * For applications that use multiple virtual screens showing a wallpaper, 605 * specify the step size between virtual screens. For example, if the 606 * launcher has 3 virtual screens, it would specify an xStep of 0.5, 607 * since the X offset for those screens are 0.0, 0.5 and 1.0 608 * @param xStep The X offset delta from one screen to the next one 609 * @param yStep The Y offset delta from one screen to the next one 610 */ 611 public void setWallpaperOffsetSteps(float xStep, float yStep) { 612 mWallpaperXStep = xStep; 613 mWallpaperYStep = yStep; 614 } 615 616 /** 617 * Send an arbitrary command to the current active wallpaper. 618 * 619 * @param windowToken The window who these offsets should be associated 620 * with, as returned by {@link android.view.View#getWindowToken() 621 * View.getWindowToken()}. 622 * @param action Name of the command to perform. This must be a scoped 623 * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT". 624 * @param x Arbitrary integer argument based on command. 625 * @param y Arbitrary integer argument based on command. 626 * @param z Arbitrary integer argument based on command. 627 * @param extras Optional additional information for the command, or null. 628 */ 629 public void sendWallpaperCommand(IBinder windowToken, String action, 630 int x, int y, int z, Bundle extras) { 631 try { 632 //Log.v(TAG, "Sending new wallpaper offsets from app..."); 633 ViewAncestor.getWindowSession(mContext.getMainLooper()).sendWallpaperCommand( 634 windowToken, action, x, y, z, extras, false); 635 //Log.v(TAG, "...app returning after sending offsets!"); 636 } catch (RemoteException e) { 637 // Ignore. 638 } 639 } 640 641 /** 642 * Clear the offsets previously associated with this window through 643 * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts 644 * the window to its default state, where it does not cause the wallpaper 645 * to scroll from whatever its last offsets were. 646 * 647 * @param windowToken The window who these offsets should be associated 648 * with, as returned by {@link android.view.View#getWindowToken() 649 * View.getWindowToken()}. 650 */ 651 public void clearWallpaperOffsets(IBinder windowToken) { 652 try { 653 ViewAncestor.getWindowSession(mContext.getMainLooper()).setWallpaperPosition( 654 windowToken, -1, -1, -1, -1); 655 } catch (RemoteException e) { 656 // Ignore. 657 } 658 } 659 660 /** 661 * Remove any currently set wallpaper, reverting to the system's default 662 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} 663 * is broadcast. 664 * 665 * @throws IOException If an error occurs reverting to the default 666 * wallpaper. 667 */ 668 public void clear() throws IOException { 669 setResource(com.android.internal.R.drawable.default_wallpaper); 670 } 671 672 static Bitmap generateBitmap(Context context, Bitmap bm, int width, int height) { 673 if (bm == null) { 674 return null; 675 } 676 677 bm.setDensity(DisplayMetrics.DENSITY_DEVICE); 678 679 if (width <= 0 || height <= 0 680 || (bm.getWidth() == width && bm.getHeight() == height)) { 681 return bm; 682 } 683 684 // This is the final bitmap we want to return. 685 try { 686 Bitmap newbm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 687 newbm.setDensity(DisplayMetrics.DENSITY_DEVICE); 688 689 Canvas c = new Canvas(newbm); 690 Rect targetRect = new Rect(); 691 targetRect.right = bm.getWidth(); 692 targetRect.bottom = bm.getHeight(); 693 694 int deltaw = width - targetRect.right; 695 int deltah = height - targetRect.bottom; 696 697 if (deltaw > 0 || deltah > 0) { 698 // We need to scale up so it covers the entire area. 699 float scale = 1.0f; 700 if (deltaw > deltah) { 701 scale = width / (float)targetRect.right; 702 } else { 703 scale = height / (float)targetRect.bottom; 704 } 705 targetRect.right = (int)(targetRect.right*scale); 706 targetRect.bottom = (int)(targetRect.bottom*scale); 707 deltaw = width - targetRect.right; 708 deltah = height - targetRect.bottom; 709 } 710 711 targetRect.offset(deltaw/2, deltah/2); 712 713 Paint paint = new Paint(); 714 paint.setFilterBitmap(true); 715 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); 716 c.drawBitmap(bm, null, targetRect, paint); 717 718 bm.recycle(); 719 return newbm; 720 } catch (OutOfMemoryError e) { 721 Log.w(TAG, "Can't generate default bitmap", e); 722 return bm; 723 } 724 } 725} 726