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