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