OSDN Git Service

Resetting launcherProvider data if restore set was empty
authorSunny Goyal <sunnygoyal@google.com>
Sat, 27 Sep 2014 05:09:29 +0000 (22:09 -0700)
committerSunny Goyal <sunnygoyal@google.com>
Mon, 29 Sep 2014 00:02:03 +0000 (17:02 -0700)
Bug: 17332300
Change-Id: I6d2187cd1b1fa7a53e49b96eb109263f6b74b258

src/com/android/launcher3/LauncherBackupAgentHelper.java
src/com/android/launcher3/LauncherBackupHelper.java
src/com/android/launcher3/LauncherProvider.java

index 7dd8cde..c20c693 100644 (file)
 package com.android.launcher3;
 
 import android.app.backup.BackupAgentHelper;
+import android.app.backup.BackupDataInput;
 import android.app.backup.BackupManager;
-import android.app.backup.SharedPreferencesBackupHelper;
 import android.content.Context;
-import android.content.SharedPreferences;
+import android.database.Cursor;
+import android.os.ParcelFileDescriptor;
 import android.provider.Settings;
 import android.util.Log;
 
+import java.io.IOException;
+
 public class LauncherBackupAgentHelper extends BackupAgentHelper {
 
     private static final String TAG = "LauncherBackupAgentHelper";
@@ -54,7 +57,7 @@ public class LauncherBackupAgentHelper extends BackupAgentHelper {
         // modifies the file outside the normal codepaths, so it looks like another
         // process.  This forces a reload of the file, in case this process persists.
         String spKey = LauncherAppState.getSharedPreferencesKey();
-        SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS);
+        getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS);
         super.onDestroy();
     }
 
@@ -71,4 +74,21 @@ public class LauncherBackupAgentHelper extends BackupAgentHelper {
         addHelper(LauncherBackupHelper.LAUNCHER_PREFIX,
                 new LauncherBackupHelper(this, restoreEnabled));
     }
+
+    @Override
+    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
+            throws IOException {
+        super.onRestore(data, appVersionCode, newState);
+
+        // If no favorite was migrated, clear the data and start fresh.
+        final Cursor c = getContentResolver().query(
+                LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, null, null, null, null);
+        boolean hasData = c.moveToNext();
+        c.close();
+
+        if (!hasData) {
+            if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
+            LauncherAppState.getLauncherProvider().createEmptyDB();
+        }
+    }
 }
index 911a41d..201f3e9 100644 (file)
@@ -148,11 +148,12 @@ public class LauncherBackupHelper implements BackupHelper {
 
     private HashMap<ComponentName, AppWidgetProviderInfo> mWidgetMap;
 
-    private ArrayList<Key> mKeys;
+    private final ArrayList<Key> mKeys;
 
     public LauncherBackupHelper(Context context, boolean restoreEnabled) {
         mContext = context;
         mRestoreEnabled = restoreEnabled;
+        mKeys = new ArrayList<Key>();
     }
 
     private void dataChanged() {
@@ -218,9 +219,6 @@ public class LauncherBackupHelper implements BackupHelper {
     @Override
     public void restoreEntity(BackupDataInputStream data) {
         if (VERBOSE) Log.v(TAG, "restoreEntity");
-        if (mKeys == null) {
-            mKeys = new ArrayList<Key>();
-        }
         byte[] buffer = new byte[512];
             String backupKey = data.getKey();
             int dataSize = data.size();
index 30086ad..c0e6487 100644 (file)
@@ -308,6 +308,13 @@ public class LauncherProvider extends ContentProvider {
     }
 
     /**
+     * Clears all the data for a fresh start.
+     */
+    synchronized public void createEmptyDB() {
+        mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
+    }
+
+    /**
      * Loads the default workspace based on the following priority scheme:
      *   1) From a package provided by play store
      *   2) From a partner configuration APK, already in the system image
@@ -908,7 +915,14 @@ public class LauncherProvider extends ContentProvider {
             // This shouldn't happen -- throw our hands up in the air and start over.
             Log.w(TAG, "Database version downgrade from: " + oldVersion + " to " + newVersion +
                     ". Wiping databse.");
+            createEmptyDB(db);
+        }
+
 
+        /**
+         * Clears all the data for a fresh start.
+         */
+        public void createEmptyDB(SQLiteDatabase db) {
             db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
             db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
             onCreate(db);