WallpaperManagerService.java revision 41297ff82c94d28fe19a6dd8f6214aee7c0af8e2
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 cropExists() { 445 return cropFile.exists(); 446 } 447 } 448 449 int makeWallpaperIdLocked() { 450 do { 451 ++mWallpaperId; 452 } while (mWallpaperId == 0); 453 return mWallpaperId; 454 } 455 456 class WallpaperConnection extends IWallpaperConnection.Stub 457 implements ServiceConnection { 458 final WallpaperInfo mInfo; 459 final Binder mToken = new Binder(); 460 IWallpaperService mService; 461 IWallpaperEngine mEngine; 462 WallpaperData mWallpaper; 463 IRemoteCallback mReply; 464 465 boolean mDimensionsChanged = false; 466 boolean mPaddingChanged = false; 467 468 public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) { 469 mInfo = info; 470 mWallpaper = wallpaper; 471 } 472 473 @Override 474 public void onServiceConnected(ComponentName name, IBinder service) { 475 synchronized (mLock) { 476 if (mWallpaper.connection == this) { 477 mService = IWallpaperService.Stub.asInterface(service); 478 attachServiceLocked(this, mWallpaper); 479 // XXX should probably do saveSettingsLocked() later 480 // when we have an engine, but I'm not sure about 481 // locking there and anyway we always need to be able to 482 // recover if there is something wrong. 483 saveSettingsLocked(mWallpaper.userId); 484 } 485 } 486 } 487 488 @Override 489 public void onServiceDisconnected(ComponentName name) { 490 synchronized (mLock) { 491 mService = null; 492 mEngine = null; 493 if (mWallpaper.connection == this) { 494 Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent); 495 if (!mWallpaper.wallpaperUpdating 496 && mWallpaper.userId == mCurrentUserId) { 497 // There is a race condition which causes 498 // {@link #mWallpaper.wallpaperUpdating} to be false even if it is 499 // currently updating since the broadcast notifying us is async. 500 // This race is overcome by the general rule that we only reset the 501 // wallpaper if its service was shut down twice 502 // during {@link #MIN_WALLPAPER_CRASH_TIME} millis. 503 if (mWallpaper.lastDiedTime != 0 504 && mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME 505 > SystemClock.uptimeMillis()) { 506 Slog.w(TAG, "Reverting to built-in wallpaper!"); 507 clearWallpaperLocked(true, FLAG_SET_SYSTEM, mWallpaper.userId, null); 508 } else { 509 mWallpaper.lastDiedTime = SystemClock.uptimeMillis(); 510 } 511 final String flattened = name.flattenToString(); 512 EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED, 513 flattened.substring(0, Math.min(flattened.length(), 514 MAX_WALLPAPER_COMPONENT_LOG_LENGTH))); 515 } 516 } 517 } 518 } 519 520 @Override 521 public void attachEngine(IWallpaperEngine engine) { 522 synchronized (mLock) { 523 mEngine = engine; 524 if (mDimensionsChanged) { 525 try { 526 mEngine.setDesiredSize(mWallpaper.width, mWallpaper.height); 527 } catch (RemoteException e) { 528 Slog.w(TAG, "Failed to set wallpaper dimensions", e); 529 } 530 mDimensionsChanged = false; 531 } 532 if (mPaddingChanged) { 533 try { 534 mEngine.setDisplayPadding(mWallpaper.padding); 535 } catch (RemoteException e) { 536 Slog.w(TAG, "Failed to set wallpaper padding", e); 537 } 538 mPaddingChanged = false; 539 } 540 } 541 } 542 543 @Override 544 public void engineShown(IWallpaperEngine engine) { 545 synchronized (mLock) { 546 if (mReply != null) { 547 long ident = Binder.clearCallingIdentity(); 548 try { 549 mReply.sendResult(null); 550 } catch (RemoteException e) { 551 Binder.restoreCallingIdentity(ident); 552 } 553 mReply = null; 554 } 555 } 556 } 557 558 @Override 559 public ParcelFileDescriptor setWallpaper(String name) { 560 synchronized (mLock) { 561 if (mWallpaper.connection == this) { 562 return updateWallpaperBitmapLocked(name, mWallpaper, null); 563 } 564 return null; 565 } 566 } 567 } 568 569 class MyPackageMonitor extends PackageMonitor { 570 @Override 571 public void onPackageUpdateFinished(String packageName, int uid) { 572 synchronized (mLock) { 573 if (mCurrentUserId != getChangingUserId()) { 574 return; 575 } 576 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 577 if (wallpaper != null) { 578 if (wallpaper.wallpaperComponent != null 579 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { 580 wallpaper.wallpaperUpdating = false; 581 ComponentName comp = wallpaper.wallpaperComponent; 582 clearWallpaperComponentLocked(wallpaper); 583 if (!bindWallpaperComponentLocked(comp, false, false, 584 wallpaper, null)) { 585 Slog.w(TAG, "Wallpaper no longer available; reverting to default"); 586 clearWallpaperLocked(false, FLAG_SET_SYSTEM, wallpaper.userId, null); 587 } 588 } 589 } 590 } 591 } 592 593 @Override 594 public void onPackageModified(String packageName) { 595 synchronized (mLock) { 596 if (mCurrentUserId != getChangingUserId()) { 597 return; 598 } 599 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 600 if (wallpaper != null) { 601 if (wallpaper.wallpaperComponent == null 602 || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { 603 return; 604 } 605 doPackagesChangedLocked(true, wallpaper); 606 } 607 } 608 } 609 610 @Override 611 public void onPackageUpdateStarted(String packageName, int uid) { 612 synchronized (mLock) { 613 if (mCurrentUserId != getChangingUserId()) { 614 return; 615 } 616 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 617 if (wallpaper != null) { 618 if (wallpaper.wallpaperComponent != null 619 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { 620 wallpaper.wallpaperUpdating = true; 621 } 622 } 623 } 624 } 625 626 @Override 627 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { 628 synchronized (mLock) { 629 boolean changed = false; 630 if (mCurrentUserId != getChangingUserId()) { 631 return false; 632 } 633 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 634 if (wallpaper != null) { 635 boolean res = doPackagesChangedLocked(doit, wallpaper); 636 changed |= res; 637 } 638 return changed; 639 } 640 } 641 642 @Override 643 public void onSomePackagesChanged() { 644 synchronized (mLock) { 645 if (mCurrentUserId != getChangingUserId()) { 646 return; 647 } 648 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 649 if (wallpaper != null) { 650 doPackagesChangedLocked(true, wallpaper); 651 } 652 } 653 } 654 655 boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) { 656 boolean changed = false; 657 if (wallpaper.wallpaperComponent != null) { 658 int change = isPackageDisappearing(wallpaper.wallpaperComponent 659 .getPackageName()); 660 if (change == PACKAGE_PERMANENT_CHANGE 661 || change == PACKAGE_TEMPORARY_CHANGE) { 662 changed = true; 663 if (doit) { 664 Slog.w(TAG, "Wallpaper uninstalled, removing: " 665 + wallpaper.wallpaperComponent); 666 clearWallpaperLocked(false, FLAG_SET_SYSTEM, wallpaper.userId, null); 667 } 668 } 669 } 670 if (wallpaper.nextWallpaperComponent != null) { 671 int change = isPackageDisappearing(wallpaper.nextWallpaperComponent 672 .getPackageName()); 673 if (change == PACKAGE_PERMANENT_CHANGE 674 || change == PACKAGE_TEMPORARY_CHANGE) { 675 wallpaper.nextWallpaperComponent = null; 676 } 677 } 678 if (wallpaper.wallpaperComponent != null 679 && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) { 680 try { 681 mContext.getPackageManager().getServiceInfo( 682 wallpaper.wallpaperComponent, 0); 683 } catch (NameNotFoundException e) { 684 Slog.w(TAG, "Wallpaper component gone, removing: " 685 + wallpaper.wallpaperComponent); 686 clearWallpaperLocked(false, FLAG_SET_SYSTEM, wallpaper.userId, null); 687 } 688 } 689 if (wallpaper.nextWallpaperComponent != null 690 && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) { 691 try { 692 mContext.getPackageManager().getServiceInfo( 693 wallpaper.nextWallpaperComponent, 0); 694 } catch (NameNotFoundException e) { 695 wallpaper.nextWallpaperComponent = null; 696 } 697 } 698 return changed; 699 } 700 } 701 702 public WallpaperManagerService(Context context) { 703 if (DEBUG) Slog.v(TAG, "WallpaperService startup"); 704 mContext = context; 705 mImageWallpaper = ComponentName.unflattenFromString( 706 context.getResources().getString(R.string.image_wallpaper_component)); 707 mIWindowManager = IWindowManager.Stub.asInterface( 708 ServiceManager.getService(Context.WINDOW_SERVICE)); 709 mIPackageManager = AppGlobals.getPackageManager(); 710 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); 711 mMonitor = new MyPackageMonitor(); 712 mMonitor.register(context, null, UserHandle.ALL, true); 713 getWallpaperDir(UserHandle.USER_SYSTEM).mkdirs(); 714 loadSettingsLocked(UserHandle.USER_SYSTEM); 715 } 716 717 private static File getWallpaperDir(int userId) { 718 return Environment.getUserSystemDirectory(userId); 719 } 720 721 @Override 722 protected void finalize() throws Throwable { 723 super.finalize(); 724 for (int i = 0; i < mWallpaperMap.size(); i++) { 725 WallpaperData wallpaper = mWallpaperMap.valueAt(i); 726 wallpaper.wallpaperObserver.stopWatching(); 727 } 728 } 729 730 public void systemRunning() { 731 if (DEBUG) Slog.v(TAG, "systemReady"); 732 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM); 733 // No crop file? Make sure we've finished the processing sequence if necessary 734 if (!wallpaper.cropExists()) { 735 generateCrop(wallpaper); 736 } 737 // Still nothing? Fall back to default. 738 if (!wallpaper.cropExists()) { 739 clearWallpaperLocked(false, FLAG_SET_SYSTEM, UserHandle.USER_SYSTEM, null); 740 } 741 switchWallpaper(wallpaper, null); 742 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); 743 wallpaper.wallpaperObserver.startWatching(); 744 745 IntentFilter userFilter = new IntentFilter(); 746 userFilter.addAction(Intent.ACTION_USER_REMOVED); 747 userFilter.addAction(Intent.ACTION_USER_STOPPING); 748 mContext.registerReceiver(new BroadcastReceiver() { 749 @Override 750 public void onReceive(Context context, Intent intent) { 751 String action = intent.getAction(); 752 if (Intent.ACTION_USER_REMOVED.equals(action)) { 753 onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 754 UserHandle.USER_NULL)); 755 } 756 // TODO: Race condition causing problems when cleaning up on stopping a user. 757 // Comment this out for now. 758 // else if (Intent.ACTION_USER_STOPPING.equals(action)) { 759 // onStoppingUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 760 // UserHandle.USER_NULL)); 761 // } 762 } 763 }, userFilter); 764 765 try { 766 ActivityManagerNative.getDefault().registerUserSwitchObserver( 767 new IUserSwitchObserver.Stub() { 768 @Override 769 public void onUserSwitching(int newUserId, IRemoteCallback reply) { 770 switchUser(newUserId, reply); 771 } 772 773 @Override 774 public void onUserSwitchComplete(int newUserId) throws RemoteException { 775 } 776 777 @Override 778 public void onForegroundProfileSwitch(int newProfileId) { 779 // Ignore. 780 } 781 }); 782 } catch (RemoteException e) { 783 // TODO Auto-generated catch block 784 e.printStackTrace(); 785 } 786 } 787 788 /** Called by SystemBackupAgent */ 789 public String getName() { 790 // Verify caller is the system 791 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { 792 throw new RuntimeException("getName() can only be called from the system process"); 793 } 794 synchronized (mLock) { 795 return mWallpaperMap.get(0).name; 796 } 797 } 798 799 void stopObserver(WallpaperData wallpaper) { 800 if (wallpaper != null) { 801 if (wallpaper.wallpaperObserver != null) { 802 wallpaper.wallpaperObserver.stopWatching(); 803 wallpaper.wallpaperObserver = null; 804 } 805 } 806 } 807 808 void stopObserversLocked(int userId) { 809 stopObserver(mWallpaperMap.get(userId)); 810 stopObserver(mLockWallpaperMap.get(userId)); 811 mWallpaperMap.remove(userId); 812 mLockWallpaperMap.remove(userId); 813 } 814 815 void onRemoveUser(int userId) { 816 if (userId < 1) return; 817 818 final File wallpaperDir = getWallpaperDir(userId); 819 synchronized (mLock) { 820 stopObserversLocked(userId); 821 for (String filename : sPerUserFiles) { 822 new File(wallpaperDir, filename).delete(); 823 } 824 } 825 } 826 827 void switchUser(int userId, IRemoteCallback reply) { 828 synchronized (mLock) { 829 mCurrentUserId = userId; 830 WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SET_SYSTEM); 831 // Not started watching yet, in case wallpaper data was loaded for other reasons. 832 if (wallpaper.wallpaperObserver == null) { 833 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); 834 wallpaper.wallpaperObserver.startWatching(); 835 } 836 switchWallpaper(wallpaper, reply); 837 } 838 } 839 840 void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) { 841 synchronized (mLock) { 842 RuntimeException e = null; 843 try { 844 ComponentName cname = wallpaper.wallpaperComponent != null ? 845 wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent; 846 if (bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) { 847 return; 848 } 849 } catch (RuntimeException e1) { 850 e = e1; 851 } 852 Slog.w(TAG, "Failure starting previous wallpaper", e); 853 clearWallpaperLocked(false, FLAG_SET_SYSTEM, wallpaper.userId, reply); 854 } 855 } 856 857 @Override 858 public void clearWallpaper(String callingPackage, int which, int userId) { 859 if (DEBUG) Slog.v(TAG, "clearWallpaper"); 860 checkPermission(android.Manifest.permission.SET_WALLPAPER); 861 if (!isWallpaperSupported(callingPackage) || !isWallpaperSettingAllowed(callingPackage)) { 862 return; 863 } 864 if (userId != UserHandle.getCallingUserId()) { 865 // cross-user call 866 mContext.enforceCallingOrSelfPermission( 867 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, 868 "WallpaperManagerService"); 869 } 870 871 synchronized (mLock) { 872 clearWallpaperLocked(false, which, userId, null); 873 } 874 } 875 876 void clearWallpaperLocked(boolean defaultFailed, int which, int userId, IRemoteCallback reply) { 877 if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) { 878 throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read"); 879 } 880 881 WallpaperData wallpaper = null; 882 if (which == FLAG_SET_LOCK) { 883 wallpaper = mLockWallpaperMap.get(userId); 884 if (wallpaper == null) { 885 // It's already gone; we're done. 886 if (DEBUG) { 887 Slog.i(TAG, "Lock wallpaper already cleared"); 888 } 889 return; 890 } 891 } else { 892 wallpaper = mWallpaperMap.get(userId); 893 if (wallpaper == null) { 894 // Might need to bring it in the first time to establish our rewrite 895 loadSettingsLocked(userId); 896 wallpaper = mWallpaperMap.get(userId); 897 } 898 } 899 if (wallpaper == null) { 900 return; 901 } 902 903 final long ident = Binder.clearCallingIdentity(); 904 try { 905 if (wallpaper.wallpaperFile.exists()) { 906 wallpaper.wallpaperFile.delete(); 907 wallpaper.cropFile.delete(); 908 if (which == FLAG_SET_LOCK) { 909 mLockWallpaperMap.remove(userId); 910 final IWallpaperManagerCallback cb = mKeyguardListener; 911 if (cb != null) { 912 if (DEBUG) { 913 Slog.i(TAG, "Notifying keyguard of lock wallpaper clear"); 914 } 915 try { 916 cb.onWallpaperChanged(); 917 } catch (RemoteException e) { 918 // Oh well it went away; no big deal 919 } 920 } 921 saveSettingsLocked(userId); 922 return; 923 } 924 } 925 926 RuntimeException e = null; 927 try { 928 wallpaper.imageWallpaperPending = false; 929 if (userId != mCurrentUserId) return; 930 if (bindWallpaperComponentLocked(defaultFailed 931 ? mImageWallpaper 932 : null, true, false, wallpaper, reply)) { 933 return; 934 } 935 } catch (IllegalArgumentException e1) { 936 e = e1; 937 } 938 939 // This can happen if the default wallpaper component doesn't 940 // exist. This should be a system configuration problem, but 941 // let's not let it crash the system and just live with no 942 // wallpaper. 943 Slog.e(TAG, "Default wallpaper component not found!", e); 944 clearWallpaperComponentLocked(wallpaper); 945 if (reply != null) { 946 try { 947 reply.sendResult(null); 948 } catch (RemoteException e1) { 949 } 950 } 951 } finally { 952 Binder.restoreCallingIdentity(ident); 953 } 954 } 955 956 public boolean hasNamedWallpaper(String name) { 957 synchronized (mLock) { 958 List<UserInfo> users; 959 long ident = Binder.clearCallingIdentity(); 960 try { 961 users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers(); 962 } finally { 963 Binder.restoreCallingIdentity(ident); 964 } 965 for (UserInfo user: users) { 966 // ignore managed profiles 967 if (user.isManagedProfile()) { 968 continue; 969 } 970 WallpaperData wd = mWallpaperMap.get(user.id); 971 if (wd == null) { 972 // User hasn't started yet, so load her settings to peek at the wallpaper 973 loadSettingsLocked(user.id); 974 wd = mWallpaperMap.get(user.id); 975 } 976 if (wd != null && name.equals(wd.name)) { 977 return true; 978 } 979 } 980 } 981 return false; 982 } 983 984 private Point getDefaultDisplaySize() { 985 Point p = new Point(); 986 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); 987 Display d = wm.getDefaultDisplay(); 988 d.getRealSize(p); 989 return p; 990 } 991 992 public void setDimensionHints(int width, int height, String callingPackage) 993 throws RemoteException { 994 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); 995 if (!isWallpaperSupported(callingPackage)) { 996 return; 997 } 998 synchronized (mLock) { 999 int userId = UserHandle.getCallingUserId(); 1000 WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SET_SYSTEM); 1001 if (width <= 0 || height <= 0) { 1002 throw new IllegalArgumentException("width and height must be > 0"); 1003 } 1004 // Make sure it is at least as large as the display. 1005 Point displaySize = getDefaultDisplaySize(); 1006 width = Math.max(width, displaySize.x); 1007 height = Math.max(height, displaySize.y); 1008 1009 if (width != wallpaper.width || height != wallpaper.height) { 1010 wallpaper.width = width; 1011 wallpaper.height = height; 1012 saveSettingsLocked(userId); 1013 if (mCurrentUserId != userId) return; // Don't change the properties now 1014 if (wallpaper.connection != null) { 1015 if (wallpaper.connection.mEngine != null) { 1016 try { 1017 wallpaper.connection.mEngine.setDesiredSize( 1018 width, height); 1019 } catch (RemoteException e) { 1020 } 1021 notifyCallbacksLocked(wallpaper); 1022 } else if (wallpaper.connection.mService != null) { 1023 // We've attached to the service but the engine hasn't attached back to us 1024 // yet. This means it will be created with the previous dimensions, so we 1025 // need to update it to the new dimensions once it attaches. 1026 wallpaper.connection.mDimensionsChanged = true; 1027 } 1028 } 1029 } 1030 } 1031 } 1032 1033 public int getWidthHint() throws RemoteException { 1034 synchronized (mLock) { 1035 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); 1036 if (wallpaper != null) { 1037 return wallpaper.width; 1038 } else { 1039 return 0; 1040 } 1041 } 1042 } 1043 1044 public int getHeightHint() throws RemoteException { 1045 synchronized (mLock) { 1046 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); 1047 if (wallpaper != null) { 1048 return wallpaper.height; 1049 } else { 1050 return 0; 1051 } 1052 } 1053 } 1054 1055 public void setDisplayPadding(Rect padding, String callingPackage) { 1056 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); 1057 if (!isWallpaperSupported(callingPackage)) { 1058 return; 1059 } 1060 synchronized (mLock) { 1061 int userId = UserHandle.getCallingUserId(); 1062 WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SET_SYSTEM); 1063 if (padding.left < 0 || padding.top < 0 || padding.right < 0 || padding.bottom < 0) { 1064 throw new IllegalArgumentException("padding must be positive: " + padding); 1065 } 1066 1067 if (!padding.equals(wallpaper.padding)) { 1068 wallpaper.padding.set(padding); 1069 saveSettingsLocked(userId); 1070 if (mCurrentUserId != userId) return; // Don't change the properties now 1071 if (wallpaper.connection != null) { 1072 if (wallpaper.connection.mEngine != null) { 1073 try { 1074 wallpaper.connection.mEngine.setDisplayPadding(padding); 1075 } catch (RemoteException e) { 1076 } 1077 notifyCallbacksLocked(wallpaper); 1078 } else if (wallpaper.connection.mService != null) { 1079 // We've attached to the service but the engine hasn't attached back to us 1080 // yet. This means it will be created with the previous dimensions, so we 1081 // need to update it to the new dimensions once it attaches. 1082 wallpaper.connection.mPaddingChanged = true; 1083 } 1084 } 1085 } 1086 } 1087 } 1088 1089 @Override 1090 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, final int which, 1091 Bundle outParams, int wallpaperUserId) { 1092 if (wallpaperUserId != UserHandle.getCallingUserId()) { 1093 // cross-user call 1094 mContext.enforceCallingOrSelfPermission( 1095 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, 1096 "WallpaperManagerService"); 1097 } 1098 1099 if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) { 1100 throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read"); 1101 } 1102 1103 synchronized (mLock) { 1104 final SparseArray<WallpaperData> whichSet = 1105 (which == FLAG_SET_LOCK) ? mLockWallpaperMap : mWallpaperMap; 1106 WallpaperData wallpaper = whichSet.get(wallpaperUserId); 1107 if (wallpaper == null) { 1108 // common case, this is the first lookup post-boot of the system or 1109 // unified lock, so we bring up the saved state lazily now and recheck. 1110 loadSettingsLocked(wallpaperUserId); 1111 wallpaper = whichSet.get(wallpaperUserId); 1112 if (wallpaper == null) { 1113 return null; 1114 } 1115 } 1116 try { 1117 if (outParams != null) { 1118 outParams.putInt("width", wallpaper.width); 1119 outParams.putInt("height", wallpaper.height); 1120 } 1121 if (cb != null) { 1122 wallpaper.callbacks.register(cb); 1123 } 1124 if (!wallpaper.cropFile.exists()) { 1125 return null; 1126 } 1127 return ParcelFileDescriptor.open(wallpaper.cropFile, MODE_READ_ONLY); 1128 } catch (FileNotFoundException e) { 1129 /* Shouldn't happen as we check to see if the file exists */ 1130 Slog.w(TAG, "Error getting wallpaper", e); 1131 } 1132 return null; 1133 } 1134 } 1135 1136 public WallpaperInfo getWallpaperInfo() { 1137 int userId = UserHandle.getCallingUserId(); 1138 synchronized (mLock) { 1139 WallpaperData wallpaper = mWallpaperMap.get(userId); 1140 if (wallpaper != null && wallpaper.connection != null) { 1141 return wallpaper.connection.mInfo; 1142 } 1143 return null; 1144 } 1145 } 1146 1147 @Override 1148 public boolean setLockWallpaperCallback(IWallpaperManagerCallback cb) { 1149 checkPermission(android.Manifest.permission.INTERNAL_SYSTEM_WINDOW); 1150 synchronized (mLock) { 1151 mKeyguardListener = cb; 1152 } 1153 return true; 1154 } 1155 1156 @Override 1157 public ParcelFileDescriptor setWallpaper(String name, String callingPackage, 1158 Rect cropHint, Bundle extras, int which, IWallpaperManagerCallback completion) { 1159 checkPermission(android.Manifest.permission.SET_WALLPAPER); 1160 1161 if ((which & (FLAG_SET_LOCK|FLAG_SET_SYSTEM)) == 0) { 1162 Slog.e(TAG, "Must specify a valid wallpaper category to set"); 1163 return null; 1164 } 1165 1166 if (!isWallpaperSupported(callingPackage) || !isWallpaperSettingAllowed(callingPackage)) { 1167 return null; 1168 } 1169 1170 // "null" means the no-op crop, preserving the full input image 1171 if (cropHint == null) { 1172 cropHint = new Rect(0, 0, 0, 0); 1173 } else { 1174 if (cropHint.isEmpty() 1175 || cropHint.left < 0 1176 || cropHint.top < 0) { 1177 return null; 1178 } 1179 } 1180 1181 final int userId = UserHandle.getCallingUserId(); 1182 1183 synchronized (mLock) { 1184 if (DEBUG) Slog.v(TAG, "setWallpaper which=0x" + Integer.toHexString(which)); 1185 WallpaperData wallpaper; 1186 1187 wallpaper = getWallpaperSafeLocked(userId, which); 1188 final long ident = Binder.clearCallingIdentity(); 1189 try { 1190 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper, extras); 1191 if (pfd != null) { 1192 wallpaper.imageWallpaperPending = true; 1193 wallpaper.whichPending = which; 1194 wallpaper.setComplete = completion; 1195 wallpaper.cropHint.set(cropHint); 1196 } 1197 return pfd; 1198 } finally { 1199 Binder.restoreCallingIdentity(ident); 1200 } 1201 } 1202 } 1203 1204 ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper, 1205 Bundle extras) { 1206 if (name == null) name = ""; 1207 try { 1208 File dir = getWallpaperDir(wallpaper.userId); 1209 if (!dir.exists()) { 1210 dir.mkdir(); 1211 FileUtils.setPermissions( 1212 dir.getPath(), 1213 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, 1214 -1, -1); 1215 } 1216 ParcelFileDescriptor fd = ParcelFileDescriptor.open(wallpaper.wallpaperFile, 1217 MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE); 1218 if (!SELinux.restorecon(wallpaper.wallpaperFile)) { 1219 return null; 1220 } 1221 wallpaper.name = name; 1222 wallpaper.wallpaperId = makeWallpaperIdLocked(); 1223 if (extras != null) { 1224 extras.putInt(WallpaperManager.EXTRA_NEW_WALLPAPER_ID, wallpaper.wallpaperId); 1225 } 1226 if (DEBUG) { 1227 Slog.v(TAG, "updateWallpaperBitmapLocked() : id=" + wallpaper.wallpaperId 1228 + " name=" + name + " file=" + wallpaper.wallpaperFile.getName()); 1229 } 1230 return fd; 1231 } catch (FileNotFoundException e) { 1232 Slog.w(TAG, "Error setting wallpaper", e); 1233 } 1234 return null; 1235 } 1236 1237 public void setWallpaperComponentChecked(ComponentName name, String callingPackage) { 1238 if (isWallpaperSupported(callingPackage) && isWallpaperSettingAllowed(callingPackage)) { 1239 setWallpaperComponent(name); 1240 } 1241 } 1242 1243 // ToDo: Remove this version of the function 1244 public void setWallpaperComponent(ComponentName name) { 1245 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT); 1246 synchronized (mLock) { 1247 if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name); 1248 int userId = UserHandle.getCallingUserId(); 1249 WallpaperData wallpaper = mWallpaperMap.get(userId); 1250 if (wallpaper == null) { 1251 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); 1252 } 1253 final long ident = Binder.clearCallingIdentity(); 1254 try { 1255 wallpaper.imageWallpaperPending = false; 1256 bindWallpaperComponentLocked(name, false, true, wallpaper, null); 1257 } finally { 1258 Binder.restoreCallingIdentity(ident); 1259 } 1260 } 1261 } 1262 1263 boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force, 1264 boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) { 1265 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName); 1266 // Has the component changed? 1267 if (!force) { 1268 if (wallpaper.connection != null) { 1269 if (wallpaper.wallpaperComponent == null) { 1270 if (componentName == null) { 1271 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default"); 1272 // Still using default wallpaper. 1273 return true; 1274 } 1275 } else if (wallpaper.wallpaperComponent.equals(componentName)) { 1276 // Changing to same wallpaper. 1277 if (DEBUG) Slog.v(TAG, "same wallpaper"); 1278 return true; 1279 } 1280 } 1281 } 1282 1283 try { 1284 if (componentName == null) { 1285 componentName = WallpaperManager.getDefaultWallpaperComponent(mContext); 1286 if (componentName == null) { 1287 // Fall back to static image wallpaper 1288 componentName = mImageWallpaper; 1289 //clearWallpaperComponentLocked(); 1290 //return; 1291 if (DEBUG) Slog.v(TAG, "Using image wallpaper"); 1292 } 1293 } 1294 int serviceUserId = wallpaper.userId; 1295 ServiceInfo si = mIPackageManager.getServiceInfo(componentName, 1296 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId); 1297 if (si == null) { 1298 // The wallpaper component we're trying to use doesn't exist 1299 Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable"); 1300 return false; 1301 } 1302 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) { 1303 String msg = "Selected service does not require " 1304 + android.Manifest.permission.BIND_WALLPAPER 1305 + ": " + componentName; 1306 if (fromUser) { 1307 throw new SecurityException(msg); 1308 } 1309 Slog.w(TAG, msg); 1310 return false; 1311 } 1312 1313 WallpaperInfo wi = null; 1314 1315 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); 1316 if (componentName != null && !componentName.equals(mImageWallpaper)) { 1317 // Make sure the selected service is actually a wallpaper service. 1318 List<ResolveInfo> ris = 1319 mIPackageManager.queryIntentServices(intent, 1320 intent.resolveTypeIfNeeded(mContext.getContentResolver()), 1321 PackageManager.GET_META_DATA, serviceUserId).getList(); 1322 for (int i=0; i<ris.size(); i++) { 1323 ServiceInfo rsi = ris.get(i).serviceInfo; 1324 if (rsi.name.equals(si.name) && 1325 rsi.packageName.equals(si.packageName)) { 1326 try { 1327 wi = new WallpaperInfo(mContext, ris.get(i)); 1328 } catch (XmlPullParserException e) { 1329 if (fromUser) { 1330 throw new IllegalArgumentException(e); 1331 } 1332 Slog.w(TAG, e); 1333 return false; 1334 } catch (IOException e) { 1335 if (fromUser) { 1336 throw new IllegalArgumentException(e); 1337 } 1338 Slog.w(TAG, e); 1339 return false; 1340 } 1341 break; 1342 } 1343 } 1344 if (wi == null) { 1345 String msg = "Selected service is not a wallpaper: " 1346 + componentName; 1347 if (fromUser) { 1348 throw new SecurityException(msg); 1349 } 1350 Slog.w(TAG, msg); 1351 return false; 1352 } 1353 } 1354 1355 // Bind the service! 1356 if (DEBUG) Slog.v(TAG, "Binding to:" + componentName); 1357 WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper); 1358 intent.setComponent(componentName); 1359 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, 1360 com.android.internal.R.string.wallpaper_binding_label); 1361 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser( 1362 mContext, 0, 1363 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), 1364 mContext.getText(com.android.internal.R.string.chooser_wallpaper)), 1365 0, null, new UserHandle(serviceUserId))); 1366 if (!mContext.bindServiceAsUser(intent, newConn, 1367 Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI 1368 | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE, 1369 new UserHandle(serviceUserId))) { 1370 String msg = "Unable to bind service: " 1371 + componentName; 1372 if (fromUser) { 1373 throw new IllegalArgumentException(msg); 1374 } 1375 Slog.w(TAG, msg); 1376 return false; 1377 } 1378 if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) { 1379 detachWallpaperLocked(mLastWallpaper); 1380 } 1381 wallpaper.wallpaperComponent = componentName; 1382 wallpaper.connection = newConn; 1383 newConn.mReply = reply; 1384 try { 1385 if (wallpaper.userId == mCurrentUserId) { 1386 if (DEBUG) 1387 Slog.v(TAG, "Adding window token: " + newConn.mToken); 1388 mIWindowManager.addWindowToken(newConn.mToken, 1389 WindowManager.LayoutParams.TYPE_WALLPAPER); 1390 mLastWallpaper = wallpaper; 1391 } 1392 } catch (RemoteException e) { 1393 } 1394 } catch (RemoteException e) { 1395 String msg = "Remote exception for " + componentName + "\n" + e; 1396 if (fromUser) { 1397 throw new IllegalArgumentException(msg); 1398 } 1399 Slog.w(TAG, msg); 1400 return false; 1401 } 1402 return true; 1403 } 1404 1405 void detachWallpaperLocked(WallpaperData wallpaper) { 1406 if (wallpaper.connection != null) { 1407 if (wallpaper.connection.mReply != null) { 1408 try { 1409 wallpaper.connection.mReply.sendResult(null); 1410 } catch (RemoteException e) { 1411 } 1412 wallpaper.connection.mReply = null; 1413 } 1414 if (wallpaper.connection.mEngine != null) { 1415 try { 1416 wallpaper.connection.mEngine.destroy(); 1417 } catch (RemoteException e) { 1418 } 1419 } 1420 mContext.unbindService(wallpaper.connection); 1421 try { 1422 if (DEBUG) 1423 Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken); 1424 mIWindowManager.removeWindowToken(wallpaper.connection.mToken); 1425 } catch (RemoteException e) { 1426 } 1427 wallpaper.connection.mService = null; 1428 wallpaper.connection.mEngine = null; 1429 wallpaper.connection = null; 1430 } 1431 } 1432 1433 void clearWallpaperComponentLocked(WallpaperData wallpaper) { 1434 wallpaper.wallpaperComponent = null; 1435 detachWallpaperLocked(wallpaper); 1436 } 1437 1438 void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) { 1439 try { 1440 conn.mService.attach(conn, conn.mToken, 1441 WindowManager.LayoutParams.TYPE_WALLPAPER, false, 1442 wallpaper.width, wallpaper.height, wallpaper.padding); 1443 } catch (RemoteException e) { 1444 Slog.w(TAG, "Failed attaching wallpaper; clearing", e); 1445 if (!wallpaper.wallpaperUpdating) { 1446 bindWallpaperComponentLocked(null, false, false, wallpaper, null); 1447 } 1448 } 1449 } 1450 1451 private void notifyCallbacksLocked(WallpaperData wallpaper) { 1452 final int n = wallpaper.callbacks.beginBroadcast(); 1453 for (int i = 0; i < n; i++) { 1454 try { 1455 wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged(); 1456 } catch (RemoteException e) { 1457 1458 // The RemoteCallbackList will take care of removing 1459 // the dead object for us. 1460 } 1461 } 1462 wallpaper.callbacks.finishBroadcast(); 1463 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED); 1464 mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId)); 1465 } 1466 1467 private void checkPermission(String permission) { 1468 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) { 1469 throw new SecurityException("Access denied to process: " + Binder.getCallingPid() 1470 + ", must have permission " + permission); 1471 } 1472 } 1473 1474 /** 1475 * Certain user types do not support wallpapers (e.g. managed profiles). The check is 1476 * implemented through through the OP_WRITE_WALLPAPER AppOp. 1477 */ 1478 public boolean isWallpaperSupported(String callingPackage) { 1479 return mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_WRITE_WALLPAPER, Binder.getCallingUid(), 1480 callingPackage) == AppOpsManager.MODE_ALLOWED; 1481 } 1482 1483 @Override 1484 public boolean isWallpaperSettingAllowed(String callingPackage) { 1485 final PackageManager pm = mContext.getPackageManager(); 1486 String[] uidPackages = pm.getPackagesForUid(Binder.getCallingUid()); 1487 boolean uidMatchPackage = Arrays.asList(uidPackages).contains(callingPackage); 1488 if (!uidMatchPackage) { 1489 return false; // callingPackage was faked. 1490 } 1491 1492 final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); 1493 if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) { 1494 return true; 1495 } 1496 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 1497 return !um.hasUserRestriction(UserManager.DISALLOW_SET_WALLPAPER); 1498 } 1499 1500 private static JournaledFile makeJournaledFile(int userId) { 1501 final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath(); 1502 return new JournaledFile(new File(base), new File(base + ".tmp")); 1503 } 1504 1505 private void saveSettingsLocked(int userId) { 1506 JournaledFile journal = makeJournaledFile(userId); 1507 FileOutputStream fstream = null; 1508 BufferedOutputStream stream = null; 1509 try { 1510 XmlSerializer out = new FastXmlSerializer(); 1511 fstream = new FileOutputStream(journal.chooseForWrite(), false); 1512 stream = new BufferedOutputStream(fstream); 1513 out.setOutput(stream, StandardCharsets.UTF_8.name()); 1514 out.startDocument(null, true); 1515 1516 WallpaperData wallpaper; 1517 1518 wallpaper = mWallpaperMap.get(userId); 1519 if (wallpaper != null) { 1520 writeWallpaperAttributes(out, "wp", wallpaper); 1521 } 1522 wallpaper = mLockWallpaperMap.get(userId); 1523 if (wallpaper != null) { 1524 writeWallpaperAttributes(out, "kwp", wallpaper); 1525 } 1526 1527 out.endDocument(); 1528 1529 stream.flush(); // also flushes fstream 1530 FileUtils.sync(fstream); 1531 stream.close(); // also closes fstream 1532 journal.commit(); 1533 } catch (IOException e) { 1534 IoUtils.closeQuietly(stream); 1535 journal.rollback(); 1536 } 1537 } 1538 1539 private void writeWallpaperAttributes(XmlSerializer out, String tag, WallpaperData wallpaper) 1540 throws IllegalArgumentException, IllegalStateException, IOException { 1541 out.startTag(null, tag); 1542 out.attribute(null, "id", Integer.toString(wallpaper.wallpaperId)); 1543 out.attribute(null, "width", Integer.toString(wallpaper.width)); 1544 out.attribute(null, "height", Integer.toString(wallpaper.height)); 1545 1546 out.attribute(null, "cropLeft", Integer.toString(wallpaper.cropHint.left)); 1547 out.attribute(null, "cropTop", Integer.toString(wallpaper.cropHint.top)); 1548 out.attribute(null, "cropRight", Integer.toString(wallpaper.cropHint.right)); 1549 out.attribute(null, "cropBottom", Integer.toString(wallpaper.cropHint.bottom)); 1550 1551 if (wallpaper.padding.left != 0) { 1552 out.attribute(null, "paddingLeft", Integer.toString(wallpaper.padding.left)); 1553 } 1554 if (wallpaper.padding.top != 0) { 1555 out.attribute(null, "paddingTop", Integer.toString(wallpaper.padding.top)); 1556 } 1557 if (wallpaper.padding.right != 0) { 1558 out.attribute(null, "paddingRight", Integer.toString(wallpaper.padding.right)); 1559 } 1560 if (wallpaper.padding.bottom != 0) { 1561 out.attribute(null, "paddingBottom", Integer.toString(wallpaper.padding.bottom)); 1562 } 1563 1564 out.attribute(null, "name", wallpaper.name); 1565 if (wallpaper.wallpaperComponent != null 1566 && !wallpaper.wallpaperComponent.equals(mImageWallpaper)) { 1567 out.attribute(null, "component", 1568 wallpaper.wallpaperComponent.flattenToShortString()); 1569 } 1570 out.endTag(null, tag); 1571 } 1572 1573 private void migrateFromOld() { 1574 File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY); 1575 File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY); 1576 if (oldWallpaper.exists()) { 1577 File newWallpaper = new File(getWallpaperDir(0), WALLPAPER); 1578 oldWallpaper.renameTo(newWallpaper); 1579 } 1580 if (oldInfo.exists()) { 1581 File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO); 1582 oldInfo.renameTo(newInfo); 1583 } 1584 } 1585 1586 private int getAttributeInt(XmlPullParser parser, String name, int defValue) { 1587 String value = parser.getAttributeValue(null, name); 1588 if (value == null) { 1589 return defValue; 1590 } 1591 return Integer.parseInt(value); 1592 } 1593 1594 /** 1595 * Sometimes it is expected the wallpaper map may not have a user's data. E.g. This could 1596 * happen during user switch. The async user switch observer may not have received 1597 * the event yet. We use this safe method when we don't care about this ordering and just 1598 * want to update the data. The data is going to be applied when the user switch observer 1599 * is eventually executed. 1600 */ 1601 private WallpaperData getWallpaperSafeLocked(int userId, int which) { 1602 // We're setting either just system (work with the system wallpaper), 1603 // both (also work with the system wallpaper), or just the lock 1604 // wallpaper (update against the existing lock wallpaper if any). 1605 // Combined or just-system operations use the 'system' WallpaperData 1606 // for this use; lock-only operations use the dedicated one. 1607 final SparseArray<WallpaperData> whichSet = 1608 (which == FLAG_SET_LOCK) ? mLockWallpaperMap : mWallpaperMap; 1609 WallpaperData wallpaper = whichSet.get(userId); 1610 if (wallpaper == null) { 1611 // common case, this is the first lookup post-boot of the system or 1612 // unified lock, so we bring up the saved state lazily now and recheck. 1613 loadSettingsLocked(userId); 1614 wallpaper = whichSet.get(userId); 1615 // if it's still null here, this is a lock-only operation and there is not 1616 // yet a lock-only wallpaper set for this user, so we need to establish 1617 // it now. 1618 if (wallpaper == null) { 1619 if (which == FLAG_SET_LOCK) { 1620 wallpaper = new WallpaperData(userId, 1621 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); 1622 mLockWallpaperMap.put(userId, wallpaper); 1623 } else { 1624 // sanity fallback: we're in bad shape, but establishing a known 1625 // valid system+lock WallpaperData will keep us from dying. 1626 Slog.wtf(TAG, "Didn't find wallpaper in non-lock case!"); 1627 wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP); 1628 mWallpaperMap.put(userId, wallpaper); 1629 } 1630 } 1631 } 1632 return wallpaper; 1633 } 1634 1635 private void loadSettingsLocked(int userId) { 1636 if (DEBUG) Slog.v(TAG, "loadSettingsLocked"); 1637 1638 JournaledFile journal = makeJournaledFile(userId); 1639 FileInputStream stream = null; 1640 File file = journal.chooseForRead(); 1641 if (!file.exists()) { 1642 // This should only happen one time, when upgrading from a legacy system 1643 migrateFromOld(); 1644 } 1645 WallpaperData wallpaper = mWallpaperMap.get(userId); 1646 if (wallpaper == null) { 1647 wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP); 1648 mWallpaperMap.put(userId, wallpaper); 1649 if (!wallpaper.cropExists()) { 1650 generateCrop(wallpaper); 1651 } 1652 } 1653 boolean success = false; 1654 try { 1655 stream = new FileInputStream(file); 1656 XmlPullParser parser = Xml.newPullParser(); 1657 parser.setInput(stream, StandardCharsets.UTF_8.name()); 1658 1659 int type; 1660 do { 1661 type = parser.next(); 1662 if (type == XmlPullParser.START_TAG) { 1663 String tag = parser.getName(); 1664 if ("wp".equals(tag)) { 1665 // Common to system + lock wallpapers 1666 parseWallpaperAttributes(parser, wallpaper); 1667 1668 // A system wallpaper might also be a live wallpaper 1669 String comp = parser.getAttributeValue(null, "component"); 1670 wallpaper.nextWallpaperComponent = comp != null 1671 ? ComponentName.unflattenFromString(comp) 1672 : null; 1673 if (wallpaper.nextWallpaperComponent == null 1674 || "android".equals(wallpaper.nextWallpaperComponent 1675 .getPackageName())) { 1676 wallpaper.nextWallpaperComponent = mImageWallpaper; 1677 } 1678 1679 if (DEBUG) { 1680 Slog.v(TAG, "mWidth:" + wallpaper.width); 1681 Slog.v(TAG, "mHeight:" + wallpaper.height); 1682 Slog.v(TAG, "cropRect:" + wallpaper.cropHint); 1683 Slog.v(TAG, "mName:" + wallpaper.name); 1684 Slog.v(TAG, "mNextWallpaperComponent:" 1685 + wallpaper.nextWallpaperComponent); 1686 } 1687 } else if ("kwp".equals(tag)) { 1688 // keyguard-specific wallpaper for this user 1689 WallpaperData lockWallpaper = mLockWallpaperMap.get(userId); 1690 if (lockWallpaper == null) { 1691 lockWallpaper = new WallpaperData(userId, 1692 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); 1693 mLockWallpaperMap.put(userId, lockWallpaper); 1694 } 1695 parseWallpaperAttributes(parser, lockWallpaper); 1696 } 1697 } 1698 } while (type != XmlPullParser.END_DOCUMENT); 1699 success = true; 1700 } catch (FileNotFoundException e) { 1701 Slog.w(TAG, "no current wallpaper -- first boot?"); 1702 } catch (NullPointerException e) { 1703 Slog.w(TAG, "failed parsing " + file + " " + e); 1704 } catch (NumberFormatException e) { 1705 Slog.w(TAG, "failed parsing " + file + " " + e); 1706 } catch (XmlPullParserException e) { 1707 Slog.w(TAG, "failed parsing " + file + " " + e); 1708 } catch (IOException e) { 1709 Slog.w(TAG, "failed parsing " + file + " " + e); 1710 } catch (IndexOutOfBoundsException e) { 1711 Slog.w(TAG, "failed parsing " + file + " " + e); 1712 } 1713 IoUtils.closeQuietly(stream); 1714 1715 if (!success) { 1716 wallpaper.width = -1; 1717 wallpaper.height = -1; 1718 wallpaper.cropHint.set(0, 0, 0, 0); 1719 wallpaper.padding.set(0, 0, 0, 0); 1720 wallpaper.name = ""; 1721 } else { 1722 if (wallpaper.wallpaperId <= 0) { 1723 wallpaper.wallpaperId = makeWallpaperIdLocked(); 1724 if (DEBUG) { 1725 Slog.w(TAG, "Didn't set wallpaper id in loadSettingsLocked(" + userId 1726 + "); now " + wallpaper.wallpaperId); 1727 } 1728 } 1729 } 1730 1731 // We always want to have some reasonable width hint. 1732 int baseSize = getMaximumSizeDimension(); 1733 if (wallpaper.width < baseSize) { 1734 wallpaper.width = baseSize; 1735 } 1736 if (wallpaper.height < baseSize) { 1737 wallpaper.height = baseSize; 1738 } 1739 // and crop, if not previously specified 1740 if (wallpaper.cropHint.width() <= 0 1741 || wallpaper.cropHint.height() <= 0) { 1742 wallpaper.cropHint.set(0, 0, wallpaper.width, wallpaper.height); 1743 } 1744 } 1745 1746 private void parseWallpaperAttributes(XmlPullParser parser, WallpaperData wallpaper) { 1747 final String idString = parser.getAttributeValue(null, "id"); 1748 if (idString != null) { 1749 final int id = wallpaper.wallpaperId = Integer.parseInt(idString); 1750 if (id > mWallpaperId) { 1751 mWallpaperId = id; 1752 } 1753 } else { 1754 wallpaper.wallpaperId = makeWallpaperIdLocked(); 1755 } 1756 1757 wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width")); 1758 wallpaper.height = Integer.parseInt(parser 1759 .getAttributeValue(null, "height")); 1760 wallpaper.cropHint.left = getAttributeInt(parser, "cropLeft", 0); 1761 wallpaper.cropHint.top = getAttributeInt(parser, "cropTop", 0); 1762 wallpaper.cropHint.right = getAttributeInt(parser, "cropRight", 0); 1763 wallpaper.cropHint.bottom = getAttributeInt(parser, "cropBottom", 0); 1764 wallpaper.padding.left = getAttributeInt(parser, "paddingLeft", 0); 1765 wallpaper.padding.top = getAttributeInt(parser, "paddingTop", 0); 1766 wallpaper.padding.right = getAttributeInt(parser, "paddingRight", 0); 1767 wallpaper.padding.bottom = getAttributeInt(parser, "paddingBottom", 0); 1768 wallpaper.name = parser.getAttributeValue(null, "name"); 1769 } 1770 1771 private int getMaximumSizeDimension() { 1772 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); 1773 Display d = wm.getDefaultDisplay(); 1774 return d.getMaximumSizeDimension(); 1775 } 1776 1777 // Called by SystemBackupAgent after files are restored to disk. 1778 public void settingsRestored() { 1779 // Verify caller is the system 1780 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { 1781 throw new RuntimeException("settingsRestored() can only be called from the system process"); 1782 } 1783 // TODO: If necessary, make it work for secondary users as well. This currently assumes 1784 // restores only to the primary user 1785 if (DEBUG) Slog.v(TAG, "settingsRestored"); 1786 WallpaperData wallpaper = null; 1787 boolean success = false; 1788 synchronized (mLock) { 1789 loadSettingsLocked(UserHandle.USER_SYSTEM); 1790 wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM); 1791 wallpaper.wallpaperId = makeWallpaperIdLocked(); // always bump id at restore 1792 if (wallpaper.nextWallpaperComponent != null 1793 && !wallpaper.nextWallpaperComponent.equals(mImageWallpaper)) { 1794 if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false, 1795 wallpaper, null)) { 1796 // No such live wallpaper or other failure; fall back to the default 1797 // live wallpaper (since the profile being restored indicated that the 1798 // user had selected a live rather than static one). 1799 bindWallpaperComponentLocked(null, false, false, wallpaper, null); 1800 } 1801 success = true; 1802 } else { 1803 // If there's a wallpaper name, we use that. If that can't be loaded, then we 1804 // use the default. 1805 if ("".equals(wallpaper.name)) { 1806 if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty"); 1807 success = true; 1808 } else { 1809 if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource"); 1810 success = restoreNamedResourceLocked(wallpaper); 1811 } 1812 if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success 1813 + " id=" + wallpaper.wallpaperId); 1814 if (success) { 1815 generateCrop(wallpaper); // based on the new image + metadata 1816 bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, true, false, 1817 wallpaper, null); 1818 } 1819 } 1820 } 1821 1822 if (!success) { 1823 Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'"); 1824 wallpaper.name = ""; 1825 getWallpaperDir(UserHandle.USER_SYSTEM).delete(); 1826 } 1827 1828 synchronized (mLock) { 1829 saveSettingsLocked(UserHandle.USER_SYSTEM); 1830 } 1831 } 1832 1833 // Restore the named resource bitmap to both source + crop files 1834 boolean restoreNamedResourceLocked(WallpaperData wallpaper) { 1835 if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) { 1836 String resName = wallpaper.name.substring(4); 1837 1838 String pkg = null; 1839 int colon = resName.indexOf(':'); 1840 if (colon > 0) { 1841 pkg = resName.substring(0, colon); 1842 } 1843 1844 String ident = null; 1845 int slash = resName.lastIndexOf('/'); 1846 if (slash > 0) { 1847 ident = resName.substring(slash+1); 1848 } 1849 1850 String type = null; 1851 if (colon > 0 && slash > 0 && (slash-colon) > 1) { 1852 type = resName.substring(colon+1, slash); 1853 } 1854 1855 if (pkg != null && ident != null && type != null) { 1856 int resId = -1; 1857 InputStream res = null; 1858 FileOutputStream fos = null; 1859 FileOutputStream cos = null; 1860 try { 1861 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED); 1862 Resources r = c.getResources(); 1863 resId = r.getIdentifier(resName, null, null); 1864 if (resId == 0) { 1865 Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type 1866 + " ident=" + ident); 1867 return false; 1868 } 1869 1870 res = r.openRawResource(resId); 1871 if (wallpaper.wallpaperFile.exists()) { 1872 wallpaper.wallpaperFile.delete(); 1873 wallpaper.cropFile.delete(); 1874 } 1875 fos = new FileOutputStream(wallpaper.wallpaperFile); 1876 cos = new FileOutputStream(wallpaper.cropFile); 1877 1878 byte[] buffer = new byte[32768]; 1879 int amt; 1880 while ((amt=res.read(buffer)) > 0) { 1881 fos.write(buffer, 0, amt); 1882 cos.write(buffer, 0, amt); 1883 } 1884 // mWallpaperObserver will notice the close and send the change broadcast 1885 1886 Slog.v(TAG, "Restored wallpaper: " + resName); 1887 return true; 1888 } catch (NameNotFoundException e) { 1889 Slog.e(TAG, "Package name " + pkg + " not found"); 1890 } catch (Resources.NotFoundException e) { 1891 Slog.e(TAG, "Resource not found: " + resId); 1892 } catch (IOException e) { 1893 Slog.e(TAG, "IOException while restoring wallpaper ", e); 1894 } finally { 1895 IoUtils.closeQuietly(res); 1896 if (fos != null) { 1897 FileUtils.sync(fos); 1898 } 1899 if (cos != null) { 1900 FileUtils.sync(cos); 1901 } 1902 IoUtils.closeQuietly(fos); 1903 IoUtils.closeQuietly(cos); 1904 } 1905 } 1906 } 1907 return false; 1908 } 1909 1910 @Override 1911 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 1912 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 1913 != PackageManager.PERMISSION_GRANTED) { 1914 1915 pw.println("Permission Denial: can't dump wallpaper service from from pid=" 1916 + Binder.getCallingPid() 1917 + ", uid=" + Binder.getCallingUid()); 1918 return; 1919 } 1920 1921 synchronized (mLock) { 1922 pw.println("Current Wallpaper Service state:"); 1923 for (int i = 0; i < mWallpaperMap.size(); i++) { 1924 WallpaperData wallpaper = mWallpaperMap.valueAt(i); 1925 pw.print(" User "); pw.print(wallpaper.userId); 1926 pw.print(": id="); pw.println(wallpaper.wallpaperId); 1927 pw.print(" mWidth="); 1928 pw.print(wallpaper.width); 1929 pw.print(" mHeight="); 1930 pw.println(wallpaper.height); 1931 pw.print(" mCropHint="); pw.println(wallpaper.cropHint); 1932 pw.print(" mPadding="); pw.println(wallpaper.padding); 1933 pw.print(" mName="); pw.println(wallpaper.name); 1934 pw.print(" mWallpaperComponent="); pw.println(wallpaper.wallpaperComponent); 1935 if (wallpaper.connection != null) { 1936 WallpaperConnection conn = wallpaper.connection; 1937 pw.print(" Wallpaper connection "); 1938 pw.print(conn); 1939 pw.println(":"); 1940 if (conn.mInfo != null) { 1941 pw.print(" mInfo.component="); 1942 pw.println(conn.mInfo.getComponent()); 1943 } 1944 pw.print(" mToken="); 1945 pw.println(conn.mToken); 1946 pw.print(" mService="); 1947 pw.println(conn.mService); 1948 pw.print(" mEngine="); 1949 pw.println(conn.mEngine); 1950 pw.print(" mLastDiedTime="); 1951 pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis()); 1952 } 1953 } 1954 } 1955 } 1956} 1957