Removing unnecessary package validation during grid migration

package validation happens during loader anyway. And using MATCH_INSTALLED
for checking cross user apps may not work all the time

Bug: 363324203
Test: atest GridSizeMigrationUtilTest
Flag: EXEMPT bug fix
Change-Id: Id36e8fdff132242705ef53c85dc6567fbde5d952
diff --git a/src/com/android/launcher3/model/GridSizeMigrationUtil.java b/src/com/android/launcher3/model/GridSizeMigrationUtil.java
index 8d2a7f9..942b97c 100644
--- a/src/com/android/launcher3/model/GridSizeMigrationUtil.java
+++ b/src/com/android/launcher3/model/GridSizeMigrationUtil.java
@@ -28,8 +28,6 @@
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.Intent;
-import android.content.pm.PackageInfo;
-import android.content.pm.PackageManager;
 import android.database.Cursor;
 import android.database.DatabaseUtils;
 import android.database.sqlite.SQLiteDatabase;
@@ -47,7 +45,6 @@
 import com.android.launcher3.Utilities;
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.model.data.ItemInfo;
-import com.android.launcher3.pm.InstallSessionHelper;
 import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
 import com.android.launcher3.util.ContentWriter;
 import com.android.launcher3.util.GridOccupancy;
@@ -100,7 +97,7 @@
     @VisibleForTesting
     public static List<DbEntry> readAllEntries(SQLiteDatabase db, String tableName,
             Context context) {
-        DbReader dbReader = new DbReader(db, tableName, context, getValidPackages(context));
+        DbReader dbReader = new DbReader(db, tableName, context);
         List<DbEntry> result = dbReader.loadAllWorkspaceEntries();
         result.addAll(dbReader.loadHotseatEntries());
         return result;
@@ -144,11 +141,10 @@
         }
         copyTable(source, TABLE_NAME, target.getWritableDatabase(), TMP_TABLE, context);
 
-        HashSet<String> validPackages = getValidPackages(context);
         long migrationStartTime = System.currentTimeMillis();
         try (SQLiteTransaction t = new SQLiteTransaction(target.getWritableDatabase())) {
-            DbReader srcReader = new DbReader(t.getDb(), TMP_TABLE, context, validPackages);
-            DbReader destReader = new DbReader(t.getDb(), TABLE_NAME, context, validPackages);
+            DbReader srcReader = new DbReader(t.getDb(), TMP_TABLE, context);
+            DbReader destReader = new DbReader(t.getDb(), TABLE_NAME, context);
 
             Point targetSize = new Point(destDeviceState.getColumns(), destDeviceState.getRows());
             migrate(target, srcReader, destReader, destDeviceState.getNumHotseat(),
@@ -348,23 +344,6 @@
                 Utilities.createDbSelectionQuery(LauncherSettings.Favorites._ID, entryIds), null);
     }
 
-    private static HashSet<String> getValidPackages(Context context) {
-        // Initialize list of valid packages. This contain all the packages which are already on
-        // the device and packages which are being installed. Any item which doesn't belong to
-        // this set is removed.
-        // Since the loader removes such items anyway, removing these items here doesn't cause
-        // any extra data loss and gives us more free space on the grid for better migration.
-        HashSet<String> validPackages = new HashSet<>();
-        for (PackageInfo info : context.getPackageManager()
-                .getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
-            validPackages.add(info.packageName);
-        }
-        InstallSessionHelper.INSTANCE.get(context)
-                .getActiveSessions().keySet()
-                .forEach(packageUserKey -> validPackages.add(packageUserKey.mPackageName));
-        return validPackages;
-    }
-
     private static void solveGridPlacement(@NonNull final DatabaseHelper helper,
             @NonNull final DbReader srcReader, @NonNull final DbReader destReader,
             final int screenId, final int trgX, final int trgY,
@@ -461,18 +440,15 @@
         private final SQLiteDatabase mDb;
         private final String mTableName;
         private final Context mContext;
-        private final Set<String> mValidPackages;
         private int mLastScreenId = -1;
 
         private final Map<Integer, ArrayList<DbEntry>> mWorkspaceEntriesByScreenId =
                 new ArrayMap<>();
 
-        public DbReader(SQLiteDatabase db, String tableName, Context context,
-                Set<String> validPackages) {
+        public DbReader(SQLiteDatabase db, String tableName, Context context) {
             mDb = db;
             mTableName = tableName;
             mContext = context;
-            mValidPackages = validPackages;
         }
 
         protected List<DbEntry> loadHotseatEntries() {
@@ -504,7 +480,6 @@
                         case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
                         case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: {
                             entry.mIntent = c.getString(indexIntent);
-                            verifyIntent(c.getString(indexIntent));
                             break;
                         }
                         case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
@@ -586,14 +561,12 @@
                         case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
                         case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: {
                             entry.mIntent = c.getString(indexIntent);
-                            verifyIntent(entry.mIntent);
                             break;
                         }
                         case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: {
                             entry.mProvider = c.getString(indexAppWidgetProvider);
                             entry.appWidgetId = c.getInt(indexAppWidgetId);
                             ComponentName cn = ComponentName.unflattenFromString(entry.mProvider);
-                            verifyPackage(cn.getPackageName());
 
                             LauncherAppWidgetProviderInfo pInfo = widgetManagerHelper
                                     .getLauncherAppWidgetInfo(entry.appWidgetId, cn);
@@ -656,7 +629,6 @@
                 try {
                     int id = c.getInt(0);
                     String intent = c.getString(1);
-                    verifyIntent(intent);
                     total++;
                     if (!entry.mFolderItems.containsKey(intent)) {
                         entry.mFolderItems.put(intent, new HashSet<>());
@@ -673,27 +645,6 @@
         private Cursor queryWorkspace(String[] columns, String where) {
             return mDb.query(mTableName, columns, where, null, null, null, null);
         }
-
-        /** Verifies if the mIntent should be restored. */
-        private void verifyIntent(String intentStr)
-                throws Exception {
-            Intent intent = Intent.parseUri(intentStr, 0);
-            if (intent.getComponent() != null) {
-                verifyPackage(intent.getComponent().getPackageName());
-            } else if (intent.getPackage() != null) {
-                // Only verify package if the component was null.
-                verifyPackage(intent.getPackage());
-            }
-        }
-
-        /** Verifies if the package should be restored */
-        private void verifyPackage(String packageName)
-                throws Exception {
-            if (!mValidPackages.contains(packageName)) {
-                // TODO(b/151468819): Handle promise app icon restoration during grid migration.
-                throw new Exception("Package not available");
-            }
-        }
     }
 
     public static class DbEntry extends ItemInfo implements Comparable<DbEntry> {
diff --git a/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationUtilTest.kt b/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationUtilTest.kt
index 761f06d..f57e8a1 100644
--- a/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationUtilTest.kt
+++ b/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationUtilTest.kt
@@ -45,7 +45,6 @@
 
     private lateinit var modelHelper: LauncherModelHelper
     private lateinit var context: Context
-    private lateinit var validPackages: Set<String>
     private lateinit var idp: InvariantDeviceProfile
     private lateinit var dbHelper: DatabaseHelper
     private lateinit var db: SQLiteDatabase
@@ -68,24 +67,10 @@
             DatabaseHelper(
                 context,
                 null,
-                UserCache.INSTANCE.get(context)::getSerialNumberForUser
+                UserCache.INSTANCE.get(context)::getSerialNumberForUser,
             ) {}
         db = dbHelper.writableDatabase
 
-        validPackages =
-            setOf(
-                testPackage1,
-                testPackage2,
-                testPackage3,
-                testPackage4,
-                testPackage5,
-                testPackage6,
-                testPackage7,
-                testPackage8,
-                testPackage9,
-                testPackage10
-            )
-
         idp = InvariantDeviceProfile.INSTANCE[context]
         val userSerial = UserCache.INSTANCE[context].getSerialNumberForUser(Process.myUserHandle())
         LauncherDbUtils.dropTable(db, TMP_TABLE)
@@ -126,8 +111,8 @@
         idp.numDatabaseHotseatIcons = 4
         idp.numColumns = 4
         idp.numRows = 4
-        val srcReader = DbReader(db, TMP_TABLE, context, validPackages)
-        val destReader = DbReader(db, TABLE_NAME, context, validPackages)
+        val srcReader = DbReader(db, TMP_TABLE, context)
+        val destReader = DbReader(db, TABLE_NAME, context)
         GridSizeMigrationUtil.migrate(
             dbHelper,
             srcReader,
@@ -135,7 +120,7 @@
             idp.numDatabaseHotseatIcons,
             Point(idp.numColumns, idp.numRows),
             DeviceGridState(context),
-            DeviceGridState(idp)
+            DeviceGridState(idp),
         )
 
         // Check hotseat items
@@ -147,9 +132,8 @@
                 null,
                 SCREEN,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
 
         assertThat(c.count).isEqualTo(idp.numDatabaseHotseatIcons)
 
@@ -178,9 +162,8 @@
                 null,
                 null,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
 
         intentIndex = c.getColumnIndex(INTENT)
         val cellXIndex = c.getColumnIndex(CELLX)
@@ -238,8 +221,8 @@
         idp.numDatabaseHotseatIcons = 4
         idp.numColumns = 4
         idp.numRows = 4
-        val readerGridA = DbReader(db, TMP_TABLE, context, validPackages)
-        val readerGridB = DbReader(db, TABLE_NAME, context, validPackages)
+        val readerGridA = DbReader(db, TMP_TABLE, context)
+        val readerGridB = DbReader(db, TABLE_NAME, context)
         // migrate from A -> B
         GridSizeMigrationUtil.migrate(
             dbHelper,
@@ -248,7 +231,7 @@
             idp.numDatabaseHotseatIcons,
             Point(idp.numColumns, idp.numRows),
             DeviceGridState(context),
-            DeviceGridState(idp)
+            DeviceGridState(idp),
         )
 
         // Check hotseat items in grid B
@@ -260,15 +243,14 @@
                 null,
                 SCREEN,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
         // Expected hotseat items in grid B
         // 2 1 3 4
         verifyHotseat(
             c,
             idp,
-            mutableListOf(testPackage2, testPackage1, testPackage3, testPackage4).toList()
+            mutableListOf(testPackage2, testPackage1, testPackage3, testPackage4).toList(),
         )
 
         // Check workspace items in grid B
@@ -280,9 +262,8 @@
                 null,
                 null,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
         var locMap = parseLocMap(c)
         // Expected items in grid B
         // _ _ _ _
@@ -306,7 +287,7 @@
             5,
             Point(5, 5),
             DeviceGridState(idp),
-            DeviceGridState(context)
+            DeviceGridState(context),
         )
         // Check hotseat items in grid A
         c =
@@ -317,15 +298,14 @@
                 null,
                 SCREEN,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
         // Expected hotseat items in grid A
         // 1 2 _ 3 4
         verifyHotseat(
             c,
             idp,
-            mutableListOf(testPackage1, testPackage2, null, testPackage3, testPackage4).toList()
+            mutableListOf(testPackage1, testPackage2, null, testPackage3, testPackage4).toList(),
         )
 
         // Check workspace items in grid A
@@ -337,9 +317,8 @@
                 null,
                 null,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
         locMap = parseLocMap(c)
         // Expected workspace items in grid A
         // _ _ _ _ _
@@ -367,7 +346,7 @@
             idp.numDatabaseHotseatIcons,
             Point(idp.numColumns, idp.numRows),
             DeviceGridState(context),
-            DeviceGridState(idp)
+            DeviceGridState(idp),
         )
 
         // Check hotseat items in grid B
@@ -379,15 +358,14 @@
                 null,
                 SCREEN,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
         // Expected hotseat items in grid B
         // 2 1 3 4
         verifyHotseat(
             c,
             idp,
-            mutableListOf(testPackage2, testPackage1, testPackage3, testPackage4).toList()
+            mutableListOf(testPackage2, testPackage1, testPackage3, testPackage4).toList(),
         )
 
         // Check workspace items in grid B
@@ -399,9 +377,8 @@
                 null,
                 null,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
         locMap = parseLocMap(c)
         // Expected workspace items in grid B
         // _ _ _ _
@@ -455,7 +432,7 @@
                     0,
                     testPackage1,
                     1,
-                    TMP_TABLE
+                    TMP_TABLE,
                 ),
                 addItem(
                     ITEM_TYPE_DEEP_SHORTCUT,
@@ -465,7 +442,7 @@
                     0,
                     testPackage2,
                     2,
-                    TMP_TABLE
+                    TMP_TABLE,
                 ),
                 addItem(
                     ITEM_TYPE_APPLICATION,
@@ -475,7 +452,7 @@
                     0,
                     testPackage3,
                     3,
-                    TMP_TABLE
+                    TMP_TABLE,
                 ),
                 addItem(
                     ITEM_TYPE_DEEP_SHORTCUT,
@@ -485,15 +462,15 @@
                     0,
                     testPackage4,
                     4,
-                    TMP_TABLE
-                )
+                    TMP_TABLE,
+                ),
             )
         val numSrcDatabaseHotseatIcons = srcHotseatItems.size
         idp.numDatabaseHotseatIcons = 6
         idp.numColumns = 4
         idp.numRows = 4
-        val srcReader = DbReader(db, TMP_TABLE, context, validPackages)
-        val destReader = DbReader(db, TABLE_NAME, context, validPackages)
+        val srcReader = DbReader(db, TMP_TABLE, context)
+        val destReader = DbReader(db, TABLE_NAME, context)
         GridSizeMigrationUtil.migrate(
             dbHelper,
             srcReader,
@@ -501,7 +478,7 @@
             idp.numDatabaseHotseatIcons,
             Point(idp.numColumns, idp.numRows),
             DeviceGridState(context),
-            DeviceGridState(idp)
+            DeviceGridState(idp),
         )
 
         // Check hotseat items
@@ -513,9 +490,8 @@
                 null,
                 SCREEN,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
 
         assertThat(c.count.toLong()).isEqualTo(numSrcDatabaseHotseatIcons.toLong())
         val screenIndex = c.getColumnIndex(SCREEN)
@@ -550,8 +526,8 @@
         idp.numDatabaseHotseatIcons = 4
         idp.numColumns = 4
         idp.numRows = 4
-        val srcReader = DbReader(db, TMP_TABLE, context, validPackages)
-        val destReader = DbReader(db, TABLE_NAME, context, validPackages)
+        val srcReader = DbReader(db, TMP_TABLE, context)
+        val destReader = DbReader(db, TABLE_NAME, context)
         GridSizeMigrationUtil.migrate(
             dbHelper,
             srcReader,
@@ -559,7 +535,7 @@
             idp.numDatabaseHotseatIcons,
             Point(idp.numColumns, idp.numRows),
             DeviceGridState(context),
-            DeviceGridState(idp)
+            DeviceGridState(idp),
         )
 
         // Check hotseat items
@@ -571,9 +547,8 @@
                 null,
                 SCREEN,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
 
         assertThat(c.count.toLong()).isEqualTo(idp.numDatabaseHotseatIcons.toLong())
         val screenIndex = c.getColumnIndex(SCREEN)
@@ -617,8 +592,8 @@
         idp.numDatabaseHotseatIcons = 4
         idp.numColumns = 5
         idp.numRows = 5
-        val srcReader = DbReader(db, TMP_TABLE, context, validPackages)
-        val destReader = DbReader(db, TABLE_NAME, context, validPackages)
+        val srcReader = DbReader(db, TMP_TABLE, context)
+        val destReader = DbReader(db, TABLE_NAME, context)
         GridSizeMigrationUtil.migrate(
             dbHelper,
             srcReader,
@@ -626,7 +601,7 @@
             idp.numDatabaseHotseatIcons,
             Point(idp.numColumns, idp.numRows),
             DeviceGridState(context),
-            DeviceGridState(idp)
+            DeviceGridState(idp),
         )
 
         // Get workspace items
@@ -638,9 +613,8 @@
                 null,
                 null,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
 
         val intentIndex = c.getColumnIndex(INTENT)
         val screenIndex = c.getColumnIndex(SCREEN)
@@ -678,8 +652,8 @@
         idp.numDatabaseHotseatIcons = 4
         idp.numColumns = 4
         idp.numRows = 4
-        val srcReader = DbReader(db, TMP_TABLE, context, validPackages)
-        val destReader = DbReader(db, TABLE_NAME, context, validPackages)
+        val srcReader = DbReader(db, TMP_TABLE, context)
+        val destReader = DbReader(db, TABLE_NAME, context)
         GridSizeMigrationUtil.migrate(
             dbHelper,
             srcReader,
@@ -687,7 +661,7 @@
             idp.numDatabaseHotseatIcons,
             Point(idp.numColumns, idp.numRows),
             DeviceGridState(context),
-            DeviceGridState(idp)
+            DeviceGridState(idp),
         )
 
         // Get workspace items
@@ -699,9 +673,8 @@
                 null,
                 null,
                 null,
-                null
-            )
-                ?: throw IllegalStateException()
+                null,
+            ) ?: throw IllegalStateException()
         val intentIndex = c.getColumnIndex(INTENT)
         val screenIndex = c.getColumnIndex(SCREEN)
 
@@ -732,7 +705,7 @@
         container: Int,
         x: Int,
         y: Int,
-        packageName: String?
+        packageName: String?,
     ): Int {
         return addItem(
             type,
@@ -742,7 +715,7 @@
             y,
             packageName,
             dbHelper.generateNewItemId(),
-            TABLE_NAME
+            TABLE_NAME,
         )
     }
 
@@ -754,7 +727,7 @@
         y: Int,
         packageName: String?,
         id: Int,
-        tableName: String
+        tableName: String,
     ): Int {
         val values = ContentValues()
         values.put(_ID, id)
diff --git a/tests/src/com/android/launcher3/model/gridmigration/ValidGridMigrationUnitTest.kt b/tests/src/com/android/launcher3/model/gridmigration/ValidGridMigrationUnitTest.kt
index 7182cf3..03d0195 100644
--- a/tests/src/com/android/launcher3/model/gridmigration/ValidGridMigrationUnitTest.kt
+++ b/tests/src/com/android/launcher3/model/gridmigration/ValidGridMigrationUnitTest.kt
@@ -114,17 +114,14 @@
         }
     }
 
-    private fun migrate(
-        srcGrid: Grid,
-        dstGrid: Grid,
-    ): List<WorkspaceItem> {
+    private fun migrate(srcGrid: Grid, dstGrid: Grid): List<WorkspaceItem> {
         val userSerial = UserCache.INSTANCE[context].getSerialNumberForUser(Process.myUserHandle())
         val dbHelper =
             DatabaseHelper(
                 context,
                 null,
                 { UserCache.INSTANCE.get(context).getSerialNumberForUser(it) },
-                {}
+                {},
             )
 
         Favorites.addTableToDb(dbHelper.writableDatabase, userSerial, false, srcGrid.tableName)
@@ -135,12 +132,12 @@
         LauncherDbUtils.SQLiteTransaction(dbHelper.writableDatabase).use {
             GridSizeMigrationUtil.migrate(
                 dbHelper,
-                GridSizeMigrationUtil.DbReader(it.db, srcGrid.tableName, context, MockSet(1)),
-                GridSizeMigrationUtil.DbReader(it.db, dstGrid.tableName, context, MockSet(1)),
+                GridSizeMigrationUtil.DbReader(it.db, srcGrid.tableName, context),
+                GridSizeMigrationUtil.DbReader(it.db, dstGrid.tableName, context),
                 dstGrid.size.x,
                 dstGrid.size,
                 srcGrid.toGridState(),
-                dstGrid.toGridState()
+                dstGrid.toGridState(),
             )
             it.commit()
         }
@@ -157,7 +154,7 @@
                 Grid(
                     tableName = Favorites.TMP_TABLE,
                     size = testCase.srcSize,
-                    items = generateItemsForTest(testCase.boards, REPEAT_AFTER)
+                    items = generateItemsForTest(testCase.boards, REPEAT_AFTER),
                 )
             val dstGrid =
                 Grid(tableName = Favorites.TABLE_NAME, size = testCase.targetSize, items = listOf())
@@ -175,13 +172,13 @@
                 Grid(
                     tableName = Favorites.TMP_TABLE,
                     size = testCase.srcSize,
-                    items = generateItemsForTest(testCase.boards, REPEAT_AFTER)
+                    items = generateItemsForTest(testCase.boards, REPEAT_AFTER),
                 )
             val dstGrid =
                 Grid(
                     tableName = Favorites.TABLE_NAME,
                     size = testCase.targetSize,
-                    items = generateItemsForTest(testCase.destBoards, REPEAT_AFTER_DST)
+                    items = generateItemsForTest(testCase.destBoards, REPEAT_AFTER_DST),
                 )
             validate(srcGrid, dstGrid, migrate(srcGrid, dstGrid))
         }
@@ -199,7 +196,7 @@
                 Grid(
                     tableName = Favorites.TMP_TABLE,
                     size = testCase.srcSize,
-                    items = generateItemsForTest(testCase.boards, REPEAT_AFTER)
+                    items = generateItemsForTest(testCase.boards, REPEAT_AFTER),
                 )
             val dstGrid =
                 Grid(tableName = Favorites.TABLE_NAME, size = testCase.targetSize, items = listOf())