From c3dd1c380f290c174fe231c188aedcb9bfc224a0 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Mon, 23 Sep 2024 10:54:38 -0700 Subject: [PATCH] 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 --- .../model/GridSizeMigrationUtil.java | 57 +------ .../model/GridSizeMigrationUtilTest.kt | 139 +++++++----------- .../ValidGridMigrationUnitTest.kt | 21 ++- 3 files changed, 69 insertions(+), 148 deletions(-) diff --git a/src/com/android/launcher3/model/GridSizeMigrationUtil.java b/src/com/android/launcher3/model/GridSizeMigrationUtil.java index 8d2a7f92d4..942b97cb19 100644 --- a/src/com/android/launcher3/model/GridSizeMigrationUtil.java +++ b/src/com/android/launcher3/model/GridSizeMigrationUtil.java @@ -28,8 +28,6 @@ import android.content.ComponentName; 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.LauncherSettings; 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 @@ public class GridSizeMigrationUtil { @VisibleForTesting public static List readAllEntries(SQLiteDatabase db, String tableName, Context context) { - DbReader dbReader = new DbReader(db, tableName, context, getValidPackages(context)); + DbReader dbReader = new DbReader(db, tableName, context); List result = dbReader.loadAllWorkspaceEntries(); result.addAll(dbReader.loadHotseatEntries()); return result; @@ -144,11 +141,10 @@ public class GridSizeMigrationUtil { } copyTable(source, TABLE_NAME, target.getWritableDatabase(), TMP_TABLE, context); - HashSet 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 @@ public class GridSizeMigrationUtil { Utilities.createDbSelectionQuery(LauncherSettings.Favorites._ID, entryIds), null); } - private static HashSet 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 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 @@ public class GridSizeMigrationUtil { private final SQLiteDatabase mDb; private final String mTableName; private final Context mContext; - private final Set mValidPackages; private int mLastScreenId = -1; private final Map> mWorkspaceEntriesByScreenId = new ArrayMap<>(); - public DbReader(SQLiteDatabase db, String tableName, Context context, - Set validPackages) { + public DbReader(SQLiteDatabase db, String tableName, Context context) { mDb = db; mTableName = tableName; mContext = context; - mValidPackages = validPackages; } protected List loadHotseatEntries() { @@ -504,7 +480,6 @@ public class GridSizeMigrationUtil { 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 @@ public class GridSizeMigrationUtil { 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 @@ public class GridSizeMigrationUtil { 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 @@ public class GridSizeMigrationUtil { 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 { diff --git a/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationUtilTest.kt b/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationUtilTest.kt index 761f06d514..f57e8a12de 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 @@ class GridSizeMigrationUtilTest { private lateinit var modelHelper: LauncherModelHelper private lateinit var context: Context - private lateinit var validPackages: Set private lateinit var idp: InvariantDeviceProfile private lateinit var dbHelper: DatabaseHelper private lateinit var db: SQLiteDatabase @@ -68,24 +67,10 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { idp.numDatabaseHotseatIcons, Point(idp.numColumns, idp.numRows), DeviceGridState(context), - DeviceGridState(idp) + DeviceGridState(idp), ) // Check hotseat items @@ -147,9 +132,8 @@ class GridSizeMigrationUtilTest { null, SCREEN, null, - null - ) - ?: throw IllegalStateException() + null, + ) ?: throw IllegalStateException() assertThat(c.count).isEqualTo(idp.numDatabaseHotseatIcons) @@ -178,9 +162,8 @@ class GridSizeMigrationUtilTest { null, null, null, - null - ) - ?: throw IllegalStateException() + null, + ) ?: throw IllegalStateException() intentIndex = c.getColumnIndex(INTENT) val cellXIndex = c.getColumnIndex(CELLX) @@ -238,8 +221,8 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { idp.numDatabaseHotseatIcons, Point(idp.numColumns, idp.numRows), DeviceGridState(context), - DeviceGridState(idp) + DeviceGridState(idp), ) // Check hotseat items in grid B @@ -260,15 +243,14 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { null, null, null, - null - ) - ?: throw IllegalStateException() + null, + ) ?: throw IllegalStateException() var locMap = parseLocMap(c) // Expected items in grid B // _ _ _ _ @@ -306,7 +287,7 @@ class GridSizeMigrationUtilTest { 5, Point(5, 5), DeviceGridState(idp), - DeviceGridState(context) + DeviceGridState(context), ) // Check hotseat items in grid A c = @@ -317,15 +298,14 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { null, null, null, - null - ) - ?: throw IllegalStateException() + null, + ) ?: throw IllegalStateException() locMap = parseLocMap(c) // Expected workspace items in grid A // _ _ _ _ _ @@ -367,7 +346,7 @@ class GridSizeMigrationUtilTest { idp.numDatabaseHotseatIcons, Point(idp.numColumns, idp.numRows), DeviceGridState(context), - DeviceGridState(idp) + DeviceGridState(idp), ) // Check hotseat items in grid B @@ -379,15 +358,14 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { null, null, null, - null - ) - ?: throw IllegalStateException() + null, + ) ?: throw IllegalStateException() locMap = parseLocMap(c) // Expected workspace items in grid B // _ _ _ _ @@ -455,7 +432,7 @@ class GridSizeMigrationUtilTest { 0, testPackage1, 1, - TMP_TABLE + TMP_TABLE, ), addItem( ITEM_TYPE_DEEP_SHORTCUT, @@ -465,7 +442,7 @@ class GridSizeMigrationUtilTest { 0, testPackage2, 2, - TMP_TABLE + TMP_TABLE, ), addItem( ITEM_TYPE_APPLICATION, @@ -475,7 +452,7 @@ class GridSizeMigrationUtilTest { 0, testPackage3, 3, - TMP_TABLE + TMP_TABLE, ), addItem( ITEM_TYPE_DEEP_SHORTCUT, @@ -485,15 +462,15 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { idp.numDatabaseHotseatIcons, Point(idp.numColumns, idp.numRows), DeviceGridState(context), - DeviceGridState(idp) + DeviceGridState(idp), ) // Check hotseat items @@ -513,9 +490,8 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { idp.numDatabaseHotseatIcons, Point(idp.numColumns, idp.numRows), DeviceGridState(context), - DeviceGridState(idp) + DeviceGridState(idp), ) // Check hotseat items @@ -571,9 +547,8 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { idp.numDatabaseHotseatIcons, Point(idp.numColumns, idp.numRows), DeviceGridState(context), - DeviceGridState(idp) + DeviceGridState(idp), ) // Get workspace items @@ -638,9 +613,8 @@ class GridSizeMigrationUtilTest { null, null, null, - null - ) - ?: throw IllegalStateException() + null, + ) ?: throw IllegalStateException() val intentIndex = c.getColumnIndex(INTENT) val screenIndex = c.getColumnIndex(SCREEN) @@ -678,8 +652,8 @@ class GridSizeMigrationUtilTest { 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 @@ class GridSizeMigrationUtilTest { idp.numDatabaseHotseatIcons, Point(idp.numColumns, idp.numRows), DeviceGridState(context), - DeviceGridState(idp) + DeviceGridState(idp), ) // Get workspace items @@ -699,9 +673,8 @@ class GridSizeMigrationUtilTest { null, null, null, - null - ) - ?: throw IllegalStateException() + null, + ) ?: throw IllegalStateException() val intentIndex = c.getColumnIndex(INTENT) val screenIndex = c.getColumnIndex(SCREEN) @@ -732,7 +705,7 @@ class GridSizeMigrationUtilTest { container: Int, x: Int, y: Int, - packageName: String? + packageName: String?, ): Int { return addItem( type, @@ -742,7 +715,7 @@ class GridSizeMigrationUtilTest { y, packageName, dbHelper.generateNewItemId(), - TABLE_NAME + TABLE_NAME, ) } @@ -754,7 +727,7 @@ class GridSizeMigrationUtilTest { 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 7182cf33af..03d01955ca 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 @@ class ValidGridMigrationUnitTest { } } - private fun migrate( - srcGrid: Grid, - dstGrid: Grid, - ): List { + private fun migrate(srcGrid: Grid, dstGrid: Grid): List { 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 @@ class ValidGridMigrationUnitTest { 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 @@ class ValidGridMigrationUnitTest { 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 @@ class ValidGridMigrationUnitTest { 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 @@ class ValidGridMigrationUnitTest { 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())