Only marking items not already restored as restore

> Moving the restore property to LoaderCursor

Bug: 34123089
Change-Id: I1f992ef086d65e0b3cf18d3b2cf37a4f254c97d2
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index 6938611..2fc1502 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -1105,10 +1105,6 @@
             final boolean isSdCardReady = Utilities.isBootCompleted();
             final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
 
-            InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
-            int countX = profile.numColumns;
-            int countY = profile.numRows;
-
             boolean clearDb = false;
             try {
                 ImportDataTask.performImportIfPossible(context);
@@ -1159,8 +1155,6 @@
                             LauncherSettings.Favorites.SPANY);
                     final int rankIndex = c.getColumnIndexOrThrow(
                             LauncherSettings.Favorites.RANK);
-                    final int restoredIndex = c.getColumnIndexOrThrow(
-                            LauncherSettings.Favorites.RESTORED);
                     final int optionsIndex = c.getColumnIndexOrThrow(
                             LauncherSettings.Favorites.OPTIONS);
 
@@ -1207,7 +1201,6 @@
                                 continue;
                             }
 
-                            boolean restored = 0 != c.getInt(restoredIndex);
                             boolean allowMissingTarget = false;
                             switch (c.itemType) {
                             case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: {
@@ -1220,7 +1213,6 @@
                             case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
                             case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
                                 intentDescription = c.getString(intentIndex);
-                                int promiseType = c.getInt(restoredIndex);
                                 int disabledState = 0;
                                 targetPackage = null;
 
@@ -1237,17 +1229,14 @@
                                         }
 
                                         if (validComponent) {
-                                            if (restored) {
-                                                // no special handling necessary for this item
-                                                c.markRestored();
-                                                restored = false;
-                                            }
+                                            // no special handling necessary for this item
+                                            c.markRestored();
                                             if (quietMode.get(c.serialNumber)) {
                                                 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
                                             }
                                         } else if (validPkg) {
                                             intent = null;
-                                            if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
+                                            if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
                                                 // We allow auto install apps to have their intent
                                                 // updated after an install.
                                                 intent = manager.getLaunchIntentForPackage(
@@ -1267,21 +1256,20 @@
                                             } else {
                                                 // no special handling necessary for this item
                                                 c.markRestored();
-                                                restored = false;
                                             }
-                                        } else if (restored) {
+                                        } else if (c.restoreFlag != 0) {
                                             // Package is not yet available but might be
                                             // installed later.
                                             FileLog.d(TAG, "package not yet restored: " + cn);
 
-                                            if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
+                                            if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
                                                 // Restore has started once.
                                             } else if (installingPkgs.containsKey(cn.getPackageName())) {
                                                 // App restore has started. Update the flag
-                                                promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
+                                                c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
                                                 c.updater().put(
                                                         LauncherSettings.Favorites.RESTORED,
-                                                        promiseType).commit();
+                                                        c.restoreFlag).commit();
                                             } else {
                                                 c.markDeleted("Unrestored package removed: " + cn);
                                                 continue;
@@ -1308,7 +1296,6 @@
                                     } else if (cn == null) {
                                         // For shortcuts with no component, keep them as they are
                                         c.markRestored();
-                                        restored = false;
                                     }
                                 } catch (URISyntaxException e) {
                                     c.markDeleted("Invalid uri: " + intentDescription);
@@ -1318,9 +1305,9 @@
                                 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
                                         c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
 
-                                if (restored) {
+                                if (c.restoreFlag != 0) {
                                     if (c.user.equals(Process.myUserHandle())) {
-                                        info = c.getRestoredItemInfo(intent, promiseType);
+                                        info = c.getRestoredItemInfo(intent);
                                     } else {
                                         // Don't restore items for other profiles.
                                         c.markDeleted("Restore from managed profile not supported");
@@ -1385,7 +1372,7 @@
                                         info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
                                     }
 
-                                    if (restored) {
+                                    if (c.restoreFlag != 0) {
                                         ComponentName cn = info.getTargetComponent();
                                         if (cn != null) {
                                             Integer progress = installingPkgs.get(cn.getPackageName());
@@ -1413,10 +1400,8 @@
                                 folderInfo.spanY = 1;
                                 folderInfo.options = c.getInt(optionsIndex);
 
-                                if (restored) {
-                                    // no special handling required for restored folders
-                                    c.markRestored();
-                                }
+                                // no special handling required for restored folders
+                                c.markRestored();
 
                                 c.checkAndAddItem(folderInfo, sBgDataModel);
                                 break;
@@ -1433,11 +1418,10 @@
                                 final ComponentName component =
                                         ComponentName.unflattenFromString(savedProvider);
 
-                                final int restoreStatus = c.getInt(restoredIndex);
-                                final boolean isIdValid = (restoreStatus &
-                                        LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
-                                final boolean wasProviderReady = (restoreStatus &
-                                        LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
+                                final boolean isIdValid = !c.hasRestoreFlag(
+                                        LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
+                                final boolean wasProviderReady = !c.hasRestoreFlag(
+                                        LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
 
                                 if (widgetProvidersMap == null) {
                                     widgetProvidersMap = AppWidgetManagerCompat
@@ -1462,7 +1446,7 @@
                                         // The provider is available. So the widget is either
                                         // available or not available. We do not need to track
                                         // any future restore updates.
-                                        int status = restoreStatus &
+                                        int status = c.restoreFlag &
                                                 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
                                         if (!wasProviderReady) {
                                             // If provider was not previously ready, update the
@@ -1480,13 +1464,13 @@
                                     } else {
                                         Log.v(TAG, "Widget restore pending id=" + c.id
                                                 + " appWidgetId=" + appWidgetId
-                                                + " status =" + restoreStatus);
+                                                + " status =" + c.restoreFlag);
                                         appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
                                                 component);
-                                        appWidgetInfo.restoreStatus = restoreStatus;
+                                        appWidgetInfo.restoreStatus = c.restoreFlag;
                                         Integer installProgress = installingPkgs.get(component.getPackageName());
 
-                                        if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
+                                        if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
                                             // Restore has started once.
                                         } else if (installProgress != null) {
                                             // App restore has started. Update the flag
@@ -1524,7 +1508,7 @@
                                         String providerName =
                                                 appWidgetInfo.providerName.flattenToString();
                                         if (!providerName.equals(savedProvider) ||
-                                                (appWidgetInfo.restoreStatus != restoreStatus)) {
+                                                (appWidgetInfo.restoreStatus != c.restoreFlag)) {
                                             c.updater()
                                                     .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
                                                             providerName)
diff --git a/src/com/android/launcher3/model/LoaderCursor.java b/src/com/android/launcher3/model/LoaderCursor.java
index b271e6f..711be37 100644
--- a/src/com/android/launcher3/model/LoaderCursor.java
+++ b/src/com/android/launcher3/model/LoaderCursor.java
@@ -83,6 +83,7 @@
     private final int cellXIndex;
     private final int cellYIndex;
     private final int profileIdIndex;
+    private final int restoredIndex;
 
     // Properties loaded per iteration
     public long serialNumber;
@@ -90,6 +91,7 @@
     public long id;
     public long container;
     public int itemType;
+    public int restoreFlag;
 
     public LoaderCursor(Cursor c, LauncherAppState app) {
         super(c);
@@ -111,6 +113,7 @@
         cellXIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
         cellYIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
         profileIdIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.PROFILE_ID);
+        restoredIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.RESTORED);
     }
 
     @Override
@@ -123,6 +126,7 @@
             id = getLong(idIndex);
             serialNumber = getInt(profileIdIndex);
             user = allUsers.get(serialNumber);
+            restoreFlag = getInt(restoredIndex);
         }
         return result;
     }
@@ -185,7 +189,7 @@
      * Make an ShortcutInfo object for a restored application or shortcut item that points
      * to a package that is not yet installed on the system.
      */
-    public ShortcutInfo getRestoredItemInfo(Intent intent, int promiseType) {
+    public ShortcutInfo getRestoredItemInfo(Intent intent) {
         final ShortcutInfo info = new ShortcutInfo();
         info.user = user;
         info.intent = intent;
@@ -196,22 +200,22 @@
             mIconCache.getTitleAndIcon(info, false /* useLowResIcon */);
         }
 
-        if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
+        if (hasRestoreFlag(ShortcutInfo.FLAG_RESTORED_ICON)) {
             String title = getTitle();
             if (!TextUtils.isEmpty(title)) {
                 info.title = Utilities.trim(title);
             }
-        } else if  ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
+        } else if  (hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
             if (TextUtils.isEmpty(info.title)) {
                 info.title = getTitle();
             }
         } else {
-            throw new InvalidParameterException("Invalid restoreType " + promiseType);
+            throw new InvalidParameterException("Invalid restoreType " + restoreFlag);
         }
 
         info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
         info.itemType = itemType;
-        info.status = promiseType;
+        info.status = restoreFlag;
         return info;
     }
 
@@ -305,7 +309,14 @@
      * Marks the current item as restored
      */
     public void markRestored() {
-        restoredRows.add(id);
+        if (restoreFlag != 0) {
+            restoredRows.add(id);
+            restoreFlag = 0;
+        }
+    }
+
+    public boolean hasRestoreFlag(int flagMask) {
+        return (restoreFlag & flagMask) != 0;
     }
 
     public void commitRestoredItems() {