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