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