This time for sure: make wallpapers work

Fix http://b/issue?id=2262556. Make restoring
work as well as switching between live and 
non-live wallpapers. Also support live wallpaper
as default. 
diff --git a/services/java/com/android/server/WallpaperManagerService.java b/services/java/com/android/server/WallpaperManagerService.java
index a24555a2..0933677 100644
--- a/services/java/com/android/server/WallpaperManagerService.java
+++ b/services/java/com/android/server/WallpaperManagerService.java
@@ -125,13 +125,29 @@
 
     int mWidth = -1;
     int mHeight = -1;
+    
+    /**
+     * Resource name if using a picture from the wallpaper gallery
+     */
     String mName = "";
     
     /**
-     * The component name of the currently set live wallpaper. This will be null if the
-     * wallpaper uses the built in ImageWallpaper component to display a bitmap.
+     * The component name of the currently set live wallpaper.
      */
     ComponentName mWallpaperComponent;
+    
+    /**
+     * The component name of the wallpaper that should be set next.
+     */
+    ComponentName mNextWallpaperComponent;
+    
+    /**
+     * Name of the component used to display bitmap wallpapers from either the gallery or
+     * built-in wallpapers.
+     */
+    ComponentName mImageWallpaperComponent = new ComponentName("android", 
+            ImageWallpaper.class.getName());
+    
     WallpaperConnection mWallpaperConnection;
     long mLastDiedTime;
     
@@ -169,7 +185,7 @@
                     if ((mLastDiedTime+MIN_WALLPAPER_CRASH_TIME)
                             < SystemClock.uptimeMillis()) {
                         Log.w(TAG, "Reverting to built-in wallpaper!");
-                        bindWallpaperComponentLocked(null, false);
+                        bindWallpaperComponentLocked(null);
                     }
                 }
             }
@@ -190,7 +206,7 @@
     }
     
     public WallpaperManagerService(Context context) {
-        if (DEBUG) Log.d(TAG, "WallpaperService startup");
+        if (DEBUG) Log.v(TAG, "WallpaperService startup");
         mContext = context;
         mIWindowManager = IWindowManager.Stub.asInterface(
                 ServiceManager.getService(Context.WINDOW_SERVICE));
@@ -206,13 +222,14 @@
     }
     
     public void systemReady() {
+        if (DEBUG) Log.v(TAG, "systemReady");
         synchronized (mLock) {
             try {
-                bindWallpaperComponentLocked(mWallpaperComponent, false);
+                bindWallpaperComponentLocked(mNextWallpaperComponent);
             } catch (RuntimeException e) {
                 Log.w(TAG, "Failure starting previous wallpaper", e);
                 try {
-                    bindWallpaperComponentLocked(null, false);
+                    bindWallpaperComponentLocked(null);
                 } catch (RuntimeException e2) {
                     Log.w(TAG, "Failure starting default wallpaper", e2);
                     clearWallpaperComponentLocked();
@@ -222,6 +239,7 @@
     }
     
     public void clearWallpaper() {
+        if (DEBUG) Log.v(TAG, "clearWallpaper");
         synchronized (mLock) {
             File f = WALLPAPER_FILE;
             if (f.exists()) {
@@ -229,7 +247,7 @@
             }
             final long ident = Binder.clearCallingIdentity();
             try {
-                bindWallpaperComponentLocked(null, false);
+                bindWallpaperComponentLocked(null);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
@@ -306,6 +324,8 @@
     }
     
     public ParcelFileDescriptor setWallpaper(String name) {
+        if (DEBUG) Log.v(TAG, "setWallpaper");
+        
         checkPermission(android.Manifest.permission.SET_WALLPAPER);
         synchronized (mLock) {
             final long ident = Binder.clearCallingIdentity();
@@ -313,7 +333,7 @@
                 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
                 if (pfd != null) {
                     // Bind the wallpaper to an ImageWallpaper
-                    bindWallpaperComponentLocked(null, true);
+                    bindWallpaperComponentLocked(mImageWallpaperComponent);
                     saveSettingsLocked();
                 }
                 return pfd;
@@ -337,62 +357,65 @@
     }
 
     public void setWallpaperComponent(ComponentName name) {
+        if (DEBUG) Log.v(TAG, "setWallpaperComponent name=" + name);
         checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
         synchronized (mLock) {
             final long ident = Binder.clearCallingIdentity();
             try {
-                bindWallpaperComponentLocked(name, false);
+                bindWallpaperComponentLocked(name);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
         }
     }
     
-    void bindWallpaperComponentLocked(ComponentName componentName, boolean isBitmap) {
+    void bindWallpaperComponentLocked(ComponentName componentName) {
+        if (DEBUG) Log.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
+        
         // Has the component changed?
         if (mWallpaperConnection != null) {
             if (mWallpaperComponent == null) {
                 if (componentName == null) {
+                    if (DEBUG) Log.v(TAG, "bindWallpaperComponentLocked: still using default");
                     // Still using default wallpaper.
                     return;
                 }
             } else if (mWallpaperComponent.equals(componentName)) {
                 // Changing to same wallpaper.
+                if (DEBUG) Log.v(TAG, "same wallpaper");
                 return;
             }
         }
         
         try {
-            ComponentName realComponentName = componentName;
-            if (realComponentName == null) {
+            if (componentName == null) {
                 String defaultComponent = 
                     mContext.getString(com.android.internal.R.string.default_wallpaper_component);
-                if (defaultComponent != null && !isBitmap) {
+                if (defaultComponent != null) {
                     // See if there is a default wallpaper component specified
-                    // Only look for this if the wallpaper is not being set to a bitmap
-                    realComponentName = ComponentName.unflattenFromString(defaultComponent);
-                    componentName = realComponentName;
+                    componentName = ComponentName.unflattenFromString(defaultComponent);
+                    if (DEBUG) Log.v(TAG, "Use default component walpaper:" + componentName);
                 }
-                if (realComponentName == null) {
+                if (componentName == null) {
                     // Fall back to static image wallpaper
-                    realComponentName = new ComponentName("android", 
-                            ImageWallpaper.class.getName());
+                    componentName = mImageWallpaperComponent;
                     //clearWallpaperComponentLocked();
                     //return;
+                    if (DEBUG) Log.v(TAG, "Using image wallpaper");
                 }
             }
-            ServiceInfo si = mContext.getPackageManager().getServiceInfo(realComponentName,
+            ServiceInfo si = mContext.getPackageManager().getServiceInfo(componentName,
                     PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS);
             if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
                 throw new SecurityException("Selected service does not require "
                         + android.Manifest.permission.BIND_WALLPAPER
-                        + ": " + realComponentName);
+                        + ": " + componentName);
             }
             
             WallpaperInfo wi = null;
             
             Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
-            if (componentName != null) {
+            if (componentName != null && !componentName.equals(mImageWallpaperComponent)) {
                 // Make sure the selected service is actually a wallpaper service.
                 List<ResolveInfo> ris = mContext.getPackageManager()
                         .queryIntentServices(intent, PackageManager.GET_META_DATA);
@@ -412,13 +435,14 @@
                 }
                 if (wi == null) {
                     throw new SecurityException("Selected service is not a wallpaper: "
-                            + realComponentName);
+                            + componentName);
                 }
             }
             
             // Bind the service!
+            if (DEBUG) Log.v(TAG, "Binding to:" + componentName);
             WallpaperConnection newConn = new WallpaperConnection(wi);
-            intent.setComponent(realComponentName);
+            intent.setComponent(componentName);
             intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
                     com.android.internal.R.string.wallpaper_binding_label);
             intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
@@ -475,7 +499,7 @@
                     mWidth, mHeight);
         } catch (RemoteException e) {
             Log.w(TAG, "Failed attaching wallpaper; clearing", e);
-            bindWallpaperComponentLocked(null, false);
+            bindWallpaperComponentLocked(null);
         }
     }
     
@@ -542,6 +566,8 @@
     }
 
     private void loadSettingsLocked() {
+        if (DEBUG) Log.v(TAG, "loadSettingsLocked");
+        
         JournaledFile journal = makeJournaledFile();
         FileInputStream stream = null;
         File file = journal.chooseForRead();
@@ -561,9 +587,16 @@
                         mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
                         mName = parser.getAttributeValue(null, "name");
                         String comp = parser.getAttributeValue(null, "component");
-                        mWallpaperComponent = comp != null
+                        mNextWallpaperComponent = comp != null
                                 ? ComponentName.unflattenFromString(comp)
                                 : null;
+                          
+                        if (DEBUG) {
+                            Log.v(TAG, "mWidth:" + mWidth);
+                            Log.v(TAG, "mHeight:" + mHeight);
+                            Log.v(TAG, "mName:" + mName);
+                            Log.v(TAG, "mNextWallpaperComponent:" + mNextWallpaperComponent);
+                        }
                     }
                 }
             } while (type != XmlPullParser.END_DOCUMENT);
@@ -595,15 +628,30 @@
     }
 
     void settingsRestored() {
+        if (DEBUG) Log.v(TAG, "settingsRestored");
+        
         boolean success = false;
         synchronized (mLock) {
             loadSettingsLocked();
-            // If there's a wallpaper name, we use that.  If that can't be loaded, then we
-            // use the default.
-            if ("".equals(mName)) {
+            if (mNextWallpaperComponent != null && 
+                    !mNextWallpaperComponent.equals(mImageWallpaperComponent)) {
+                // We can't restore live wallpapers, so just go with the default
+                bindWallpaperComponentLocked(null);
                 success = true;
             } else {
-                success = restoreNamedResourceLocked();
+                // If there's a wallpaper name, we use that.  If that can't be loaded, then we
+                // use the default.
+                if ("".equals(mName)) {
+                    if (DEBUG) Log.v(TAG, "settingsRestored: name is empty");
+                    success = true;
+                } else {
+                    if (DEBUG) Log.v(TAG, "settingsRestored: attempting to restore named resource");
+                    success = restoreNamedResourceLocked();
+                }
+                if (DEBUG) Log.v(TAG, "settingsRestored: success=" + success);
+                if (success) {
+                    bindWallpaperComponentLocked(mImageWallpaperComponent);
+                }
             }
         }
 
@@ -660,7 +708,7 @@
                     }
                     // mWallpaperObserver will notice the close and send the change broadcast
 
-                    Log.d(TAG, "Restored wallpaper: " + resName);
+                    Log.v(TAG, "Restored wallpaper: " + resName);
                     return true;
                 } catch (NameNotFoundException e) {
                     Log.e(TAG, "Package name " + pkg + " not found");