Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f84b6ede authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Removing unnecessary package validation during grid migration" into main

parents d3460af4 c3dd1c38
Loading
Loading
Loading
Loading
+4 −53
Original line number Diff line number Diff line
@@ -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<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 @@ public class GridSizeMigrationUtil {
        }
        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 @@ public class GridSizeMigrationUtil {
                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 @@ public class GridSizeMigrationUtil {
        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 @@ 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<DbEntry> {
+56 −83
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ class GridSizeMigrationUtilTest {

    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 @@ 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)
+9 −12
Original line number Diff line number Diff line
@@ -114,17 +114,14 @@ class ValidGridMigrationUnitTest {
        }
    }

    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 @@ 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())