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