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