WallpaperManagerService.java revision 5a589430b3be99a6fdaf03e621162d08c8b1ad8a
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.os.ParcelFileDescriptor.*; 20 21import android.app.ActivityManagerNative; 22import android.app.AppGlobals; 23import android.app.IUserSwitchObserver; 24import android.app.IWallpaperManager; 25import android.app.IWallpaperManagerCallback; 26import android.app.PendingIntent; 27import android.app.WallpaperInfo; 28import android.app.WallpaperManager; 29import android.app.backup.BackupManager; 30import android.app.backup.WallpaperBackupHelper; 31import android.content.BroadcastReceiver; 32import android.content.ComponentName; 33import android.content.Context; 34import android.content.Intent; 35import android.content.IntentFilter; 36import android.content.ServiceConnection; 37import android.content.pm.IPackageManager; 38import android.content.pm.PackageManager; 39import android.content.pm.ResolveInfo; 40import android.content.pm.ServiceInfo; 41import android.content.pm.PackageManager.NameNotFoundException; 42import android.content.pm.UserInfo; 43import android.content.res.Resources; 44import android.graphics.Point; 45import android.graphics.Rect; 46import android.os.Binder; 47import android.os.Bundle; 48import android.os.Environment; 49import android.os.FileUtils; 50import android.os.IBinder; 51import android.os.IRemoteCallback; 52import android.os.RemoteException; 53import android.os.FileObserver; 54import android.os.ParcelFileDescriptor; 55import android.os.RemoteCallbackList; 56import android.os.SELinux; 57import android.os.ServiceManager; 58import android.os.SystemClock; 59import android.os.UserHandle; 60import android.os.UserManager; 61import android.service.wallpaper.IWallpaperConnection; 62import android.service.wallpaper.IWallpaperEngine; 63import android.service.wallpaper.IWallpaperService; 64import android.service.wallpaper.WallpaperService; 65import android.util.EventLog; 66import android.util.Slog; 67import android.util.SparseArray; 68import android.util.Xml; 69import android.view.Display; 70import android.view.IWindowManager; 71import android.view.WindowManager; 72 73import java.io.FileDescriptor; 74import java.io.IOException; 75import java.io.InputStream; 76import java.io.File; 77import java.io.FileNotFoundException; 78import java.io.FileInputStream; 79import java.io.FileOutputStream; 80import java.io.PrintWriter; 81import java.util.List; 82 83import org.xmlpull.v1.XmlPullParser; 84import org.xmlpull.v1.XmlPullParserException; 85import org.xmlpull.v1.XmlSerializer; 86 87import com.android.internal.content.PackageMonitor; 88import com.android.internal.util.FastXmlSerializer; 89import com.android.internal.util.JournaledFile; 90import com.android.internal.R; 91import com.android.server.EventLogTags; 92 93public class WallpaperManagerService extends IWallpaperManager.Stub { 94 static final String TAG = "WallpaperManagerService"; 95 static final boolean DEBUG = false; 96 97 final Object mLock = new Object[0]; 98 99 /** 100 * Minimum time between crashes of a wallpaper service for us to consider 101 * restarting it vs. just reverting to the static wallpaper. 102 */ 103 static final long MIN_WALLPAPER_CRASH_TIME = 10000; 104 static final int MAX_WALLPAPER_COMPONENT_LOG_LENGTH = 128; 105 static final String WALLPAPER = "wallpaper"; 106 static final String WALLPAPER_INFO = "wallpaper_info.xml"; 107 108 /** 109 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks 110 * that the wallpaper has changed. The CREATE is triggered when there is no 111 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered 112 * everytime the wallpaper is changed. 113 */ 114 private class WallpaperObserver extends FileObserver { 115 116 final WallpaperData mWallpaper; 117 final File mWallpaperDir; 118 final File mWallpaperFile; 119 120 public WallpaperObserver(WallpaperData wallpaper) { 121 super(getWallpaperDir(wallpaper.userId).getAbsolutePath(), 122 CLOSE_WRITE | MOVED_TO | DELETE | DELETE_SELF); 123 mWallpaperDir = getWallpaperDir(wallpaper.userId); 124 mWallpaper = wallpaper; 125 mWallpaperFile = new File(mWallpaperDir, WALLPAPER); 126 } 127 128 @Override 129 public void onEvent(int event, String path) { 130 if (path == null) { 131 return; 132 } 133 synchronized (mLock) { 134 // changing the wallpaper means we'll need to back up the new one 135 long origId = Binder.clearCallingIdentity(); 136 BackupManager bm = new BackupManager(mContext); 137 bm.dataChanged(); 138 Binder.restoreCallingIdentity(origId); 139 140 File changedFile = new File(mWallpaperDir, path); 141 if (mWallpaperFile.equals(changedFile)) { 142 notifyCallbacksLocked(mWallpaper); 143 final boolean written = (event == CLOSE_WRITE || event == MOVED_TO); 144 if (mWallpaper.wallpaperComponent == null 145 || event != CLOSE_WRITE // includes the MOVED_TO case 146 || mWallpaper.imageWallpaperPending) { 147 if (written) { 148 mWallpaper.imageWallpaperPending = false; 149 } 150 bindWallpaperComponentLocked(mImageWallpaper, true, 151 false, mWallpaper, null); 152 saveSettingsLocked(mWallpaper); 153 } 154 } 155 } 156 } 157 } 158 159 final Context mContext; 160 final IWindowManager mIWindowManager; 161 final IPackageManager mIPackageManager; 162 final MyPackageMonitor mMonitor; 163 WallpaperData mLastWallpaper; 164 165 /** 166 * Name of the component used to display bitmap wallpapers from either the gallery or 167 * built-in wallpapers. 168 */ 169 final ComponentName mImageWallpaper; 170 171 SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>(); 172 173 int mCurrentUserId; 174 175 static class WallpaperData { 176 177 int userId; 178 179 File wallpaperFile; 180 181 /** 182 * Client is currently writing a new image wallpaper. 183 */ 184 boolean imageWallpaperPending; 185 186 /** 187 * Resource name if using a picture from the wallpaper gallery 188 */ 189 String name = ""; 190 191 /** 192 * The component name of the currently set live wallpaper. 193 */ 194 ComponentName wallpaperComponent; 195 196 /** 197 * The component name of the wallpaper that should be set next. 198 */ 199 ComponentName nextWallpaperComponent; 200 201 WallpaperConnection connection; 202 long lastDiedTime; 203 boolean wallpaperUpdating; 204 WallpaperObserver wallpaperObserver; 205 206 /** 207 * List of callbacks registered they should each be notified when the wallpaper is changed. 208 */ 209 private RemoteCallbackList<IWallpaperManagerCallback> callbacks 210 = new RemoteCallbackList<IWallpaperManagerCallback>(); 211 212 int width = -1; 213 int height = -1; 214 215 final Rect padding = new Rect(0, 0, 0, 0); 216 217 WallpaperData(int userId) { 218 this.userId = userId; 219 wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER); 220 } 221 } 222 223 class WallpaperConnection extends IWallpaperConnection.Stub 224 implements ServiceConnection { 225 final WallpaperInfo mInfo; 226 final Binder mToken = new Binder(); 227 IWallpaperService mService; 228 IWallpaperEngine mEngine; 229 WallpaperData mWallpaper; 230 IRemoteCallback mReply; 231 232 boolean mDimensionsChanged = false; 233 boolean mPaddingChanged = false; 234 235 public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) { 236 mInfo = info; 237 mWallpaper = wallpaper; 238 } 239 240 @Override 241 public void onServiceConnected(ComponentName name, IBinder service) { 242 synchronized (mLock) { 243 if (mWallpaper.connection == this) { 244 mService = IWallpaperService.Stub.asInterface(service); 245 attachServiceLocked(this, mWallpaper); 246 // XXX should probably do saveSettingsLocked() later 247 // when we have an engine, but I'm not sure about 248 // locking there and anyway we always need to be able to 249 // recover if there is something wrong. 250 saveSettingsLocked(mWallpaper); 251 } 252 } 253 } 254 255 @Override 256 public void onServiceDisconnected(ComponentName name) { 257 synchronized (mLock) { 258 mService = null; 259 mEngine = null; 260 if (mWallpaper.connection == this) { 261 Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent); 262 if (!mWallpaper.wallpaperUpdating 263 && mWallpaper.userId == mCurrentUserId) { 264 // There is a race condition which causes 265 // {@link #mWallpaper.wallpaperUpdating} to be false even if it is 266 // currently updating since the broadcast notifying us is async. 267 // This race is overcome by the general rule that we only reset the 268 // wallpaper if its service was shut down twice 269 // during {@link #MIN_WALLPAPER_CRASH_TIME} millis. 270 if (mWallpaper.lastDiedTime != 0 271 && mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME 272 > SystemClock.uptimeMillis()) { 273 Slog.w(TAG, "Reverting to built-in wallpaper!"); 274 clearWallpaperLocked(true, mWallpaper.userId, null); 275 } else { 276 mWallpaper.lastDiedTime = SystemClock.uptimeMillis(); 277 } 278 final String flattened = name.flattenToString(); 279 EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED, 280 flattened.substring(0, Math.min(flattened.length(), 281 MAX_WALLPAPER_COMPONENT_LOG_LENGTH))); 282 } 283 } 284 } 285 } 286 287 @Override 288 public void attachEngine(IWallpaperEngine engine) { 289 synchronized (mLock) { 290 mEngine = engine; 291 if (mDimensionsChanged) { 292 try { 293 mEngine.setDesiredSize(mWallpaper.width, mWallpaper.height); 294 } catch (RemoteException e) { 295 Slog.w(TAG, "Failed to set wallpaper dimensions", e); 296 } 297 mDimensionsChanged = false; 298 } 299 if (mPaddingChanged) { 300 try { 301 mEngine.setDisplayPadding(mWallpaper.padding); 302 } catch (RemoteException e) { 303 Slog.w(TAG, "Failed to set wallpaper padding", e); 304 } 305 mPaddingChanged = false; 306 } 307 } 308 } 309 310 @Override 311 public void engineShown(IWallpaperEngine engine) { 312 synchronized (mLock) { 313 if (mReply != null) { 314 long ident = Binder.clearCallingIdentity(); 315 try { 316 mReply.sendResult(null); 317 } catch (RemoteException e) { 318 Binder.restoreCallingIdentity(ident); 319 } 320 mReply = null; 321 } 322 } 323 } 324 325 @Override 326 public ParcelFileDescriptor setWallpaper(String name) { 327 synchronized (mLock) { 328 if (mWallpaper.connection == this) { 329 return updateWallpaperBitmapLocked(name, mWallpaper); 330 } 331 return null; 332 } 333 } 334 } 335 336 class MyPackageMonitor extends PackageMonitor { 337 @Override 338 public void onPackageUpdateFinished(String packageName, int uid) { 339 synchronized (mLock) { 340 if (mCurrentUserId != getChangingUserId()) { 341 return; 342 } 343 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 344 if (wallpaper != null) { 345 if (wallpaper.wallpaperComponent != null 346 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { 347 wallpaper.wallpaperUpdating = false; 348 ComponentName comp = wallpaper.wallpaperComponent; 349 clearWallpaperComponentLocked(wallpaper); 350 if (!bindWallpaperComponentLocked(comp, false, false, 351 wallpaper, null)) { 352 Slog.w(TAG, "Wallpaper no longer available; reverting to default"); 353 clearWallpaperLocked(false, wallpaper.userId, null); 354 } 355 } 356 } 357 } 358 } 359 360 @Override 361 public void onPackageModified(String packageName) { 362 synchronized (mLock) { 363 if (mCurrentUserId != getChangingUserId()) { 364 return; 365 } 366 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 367 if (wallpaper != null) { 368 if (wallpaper.wallpaperComponent == null 369 || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { 370 return; 371 } 372 doPackagesChangedLocked(true, wallpaper); 373 } 374 } 375 } 376 377 @Override 378 public void onPackageUpdateStarted(String packageName, int uid) { 379 synchronized (mLock) { 380 if (mCurrentUserId != getChangingUserId()) { 381 return; 382 } 383 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 384 if (wallpaper != null) { 385 if (wallpaper.wallpaperComponent != null 386 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { 387 wallpaper.wallpaperUpdating = true; 388 } 389 } 390 } 391 } 392 393 @Override 394 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { 395 synchronized (mLock) { 396 boolean changed = false; 397 if (mCurrentUserId != getChangingUserId()) { 398 return false; 399 } 400 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 401 if (wallpaper != null) { 402 boolean res = doPackagesChangedLocked(doit, wallpaper); 403 changed |= res; 404 } 405 return changed; 406 } 407 } 408 409 @Override 410 public void onSomePackagesChanged() { 411 synchronized (mLock) { 412 if (mCurrentUserId != getChangingUserId()) { 413 return; 414 } 415 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); 416 if (wallpaper != null) { 417 doPackagesChangedLocked(true, wallpaper); 418 } 419 } 420 } 421 422 boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) { 423 boolean changed = false; 424 if (wallpaper.wallpaperComponent != null) { 425 int change = isPackageDisappearing(wallpaper.wallpaperComponent 426 .getPackageName()); 427 if (change == PACKAGE_PERMANENT_CHANGE 428 || change == PACKAGE_TEMPORARY_CHANGE) { 429 changed = true; 430 if (doit) { 431 Slog.w(TAG, "Wallpaper uninstalled, removing: " 432 + wallpaper.wallpaperComponent); 433 clearWallpaperLocked(false, wallpaper.userId, null); 434 } 435 } 436 } 437 if (wallpaper.nextWallpaperComponent != null) { 438 int change = isPackageDisappearing(wallpaper.nextWallpaperComponent 439 .getPackageName()); 440 if (change == PACKAGE_PERMANENT_CHANGE 441 || change == PACKAGE_TEMPORARY_CHANGE) { 442 wallpaper.nextWallpaperComponent = null; 443 } 444 } 445 if (wallpaper.wallpaperComponent != null 446 && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) { 447 try { 448 mContext.getPackageManager().getServiceInfo( 449 wallpaper.wallpaperComponent, 0); 450 } catch (NameNotFoundException e) { 451 Slog.w(TAG, "Wallpaper component gone, removing: " 452 + wallpaper.wallpaperComponent); 453 clearWallpaperLocked(false, wallpaper.userId, null); 454 } 455 } 456 if (wallpaper.nextWallpaperComponent != null 457 && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) { 458 try { 459 mContext.getPackageManager().getServiceInfo( 460 wallpaper.nextWallpaperComponent, 0); 461 } catch (NameNotFoundException e) { 462 wallpaper.nextWallpaperComponent = null; 463 } 464 } 465 return changed; 466 } 467 } 468 469 public WallpaperManagerService(Context context) { 470 if (DEBUG) Slog.v(TAG, "WallpaperService startup"); 471 mContext = context; 472 mImageWallpaper = ComponentName.unflattenFromString( 473 context.getResources().getString(R.string.image_wallpaper_component)); 474 mIWindowManager = IWindowManager.Stub.asInterface( 475 ServiceManager.getService(Context.WINDOW_SERVICE)); 476 mIPackageManager = AppGlobals.getPackageManager(); 477 mMonitor = new MyPackageMonitor(); 478 mMonitor.register(context, null, UserHandle.ALL, true); 479 getWallpaperDir(UserHandle.USER_OWNER).mkdirs(); 480 loadSettingsLocked(UserHandle.USER_OWNER); 481 } 482 483 private static File getWallpaperDir(int userId) { 484 return Environment.getUserSystemDirectory(userId); 485 } 486 487 @Override 488 protected void finalize() throws Throwable { 489 super.finalize(); 490 for (int i = 0; i < mWallpaperMap.size(); i++) { 491 WallpaperData wallpaper = mWallpaperMap.valueAt(i); 492 wallpaper.wallpaperObserver.stopWatching(); 493 } 494 } 495 496 public void systemRunning() { 497 if (DEBUG) Slog.v(TAG, "systemReady"); 498 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_OWNER); 499 switchWallpaper(wallpaper, null); 500 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); 501 wallpaper.wallpaperObserver.startWatching(); 502 503 IntentFilter userFilter = new IntentFilter(); 504 userFilter.addAction(Intent.ACTION_USER_REMOVED); 505 userFilter.addAction(Intent.ACTION_USER_STOPPING); 506 mContext.registerReceiver(new BroadcastReceiver() { 507 @Override 508 public void onReceive(Context context, Intent intent) { 509 String action = intent.getAction(); 510 if (Intent.ACTION_USER_REMOVED.equals(action)) { 511 onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 512 UserHandle.USER_NULL)); 513 } 514 // TODO: Race condition causing problems when cleaning up on stopping a user. 515 // Comment this out for now. 516 // else if (Intent.ACTION_USER_STOPPING.equals(action)) { 517 // onStoppingUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 518 // UserHandle.USER_NULL)); 519 // } 520 } 521 }, userFilter); 522 523 try { 524 ActivityManagerNative.getDefault().registerUserSwitchObserver( 525 new IUserSwitchObserver.Stub() { 526 @Override 527 public void onUserSwitching(int newUserId, IRemoteCallback reply) { 528 switchUser(newUserId, reply); 529 } 530 531 @Override 532 public void onUserSwitchComplete(int newUserId) throws RemoteException { 533 } 534 }); 535 } catch (RemoteException e) { 536 // TODO Auto-generated catch block 537 e.printStackTrace(); 538 } 539 } 540 541 /** Called by SystemBackupAgent */ 542 public String getName() { 543 // Verify caller is the system 544 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { 545 throw new RuntimeException("getName() can only be called from the system process"); 546 } 547 synchronized (mLock) { 548 return mWallpaperMap.get(0).name; 549 } 550 } 551 552 void onStoppingUser(int userId) { 553 if (userId < 1) return; 554 synchronized (mLock) { 555 WallpaperData wallpaper = mWallpaperMap.get(userId); 556 if (wallpaper != null) { 557 if (wallpaper.wallpaperObserver != null) { 558 wallpaper.wallpaperObserver.stopWatching(); 559 wallpaper.wallpaperObserver = null; 560 } 561 mWallpaperMap.remove(userId); 562 } 563 } 564 } 565 566 void onRemoveUser(int userId) { 567 if (userId < 1) return; 568 synchronized (mLock) { 569 onStoppingUser(userId); 570 File wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER); 571 wallpaperFile.delete(); 572 File wallpaperInfoFile = new File(getWallpaperDir(userId), WALLPAPER_INFO); 573 wallpaperInfoFile.delete(); 574 } 575 } 576 577 void switchUser(int userId, IRemoteCallback reply) { 578 synchronized (mLock) { 579 mCurrentUserId = userId; 580 WallpaperData wallpaper = mWallpaperMap.get(userId); 581 if (wallpaper == null) { 582 wallpaper = new WallpaperData(userId); 583 mWallpaperMap.put(userId, wallpaper); 584 loadSettingsLocked(userId); 585 } 586 // Not started watching yet, in case wallpaper data was loaded for other reasons. 587 if (wallpaper.wallpaperObserver == null) { 588 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); 589 wallpaper.wallpaperObserver.startWatching(); 590 } 591 switchWallpaper(wallpaper, reply); 592 } 593 } 594 595 void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) { 596 synchronized (mLock) { 597 RuntimeException e = null; 598 try { 599 ComponentName cname = wallpaper.wallpaperComponent != null ? 600 wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent; 601 if (bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) { 602 return; 603 } 604 } catch (RuntimeException e1) { 605 e = e1; 606 } 607 Slog.w(TAG, "Failure starting previous wallpaper", e); 608 clearWallpaperLocked(false, wallpaper.userId, reply); 609 } 610 } 611 612 public void clearWallpaper() { 613 if (DEBUG) Slog.v(TAG, "clearWallpaper"); 614 synchronized (mLock) { 615 clearWallpaperLocked(false, UserHandle.getCallingUserId(), null); 616 } 617 } 618 619 void clearWallpaperLocked(boolean defaultFailed, int userId, IRemoteCallback reply) { 620 WallpaperData wallpaper = mWallpaperMap.get(userId); 621 File f = new File(getWallpaperDir(userId), WALLPAPER); 622 if (f.exists()) { 623 f.delete(); 624 } 625 final long ident = Binder.clearCallingIdentity(); 626 try { 627 RuntimeException e = null; 628 try { 629 wallpaper.imageWallpaperPending = false; 630 if (userId != mCurrentUserId) return; 631 if (bindWallpaperComponentLocked(defaultFailed 632 ? mImageWallpaper 633 : null, true, false, wallpaper, reply)) { 634 return; 635 } 636 } catch (IllegalArgumentException e1) { 637 e = e1; 638 } 639 640 // This can happen if the default wallpaper component doesn't 641 // exist. This should be a system configuration problem, but 642 // let's not let it crash the system and just live with no 643 // wallpaper. 644 Slog.e(TAG, "Default wallpaper component not found!", e); 645 clearWallpaperComponentLocked(wallpaper); 646 if (reply != null) { 647 try { 648 reply.sendResult(null); 649 } catch (RemoteException e1) { 650 } 651 } 652 } finally { 653 Binder.restoreCallingIdentity(ident); 654 } 655 } 656 657 public boolean hasNamedWallpaper(String name) { 658 synchronized (mLock) { 659 List<UserInfo> users; 660 long ident = Binder.clearCallingIdentity(); 661 try { 662 users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers(); 663 } finally { 664 Binder.restoreCallingIdentity(ident); 665 } 666 for (UserInfo user: users) { 667 WallpaperData wd = mWallpaperMap.get(user.id); 668 if (wd == null) { 669 // User hasn't started yet, so load her settings to peek at the wallpaper 670 loadSettingsLocked(user.id); 671 wd = mWallpaperMap.get(user.id); 672 } 673 if (wd != null && name.equals(wd.name)) { 674 return true; 675 } 676 } 677 } 678 return false; 679 } 680 681 private Point getDefaultDisplaySize() { 682 Point p = new Point(); 683 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); 684 Display d = wm.getDefaultDisplay(); 685 d.getRealSize(p); 686 return p; 687 } 688 689 public void setDimensionHints(int width, int height) throws RemoteException { 690 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); 691 synchronized (mLock) { 692 int userId = UserHandle.getCallingUserId(); 693 WallpaperData wallpaper = mWallpaperMap.get(userId); 694 if (wallpaper == null) { 695 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); 696 } 697 if (width <= 0 || height <= 0) { 698 throw new IllegalArgumentException("width and height must be > 0"); 699 } 700 // Make sure it is at least as large as the display. 701 Point displaySize = getDefaultDisplaySize(); 702 width = Math.max(width, displaySize.x); 703 height = Math.max(height, displaySize.y); 704 705 if (width != wallpaper.width || height != wallpaper.height) { 706 wallpaper.width = width; 707 wallpaper.height = height; 708 saveSettingsLocked(wallpaper); 709 if (mCurrentUserId != userId) return; // Don't change the properties now 710 if (wallpaper.connection != null) { 711 if (wallpaper.connection.mEngine != null) { 712 try { 713 wallpaper.connection.mEngine.setDesiredSize( 714 width, height); 715 } catch (RemoteException e) { 716 } 717 notifyCallbacksLocked(wallpaper); 718 } else if (wallpaper.connection.mService != null) { 719 // We've attached to the service but the engine hasn't attached back to us 720 // yet. This means it will be created with the previous dimensions, so we 721 // need to update it to the new dimensions once it attaches. 722 wallpaper.connection.mDimensionsChanged = true; 723 } 724 } 725 } 726 } 727 } 728 729 public int getWidthHint() throws RemoteException { 730 synchronized (mLock) { 731 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); 732 return wallpaper.width; 733 } 734 } 735 736 public int getHeightHint() throws RemoteException { 737 synchronized (mLock) { 738 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); 739 return wallpaper.height; 740 } 741 } 742 743 public void setDisplayPadding(Rect padding) { 744 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); 745 synchronized (mLock) { 746 int userId = UserHandle.getCallingUserId(); 747 WallpaperData wallpaper = mWallpaperMap.get(userId); 748 if (wallpaper == null) { 749 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); 750 } 751 if (padding.left < 0 || padding.top < 0 || padding.right < 0 || padding.bottom < 0) { 752 throw new IllegalArgumentException("padding must be positive: " + padding); 753 } 754 755 if (!padding.equals(wallpaper.padding)) { 756 wallpaper.padding.set(padding); 757 saveSettingsLocked(wallpaper); 758 if (mCurrentUserId != userId) return; // Don't change the properties now 759 if (wallpaper.connection != null) { 760 if (wallpaper.connection.mEngine != null) { 761 try { 762 wallpaper.connection.mEngine.setDisplayPadding(padding); 763 } catch (RemoteException e) { 764 } 765 notifyCallbacksLocked(wallpaper); 766 } else if (wallpaper.connection.mService != null) { 767 // We've attached to the service but the engine hasn't attached back to us 768 // yet. This means it will be created with the previous dimensions, so we 769 // need to update it to the new dimensions once it attaches. 770 wallpaper.connection.mPaddingChanged = true; 771 } 772 } 773 } 774 } 775 } 776 777 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, 778 Bundle outParams) { 779 synchronized (mLock) { 780 // This returns the current user's wallpaper, if called by a system service. Else it 781 // returns the wallpaper for the calling user. 782 int callingUid = Binder.getCallingUid(); 783 int wallpaperUserId = 0; 784 if (callingUid == android.os.Process.SYSTEM_UID) { 785 wallpaperUserId = mCurrentUserId; 786 } else { 787 wallpaperUserId = UserHandle.getUserId(callingUid); 788 } 789 WallpaperData wallpaper = mWallpaperMap.get(wallpaperUserId); 790 try { 791 if (outParams != null) { 792 outParams.putInt("width", wallpaper.width); 793 outParams.putInt("height", wallpaper.height); 794 } 795 wallpaper.callbacks.register(cb); 796 File f = new File(getWallpaperDir(wallpaperUserId), WALLPAPER); 797 if (!f.exists()) { 798 return null; 799 } 800 return ParcelFileDescriptor.open(f, MODE_READ_ONLY); 801 } catch (FileNotFoundException e) { 802 /* Shouldn't happen as we check to see if the file exists */ 803 Slog.w(TAG, "Error getting wallpaper", e); 804 } 805 return null; 806 } 807 } 808 809 public WallpaperInfo getWallpaperInfo() { 810 int userId = UserHandle.getCallingUserId(); 811 synchronized (mLock) { 812 WallpaperData wallpaper = mWallpaperMap.get(userId); 813 if (wallpaper.connection != null) { 814 return wallpaper.connection.mInfo; 815 } 816 return null; 817 } 818 } 819 820 public ParcelFileDescriptor setWallpaper(String name) { 821 checkPermission(android.Manifest.permission.SET_WALLPAPER); 822 synchronized (mLock) { 823 if (DEBUG) Slog.v(TAG, "setWallpaper"); 824 int userId = UserHandle.getCallingUserId(); 825 WallpaperData wallpaper = mWallpaperMap.get(userId); 826 if (wallpaper == null) { 827 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); 828 } 829 final long ident = Binder.clearCallingIdentity(); 830 try { 831 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper); 832 if (pfd != null) { 833 wallpaper.imageWallpaperPending = true; 834 } 835 return pfd; 836 } finally { 837 Binder.restoreCallingIdentity(ident); 838 } 839 } 840 } 841 842 ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper) { 843 if (name == null) name = ""; 844 try { 845 File dir = getWallpaperDir(wallpaper.userId); 846 if (!dir.exists()) { 847 dir.mkdir(); 848 FileUtils.setPermissions( 849 dir.getPath(), 850 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, 851 -1, -1); 852 } 853 File file = new File(dir, WALLPAPER); 854 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, 855 MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE); 856 if (!SELinux.restorecon(file)) { 857 return null; 858 } 859 wallpaper.name = name; 860 return fd; 861 } catch (FileNotFoundException e) { 862 Slog.w(TAG, "Error setting wallpaper", e); 863 } 864 return null; 865 } 866 867 public void setWallpaperComponent(ComponentName name) { 868 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT); 869 synchronized (mLock) { 870 if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name); 871 int userId = UserHandle.getCallingUserId(); 872 WallpaperData wallpaper = mWallpaperMap.get(userId); 873 if (wallpaper == null) { 874 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); 875 } 876 final long ident = Binder.clearCallingIdentity(); 877 try { 878 wallpaper.imageWallpaperPending = false; 879 bindWallpaperComponentLocked(name, false, true, wallpaper, null); 880 } finally { 881 Binder.restoreCallingIdentity(ident); 882 } 883 } 884 } 885 886 boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force, 887 boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) { 888 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName); 889 // Has the component changed? 890 if (!force) { 891 if (wallpaper.connection != null) { 892 if (wallpaper.wallpaperComponent == null) { 893 if (componentName == null) { 894 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default"); 895 // Still using default wallpaper. 896 return true; 897 } 898 } else if (wallpaper.wallpaperComponent.equals(componentName)) { 899 // Changing to same wallpaper. 900 if (DEBUG) Slog.v(TAG, "same wallpaper"); 901 return true; 902 } 903 } 904 } 905 906 try { 907 if (componentName == null) { 908 componentName = WallpaperManager.getDefaultWallpaperComponent(mContext); 909 if (componentName == null) { 910 // Fall back to static image wallpaper 911 componentName = mImageWallpaper; 912 //clearWallpaperComponentLocked(); 913 //return; 914 if (DEBUG) Slog.v(TAG, "Using image wallpaper"); 915 } 916 } 917 int serviceUserId = wallpaper.userId; 918 ServiceInfo si = mIPackageManager.getServiceInfo(componentName, 919 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId); 920 if (si == null) { 921 // The wallpaper component we're trying to use doesn't exist 922 Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable"); 923 return false; 924 } 925 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) { 926 String msg = "Selected service does not require " 927 + android.Manifest.permission.BIND_WALLPAPER 928 + ": " + componentName; 929 if (fromUser) { 930 throw new SecurityException(msg); 931 } 932 Slog.w(TAG, msg); 933 return false; 934 } 935 936 WallpaperInfo wi = null; 937 938 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); 939 if (componentName != null && !componentName.equals(mImageWallpaper)) { 940 // Make sure the selected service is actually a wallpaper service. 941 List<ResolveInfo> ris = 942 mIPackageManager.queryIntentServices(intent, 943 intent.resolveTypeIfNeeded(mContext.getContentResolver()), 944 PackageManager.GET_META_DATA, serviceUserId); 945 for (int i=0; i<ris.size(); i++) { 946 ServiceInfo rsi = ris.get(i).serviceInfo; 947 if (rsi.name.equals(si.name) && 948 rsi.packageName.equals(si.packageName)) { 949 try { 950 wi = new WallpaperInfo(mContext, ris.get(i)); 951 } catch (XmlPullParserException e) { 952 if (fromUser) { 953 throw new IllegalArgumentException(e); 954 } 955 Slog.w(TAG, e); 956 return false; 957 } catch (IOException e) { 958 if (fromUser) { 959 throw new IllegalArgumentException(e); 960 } 961 Slog.w(TAG, e); 962 return false; 963 } 964 break; 965 } 966 } 967 if (wi == null) { 968 String msg = "Selected service is not a wallpaper: " 969 + componentName; 970 if (fromUser) { 971 throw new SecurityException(msg); 972 } 973 Slog.w(TAG, msg); 974 return false; 975 } 976 } 977 978 // Bind the service! 979 if (DEBUG) Slog.v(TAG, "Binding to:" + componentName); 980 WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper); 981 intent.setComponent(componentName); 982 intent.putExtra(Intent.EXTRA_CLIENT_LABEL, 983 com.android.internal.R.string.wallpaper_binding_label); 984 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser( 985 mContext, 0, 986 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), 987 mContext.getText(com.android.internal.R.string.chooser_wallpaper)), 988 0, null, new UserHandle(serviceUserId))); 989 if (!mContext.bindServiceAsUser(intent, newConn, 990 Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI, 991 new UserHandle(serviceUserId))) { 992 String msg = "Unable to bind service: " 993 + componentName; 994 if (fromUser) { 995 throw new IllegalArgumentException(msg); 996 } 997 Slog.w(TAG, msg); 998 return false; 999 } 1000 if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) { 1001 detachWallpaperLocked(mLastWallpaper); 1002 } 1003 wallpaper.wallpaperComponent = componentName; 1004 wallpaper.connection = newConn; 1005 newConn.mReply = reply; 1006 try { 1007 if (wallpaper.userId == mCurrentUserId) { 1008 if (DEBUG) 1009 Slog.v(TAG, "Adding window token: " + newConn.mToken); 1010 mIWindowManager.addWindowToken(newConn.mToken, 1011 WindowManager.LayoutParams.TYPE_WALLPAPER); 1012 mLastWallpaper = wallpaper; 1013 } 1014 } catch (RemoteException e) { 1015 } 1016 } catch (RemoteException e) { 1017 String msg = "Remote exception for " + componentName + "\n" + e; 1018 if (fromUser) { 1019 throw new IllegalArgumentException(msg); 1020 } 1021 Slog.w(TAG, msg); 1022 return false; 1023 } 1024 return true; 1025 } 1026 1027 void detachWallpaperLocked(WallpaperData wallpaper) { 1028 if (wallpaper.connection != null) { 1029 if (wallpaper.connection.mReply != null) { 1030 try { 1031 wallpaper.connection.mReply.sendResult(null); 1032 } catch (RemoteException e) { 1033 } 1034 wallpaper.connection.mReply = null; 1035 } 1036 if (wallpaper.connection.mEngine != null) { 1037 try { 1038 wallpaper.connection.mEngine.destroy(); 1039 } catch (RemoteException e) { 1040 } 1041 } 1042 mContext.unbindService(wallpaper.connection); 1043 try { 1044 if (DEBUG) 1045 Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken); 1046 mIWindowManager.removeWindowToken(wallpaper.connection.mToken); 1047 } catch (RemoteException e) { 1048 } 1049 wallpaper.connection.mService = null; 1050 wallpaper.connection.mEngine = null; 1051 wallpaper.connection = null; 1052 } 1053 } 1054 1055 void clearWallpaperComponentLocked(WallpaperData wallpaper) { 1056 wallpaper.wallpaperComponent = null; 1057 detachWallpaperLocked(wallpaper); 1058 } 1059 1060 void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) { 1061 try { 1062 conn.mService.attach(conn, conn.mToken, 1063 WindowManager.LayoutParams.TYPE_WALLPAPER, false, 1064 wallpaper.width, wallpaper.height, wallpaper.padding); 1065 } catch (RemoteException e) { 1066 Slog.w(TAG, "Failed attaching wallpaper; clearing", e); 1067 if (!wallpaper.wallpaperUpdating) { 1068 bindWallpaperComponentLocked(null, false, false, wallpaper, null); 1069 } 1070 } 1071 } 1072 1073 private void notifyCallbacksLocked(WallpaperData wallpaper) { 1074 final int n = wallpaper.callbacks.beginBroadcast(); 1075 for (int i = 0; i < n; i++) { 1076 try { 1077 wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged(); 1078 } catch (RemoteException e) { 1079 1080 // The RemoteCallbackList will take care of removing 1081 // the dead object for us. 1082 } 1083 } 1084 wallpaper.callbacks.finishBroadcast(); 1085 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED); 1086 mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId)); 1087 } 1088 1089 private void checkPermission(String permission) { 1090 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) { 1091 throw new SecurityException("Access denied to process: " + Binder.getCallingPid() 1092 + ", must have permission " + permission); 1093 } 1094 } 1095 1096 private static JournaledFile makeJournaledFile(int userId) { 1097 final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath(); 1098 return new JournaledFile(new File(base), new File(base + ".tmp")); 1099 } 1100 1101 private void saveSettingsLocked(WallpaperData wallpaper) { 1102 JournaledFile journal = makeJournaledFile(wallpaper.userId); 1103 FileOutputStream stream = null; 1104 try { 1105 stream = new FileOutputStream(journal.chooseForWrite(), false); 1106 XmlSerializer out = new FastXmlSerializer(); 1107 out.setOutput(stream, "utf-8"); 1108 out.startDocument(null, true); 1109 1110 out.startTag(null, "wp"); 1111 out.attribute(null, "width", Integer.toString(wallpaper.width)); 1112 out.attribute(null, "height", Integer.toString(wallpaper.height)); 1113 if (wallpaper.padding.left != 0) { 1114 out.attribute(null, "paddingLeft", Integer.toString(wallpaper.padding.left)); 1115 } 1116 if (wallpaper.padding.top != 0) { 1117 out.attribute(null, "paddingTop", Integer.toString(wallpaper.padding.top)); 1118 } 1119 if (wallpaper.padding.right != 0) { 1120 out.attribute(null, "paddingRight", Integer.toString(wallpaper.padding.right)); 1121 } 1122 if (wallpaper.padding.bottom != 0) { 1123 out.attribute(null, "paddingBottom", Integer.toString(wallpaper.padding.bottom)); 1124 } 1125 out.attribute(null, "name", wallpaper.name); 1126 if (wallpaper.wallpaperComponent != null 1127 && !wallpaper.wallpaperComponent.equals(mImageWallpaper)) { 1128 out.attribute(null, "component", 1129 wallpaper.wallpaperComponent.flattenToShortString()); 1130 } 1131 out.endTag(null, "wp"); 1132 1133 out.endDocument(); 1134 stream.close(); 1135 journal.commit(); 1136 } catch (IOException e) { 1137 try { 1138 if (stream != null) { 1139 stream.close(); 1140 } 1141 } catch (IOException ex) { 1142 // Ignore 1143 } 1144 journal.rollback(); 1145 } 1146 } 1147 1148 private void migrateFromOld() { 1149 File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY); 1150 File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY); 1151 if (oldWallpaper.exists()) { 1152 File newWallpaper = new File(getWallpaperDir(0), WALLPAPER); 1153 oldWallpaper.renameTo(newWallpaper); 1154 } 1155 if (oldInfo.exists()) { 1156 File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO); 1157 oldInfo.renameTo(newInfo); 1158 } 1159 } 1160 1161 private int getAttributeInt(XmlPullParser parser, String name, int defValue) { 1162 String value = parser.getAttributeValue(null, name); 1163 if (value == null) { 1164 return defValue; 1165 } 1166 return Integer.parseInt(value); 1167 } 1168 1169 private void loadSettingsLocked(int userId) { 1170 if (DEBUG) Slog.v(TAG, "loadSettingsLocked"); 1171 1172 JournaledFile journal = makeJournaledFile(userId); 1173 FileInputStream stream = null; 1174 File file = journal.chooseForRead(); 1175 if (!file.exists()) { 1176 // This should only happen one time, when upgrading from a legacy system 1177 migrateFromOld(); 1178 } 1179 WallpaperData wallpaper = mWallpaperMap.get(userId); 1180 if (wallpaper == null) { 1181 wallpaper = new WallpaperData(userId); 1182 mWallpaperMap.put(userId, wallpaper); 1183 } 1184 boolean success = false; 1185 try { 1186 stream = new FileInputStream(file); 1187 XmlPullParser parser = Xml.newPullParser(); 1188 parser.setInput(stream, null); 1189 1190 int type; 1191 do { 1192 type = parser.next(); 1193 if (type == XmlPullParser.START_TAG) { 1194 String tag = parser.getName(); 1195 if ("wp".equals(tag)) { 1196 wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width")); 1197 wallpaper.height = Integer.parseInt(parser 1198 .getAttributeValue(null, "height")); 1199 wallpaper.padding.left = getAttributeInt(parser, "paddingLeft", 0); 1200 wallpaper.padding.top = getAttributeInt(parser, "paddingTop", 0); 1201 wallpaper.padding.right = getAttributeInt(parser, "paddingRight", 0); 1202 wallpaper.padding.bottom = getAttributeInt(parser, "paddingBottom", 0); 1203 wallpaper.name = parser.getAttributeValue(null, "name"); 1204 String comp = parser.getAttributeValue(null, "component"); 1205 wallpaper.nextWallpaperComponent = comp != null 1206 ? ComponentName.unflattenFromString(comp) 1207 : null; 1208 if (wallpaper.nextWallpaperComponent == null 1209 || "android".equals(wallpaper.nextWallpaperComponent 1210 .getPackageName())) { 1211 wallpaper.nextWallpaperComponent = mImageWallpaper; 1212 } 1213 1214 if (DEBUG) { 1215 Slog.v(TAG, "mWidth:" + wallpaper.width); 1216 Slog.v(TAG, "mHeight:" + wallpaper.height); 1217 Slog.v(TAG, "mName:" + wallpaper.name); 1218 Slog.v(TAG, "mNextWallpaperComponent:" 1219 + wallpaper.nextWallpaperComponent); 1220 } 1221 } 1222 } 1223 } while (type != XmlPullParser.END_DOCUMENT); 1224 success = true; 1225 } catch (FileNotFoundException e) { 1226 Slog.w(TAG, "no current wallpaper -- first boot?"); 1227 } catch (NullPointerException e) { 1228 Slog.w(TAG, "failed parsing " + file + " " + e); 1229 } catch (NumberFormatException e) { 1230 Slog.w(TAG, "failed parsing " + file + " " + e); 1231 } catch (XmlPullParserException e) { 1232 Slog.w(TAG, "failed parsing " + file + " " + e); 1233 } catch (IOException e) { 1234 Slog.w(TAG, "failed parsing " + file + " " + e); 1235 } catch (IndexOutOfBoundsException e) { 1236 Slog.w(TAG, "failed parsing " + file + " " + e); 1237 } 1238 try { 1239 if (stream != null) { 1240 stream.close(); 1241 } 1242 } catch (IOException e) { 1243 // Ignore 1244 } 1245 1246 if (!success) { 1247 wallpaper.width = -1; 1248 wallpaper.height = -1; 1249 wallpaper.padding.set(0, 0, 0, 0); 1250 wallpaper.name = ""; 1251 } 1252 1253 // We always want to have some reasonable width hint. 1254 int baseSize = getMaximumSizeDimension(); 1255 if (wallpaper.width < baseSize) { 1256 wallpaper.width = baseSize; 1257 } 1258 if (wallpaper.height < baseSize) { 1259 wallpaper.height = baseSize; 1260 } 1261 } 1262 1263 private int getMaximumSizeDimension() { 1264 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); 1265 Display d = wm.getDefaultDisplay(); 1266 return d.getMaximumSizeDimension(); 1267 } 1268 1269 // Called by SystemBackupAgent after files are restored to disk. 1270 public void settingsRestored() { 1271 // Verify caller is the system 1272 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { 1273 throw new RuntimeException("settingsRestored() can only be called from the system process"); 1274 } 1275 // TODO: If necessary, make it work for secondary users as well. This currently assumes 1276 // restores only to the primary user 1277 if (DEBUG) Slog.v(TAG, "settingsRestored"); 1278 WallpaperData wallpaper = null; 1279 boolean success = false; 1280 synchronized (mLock) { 1281 loadSettingsLocked(0); 1282 wallpaper = mWallpaperMap.get(0); 1283 if (wallpaper.nextWallpaperComponent != null 1284 && !wallpaper.nextWallpaperComponent.equals(mImageWallpaper)) { 1285 if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false, 1286 wallpaper, null)) { 1287 // No such live wallpaper or other failure; fall back to the default 1288 // live wallpaper (since the profile being restored indicated that the 1289 // user had selected a live rather than static one). 1290 bindWallpaperComponentLocked(null, false, false, wallpaper, null); 1291 } 1292 success = true; 1293 } else { 1294 // If there's a wallpaper name, we use that. If that can't be loaded, then we 1295 // use the default. 1296 if ("".equals(wallpaper.name)) { 1297 if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty"); 1298 success = true; 1299 } else { 1300 if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource"); 1301 success = restoreNamedResourceLocked(wallpaper); 1302 } 1303 if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success); 1304 if (success) { 1305 bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false, 1306 wallpaper, null); 1307 } 1308 } 1309 } 1310 1311 if (!success) { 1312 Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'"); 1313 wallpaper.name = ""; 1314 getWallpaperDir(0).delete(); 1315 } 1316 1317 synchronized (mLock) { 1318 saveSettingsLocked(wallpaper); 1319 } 1320 } 1321 1322 boolean restoreNamedResourceLocked(WallpaperData wallpaper) { 1323 if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) { 1324 String resName = wallpaper.name.substring(4); 1325 1326 String pkg = null; 1327 int colon = resName.indexOf(':'); 1328 if (colon > 0) { 1329 pkg = resName.substring(0, colon); 1330 } 1331 1332 String ident = null; 1333 int slash = resName.lastIndexOf('/'); 1334 if (slash > 0) { 1335 ident = resName.substring(slash+1); 1336 } 1337 1338 String type = null; 1339 if (colon > 0 && slash > 0 && (slash-colon) > 1) { 1340 type = resName.substring(colon+1, slash); 1341 } 1342 1343 if (pkg != null && ident != null && type != null) { 1344 int resId = -1; 1345 InputStream res = null; 1346 FileOutputStream fos = null; 1347 try { 1348 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED); 1349 Resources r = c.getResources(); 1350 resId = r.getIdentifier(resName, null, null); 1351 if (resId == 0) { 1352 Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type 1353 + " ident=" + ident); 1354 return false; 1355 } 1356 1357 res = r.openRawResource(resId); 1358 if (wallpaper.wallpaperFile.exists()) { 1359 wallpaper.wallpaperFile.delete(); 1360 } 1361 fos = new FileOutputStream(wallpaper.wallpaperFile); 1362 1363 byte[] buffer = new byte[32768]; 1364 int amt; 1365 while ((amt=res.read(buffer)) > 0) { 1366 fos.write(buffer, 0, amt); 1367 } 1368 // mWallpaperObserver will notice the close and send the change broadcast 1369 1370 Slog.v(TAG, "Restored wallpaper: " + resName); 1371 return true; 1372 } catch (NameNotFoundException e) { 1373 Slog.e(TAG, "Package name " + pkg + " not found"); 1374 } catch (Resources.NotFoundException e) { 1375 Slog.e(TAG, "Resource not found: " + resId); 1376 } catch (IOException e) { 1377 Slog.e(TAG, "IOException while restoring wallpaper ", e); 1378 } finally { 1379 if (res != null) { 1380 try { 1381 res.close(); 1382 } catch (IOException ex) {} 1383 } 1384 if (fos != null) { 1385 FileUtils.sync(fos); 1386 try { 1387 fos.close(); 1388 } catch (IOException ex) {} 1389 } 1390 } 1391 } 1392 } 1393 return false; 1394 } 1395 1396 @Override 1397 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 1398 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 1399 != PackageManager.PERMISSION_GRANTED) { 1400 1401 pw.println("Permission Denial: can't dump wallpaper service from from pid=" 1402 + Binder.getCallingPid() 1403 + ", uid=" + Binder.getCallingUid()); 1404 return; 1405 } 1406 1407 synchronized (mLock) { 1408 pw.println("Current Wallpaper Service state:"); 1409 for (int i = 0; i < mWallpaperMap.size(); i++) { 1410 WallpaperData wallpaper = mWallpaperMap.valueAt(i); 1411 pw.println(" User " + wallpaper.userId + ":"); 1412 pw.print(" mWidth="); 1413 pw.print(wallpaper.width); 1414 pw.print(" mHeight="); 1415 pw.println(wallpaper.height); 1416 pw.print(" mPadding="); pw.println(wallpaper.padding); 1417 pw.print(" mName="); pw.println(wallpaper.name); 1418 pw.print(" mWallpaperComponent="); pw.println(wallpaper.wallpaperComponent); 1419 if (wallpaper.connection != null) { 1420 WallpaperConnection conn = wallpaper.connection; 1421 pw.print(" Wallpaper connection "); 1422 pw.print(conn); 1423 pw.println(":"); 1424 if (conn.mInfo != null) { 1425 pw.print(" mInfo.component="); 1426 pw.println(conn.mInfo.getComponent()); 1427 } 1428 pw.print(" mToken="); 1429 pw.println(conn.mToken); 1430 pw.print(" mService="); 1431 pw.println(conn.mService); 1432 pw.print(" mEngine="); 1433 pw.println(conn.mEngine); 1434 pw.print(" mLastDiedTime="); 1435 pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis()); 1436 } 1437 } 1438 } 1439 } 1440} 1441