OSDN Git Service

Fixing crash in restore
authorSunny Goyal <sunnygoyal@google.com>
Fri, 5 Jun 2015 18:14:01 +0000 (11:14 -0700)
committerSunny Goyal <sunnygoyal@google.com>
Fri, 5 Jun 2015 18:36:20 +0000 (11:36 -0700)
> LauncherAppState cannot be initialized during restore, as it is not
called from a looper thread.

Bug: 21275736
Change-Id: Ifdb3f9913fa2ee63a7e1566d0c5cfc6f72b4f41e

src/com/android/launcher3/LauncherAppState.java
src/com/android/launcher3/LauncherBackupHelper.java

index afa09ec..0950f9f 100644 (file)
@@ -21,7 +21,6 @@ import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.util.Log;
 
index b40ace3..dc0bccc 100644 (file)
@@ -146,7 +146,6 @@ public class LauncherBackupHelper implements BackupHelper {
     private long mLastBackupRestoreTime;
     private boolean mBackupDataWasUpdated;
 
-    private LauncherAppState mLauncherAppState;
     private IconCache mIconCache;
     private DeviceProfieData mDeviceProfileData;
 
@@ -205,7 +204,11 @@ public class LauncherBackupHelper implements BackupHelper {
             return;
         }
 
-        lazyInitAppState(true /* noCreate */);
+        if (mDeviceProfileData == null) {
+            LauncherAppState app = LauncherAppState.getInstance();
+            mDeviceProfileData = initDeviceProfileData(app.getInvariantDeviceProfile());
+            mIconCache = app.getIconCache();
+        }
 
         Log.v(TAG, "lastBackupTime = " + in.t);
         mKeys.clear();
@@ -240,7 +243,7 @@ public class LauncherBackupHelper implements BackupHelper {
                 // Check if any metadata has changed
                 mBackupDataWasUpdated = (in.profile == null)
                         || !Arrays.equals(DeviceProfieData.toByteArray(in.profile),
-                            DeviceProfieData.toByteArray(getDeviceProfieData()))
+                            DeviceProfieData.toByteArray(mDeviceProfileData))
                         || (in.backupVersion != BACKUP_VERSION)
                         || (in.appVersion != getAppVersion());
             }
@@ -268,8 +271,7 @@ public class LauncherBackupHelper implements BackupHelper {
      * to this device.
      */
     private boolean isBackupCompatible(Journal oldState) {
-        DeviceProfieData currentProfile = getDeviceProfieData();
-
+        DeviceProfieData currentProfile = mDeviceProfileData;
         DeviceProfieData oldProfile = oldState.profile;
 
         if (oldProfile == null || oldProfile.desktopCols == 0) {
@@ -302,7 +304,14 @@ public class LauncherBackupHelper implements BackupHelper {
         if (!restoreSuccessful) {
             return;
         }
-        lazyInitAppState(false /* noCreate */);
+
+        if (mDeviceProfileData == null) {
+            // This call does not happen on a looper thread. So LauncherAppState
+            // can't be created . Instead initialize required dependencies directly.
+            InvariantDeviceProfile profile = new InvariantDeviceProfile(mContext);
+            mDeviceProfileData = initDeviceProfileData(profile);
+            mIconCache = new IconCache(mContext, profile);
+        }
 
         int dataSize = data.size();
         if (mBuffer.length < dataSize) {
@@ -379,7 +388,7 @@ public class LauncherBackupHelper implements BackupHelper {
         journal.key = mKeys.toArray(new BackupProtos.Key[mKeys.size()]);
         journal.appVersion = getAppVersion();
         journal.backupVersion = BACKUP_VERSION;
-        journal.profile = getDeviceProfieData();
+        journal.profile = mDeviceProfileData;
         return journal;
     }
 
@@ -392,31 +401,6 @@ public class LauncherBackupHelper implements BackupHelper {
         }
     }
 
-    /**
-     * @return the current device profile information.
-     */
-    private DeviceProfieData getDeviceProfieData() {
-        return mDeviceProfileData;
-    }
-
-    private void lazyInitAppState(boolean noCreate) {
-        if (mLauncherAppState != null) {
-            return;
-        }
-
-        if (noCreate) {
-            mLauncherAppState = LauncherAppState.getInstanceNoCreate();
-        } else {
-            LauncherAppState.setApplicationContext(mContext);
-            mLauncherAppState = LauncherAppState.getInstance();
-        }
-
-        mIconCache = mLauncherAppState.getIconCache();
-        InvariantDeviceProfile profile = mLauncherAppState.getInvariantDeviceProfile();
-
-        mDeviceProfileData = initDeviceProfileData(profile);
-    }
-
     private DeviceProfieData initDeviceProfileData(InvariantDeviceProfile profile) {
         DeviceProfieData data = new DeviceProfieData();
         data.desktopRows = profile.numRows;
@@ -631,7 +615,6 @@ public class LauncherBackupHelper implements BackupHelper {
     private void backupWidgets(BackupDataOutput data) throws IOException {
         // persist static widget info that hasn't been persisted yet
         final ContentResolver cr = mContext.getContentResolver();
-        final WidgetPreviewLoader previewLoader = mLauncherAppState.getWidgetCache();
         final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
         int backupWidgetCount = 0;
 
@@ -644,7 +627,6 @@ public class LauncherBackupHelper implements BackupHelper {
             while(cursor.moveToNext()) {
                 final long id = cursor.getLong(ID_INDEX);
                 final String providerName = cursor.getString(APPWIDGET_PROVIDER_INDEX);
-                final int spanX = cursor.getInt(SPANX_INDEX);
                 final ComponentName provider = ComponentName.unflattenFromString(providerName);
                 Key key = null;
                 String backupKey = null;
@@ -664,9 +646,7 @@ public class LauncherBackupHelper implements BackupHelper {
                     if (backupWidgetCount < MAX_WIDGETS_PER_PASS) {
                         if (DEBUG) Log.d(TAG, "saving widget " + backupKey);
                         UserHandleCompat user = UserHandleCompat.myUserHandle();
-                        writeRowToBackup(key,
-                                packWidget(dpi, previewLoader, mIconCache, provider, user),
-                                data);
+                        writeRowToBackup(key, packWidget(dpi, provider, user), data);
                         mKeys.add(key);
                         backupWidgetCount ++;
                     } else {
@@ -895,7 +875,7 @@ public class LauncherBackupHelper implements BackupHelper {
                 UserManagerCompat.getInstance(mContext).getSerialNumberForUser(myUserHandle);
         values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
 
-        DeviceProfieData currentProfile = getDeviceProfieData();
+        DeviceProfieData currentProfile = mDeviceProfileData;
 
         if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) {
             if (!TextUtils.isEmpty(favorite.appWidgetProvider)) {
@@ -972,8 +952,7 @@ public class LauncherBackupHelper implements BackupHelper {
     }
 
     /** Serialize a widget for persistence, including a checksum wrapper. */
-    private Widget packWidget(int dpi, WidgetPreviewLoader previewLoader, IconCache iconCache,
-            ComponentName provider, UserHandleCompat user) {
+    private Widget packWidget(int dpi, ComponentName provider, UserHandleCompat user) {
         final LauncherAppWidgetProviderInfo info =
                 LauncherModel.getProviderInfo(mContext, provider, user);
         Widget widget = new Widget();
@@ -982,7 +961,7 @@ public class LauncherBackupHelper implements BackupHelper {
         widget.configure = info.configure != null;
         if (info.icon != 0) {
             widget.icon = new Resource();
-            Drawable fullResIcon = iconCache.getFullResIcon(provider.getPackageName(), info.icon);
+            Drawable fullResIcon = mIconCache.getFullResIcon(provider.getPackageName(), info.icon);
             Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext);
             widget.icon.data = Utilities.flattenBitmap(icon);
             widget.icon.dpi = dpi;