WallpaperManagerService.java revision 1a96b63b6c8c98c9d3f04ff2d5b7616e5707aedd
1/* 2 * Copyright (C) 2008 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 com.android.server.wallpaper; 18 19import static android.app.WallpaperManager.FLAG_SYSTEM; 20import static android.app.WallpaperManager.FLAG_LOCK; 21import static android.os.ParcelFileDescriptor.*; 22 23import android.app.ActivityManager; 24import android.app.ActivityManagerNative; 25import android.app.AppGlobals; 26import android.app.AppOpsManager; 27import android.app.IUserSwitchObserver; 28import android.app.IWallpaperManager; 29import android.app.IWallpaperManagerCallback; 30import android.app.PendingIntent; 31import android.app.WallpaperInfo; 32import android.app.WallpaperManager; 33import android.app.admin.DevicePolicyManager; 34import android.app.backup.BackupManager; 35import android.app.backup.WallpaperBackupHelper; 36import android.content.BroadcastReceiver; 37import android.content.ComponentName; 38import android.content.Context; 39import android.content.Intent; 40import android.content.IntentFilter; 41import android.content.ServiceConnection; 42import android.content.pm.IPackageManager; 43import android.content.pm.PackageManager; 44import android.content.pm.ResolveInfo; 45import android.content.pm.ServiceInfo; 46import android.content.pm.PackageManager.NameNotFoundException; 47import android.content.pm.UserInfo; 48import android.content.res.Resources; 49import android.graphics.Bitmap; 50import android.graphics.BitmapFactory; 51import android.graphics.BitmapRegionDecoder; 52import android.graphics.Matrix; 53import android.graphics.Point; 54import android.graphics.Rect; 55import android.os.Binder; 56import android.os.Bundle; 57import android.os.Environment; 58import android.os.FileUtils; 59import android.os.IBinder; 60import android.os.IRemoteCallback; 61import android.os.RemoteException; 62import android.os.FileObserver; 63import android.os.ParcelFileDescriptor; 64import android.os.RemoteCallbackList; 65import android.os.SELinux; 66import android.os.ServiceManager; 67import android.os.SystemClock; 68import android.os.UserHandle; 69import android.os.UserManager; 70import android.service.wallpaper.IWallpaperConnection; 71import android.service.wallpaper.IWallpaperEngine; 72import android.service.wallpaper.IWallpaperService; 73import android.service.wallpaper.WallpaperService; 74import android.util.EventLog; 75import android.util.Slog; 76import android.util.SparseArray; 77import android.util.Xml; 78import android.view.Display; 79import android.view.IWindowManager; 80import android.view.WindowManager; 81 82import java.io.BufferedOutputStream; 83import java.io.FileDescriptor; 84import java.io.IOException; 85import java.io.InputStream; 86import java.io.File; 87import java.io.FileNotFoundException; 88import java.io.FileInputStream; 89import java.io.FileOutputStream; 90import java.io.PrintWriter; 91import java.nio.charset.StandardCharsets; 92import java.util.Arrays; 93import java.util.List; 94 95import org.xmlpull.v1.XmlPullParser; 96import org.xmlpull.v1.XmlPullParserException; 97import org.xmlpull.v1.XmlSerializer; 98 99import com.android.internal.content.PackageMonitor; 100import com.android.internal.util.FastXmlSerializer; 101import com.android.internal.util.JournaledFile; 102import com.android.internal.R; 103import com.android.server.EventLogTags; 104 105import libcore.io.IoUtils; 106 107public class WallpaperManagerService extends IWallpaperManager.Stub { 108 static final String TAG = "WallpaperManagerService"; 109 static final boolean DEBUG = false; 110 111 final Object mLock = new Object(); 112 113 /** 114 * Minimum time between crashes of a wallpaper service for us to consider 115 * restarting it vs. just reverting to the static wallpaper. 116 */ 117 static final long MIN_WALLPAPER_CRASH_TIME = 10000; 118 static final int MAX_WALLPAPER_COMPONENT_LOG_LENGTH = 128; 119 static final String WALLPAPER = "wallpaper_orig"; 120 static final String WALLPAPER_CROP = "wallpaper"; 121 static final String WALLPAPER_LOCK_ORIG = "wallpaper_lock_orig"; 122 static final String WALLPAPER_LOCK_CROP = "wallpaper_lock"; 123 static final String WALLPAPER_INFO = "wallpaper_info.xml"; 124 125 // All the various per-user state files we need to be aware of 126 static final String[] sPerUserFiles = new String[] { 127 WALLPAPER, WALLPAPER_CROP, 128 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP, 129 WALLPAPER_INFO 130 }; 131 132 /** 133 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks 134 * that the wallpaper has changed. The CREATE is triggered when there is no 135 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered 136 * everytime the wallpaper is changed. 137 */ 138 private class WallpaperObserver extends FileObserver { 139 140 final int mUserId; 141 final WallpaperData mWallpaper; 142 final File mWallpaperDir; 143 final File mWallpaperFile; 144 final File mWallpaperLockFile; 145 final File mWallpaperInfoFile; 146 147 public WallpaperObserver(WallpaperData wallpaper) { 148 super(getWallpaperDir(wallpaper.userId).getAbsolutePath(), 149 CLOSE_WRITE | MOVED_TO | DELETE | DELETE_SELF); 150 mUserId = wallpaper.userId; 151 mWallpaperDir = getWallpaperDir(wallpaper.userId); 152 mWallpaper = wallpaper; 153 mWallpaperFile = new File(mWallpaperDir, WALLPAPER); 154 mWallpaperLockFile = new File(mWallpaperDir, WALLPAPER_LOCK_ORIG); 155 mWallpaperInfoFile = new File(mWallpaperDir, WALLPAPER_INFO); 156 } 157 158 private WallpaperData dataForEvent(boolean sysChanged, boolean lockChanged) { 159 WallpaperData wallpaper = null; 160 synchronized (mLock) { 161 if (lockChanged) { 162 wallpaper = mLockWallpaperMap.get(mUserId); 163 } 164 if (wallpaper == null) { 165 // no lock-specific wallpaper exists, or sys case, handled together 166 wallpaper = mWallpaperMap.get(mUserId); 167 } 168 } 169 return (wallpaper != null) ? wallpaper : mWallpaper; 170 } 171 172 @Override 173 public void onEvent(int event, String path) { 174 if (path == null) { 175 return; 176 } 177 final boolean written = (event == CLOSE_WRITE || event == MOVED_TO); 178 final File changedFile = new File(mWallpaperDir, path); 179 180 // System and system+lock changes happen on the system wallpaper input file; 181 // lock-only changes happen on the dedicated lock wallpaper input file 182 final boolean sysWallpaperChanged = (mWallpaperFile.equals(changedFile)); 183 final boolean lockWallpaperChanged = (mWallpaperLockFile.equals(changedFile)); 184 WallpaperData wallpaper = dataForEvent(sysWallpaperChanged, lockWallpaperChanged); 185 186 if (DEBUG) { 187 Slog.v(TAG, "Wallpaper file change: evt=" + event 188 + " path=" + path 189 + " sys=" + sysWallpaperChanged 190 + " lock=" + lockWallpaperChanged 191 + " imagePending=" + wallpaper.imageWallpaperPending 192 + " whichPending=0x" + Integer.toHexString(wallpaper.whichPending) 193 + " written=" + written); 194 } 195 synchronized (mLock) { 196 if (sysWallpaperChanged || mWallpaperInfoFile.equals(changedFile)) { 197 // changing the wallpaper means we'll need to back up the new one 198 long origId = Binder.clearCallingIdentity(); 199 BackupManager bm = new BackupManager(mContext); 200 bm.dataChanged(); 201 Binder.restoreCallingIdentity(origId); 202 } 203 if (sysWallpaperChanged || lockWallpaperChanged) { 204 notifyCallbacksLocked(wallpaper); 205 if (wallpaper.wallpaperComponent == null 206 || event != CLOSE_WRITE // includes the MOVED_TO case 207 || wallpaper.imageWallpaperPending) { 208 if (written) { 209 // The image source has finished writing the source image, 210 // so we now produce the crop rect (in the background), and 211 // only publish the new displayable (sub)image as a result 212 // of that work. 213 if (DEBUG) { 214 Slog.v(TAG, "Wallpaper written; generating crop"); 215 } 216 generateCrop(wallpaper); 217 if (DEBUG) { 218 Slog.v(TAG, "Crop done; invoking completion callback"); 219 } 220 wallpaper.imageWallpaperPending = false; 221 if (wallpaper.setComplete != null) { 222 try { 223 wallpaper.setComplete.onWallpaperChanged(); 224 } catch (RemoteException e) { 225 // if this fails we don't really care; the setting app may just 226 // have crashed and that sort of thing is a fact of life. 227 } 228 } 229 if (sysWallpaperChanged) { 230 // If this was the system wallpaper, rebind... 231 bindWallpaperComponentLocked(mImageWallpaper, true, 232 false, wallpaper, null); 233 } 234 if (lockWallpaperChanged 235 || (wallpaper.whichPending & FLAG_LOCK) != 0) { 236 if (DEBUG) { 237 Slog.i(TAG, "Lock-relevant wallpaper changed"); 238 } 239 // either a lock-only wallpaper commit or a system+lock event. 240 // if it's system-plus-lock we need to wipe the lock bookkeeping; 241 // we're falling back to displaying the system wallpaper there. 242 if (!lockWallpaperChanged) { 243 mLockWallpaperMap.remove(wallpaper.userId); 244 } 245 // and in any case, tell keyguard about it 246 final IWallpaperManagerCallback cb = mKeyguardListener; 247 if (cb != null) { 248 try { 249 cb.onWallpaperChanged(); 250 } catch (RemoteException e) { 251 // Oh well it went away; no big deal 252 } 253 } 254 } 255 saveSettingsLocked(wallpaper.userId); 256 } 257 } 258 } 259 } 260 } 261 } 262 263 /** 264 * Once a new wallpaper has been written via setWallpaper(...), it needs to be cropped 265 * for display. 266 */ 267 private void generateCrop(WallpaperData wallpaper) { 268 boolean success = false; 269 270 Rect cropHint = new Rect(wallpaper.cropHint); 271 272 if (DEBUG) { 273 Slog.v(TAG, "Generating crop for new wallpaper(s): 0x" 274 + Integer.toHexString(wallpaper.whichPending) 275 + " to " + wallpaper.cropFile.getName() 276 + " crop=(" + cropHint.width() + 'x' + cropHint.height() 277 + ") dim=(" + wallpaper.width + 'x' + wallpaper.height + ')'); 278 } 279 280 // Analyse the source; needed in multiple cases 281 BitmapFactory.Options options = new BitmapFactory.Options(); 282 options.inJustDecodeBounds = true; 283 BitmapFactory.decodeFile(wallpaper.wallpaperFile.getAbsolutePath(), options); 284 if (options.outWidth <= 0 || options.outHeight <= 0) { 285 Slog.e(TAG, "Invalid wallpaper data"); 286 success = false; 287 } else { 288 boolean needCrop = false; 289 boolean needScale = false; 290 291 // Empty crop means use the full image 292 if (cropHint.isEmpty()) { 293 cropHint.left = cropHint.top = 0; 294 cropHint.right = options.outWidth; 295 cropHint.bottom = options.outHeight; 296 } else { 297 // force the crop rect to lie within the measured bounds 298 cropHint.offset( 299 (cropHint.right > options.outWidth ? options.outWidth - cropHint.right : 0), 300 (cropHint.bottom > options.outHeight ? options.outHeight - cropHint.bottom : 0)); 301 302 // Don't bother cropping if what we're left with is identity 303 needCrop = (options.outHeight >= cropHint.height() 304 && options.outWidth >= cropHint.width()); 305 } 306 307 // scale if the crop height winds up not matching the recommended metrics 308 needScale = (wallpaper.height != cropHint.height()); 309 310 if (DEBUG) { 311 Slog.v(TAG, "crop: w=" + cropHint.width() + " h=" + cropHint.height()); 312 Slog.v(TAG, "dims: w=" + wallpaper.width + " h=" + wallpaper.height); 313 Slog.v(TAG, "meas: w=" + options.outWidth + " h=" + options.outHeight); 314 Slog.v(TAG, "crop?=" + needCrop + " scale?=" + needScale); 315 } 316 317 if (!needCrop && !needScale) { 318 // Simple case: the nominal crop fits what we want, so we take 319 // the whole thing and just copy the image file directly. 320 if (DEBUG) { 321 Slog.v(TAG, "Null crop of new wallpaper; copying"); 322 } 323 success = FileUtils.copyFile(wallpaper.wallpaperFile, wallpaper.cropFile); 324 if (!success) { 325 wallpaper.cropFile.delete(); 326 // TODO: fall back to default wallpaper in this case 327 } 328 } else { 329 // Fancy case: crop and scale. First, we decode and scale down if appropriate. 330 FileOutputStream f = null; 331 BufferedOutputStream bos = null; 332 try { 333 BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance( 334 wallpaper.wallpaperFile.getAbsolutePath(), false); 335 336 // This actually downsamples only by powers of two, but that's okay; we do 337 // a proper scaling blit later. This is to minimize transient RAM use. 338 // We calculate the largest power-of-two under the actual ratio rather than 339 // just let the decode take care of it because we also want to remap where the 340 // cropHint rectangle lies in the decoded [super]rect. 341 final BitmapFactory.Options scaler; 342 final int actualScale = cropHint.height() / wallpaper.height; 343 int scale = 1; 344 while (2*scale < actualScale) { 345 scale *= 2; 346 } 347 if (scale > 1) { 348 scaler = new BitmapFactory.Options(); 349 scaler.inSampleSize = scale; 350 if (DEBUG) { 351 Slog.v(TAG, "Downsampling cropped rect with scale " + scale); 352 } 353 } else { 354 scaler = null; 355 } 356 Bitmap cropped = decoder.decodeRegion(cropHint, scaler); 357 decoder.recycle(); 358 359 if (cropped == null) { 360 Slog.e(TAG, "Could not decode new wallpaper"); 361 } else { 362 // We've got the extracted crop; now we want to scale it properly to 363 // the desired rectangle. That's a height-biased operation: make it 364 // fit the hinted height, and accept whatever width we end up with. 365 cropHint.offsetTo(0, 0); 366 cropHint.right /= scale; // adjust by downsampling factor 367 cropHint.bottom /= scale; 368 final float heightR = ((float)wallpaper.height) / ((float)cropHint.height()); 369 if (DEBUG) { 370 Slog.v(TAG, "scale " + heightR + ", extracting " + cropHint); 371 } 372 final int destWidth = (int)(cropHint.width() * heightR); 373 final Bitmap finalCrop = Bitmap.createScaledBitmap(cropped, 374 destWidth, wallpaper.height, true); 375 if (DEBUG) { 376 Slog.v(TAG, "Final extract:"); 377 Slog.v(TAG, " dims: w=" + wallpaper.width 378 + " h=" + wallpaper.height); 379 Slog.v(TAG, " out: w=" + finalCrop.getWidth() 380 + " h=" + finalCrop.getHeight()); 381 } 382 383 f = new FileOutputStream(wallpaper.cropFile); 384 bos = new BufferedOutputStream(f, 32*1024); 385 finalCrop.compress(Bitmap.CompressFormat.PNG, 90, bos); 386 bos.flush(); // don't rely on the implicit flush-at-close when noting success 387 success = true; 388 } 389 } catch (Exception e) { 390 if (DEBUG) { 391 Slog.e(TAG, "Error decoding crop", e); 392 } 393 } finally { 394 IoUtils.closeQuietly(bos); 395 IoUtils.closeQuietly(f); 396 } 397 } 398 } 399 400 if (!success) { 401 Slog.e(TAG, "Unable to apply new wallpaper"); 402 wallpaper.cropFile.delete(); 403 } 404 405 if (wallpaper.cropFile.exists()) { 406 boolean didRestorecon = SELinux.restorecon(wallpaper.cropFile.getAbsoluteFile()); 407 if (DEBUG) { 408 Slog.v(TAG, "restorecon() of crop file returned " + didRestorecon); 409 } 410 } 411 } 412 413 final Context mContext; 414 final IWindowManager mIWindowManager; 415 final IPackageManager mIPackageManager; 416 final MyPackageMonitor mMonitor; 417 final AppOpsManager mAppOpsManager; 418 WallpaperData mLastWallpaper; 419 IWallpaperManagerCallback mKeyguardListener; 420 421 /** 422 * ID of the current wallpaper, changed every time anything sets a wallpaper. 423 * This is used for external detection of wallpaper update activity. 424 */ 425 int mWallpaperId; 426 427 /** 428 * Name of the component used to display bitmap wallpapers from either the gallery or 429 * built-in wallpapers. 430 */ 431 final ComponentName mImageWallpaper; 432 433 final SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>(); 434 final SparseArray<WallpaperData> mLockWallpaperMap = new SparseArray<WallpaperData>(); 435 436 int mCurrentUserId; 437 438 static class WallpaperData { 439 440 int userId; 441 442 final File wallpaperFile; // source image 443 final File cropFile; // eventual destination 444 445 /** 446 * True while the client is writing a new wallpaper 447 */ 448 boolean imageWallpaperPending; 449 450 /** 451 * Which new wallpapers are being written; mirrors the 'which' 452 * selector bit field to setWallpaper(). 453 */ 454 int whichPending; 455 456 /** 457 * Callback once the set + crop is finished 458 */ 459 IWallpaperManagerCallback setComplete; 460 461 /** 462 * Resource name if using a picture from the wallpaper gallery 463 */ 464 String name = ""; 465 466 /** 467 * The component name of the currently set live wallpaper. 468 */ 469 ComponentName wallpaperComponent; 470 471 /** 472 * The component name of the wallpaper that should be set next. 473 */ 474 ComponentName nextWallpaperComponent; 475 476 /** 477 * The ID of this wallpaper 478 */ 479 int wallpaperId; 480 481 WallpaperConnection connection; 482 long lastDiedTime; 483 boolean wallpaperUpdating; 484 WallpaperObserver wallpaperObserver; 485 486 /** 487 * List of callbacks registered they should each be notified when the wallpaper is changed. 488 */ 489 private RemoteCallbackList<IWallpaperManagerCallback> callbacks 490 = new RemoteCallbackList<IWallpaperManagerCallback>(); 491 492 int width = -1; 493 int height = -1; 494 495 /** 496 * The crop hint supplied for displaying a subset of the source image 497 */ 498 final Rect cropHint = new Rect(0, 0, 0, 0); 499 500 final Rect padding = new Rect(0, 0, 0, 0); 501 502 WallpaperData(int userId, String inputFileName, String cropFileName) { 503 this.userId = userId; 504 final File wallpaperDir = getWallpaperDir(userId); 505 wallpaperFile = new File(wallpaperDir, inputFileName); 506 cropFile = new File(wallpaperDir, cropFileName); 507 } 508 509 // Called during initialization of a given user's wallpaper bookkeeping 510 boolean cropExists() { 511 return cropFile.exists(); 512 } 513 } 514 515 int makeWallpaperIdLocked() { 516 do { 517 ++mWallpaperId; 518 } while (mWallpaperId == 0); 519 return mWallpaperId; 520 } 521 522 class WallpaperConnection extends IWallpaperConnection.Stub 523 implements ServiceConnection { 524 final WallpaperInfo mInfo; 525 final Binder mToken = new Binder(); 526 IWallpaperService mService; 527 IWallpaperEngine mEngine; 528 WallpaperData mWallpaper; 529 IRemoteCallback mReply; 530 531 boolean mDimensionsChanged = false; 532 boolean mPaddingChanged = false; 533 534 public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) { 535 mInfo = info; 536 mWallpaper = wallpaper; 537 } 538 539 @Override 540 public void onServiceConnected(ComponentName name, IBinder service) { 541 synchronized (mLock) { 542 if (mWallpaper.connection == this) { 543 mService = IWallpaperService.Stub.asInterface(service); 544 attachServiceLocked(this, mWallpaper); 545 // XXX should probably do saveSettingsLocked() later 546 // when we have an engine, but I'm not sure about 547 // locking there and anyway we always need to be able to 548 // recover if there is something wrong. 549 saveSettingsLocked(mWallpaper.userId); 550 } 551 } 552 } 553 554 @Override 555 public void onServiceDisconnected(ComponentName name) { 556 synchronized (mLock) { 557 mService = null; 558 mEngine = null; 559 if (mWallpaper.connection == this) { 560 Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent); 561 if (!mWallpaper.wallpaperUpdating 562 && mWallpaper.userId == mCurrentUserId) { 563 // There is a race condition which causes 564 // {@link #mWallpaper.wallpaperUpdating} to be false even if it is 565 // currently updating since the broadcast notifying us is async. 566 // This race is overcome by the general rule that we only reset the 567 // wallpaper if its service was shut down twice 568 // during {@link #MIN_WALLPAPER_CRASH_TIME} millis. 569 if (mWallpaper.lastDiedTime != 0 570 && mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME 571 > SystemClock.uptimeMillis()) { 572 Slog.w(TAG, "Reverting to built-in wallpaper!"); 573 clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId, null); 574 } else { 575 mWallpaper.lastDiedTime = SystemClock.uptimeMillis(); 576 } 577 final String flattened = name.flattenToString(); 578 EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED, 579 flattened.substring(0, Math.min(flattened.length(), 580 MAX_WALLPAPER_COMPONENT_LOG_LENGTH))); 581 } 582 } 583 } 584 } 585 586 @Override 587 public void attachEngine(IWallpaperEngine engine) { 588 synchronized (mLock) { 589 mEngine = engine; 590 if (mDimensionsChanged) { 591 try { 592 mEngine.setDesiredSize(mWallpaper.width, mWallpaper.height); 593 } catch (RemoteException e) { 594 Slog.w(TAG, "Failed to set wallpaper dimensions", e); 595 } 596 mDimensionsChanged = false; 597 } 598 if (mPaddingChanged) { 599 try { 600 mEngine.setDisplayPadding(mWallpaper.padding); 601 } catch (RemoteException e) { 602 Slog.w(TAG, "Failed to set wallpaper padding", e); 603 } 604 mPaddingChanged = false; 605 } 606 } 607 } 608 609 @Override 610 public void engineShown(IWallpaperEngine engine) { 611 synchronized (mLock) { 612 if (mReply != null) { 613 long ident = Binder.clearCallingIdentity(); 614 try { 615 mReply.sendResult(null); 616 } catch (RemoteException e) { 617 Binder.restoreCallingIdentity(ident); 618 } 619 mReply = null; 620 } 621 } 622 } 623 624 @Override 625 public ParcelFileDescriptor setWallpaper(String name) { 626 synchronized (mLock) { 627 if (mWallpaper.connection == this) { 628 return updateWallpaperBitmapLocked(name, mWallpaper, null); 629 } 630 return null; 631 } 632 } 633 } 634 635 class MyPackageMonitor extends PackageMonitor { 636 @Override 637 public void onPackageUpdateFinished(String packageName, int uid) { 638 synchronized (mLock) { 639 if (mCurrentUserId != getChangingUserId()) { 640 return; 641 } 642 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 643 if (wallpaper != null) { 644 if (wallpaper.wallpaperComponent != null 645 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { 646 wallpaper.wallpaperUpdating = false; 647 ComponentName comp = wallpaper.wallpaperComponent; 648 clearWallpaperComponentLocked(wallpaper); 649 if (!bindWallpaperComponentLocked(comp, false, false, 650 wallpaper, null)) { 651 Slog.w(TAG, "Wallpaper no longer available; reverting to default"); 652 clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null); 653 } 654 } 655 } 656 } 657 } 658 659 @Override 660 public void onPackageModified(String packageName) { 661 synchronized (mLock) { 662 if (mCurrentUserId != getChangingUserId()) { 663 return; 664 } 665 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 666 if (wallpaper != null) { 667 if (wallpaper.wallpaperComponent == null 668 || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { 669 return; 670 } 671 doPackagesChangedLocked(true, wallpaper); 672 } 673 } 674 } 675 676 @Override 677 public void onPackageUpdateStarted(String packageName, int uid) { 678 synchronized (mLock) { 679 if (mCurrentUserId != getChangingUserId()) { 680 return; 681 } 682 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 683 if (wallpaper != null) { 684 if (wallpaper.wallpaperComponent != null 685 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { 686 wallpaper.wallpaperUpdating = true; 687 } 688 } 689 } 690 } 691 692 @Override 693 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { 694 synchronized (mLock) { 695 boolean changed = false; 696 if (mCurrentUserId != getChangingUserId()) { 697 return false; 698 } 699 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 700 if (wallpaper != null) { 701 boolean res = doPackagesChangedLocked(doit, wallpaper); 702 changed |= res; 703 } 704 return changed; 705 } 706 } 707 708 @Override 709 public void onSomePackagesChanged() { 710 synchronized (mLock) { 711 if (mCurrentUserId != getChangingUserId()) { 712 return; 713 } 714 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 715 if (wallpaper != null) { 716 doPackagesChangedLocked(true, wallpaper); 717 } 718 } 719 } 720 721 boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) { 722 boolean changed = false; 723 if (wallpaper.wallpaperComponent != null) { 724 int change = isPackageDisappearing(wallpaper.wallpaperComponent 725 .getPackageName()); 726 if (change == PACKAGE_PERMANENT_CHANGE 727 || change == PACKAGE_TEMPORARY_CHANGE) { 728 changed = true; 729 if (doit) { 730 Slog.w(TAG, "Wallpaper uninstalled, removing: " 731 + wallpaper.wallpaperComponent); 732 clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null); 733 } 734 } 735 } 736 if (wallpaper.nextWallpaperComponent != null) { 737 int change = isPackageDisappearing(wallpaper.nextWallpaperComponent 738 .getPackageName()); 739 if (change == PACKAGE_PERMANENT_CHANGE 740 || change == PACKAGE_TEMPORARY_CHANGE) { 741 wallpaper.nextWallpaperComponent = null; 742 } 743 } 744 if (wallpaper.wallpaperComponent != null 745 && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) { 746 try { 747 mContext.getPackageManager().getServiceInfo( 748 wallpaper.wallpaperComponent, 0); 749 } catch (NameNotFoundException e) { 750 Slog.w(TAG, "Wallpaper component gone, removing: " 751 + wallpaper.wallpaperComponent); 752 clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null); 753 } 754 } 755 if (wallpaper.nextWallpaperComponent != null 756 && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) { 757 try { 758 mContext.getPackageManager().getServiceInfo( 759 wallpaper.nextWallpaperComponent, 0); 760 } catch (NameNotFoundException e) { 761 wallpaper.nextWallpaperComponent = null; 762 } 763 } 764 return changed; 765 } 766 } 767 768 public WallpaperManagerService(Context context) { 769 if (DEBUG) Slog.v(TAG, "WallpaperService startup"); 770 mContext = context; 771 mImageWallpaper = ComponentName.unflattenFromString( 772 context.getResources().getString(R.string.image_wallpaper_component)); 773 mIWindowManager = IWindowManager.Stub.asInterface( 774 ServiceManager.getService(Context.WINDOW_SERVICE)); 775 mIPackageManager = AppGlobals.getPackageManager(); 776 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); 777 mMonitor = new MyPackageMonitor(); 778 mMonitor.register(context, null, UserHandle.ALL, true); 779 getWallpaperDir(UserHandle.USER_SYSTEM).mkdirs(); 780 loadSettingsLocked(UserHandle.USER_SYSTEM); 781 } 782 783 private static File getWallpaperDir(int userId) { 784 return Environment.getUserSystemDirectory(userId); 785 } 786 787 @Override 788 protected void finalize() throws Throwable { 789 super.finalize(); 790 for (int i = 0; i < mWallpaperMap.size(); i++) { 791 WallpaperData wallpaper = mWallpaperMap.valueAt(i); 792 wallpaper.wallpaperObserver.stopWatching(); 793 } 794 } 795 796 public void systemRunning() { 797 if (DEBUG) Slog.v(TAG, "systemReady"); 798 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM); 799 // If we think we're going to be using the system image wallpaper imagery, make 800 // sure we have something to render 801 if (mImageWallpaper.equals(wallpaper.nextWallpaperComponent)) { 802 // No crop file? Make sure we've finished the processing sequence if necessary 803 if (!wallpaper.cropExists()) { 804 if (DEBUG) { 805 Slog.i(TAG, "No crop; regenerating from source"); 806 } 807 generateCrop(wallpaper); 808 } 809 // Still nothing? Fall back to default. 810 if (!wallpaper.cropExists()) { 811 if (DEBUG) { 812 Slog.i(TAG, "Unable to regenerate crop; resetting"); 813 } 814 clearWallpaperLocked(false, FLAG_SYSTEM, UserHandle.USER_SYSTEM, null); 815 } 816 } else { 817 if (DEBUG) { 818 Slog.i(TAG, "Nondefault wallpaper component; gracefully ignoring"); 819 } 820 } 821 switchWallpaper(wallpaper, null); 822 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); 823 wallpaper.wallpaperObserver.startWatching(); 824 825 IntentFilter userFilter = new IntentFilter(); 826 userFilter.addAction(Intent.ACTION_USER_REMOVED); 827 userFilter.addAction(Intent.ACTION_USER_STOPPING); 828 mContext.registerReceiver(new BroadcastReceiver() { 829 @Override 830 public void onReceive(Context context, Intent intent) { 831 String action = intent.getAction(); 832 if (Intent.ACTION_USER_REMOVED.equals(action)) { 833 onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 834 UserHandle.USER_NULL)); 835 } 836 // TODO: Race condition causing problems when cleaning up on stopping a user. 837 // Comment this out for now. 838 // else if (Intent.ACTION_USER_STOPPING.equals(action)) { 839 // onStoppingUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 840 // UserHandle.USER_NULL)); 841 // } 842 } 843 }, userFilter); 844 845 try { 846 ActivityManagerNative.getDefault().registerUserSwitchObserver( 847 new IUserSwitchObserver.Stub() { 848 @Override 849 public void onUserSwitching(int newUserId, IRemoteCallback reply) { 850 switchUser(newUserId, reply); 851 } 852 853 @Override 854 public void onUserSwitchComplete(int newUserId) throws RemoteException { 855 } 856 857 @Override 858 public void onForegroundProfileSwitch(int newProfileId) { 859 // Ignore. 860 } 861 }); 862 } catch (RemoteException e) { 863 // TODO Auto-generated catch block 864 e.printStackTrace(); 865 } 866 } 867 868 /** Called by SystemBackupAgent */ 869 public String getName() { 870 // Verify caller is the system 871 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { 872 throw new RuntimeException("getName() can only be called from the system process"); 873 } 874 synchronized (mLock) { 875 return mWallpaperMap.get(0).name; 876 } 877 } 878 879 void stopObserver(WallpaperData wallpaper) { 880 if (wallpaper != null) { 881 if (wallpaper.wallpaperObserver != null) { 882 wallpaper.wallpaperObserver.stopWatching(); 883 wallpaper.wallpaperObserver = null; 884 } 885 } 886 } 887 888 void stopObserversLocked(int userId) { 889 stopObserver(mWallpaperMap.get(userId)); 890 stopObserver(mLockWallpaperMap.get(userId)); 891 mWallpaperMap.remove(userId); 892 mLockWallpaperMap.remove(userId); 893 } 894 895 void onRemoveUser(int userId) { 896 if (userId < 1) return; 897 898 final File wallpaperDir = getWallpaperDir(userId); 899 synchronized (mLock) { 900 stopObserversLocked(userId); 901 for (String filename : sPerUserFiles) { 902 new File(wallpaperDir, filename).delete(); 903 } 904 } 905 } 906 907 void switchUser(int userId, IRemoteCallback reply) { 908 synchronized (mLock) { 909 mCurrentUserId = userId; 910 WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM); 911 // Not started watching yet, in case wallpaper data was loaded for other reasons. 912 if (wallpaper.wallpaperObserver == null) { 913 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); 914 wallpaper.wallpaperObserver.startWatching(); 915 } 916 switchWallpaper(wallpaper, reply); 917 } 918 } 919 920 void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) { 921 synchronized (mLock) { 922 RuntimeException e = null; 923 try { 924 ComponentName cname = wallpaper.wallpaperComponent != null ? 925 wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent; 926 if (bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) { 927 return; 928 } 929 } catch (RuntimeException e1) { 930 e = e1; 931 } 932 Slog.w(TAG, "Failure starting previous wallpaper", e); 933 clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, reply); 934 } 935 } 936 937 @Override 938 public void clearWallpaper(String callingPackage, int which, int userId) { 939 if (DEBUG) Slog.v(TAG, "clearWallpaper"); 940 checkPermission(android.Manifest.permission.SET_WALLPAPER); 941 if (!isWallpaperSupported(callingPackage) || !isWallpaperSettingAllowed(callingPackage)) { 942 return; 943 } 944 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), 945 Binder.getCallingUid(), userId, false, true, "clearWallpaper", null); 946 947 synchronized (mLock) { 948 clearWallpaperLocked(false, which, userId, null); 949 } 950 } 951 952 void clearWallpaperLocked(boolean defaultFailed, int which, int userId, IRemoteCallback reply) { 953 if (which != FLAG_SYSTEM && which != FLAG_LOCK) { 954 throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read"); 955 } 956 957 WallpaperData wallpaper = null; 958 if (which == FLAG_LOCK) { 959 wallpaper = mLockWallpaperMap.get(userId); 960 if (wallpaper == null) { 961 // It's already gone; we're done. 962 if (DEBUG) { 963 Slog.i(TAG, "Lock wallpaper already cleared"); 964 } 965 return; 966 } 967 } else { 968 wallpaper = mWallpaperMap.get(userId); 969 if (wallpaper == null) { 970 // Might need to bring it in the first time to establish our rewrite 971 loadSettingsLocked(userId); 972 wallpaper = mWallpaperMap.get(userId); 973 } 974 } 975 if (wallpaper == null) { 976 return; 977 } 978 979 final long ident = Binder.clearCallingIdentity(); 980 try { 981 if (wallpaper.wallpaperFile.exists()) { 982 wallpaper.wallpaperFile.delete(); 983 wallpaper.cropFile.delete(); 984 if (which == FLAG_LOCK) { 985 mLockWallpaperMap.remove(userId); 986 final IWallpaperManagerCallback cb = mKeyguardListener; 987 if (cb != null) { 988 if (DEBUG) { 989 Slog.i(TAG, "Notifying keyguard of lock wallpaper clear"); 990 } 991 try { 992 cb.onWallpaperChanged(); 993 } catch (RemoteException e) { 994 // Oh well it went away; no big deal 995 } 996 } 997 saveSettingsLocked(userId); 998 return; 999 } 1000 } 1001 1002 RuntimeException e = null; 1003 try { 1004 wallpaper.imageWallpaperPending = false; 1005 if (userId != mCurrentUserId) return; 1006 if (bindWallpaperComponentLocked(defaultFailed 1007 ? mImageWallpaper 1008 : null, true, false, wallpaper, reply)) { 1009 return; 1010 } 1011 } catch (IllegalArgumentException e1) { 1012 e = e1; 1013 } 1014 1015 // This can happen if the default wallpaper component doesn't 1016 // exist. This should be a system configuration problem, but 1017 // let's not let it crash the system and just live with no 1018 // wallpaper. 1019 Slog.e(TAG, "Default wallpaper component not found!", e); 1020 clearWallpaperComponentLocked(wallpaper); 1021 if (reply != null) { 1022 try { 1023 reply.sendResult(null); 1024 } catch (RemoteException e1) { 1025 } 1026 } 1027 } finally { 1028 Binder.restoreCallingIdentity(ident); 1029 } 1030 } 1031 1032 public boolean hasNamedWallpaper(String name) { 1033 synchronized (mLock) { 1034 List<UserInfo> users; 1035 long ident = Binder.clearCallingIdentity(); 1036 try { 1037 users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers(); 1038 } finally { 1039 Binder.restoreCallingIdentity(ident); 1040 } 1041 for (UserInfo user: users) { 1042 // ignore managed profiles 1043 if (user.isManagedProfile()) { 1044 continue; 1045 } 1046 WallpaperData wd = mWallpaperMap.get(user.id); 1047 if (wd == null) { 1048 // User hasn't started yet, so load her settings to peek at the wallpaper 1049 loadSettingsLocked(user.id); 1050 wd = mWallpaperMap.get(user.id); 1051 } 1052 if (wd != null && name.equals(wd.name)) { 1053 return true; 1054 } 1055 } 1056 } 1057 return false; 1058 } 1059 1060 private Point getDefaultDisplaySize() { 1061 Point p = new Point(); 1062 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); 1063 Display d = wm.getDefaultDisplay(); 1064 d.getRealSize(p); 1065 return p; 1066 } 1067 1068 public void setDimensionHints(int width, int height, String callingPackage) 1069 throws RemoteException { 1070 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); 1071 if (!isWallpaperSupported(callingPackage)) { 1072 return; 1073 } 1074 synchronized (mLock) { 1075 int userId = UserHandle.getCallingUserId(); 1076 WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM); 1077 if (width <= 0 || height <= 0) { 1078 throw new IllegalArgumentException("width and height must be > 0"); 1079 } 1080 // Make sure it is at least as large as the display. 1081 Point displaySize = getDefaultDisplaySize(); 1082 width = Math.max(width, displaySize.x); 1083 height = Math.max(height, displaySize.y); 1084 1085 if (width != wallpaper.width || height != wallpaper.height) { 1086 wallpaper.width = width; 1087 wallpaper.height = height; 1088 saveSettingsLocked(userId); 1089 if (mCurrentUserId != userId) return; // Don't change the properties now 1090 if (wallpaper.connection != null) { 1091 if (wallpaper.connection.mEngine != null) { 1092 try { 1093 wallpaper.connection.mEngine.setDesiredSize( 1094 width, height); 1095 } catch (RemoteException e) { 1096 } 1097 notifyCallbacksLocked(wallpaper); 1098 } else if (wallpaper.connection.mService != null) { 1099 // We've attached to the service but the engine hasn't attached back to us 1100 // yet. This means it will be created with the previous dimensions, so we 1101 // need to update it to the new dimensions once it attaches. 1102 wallpaper.connection.mDimensionsChanged = true; 1103 } 1104 } 1105 } 1106 } 1107 } 1108 1109 public int getWidthHint() throws RemoteException { 1110 synchronized (mLock) { 1111 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); 1112 if (wallpaper != null) { 1113 return wallpaper.width; 1114 } else { 1115 return 0; 1116 } 1117 } 1118 } 1119 1120 public int getHeightHint() throws RemoteException { 1121 synchronized (mLock) { 1122 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); 1123 if (wallpaper != null) { 1124 return wallpaper.height; 1125 } else { 1126 return 0; 1127 } 1128 } 1129 } 1130 1131 public void setDisplayPadding(Rect padding, String callingPackage) { 1132 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); 1133 if (!isWallpaperSupported(callingPackage)) { 1134 return; 1135 } 1136 synchronized (mLock) { 1137 int userId = UserHandle.getCallingUserId(); 1138 WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM); 1139 if (padding.left < 0 || padding.top < 0 || padding.right < 0 || padding.bottom < 0) { 1140 throw new IllegalArgumentException("padding must be positive: " + padding); 1141 } 1142 1143 if (!padding.equals(wallpaper.padding)) { 1144 wallpaper.padding.set(padding); 1145 saveSettingsLocked(userId); 1146 if (mCurrentUserId != userId) return; // Don't change the properties now 1147 if (wallpaper.connection != null) { 1148 if (wallpaper.connection.mEngine != null) { 1149 try { 1150 wallpaper.connection.mEngine.setDisplayPadding(padding); 1151 } catch (RemoteException e) { 1152 } 1153 notifyCallbacksLocked(wallpaper); 1154 } else if (wallpaper.connection.mService != null) { 1155 // We've attached to the service but the engine hasn't attached back to us 1156 // yet. This means it will be created with the previous dimensions, so we 1157 // need to update it to the new dimensions once it attaches. 1158 wallpaper.connection.mPaddingChanged = true; 1159 } 1160 } 1161 } 1162 } 1163 } 1164 1165 @Override 1166 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, final int which, 1167 Bundle outParams, int wallpaperUserId) { 1168 wallpaperUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), 1169 Binder.getCallingUid(), wallpaperUserId, false, true, "getWallpaper", null); 1170 1171 if (which != FLAG_SYSTEM && which != FLAG_LOCK) { 1172 throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read"); 1173 } 1174 1175 synchronized (mLock) { 1176 final SparseArray<WallpaperData> whichSet = 1177 (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap; 1178 WallpaperData wallpaper = whichSet.get(wallpaperUserId); 1179 if (wallpaper == null) { 1180 // common case, this is the first lookup post-boot of the system or 1181 // unified lock, so we bring up the saved state lazily now and recheck. 1182 loadSettingsLocked(wallpaperUserId); 1183 wallpaper = whichSet.get(wallpaperUserId); 1184 if (wallpaper == null) { 1185 return null; 1186 } 1187 } 1188 try { 1189 if (outParams != null) { 1190 outParams.putInt("width", wallpaper.width); 1191 outParams.putInt("height", wallpaper.height); 1192 } 1193 if (cb != null) { 1194 wallpaper.callbacks.register(cb); 1195 } 1196 if (!wallpaper.cropFile.exists()) { 1197 return null; 1198 } 1199 return ParcelFileDescriptor.open(wallpaper.cropFile, MODE_READ_ONLY); 1200 } catch (FileNotFoundException e) { 1201 /* Shouldn't happen as we check to see if the file exists */ 1202 Slog.w(TAG, "Error getting wallpaper", e); 1203 } 1204 return null; 1205 } 1206 } 1207 1208 @Override 1209 public WallpaperInfo getWallpaperInfo() { 1210 int userId = UserHandle.getCallingUserId(); 1211 synchronized (mLock) { 1212 WallpaperData wallpaper = mWallpaperMap.get(userId); 1213 if (wallpaper != null && wallpaper.connection != null) { 1214 return wallpaper.connection.mInfo; 1215 } 1216 return null; 1217 } 1218 } 1219 1220 @Override 1221 public int getWallpaperIdForUser(int which, int userId) { 1222 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), 1223 Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null); 1224 1225 if (which != FLAG_SYSTEM && which != FLAG_LOCK) { 1226 throw new IllegalArgumentException("Must specify exactly one kind of wallpaper"); 1227 } 1228 1229 final SparseArray<WallpaperData> map = 1230 (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap; 1231 synchronized (mLock) { 1232 WallpaperData wallpaper = map.get(userId); 1233 if (wallpaper != null) { 1234 return wallpaper.wallpaperId; 1235 } 1236 } 1237 return -1; 1238 } 1239 1240 @Override 1241 public boolean setLockWallpaperCallback(IWallpaperManagerCallback cb) { 1242 checkPermission(android.Manifest.permission.INTERNAL_SYSTEM_WINDOW); 1243 synchronized (mLock) { 1244 mKeyguardListener = cb; 1245 } 1246 return true; 1247 } 1248 1249 @Override 1250 public ParcelFileDescriptor setWallpaper(String name, String callingPackage, 1251 Rect cropHint, Bundle extras, int which, IWallpaperManagerCallback completion) { 1252 checkPermission(android.Manifest.permission.SET_WALLPAPER); 1253 1254 if ((which & (FLAG_LOCK|FLAG_SYSTEM)) == 0) { 1255 Slog.e(TAG, "Must specify a valid wallpaper category to set"); 1256 return null; 1257 } 1258 1259 if (!isWallpaperSupported(callingPackage) || !isWallpaperSettingAllowed(callingPackage)) { 1260 return null; 1261 } 1262 1263 // "null" means the no-op crop, preserving the full input image 1264 if (cropHint == null) { 1265 cropHint = new Rect(0, 0, 0, 0); 1266 } else { 1267 if (cropHint.isEmpty() 1268 || cropHint.left < 0 1269 || cropHint.top < 0) { 1270 return null; 1271 } 1272 } 1273 1274 final int userId = UserHandle.getCallingUserId(); 1275 1276 synchronized (mLock) { 1277 if (DEBUG) Slog.v(TAG, "setWallpaper which=0x" + Integer.toHexString(which)); 1278 WallpaperData wallpaper; 1279 1280 wallpaper = getWallpaperSafeLocked(userId, which); 1281 final long ident = Binder.clearCallingIdentity(); 1282 try { 1283 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper, extras); 1284 if (pfd != null) { 1285 wallpaper.imageWallpaperPending = true; 1286 wallpaper.whichPending = which; 1287 wallpaper.setComplete = completion; 1288 wallpaper.cropHint.set(cropHint); 1289 } 1290 return pfd; 1291 } finally { 1292 Binder.restoreCallingIdentity(ident); 1293 } 1294 } 1295 } 1296 1297 ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper, 1298 Bundle extras) { 1299 if (name == null) name = ""; 1300 try { 1301 File dir = getWallpaperDir(wallpaper.userId); 1302 if (!dir.exists()) { 1303 dir.mkdir(); 1304 FileUtils.setPermissions( 1305 dir.getPath(), 1306 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, 1307 -1, -1); 1308 } 1309 ParcelFileDescriptor fd = ParcelFileDescriptor.open(wallpaper.wallpaperFile, 1310 MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE); 1311 if (!SELinux.restorecon(wallpaper.wallpaperFile)) { 1312 return null; 1313 } 1314 wallpaper.name = name; 1315 wallpaper.wallpaperId = makeWallpaperIdLocked(); 1316 if (extras != null) { 1317 extras.putInt(WallpaperManager.EXTRA_NEW_WALLPAPER_ID, wallpaper.wallpaperId); 1318 } 1319 if (DEBUG) { 1320 Slog.v(TAG, "updateWallpaperBitmapLocked() : id=" + wallpaper.wallpaperId 1321 + " name=" + name + " file=" + wallpaper.wallpaperFile.getName()); 1322 } 1323 return fd; 1324 } catch (FileNotFoundException e) { 1325 Slog.w(TAG, "Error setting wallpaper", e); 1326 } 1327 return null; 1328 } 1329 1330 @Override 1331 public void setWallpaperComponentChecked(ComponentName name, String callingPackage) { 1332 if (isWallpaperSupported(callingPackage) && isWallpaperSettingAllowed(callingPackage)) { 1333 setWallpaperComponent(name); 1334 } 1335 } 1336 1337 // ToDo: Remove this version of the function 1338 @Override 1339 public void setWallpaperComponent(ComponentName name) { 1340 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT); 1341 synchronized (mLock) { 1342 if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name); 1343 int userId = UserHandle.getCallingUserId(); 1344 WallpaperData wallpaper = mWallpaperMap.get(userId); 1345 if (wallpaper == null) { 1346 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); 1347 } 1348 final long ident = Binder.clearCallingIdentity(); 1349 try { 1350 wallpaper.imageWallpaperPending = false; 1351 if (bindWallpaperComponentLocked(name, false, true, wallpaper, null)) { 1352 wallpaper.wallpaperId = makeWallpaperIdLocked(); 1353 notifyCallbacksLocked(wallpaper); 1354 } 1355 } finally { 1356 Binder.restoreCallingIdentity(ident); 1357 } 1358 } 1359 } 1360 1361 boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force, 1362 boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) { 1363 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName); 1364 // Has the component changed? 1365 if (!force) { 1366 if (wallpaper.connection != null) { 1367 if (wallpaper.wallpaperComponent == null) { 1368 if (componentName == null) { 1369 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default"); 1370 // Still using default wallpaper. 1371 return true; 1372 } 1373 } else if (wallpaper.wallpaperComponent.equals(componentName)) { 1374 // Changing to same wallpaper. 1375 if (DEBUG) Slog.v(TAG, "same wallpaper"); 1376 return true; 1377 } 1378 } 1379 } 1380 1381 try { 1382 if (componentName == null) { 1383 componentName = WallpaperManager.getDefaultWallpaperComponent(mContext); 1384 if (componentName == null) { 1385 // Fall back to static image wallpaper 1386 componentName = mImageWallpaper; 1387 //clearWallpaperComponentLocked(); 1388 //return; 1389 if (DEBUG) Slog.v(TAG, "Using image wallpaper"); 1390 } 1391 } 1392 int serviceUserId = wallpaper.userId; 1393 ServiceInfo si = mIPackageManager.getServiceInfo(componentName, 1394 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId); 1395 if (si == null) { 1396 // The wallpaper component we're trying to use doesn't exist 1397 Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable"); 1398 return false; 1399 } 1400 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) { 1401 String msg = "Selected service does not require " 1402 + android.Manifest.permission.BIND_WALLPAPER 1403 + ": " + componentName; 1404 if (fromUser) { 1405 throw new SecurityException(msg); 1406 } 1407 Slog.w(TAG, msg); 1408 return false; 1409 } 1410 1411 WallpaperInfo wi = null; 1412 1413 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); 1414 if (componentName != null && !componentName.equals(mImageWallpaper)) { 1415 // Make sure the selected service is actually a wallpaper service. 1416 List<ResolveInfo> ris = 1417 mIPackageManager.queryIntentServices(intent, 1418 intent.resolveTypeIfNeeded(mContext.getContentResolver()), 1419 PackageManager.GET_META_DATA, serviceUserId).getList(); 1420 for (int i=0; i<ris.size(); i++) { 1421 ServiceInfo rsi = ris.get(i).serviceInfo; 1422 if (rsi.name.equals(si.name) && 1423 rsi.packageName.equals(si.packageName)) { 1424 try { 1425 wi = new WallpaperInfo(mContext, ris.get(i)); 1426 } catch (XmlPullParserException e) { 1427 if (fromUser) { 1428 throw new IllegalArgumentException(e); 1429 } 1430 Slog.w(TAG, e); 1431 return false; 1432 } catch (IOException e) { 1433 if (fromUser) { 1434 throw new IllegalArgumentException(e); 1435 } 1436 Slog.w(TAG, e); 1437 return false; 1438 } 1439 break; 1440 } 1441 } 1442 if (wi == null) { 1443 String msg = "Selected service is not a wallpaper: " 1444 + componentName; 1445 if (fromUser) { 1446 throw new SecurityException(msg); 1447 } 1448 Slog.w(TAG, msg); 1449 return false; 1450 } 1451 } 1452 1453 // Bind the service! 1454 if (DEBUG) Slog.v(TAG, "Binding to:" + componentName); 1455 WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper); 1456 intent.setComponent(componentName); 1457 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, 1458 com.android.internal.R.string.wallpaper_binding_label); 1459 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser( 1460 mContext, 0, 1461 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), 1462 mContext.getText(com.android.internal.R.string.chooser_wallpaper)), 1463 0, null, new UserHandle(serviceUserId))); 1464 if (!mContext.bindServiceAsUser(intent, newConn, 1465 Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI 1466 | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE, 1467 new UserHandle(serviceUserId))) { 1468 String msg = "Unable to bind service: " 1469 + componentName; 1470 if (fromUser) { 1471 throw new IllegalArgumentException(msg); 1472 } 1473 Slog.w(TAG, msg); 1474 return false; 1475 } 1476 if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) { 1477 detachWallpaperLocked(mLastWallpaper); 1478 } 1479 wallpaper.wallpaperComponent = componentName; 1480 wallpaper.connection = newConn; 1481 newConn.mReply = reply; 1482 try { 1483 if (wallpaper.userId == mCurrentUserId) { 1484 if (DEBUG) 1485 Slog.v(TAG, "Adding window token: " + newConn.mToken); 1486 mIWindowManager.addWindowToken(newConn.mToken, 1487 WindowManager.LayoutParams.TYPE_WALLPAPER); 1488 mLastWallpaper = wallpaper; 1489 } 1490 } catch (RemoteException e) { 1491 } 1492 } catch (RemoteException e) { 1493 String msg = "Remote exception for " + componentName + "\n" + e; 1494 if (fromUser) { 1495 throw new IllegalArgumentException(msg); 1496 } 1497 Slog.w(TAG, msg); 1498 return false; 1499 } 1500 return true; 1501 } 1502 1503 void detachWallpaperLocked(WallpaperData wallpaper) { 1504 if (wallpaper.connection != null) { 1505 if (wallpaper.connection.mReply != null) { 1506 try { 1507 wallpaper.connection.mReply.sendResult(null); 1508 } catch (RemoteException e) { 1509 } 1510 wallpaper.connection.mReply = null; 1511 } 1512 if (wallpaper.connection.mEngine != null) { 1513 try { 1514 wallpaper.connection.mEngine.destroy(); 1515 } catch (RemoteException e) { 1516 } 1517 } 1518 mContext.unbindService(wallpaper.connection); 1519 try { 1520 if (DEBUG) 1521 Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken); 1522 mIWindowManager.removeWindowToken(wallpaper.connection.mToken); 1523 } catch (RemoteException e) { 1524 } 1525 wallpaper.connection.mService = null; 1526 wallpaper.connection.mEngine = null; 1527 wallpaper.connection = null; 1528 } 1529 } 1530 1531 void clearWallpaperComponentLocked(WallpaperData wallpaper) { 1532 wallpaper.wallpaperComponent = null; 1533 detachWallpaperLocked(wallpaper); 1534 } 1535 1536 void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) { 1537 try { 1538 conn.mService.attach(conn, conn.mToken, 1539 WindowManager.LayoutParams.TYPE_WALLPAPER, false, 1540 wallpaper.width, wallpaper.height, wallpaper.padding); 1541 } catch (RemoteException e) { 1542 Slog.w(TAG, "Failed attaching wallpaper; clearing", e); 1543 if (!wallpaper.wallpaperUpdating) { 1544 bindWallpaperComponentLocked(null, false, false, wallpaper, null); 1545 } 1546 } 1547 } 1548 1549 private void notifyCallbacksLocked(WallpaperData wallpaper) { 1550 final int n = wallpaper.callbacks.beginBroadcast(); 1551 for (int i = 0; i < n; i++) { 1552 try { 1553 wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged(); 1554 } catch (RemoteException e) { 1555 1556 // The RemoteCallbackList will take care of removing 1557 // the dead object for us. 1558 } 1559 } 1560 wallpaper.callbacks.finishBroadcast(); 1561 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED); 1562 mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId)); 1563 } 1564 1565 private void checkPermission(String permission) { 1566 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) { 1567 throw new SecurityException("Access denied to process: " + Binder.getCallingPid() 1568 + ", must have permission " + permission); 1569 } 1570 } 1571 1572 /** 1573 * Certain user types do not support wallpapers (e.g. managed profiles). The check is 1574 * implemented through through the OP_WRITE_WALLPAPER AppOp. 1575 */ 1576 public boolean isWallpaperSupported(String callingPackage) { 1577 return mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_WRITE_WALLPAPER, Binder.getCallingUid(), 1578 callingPackage) == AppOpsManager.MODE_ALLOWED; 1579 } 1580 1581 @Override 1582 public boolean isWallpaperSettingAllowed(String callingPackage) { 1583 final PackageManager pm = mContext.getPackageManager(); 1584 String[] uidPackages = pm.getPackagesForUid(Binder.getCallingUid()); 1585 boolean uidMatchPackage = Arrays.asList(uidPackages).contains(callingPackage); 1586 if (!uidMatchPackage) { 1587 return false; // callingPackage was faked. 1588 } 1589 1590 final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); 1591 if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) { 1592 return true; 1593 } 1594 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 1595 return !um.hasUserRestriction(UserManager.DISALLOW_SET_WALLPAPER); 1596 } 1597 1598 private static JournaledFile makeJournaledFile(int userId) { 1599 final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath(); 1600 return new JournaledFile(new File(base), new File(base + ".tmp")); 1601 } 1602 1603 private void saveSettingsLocked(int userId) { 1604 JournaledFile journal = makeJournaledFile(userId); 1605 FileOutputStream fstream = null; 1606 BufferedOutputStream stream = null; 1607 try { 1608 XmlSerializer out = new FastXmlSerializer(); 1609 fstream = new FileOutputStream(journal.chooseForWrite(), false); 1610 stream = new BufferedOutputStream(fstream); 1611 out.setOutput(stream, StandardCharsets.UTF_8.name()); 1612 out.startDocument(null, true); 1613 1614 WallpaperData wallpaper; 1615 1616 wallpaper = mWallpaperMap.get(userId); 1617 if (wallpaper != null) { 1618 writeWallpaperAttributes(out, "wp", wallpaper); 1619 } 1620 wallpaper = mLockWallpaperMap.get(userId); 1621 if (wallpaper != null) { 1622 writeWallpaperAttributes(out, "kwp", wallpaper); 1623 } 1624 1625 out.endDocument(); 1626 1627 stream.flush(); // also flushes fstream 1628 FileUtils.sync(fstream); 1629 stream.close(); // also closes fstream 1630 journal.commit(); 1631 } catch (IOException e) { 1632 IoUtils.closeQuietly(stream); 1633 journal.rollback(); 1634 } 1635 } 1636 1637 private void writeWallpaperAttributes(XmlSerializer out, String tag, WallpaperData wallpaper) 1638 throws IllegalArgumentException, IllegalStateException, IOException { 1639 out.startTag(null, tag); 1640 out.attribute(null, "id", Integer.toString(wallpaper.wallpaperId)); 1641 out.attribute(null, "width", Integer.toString(wallpaper.width)); 1642 out.attribute(null, "height", Integer.toString(wallpaper.height)); 1643 1644 out.attribute(null, "cropLeft", Integer.toString(wallpaper.cropHint.left)); 1645 out.attribute(null, "cropTop", Integer.toString(wallpaper.cropHint.top)); 1646 out.attribute(null, "cropRight", Integer.toString(wallpaper.cropHint.right)); 1647 out.attribute(null, "cropBottom", Integer.toString(wallpaper.cropHint.bottom)); 1648 1649 if (wallpaper.padding.left != 0) { 1650 out.attribute(null, "paddingLeft", Integer.toString(wallpaper.padding.left)); 1651 } 1652 if (wallpaper.padding.top != 0) { 1653 out.attribute(null, "paddingTop", Integer.toString(wallpaper.padding.top)); 1654 } 1655 if (wallpaper.padding.right != 0) { 1656 out.attribute(null, "paddingRight", Integer.toString(wallpaper.padding.right)); 1657 } 1658 if (wallpaper.padding.bottom != 0) { 1659 out.attribute(null, "paddingBottom", Integer.toString(wallpaper.padding.bottom)); 1660 } 1661 1662 out.attribute(null, "name", wallpaper.name); 1663 if (wallpaper.wallpaperComponent != null 1664 && !wallpaper.wallpaperComponent.equals(mImageWallpaper)) { 1665 out.attribute(null, "component", 1666 wallpaper.wallpaperComponent.flattenToShortString()); 1667 } 1668 out.endTag(null, tag); 1669 } 1670 1671 private void migrateFromOld() { 1672 File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY); 1673 File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY); 1674 if (oldWallpaper.exists()) { 1675 File newWallpaper = new File(getWallpaperDir(0), WALLPAPER); 1676 oldWallpaper.renameTo(newWallpaper); 1677 } 1678 if (oldInfo.exists()) { 1679 File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO); 1680 oldInfo.renameTo(newInfo); 1681 } 1682 } 1683 1684 private int getAttributeInt(XmlPullParser parser, String name, int defValue) { 1685 String value = parser.getAttributeValue(null, name); 1686 if (value == null) { 1687 return defValue; 1688 } 1689 return Integer.parseInt(value); 1690 } 1691 1692 /** 1693 * Sometimes it is expected the wallpaper map may not have a user's data. E.g. This could 1694 * happen during user switch. The async user switch observer may not have received 1695 * the event yet. We use this safe method when we don't care about this ordering and just 1696 * want to update the data. The data is going to be applied when the user switch observer 1697 * is eventually executed. 1698 */ 1699 private WallpaperData getWallpaperSafeLocked(int userId, int which) { 1700 // We're setting either just system (work with the system wallpaper), 1701 // both (also work with the system wallpaper), or just the lock 1702 // wallpaper (update against the existing lock wallpaper if any). 1703 // Combined or just-system operations use the 'system' WallpaperData 1704 // for this use; lock-only operations use the dedicated one. 1705 final SparseArray<WallpaperData> whichSet = 1706 (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap; 1707 WallpaperData wallpaper = whichSet.get(userId); 1708 if (wallpaper == null) { 1709 // common case, this is the first lookup post-boot of the system or 1710 // unified lock, so we bring up the saved state lazily now and recheck. 1711 loadSettingsLocked(userId); 1712 wallpaper = whichSet.get(userId); 1713 // if it's still null here, this is a lock-only operation and there is not 1714 // yet a lock-only wallpaper set for this user, so we need to establish 1715 // it now. 1716 if (wallpaper == null) { 1717 if (which == FLAG_LOCK) { 1718 wallpaper = new WallpaperData(userId, 1719 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); 1720 mLockWallpaperMap.put(userId, wallpaper); 1721 } else { 1722 // sanity fallback: we're in bad shape, but establishing a known 1723 // valid system+lock WallpaperData will keep us from dying. 1724 Slog.wtf(TAG, "Didn't find wallpaper in non-lock case!"); 1725 wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP); 1726 mWallpaperMap.put(userId, wallpaper); 1727 } 1728 } 1729 } 1730 return wallpaper; 1731 } 1732 1733 private void loadSettingsLocked(int userId) { 1734 if (DEBUG) Slog.v(TAG, "loadSettingsLocked"); 1735 1736 JournaledFile journal = makeJournaledFile(userId); 1737 FileInputStream stream = null; 1738 File file = journal.chooseForRead(); 1739 if (!file.exists()) { 1740 // This should only happen one time, when upgrading from a legacy system 1741 migrateFromOld(); 1742 } 1743 WallpaperData wallpaper = mWallpaperMap.get(userId); 1744 if (wallpaper == null) { 1745 wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP); 1746 mWallpaperMap.put(userId, wallpaper); 1747 if (!wallpaper.cropExists()) { 1748 generateCrop(wallpaper); 1749 } 1750 } 1751 boolean success = false; 1752 try { 1753 stream = new FileInputStream(file); 1754 XmlPullParser parser = Xml.newPullParser(); 1755 parser.setInput(stream, StandardCharsets.UTF_8.name()); 1756 1757 int type; 1758 do { 1759 type = parser.next(); 1760 if (type == XmlPullParser.START_TAG) { 1761 String tag = parser.getName(); 1762 if ("wp".equals(tag)) { 1763 // Common to system + lock wallpapers 1764 parseWallpaperAttributes(parser, wallpaper); 1765 1766 // A system wallpaper might also be a live wallpaper 1767 String comp = parser.getAttributeValue(null, "component"); 1768 wallpaper.nextWallpaperComponent = comp != null 1769 ? ComponentName.unflattenFromString(comp) 1770 : null; 1771 if (wallpaper.nextWallpaperComponent == null 1772 || "android".equals(wallpaper.nextWallpaperComponent 1773 .getPackageName())) { 1774 wallpaper.nextWallpaperComponent = mImageWallpaper; 1775 } 1776 1777 if (DEBUG) { 1778 Slog.v(TAG, "mWidth:" + wallpaper.width); 1779 Slog.v(TAG, "mHeight:" + wallpaper.height); 1780 Slog.v(TAG, "cropRect:" + wallpaper.cropHint); 1781 Slog.v(TAG, "mName:" + wallpaper.name); 1782 Slog.v(TAG, "mNextWallpaperComponent:" 1783 + wallpaper.nextWallpaperComponent); 1784 } 1785 } else if ("kwp".equals(tag)) { 1786 // keyguard-specific wallpaper for this user 1787 WallpaperData lockWallpaper = mLockWallpaperMap.get(userId); 1788 if (lockWallpaper == null) { 1789 lockWallpaper = new WallpaperData(userId, 1790 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); 1791 mLockWallpaperMap.put(userId, lockWallpaper); 1792 } 1793 parseWallpaperAttributes(parser, lockWallpaper); 1794 } 1795 } 1796 } while (type != XmlPullParser.END_DOCUMENT); 1797 success = true; 1798 } catch (FileNotFoundException e) { 1799 Slog.w(TAG, "no current wallpaper -- first boot?"); 1800 } catch (NullPointerException e) { 1801 Slog.w(TAG, "failed parsing " + file + " " + e); 1802 } catch (NumberFormatException e) { 1803 Slog.w(TAG, "failed parsing " + file + " " + e); 1804 } catch (XmlPullParserException e) { 1805 Slog.w(TAG, "failed parsing " + file + " " + e); 1806 } catch (IOException e) { 1807 Slog.w(TAG, "failed parsing " + file + " " + e); 1808 } catch (IndexOutOfBoundsException e) { 1809 Slog.w(TAG, "failed parsing " + file + " " + e); 1810 } 1811 IoUtils.closeQuietly(stream); 1812 1813 if (!success) { 1814 wallpaper.width = -1; 1815 wallpaper.height = -1; 1816 wallpaper.cropHint.set(0, 0, 0, 0); 1817 wallpaper.padding.set(0, 0, 0, 0); 1818 wallpaper.name = ""; 1819 } else { 1820 if (wallpaper.wallpaperId <= 0) { 1821 wallpaper.wallpaperId = makeWallpaperIdLocked(); 1822 if (DEBUG) { 1823 Slog.w(TAG, "Didn't set wallpaper id in loadSettingsLocked(" + userId 1824 + "); now " + wallpaper.wallpaperId); 1825 } 1826 } 1827 } 1828 1829 // We always want to have some reasonable width hint. 1830 int baseSize = getMaximumSizeDimension(); 1831 if (wallpaper.width < baseSize) { 1832 wallpaper.width = baseSize; 1833 } 1834 if (wallpaper.height < baseSize) { 1835 wallpaper.height = baseSize; 1836 } 1837 // and crop, if not previously specified 1838 if (wallpaper.cropHint.width() <= 0 1839 || wallpaper.cropHint.height() <= 0) { 1840 wallpaper.cropHint.set(0, 0, wallpaper.width, wallpaper.height); 1841 } 1842 } 1843 1844 private void parseWallpaperAttributes(XmlPullParser parser, WallpaperData wallpaper) { 1845 final String idString = parser.getAttributeValue(null, "id"); 1846 if (idString != null) { 1847 final int id = wallpaper.wallpaperId = Integer.parseInt(idString); 1848 if (id > mWallpaperId) { 1849 mWallpaperId = id; 1850 } 1851 } else { 1852 wallpaper.wallpaperId = makeWallpaperIdLocked(); 1853 } 1854 1855 wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width")); 1856 wallpaper.height = Integer.parseInt(parser 1857 .getAttributeValue(null, "height")); 1858 wallpaper.cropHint.left = getAttributeInt(parser, "cropLeft", 0); 1859 wallpaper.cropHint.top = getAttributeInt(parser, "cropTop", 0); 1860 wallpaper.cropHint.right = getAttributeInt(parser, "cropRight", 0); 1861 wallpaper.cropHint.bottom = getAttributeInt(parser, "cropBottom", 0); 1862 wallpaper.padding.left = getAttributeInt(parser, "paddingLeft", 0); 1863 wallpaper.padding.top = getAttributeInt(parser, "paddingTop", 0); 1864 wallpaper.padding.right = getAttributeInt(parser, "paddingRight", 0); 1865 wallpaper.padding.bottom = getAttributeInt(parser, "paddingBottom", 0); 1866 wallpaper.name = parser.getAttributeValue(null, "name"); 1867 } 1868 1869 private int getMaximumSizeDimension() { 1870 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); 1871 Display d = wm.getDefaultDisplay(); 1872 return d.getMaximumSizeDimension(); 1873 } 1874 1875 // Called by SystemBackupAgent after files are restored to disk. 1876 public void settingsRestored() { 1877 // Verify caller is the system 1878 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { 1879 throw new RuntimeException("settingsRestored() can only be called from the system process"); 1880 } 1881 // TODO: If necessary, make it work for secondary users as well. This currently assumes 1882 // restores only to the primary user 1883 if (DEBUG) Slog.v(TAG, "settingsRestored"); 1884 WallpaperData wallpaper = null; 1885 boolean success = false; 1886 synchronized (mLock) { 1887 loadSettingsLocked(UserHandle.USER_SYSTEM); 1888 wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM); 1889 wallpaper.wallpaperId = makeWallpaperIdLocked(); // always bump id at restore 1890 if (wallpaper.nextWallpaperComponent != null 1891 && !wallpaper.nextWallpaperComponent.equals(mImageWallpaper)) { 1892 if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false, 1893 wallpaper, null)) { 1894 // No such live wallpaper or other failure; fall back to the default 1895 // live wallpaper (since the profile being restored indicated that the 1896 // user had selected a live rather than static one). 1897 bindWallpaperComponentLocked(null, false, false, wallpaper, null); 1898 } 1899 success = true; 1900 } else { 1901 // If there's a wallpaper name, we use that. If that can't be loaded, then we 1902 // use the default. 1903 if ("".equals(wallpaper.name)) { 1904 if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty"); 1905 success = true; 1906 } else { 1907 if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource"); 1908 success = restoreNamedResourceLocked(wallpaper); 1909 } 1910 if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success 1911 + " id=" + wallpaper.wallpaperId); 1912 if (success) { 1913 generateCrop(wallpaper); // based on the new image + metadata 1914 bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, true, false, 1915 wallpaper, null); 1916 } 1917 } 1918 } 1919 1920 if (!success) { 1921 Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'"); 1922 wallpaper.name = ""; 1923 getWallpaperDir(UserHandle.USER_SYSTEM).delete(); 1924 } 1925 1926 synchronized (mLock) { 1927 saveSettingsLocked(UserHandle.USER_SYSTEM); 1928 } 1929 } 1930 1931 // Restore the named resource bitmap to both source + crop files 1932 boolean restoreNamedResourceLocked(WallpaperData wallpaper) { 1933 if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) { 1934 String resName = wallpaper.name.substring(4); 1935 1936 String pkg = null; 1937 int colon = resName.indexOf(':'); 1938 if (colon > 0) { 1939 pkg = resName.substring(0, colon); 1940 } 1941 1942 String ident = null; 1943 int slash = resName.lastIndexOf('/'); 1944 if (slash > 0) { 1945 ident = resName.substring(slash+1); 1946 } 1947 1948 String type = null; 1949 if (colon > 0 && slash > 0 && (slash-colon) > 1) { 1950 type = resName.substring(colon+1, slash); 1951 } 1952 1953 if (pkg != null && ident != null && type != null) { 1954 int resId = -1; 1955 InputStream res = null; 1956 FileOutputStream fos = null; 1957 FileOutputStream cos = null; 1958 try { 1959 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED); 1960 Resources r = c.getResources(); 1961 resId = r.getIdentifier(resName, null, null); 1962 if (resId == 0) { 1963 Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type 1964 + " ident=" + ident); 1965 return false; 1966 } 1967 1968 res = r.openRawResource(resId); 1969 if (wallpaper.wallpaperFile.exists()) { 1970 wallpaper.wallpaperFile.delete(); 1971 wallpaper.cropFile.delete(); 1972 } 1973 fos = new FileOutputStream(wallpaper.wallpaperFile); 1974 cos = new FileOutputStream(wallpaper.cropFile); 1975 1976 byte[] buffer = new byte[32768]; 1977 int amt; 1978 while ((amt=res.read(buffer)) > 0) { 1979 fos.write(buffer, 0, amt); 1980 cos.write(buffer, 0, amt); 1981 } 1982 // mWallpaperObserver will notice the close and send the change broadcast 1983 1984 Slog.v(TAG, "Restored wallpaper: " + resName); 1985 return true; 1986 } catch (NameNotFoundException e) { 1987 Slog.e(TAG, "Package name " + pkg + " not found"); 1988 } catch (Resources.NotFoundException e) { 1989 Slog.e(TAG, "Resource not found: " + resId); 1990 } catch (IOException e) { 1991 Slog.e(TAG, "IOException while restoring wallpaper ", e); 1992 } finally { 1993 IoUtils.closeQuietly(res); 1994 if (fos != null) { 1995 FileUtils.sync(fos); 1996 } 1997 if (cos != null) { 1998 FileUtils.sync(cos); 1999 } 2000 IoUtils.closeQuietly(fos); 2001 IoUtils.closeQuietly(cos); 2002 } 2003 } 2004 } 2005 return false; 2006 } 2007 2008 @Override 2009 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 2010 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 2011 != PackageManager.PERMISSION_GRANTED) { 2012 2013 pw.println("Permission Denial: can't dump wallpaper service from from pid=" 2014 + Binder.getCallingPid() 2015 + ", uid=" + Binder.getCallingUid()); 2016 return; 2017 } 2018 2019 synchronized (mLock) { 2020 pw.println("Current Wallpaper Service state:"); 2021 for (int i = 0; i < mWallpaperMap.size(); i++) { 2022 WallpaperData wallpaper = mWallpaperMap.valueAt(i); 2023 pw.print(" User "); pw.print(wallpaper.userId); 2024 pw.print(": id="); pw.println(wallpaper.wallpaperId); 2025 pw.print(" mWidth="); 2026 pw.print(wallpaper.width); 2027 pw.print(" mHeight="); 2028 pw.println(wallpaper.height); 2029 pw.print(" mCropHint="); pw.println(wallpaper.cropHint); 2030 pw.print(" mPadding="); pw.println(wallpaper.padding); 2031 pw.print(" mName="); pw.println(wallpaper.name); 2032 pw.print(" mWallpaperComponent="); pw.println(wallpaper.wallpaperComponent); 2033 if (wallpaper.connection != null) { 2034 WallpaperConnection conn = wallpaper.connection; 2035 pw.print(" Wallpaper connection "); 2036 pw.print(conn); 2037 pw.println(":"); 2038 if (conn.mInfo != null) { 2039 pw.print(" mInfo.component="); 2040 pw.println(conn.mInfo.getComponent()); 2041 } 2042 pw.print(" mToken="); 2043 pw.println(conn.mToken); 2044 pw.print(" mService="); 2045 pw.println(conn.mService); 2046 pw.print(" mEngine="); 2047 pw.println(conn.mEngine); 2048 pw.print(" mLastDiedTime="); 2049 pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis()); 2050 } 2051 } 2052 } 2053 } 2054} 2055