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

Commit 02cb0607 authored by Tracy Zhou's avatar Tracy Zhou
Browse files

Follow up change to GridBackupTable per V2 backup / restore (multi-db)

Removed the code to handle table copying across dbs. We don't really need this since per ag/10298497, we can just copy db files directly.

Bug: 149236106
Test: N/A
Change-Id: I90bd7b6779fcaa841c71f5d0f27ec91907f926fc
parent b3ea58d5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -134,7 +134,7 @@ public class BackupRestoreTest {
                { APP_ICON, SHORTCUT, SHORTCUT, NO__ICON},
            }}, 2, OLD_WORK_PROFILE_ID);
        // simulates the creation of backup upon restore
        new GridBackupTable(RuntimeEnvironment.application, mDb, mDb, mIdp.numHotseatIcons,
        new GridBackupTable(RuntimeEnvironment.application, mDb, mIdp.numHotseatIcons,
                mIdp.numColumns, mIdp.numRows).doBackup(
                        MY_OLD_PROFILE_ID, GridBackupTable.OPTION_REQUIRES_SANITIZATION);
        // reset favorites table
+6 −6
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ public class GridBackupTableTest {

    @Test
    public void backupTableCreated() {
        GridBackupTable backupTable = new GridBackupTable(mContext, mDb, mDb, 4, 4, 4);
        GridBackupTable backupTable = new GridBackupTable(mContext, mDb, 4, 4, 4);
        assertFalse(backupTable.backupOrRestoreAsNeeded());
        Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);

@@ -75,14 +75,14 @@ public class GridBackupTableTest {

    @Test
    public void backupTableRestored() {
        assertFalse(new GridBackupTable(mContext, mDb, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
        assertFalse(new GridBackupTable(mContext, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
        Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);

        // Delete entries
        mDb.delete(TABLE_NAME, null, null);
        assertEquals(0, queryNumEntries(mDb, TABLE_NAME));

        GridBackupTable backupTable = new GridBackupTable(mContext, mDb, mDb, 3, 3, 3);
        GridBackupTable backupTable = new GridBackupTable(mContext, mDb, 3, 3, 3);
        assertTrue(backupTable.backupOrRestoreAsNeeded());

        // Items have been restored
@@ -96,7 +96,7 @@ public class GridBackupTableTest {

    @Test
    public void backupTableRemovedOnAdd() {
        assertFalse(new GridBackupTable(mContext, mDb, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
        assertFalse(new GridBackupTable(mContext, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
        Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);

        assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
@@ -107,7 +107,7 @@ public class GridBackupTableTest {

    @Test
    public void backupTableRemovedOnDelete() {
        assertFalse(new GridBackupTable(mContext, mDb, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
        assertFalse(new GridBackupTable(mContext, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
        Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);

        assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
@@ -118,7 +118,7 @@ public class GridBackupTableTest {

    @Test
    public void backupTableRetainedOnUpdate() {
        assertFalse(new GridBackupTable(mContext, mDb, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
        assertFalse(new GridBackupTable(mContext, mDb, 4, 4, 4).backupOrRestoreAsNeeded());
        Settings.call(mContext.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);

        assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
+17 −32
Original line number Diff line number Diff line
@@ -33,8 +33,6 @@ import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.LauncherSettings.Settings;
import com.android.launcher3.pm.UserCache;

import java.util.Objects;

/**
 * Helper class to backup and restore Favorites table into a separate table
 * within the same data base.
@@ -63,8 +61,7 @@ public class GridBackupTable {
    private static final int STATE_SANITIZED = 2;

    private final Context mContext;
    private final SQLiteDatabase mFavoritesDb;
    private final SQLiteDatabase mBackupDb;
    private final SQLiteDatabase mDb;

    private final int mOldHotseatSize;
    private final int mOldGridX;
@@ -77,11 +74,10 @@ public class GridBackupTable {
    @IntDef({STATE_NOT_FOUND, STATE_RAW, STATE_SANITIZED})
    private @interface BackupState { }

    public GridBackupTable(Context context, SQLiteDatabase favoritesDb, SQLiteDatabase backupDb,
            int hotseatSize, int gridX, int gridY) {
    public GridBackupTable(Context context, SQLiteDatabase db, int hotseatSize, int gridX,
            int gridY) {
        mContext = context;
        mFavoritesDb = favoritesDb;
        mBackupDb = backupDb;
        mDb = db;

        mOldHotseatSize = hotseatSize;
        mOldGridX = gridX;
@@ -94,7 +90,7 @@ public class GridBackupTable {
     */
    public boolean backupOrRestoreAsNeeded() {
        // Check if backup table exists
        if (!tableExists(mBackupDb, BACKUP_TABLE_NAME)) {
        if (!tableExists(mDb, BACKUP_TABLE_NAME)) {
            if (Settings.call(mContext.getContentResolver(), Settings.METHOD_WAS_EMPTY_DB_CREATED)
                    .getBoolean(Settings.EXTRA_VALUE, false)) {
                // No need to copy if empty DB was created.
@@ -109,7 +105,7 @@ public class GridBackupTable {
        }
        long userSerial = UserCache.INSTANCE.get(mContext).getSerialNumberForUser(
                Process.myUserHandle());
        copyTable(mBackupDb, BACKUP_TABLE_NAME, mFavoritesDb, Favorites.TABLE_NAME, userSerial);
        copyTable(mDb, BACKUP_TABLE_NAME, Favorites.TABLE_NAME, userSerial);
        Log.d(TAG, "Backup table found");
        return true;
    }
@@ -122,37 +118,28 @@ public class GridBackupTable {
    /**
     * Copy valid grid entries from one table to another.
     */
    private static void copyTable(SQLiteDatabase fromDb, String fromTable, SQLiteDatabase toDb,
            String toTable, long userSerial) {
        dropTable(toDb, toTable);
        Favorites.addTableToDb(toDb, userSerial, false, toTable);
        if (fromDb != toDb) {
            toDb.execSQL("ATTACH DATABASE '" + fromDb.getPath() + "' AS from_db");
            toDb.execSQL(
                    "INSERT INTO " + toTable + " SELECT * FROM from_db." + fromTable
                            + " where _id > " + ID_PROPERTY);
        } else {
            toDb.execSQL("INSERT INTO " + toTable + " SELECT * FROM " + fromTable + " where _id > "
                    + ID_PROPERTY);
        }
    private static void copyTable(SQLiteDatabase db, String from, String to, long userSerial) {
        dropTable(db, to);
        Favorites.addTableToDb(db, userSerial, false, to);
        db.execSQL("INSERT INTO " + to + " SELECT * FROM " + from + " where _id > " + ID_PROPERTY);
    }

    private void encodeDBProperties(int options) {
        ContentValues values = new ContentValues();
        values.put(Favorites._ID, ID_PROPERTY);
        values.put(KEY_DB_VERSION, mFavoritesDb.getVersion());
        values.put(KEY_DB_VERSION, mDb.getVersion());
        values.put(KEY_GRID_X_SIZE, mOldGridX);
        values.put(KEY_GRID_Y_SIZE, mOldGridY);
        values.put(KEY_HOTSEAT_SIZE, mOldHotseatSize);
        values.put(Favorites.OPTIONS, options);
        mBackupDb.insert(BACKUP_TABLE_NAME, null, values);
        mDb.insert(BACKUP_TABLE_NAME, null, values);
    }

    /**
     * Load DB properties from grid backup table.
     */
    public @BackupState int loadDBProperties() {
        try (Cursor c = mBackupDb.query(BACKUP_TABLE_NAME, new String[] {
        try (Cursor c = mDb.query(BACKUP_TABLE_NAME, new String[] {
                KEY_DB_VERSION,     // 0
                KEY_GRID_X_SIZE,    // 1
                KEY_GRID_Y_SIZE,    // 2
@@ -163,7 +150,7 @@ public class GridBackupTable {
                Log.e(TAG, "Meta data not found in backup table");
                return STATE_NOT_FOUND;
            }
            if (!validateDBVersion(mBackupDb.getVersion(), c.getInt(0))) {
            if (!validateDBVersion(mDb.getVersion(), c.getInt(0))) {
                return STATE_NOT_FOUND;
            }

@@ -179,7 +166,7 @@ public class GridBackupTable {
     * Restore workspace from raw backup if available.
     */
    public boolean restoreFromRawBackupIfAvailable(long oldProfileId) {
        if (!tableExists(mBackupDb, Favorites.BACKUP_TABLE_NAME)
        if (!tableExists(mDb, Favorites.BACKUP_TABLE_NAME)
                || loadDBProperties() != STATE_RAW
                || mOldHotseatSize != mRestoredHotseatSize
                || mOldGridX != mRestoredGridX
@@ -187,8 +174,7 @@ public class GridBackupTable {
            // skip restore if dimensions in backup table differs from current setup.
            return false;
        }
        copyTable(mBackupDb, Favorites.BACKUP_TABLE_NAME, mFavoritesDb, Favorites.TABLE_NAME,
                oldProfileId);
        copyTable(mDb, Favorites.BACKUP_TABLE_NAME, Favorites.TABLE_NAME, oldProfileId);
        Log.d(TAG, "Backup restored");
        return true;
    }
@@ -197,8 +183,7 @@ public class GridBackupTable {
     * Performs a backup on the workspace layout.
     */
    public void doBackup(long profileId, int options) {
        copyTable(mFavoritesDb, Favorites.TABLE_NAME, mBackupDb, Favorites.BACKUP_TABLE_NAME,
                profileId);
        copyTable(mDb, Favorites.TABLE_NAME, Favorites.BACKUP_TABLE_NAME, profileId);
        encodeDBProperties(options);
    }

+1 −1
Original line number Diff line number Diff line
@@ -909,7 +909,7 @@ public class GridSizeMigrationTask {
            boolean dbChanged = false;

            GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb(),
                    transaction.getDb(), srcHotseatCount, sourceSize.x, sourceSize.y);
                    srcHotseatCount, sourceSize.x, sourceSize.y);
            if (backupTable.backupOrRestoreAsNeeded()) {
                dbChanged = true;
                srcHotseatCount = backupTable.getRestoreHotseatAndGridSize(sourceSize);
+3 −3
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ public class RestoreDbTask {
    private void backupWorkspace(Context context, SQLiteDatabase db) throws Exception {
        InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
        // TODO(pinyaoting): Support backing up workspace with multiple grid options.
        new GridBackupTable(context, db, db, idp.numHotseatIcons, idp.numColumns, idp.numRows)
        new GridBackupTable(context, db, idp.numHotseatIcons, idp.numColumns, idp.numRows)
                .doBackup(getDefaultProfileId(db), GridBackupTable.OPTION_REQUIRES_SANITIZATION);
    }

@@ -117,8 +117,8 @@ public class RestoreDbTask {
            throws Exception {
        // TODO(pinyaoting): Support restoring workspace with multiple grid options.
        final InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
        GridBackupTable backupTable = new GridBackupTable(context, db, db,
                idp.numHotseatIcons, idp.numColumns, idp.numRows);
        GridBackupTable backupTable = new GridBackupTable(context, db, idp.numHotseatIcons,
                idp.numColumns, idp.numRows);
        if (backupTable.restoreFromRawBackupIfAvailable(getDefaultProfileId(db))) {
            sanitizeDB(helper, db, backupManager);
            LauncherAppState.getInstance(context).getModel().forceReload();