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

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

Merge "Removing remaining code around ENABLE_DATABASE_RESTORE" into udc-dev

parents c745faf8 eb27546c
Loading
Loading
Loading
Loading
+2 −9
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;

import android.content.Context;

import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.model.GridBackupTable;
@@ -42,10 +41,7 @@ public class HotseatRestoreHelper {
                            context.getContentResolver(),
                            LauncherSettings.Settings.METHOD_NEW_TRANSACTION)
                            .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) {
                InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
                GridBackupTable backupTable = new GridBackupTable(context,
                        transaction.getDb(), idp.numDatabaseHotseatIcons, idp.numColumns,
                        idp.numRows);
                GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb());
                backupTable.createCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE);
                transaction.commit();
                LauncherSettings.Settings.call(context.getContentResolver(),
@@ -67,10 +63,7 @@ public class HotseatRestoreHelper {
                if (!tableExists(transaction.getDb(), HYBRID_HOTSEAT_BACKUP_TABLE)) {
                    return;
                }
                InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
                GridBackupTable backupTable = new GridBackupTable(context,
                        transaction.getDb(), idp.numDatabaseHotseatIcons, idp.numColumns,
                        idp.numRows);
                GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb());
                backupTable.restoreFromCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE, true);
                transaction.commit();
                LauncherAppState.getInstance(context).getModel().forceReload();
+0 −6
Original line number Diff line number Diff line
@@ -107,12 +107,6 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
                    cursor.close();
                }
            }
            // attempt to update widget id in backup table as well
            new ContentWriter(context, ContentWriter.CommitParams.backupCommitParams(
                    "appWidgetId=? and profileId=?", args))
                    .put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i])
                    .put(LauncherSettings.Favorites.RESTORED, state)
                    .commit();
        }

        LauncherAppState app = LauncherAppState.getInstanceNoCreate();
+0 −11
Original line number Diff line number Diff line
@@ -138,11 +138,6 @@ public class LauncherSettings {

        public static final String TABLE_NAME = "favorites";

        /**
         * Backup table created when the favorites table is modified during grid migration
         */
        public static final String BACKUP_TABLE_NAME = "favorites_bakup";

        /**
         * Backup table created when user hotseat is moved to workspace for hybrid hotseat
         */
@@ -164,12 +159,6 @@ public class LauncherSettings {
        public static final Uri CONTENT_URI = Uri.parse("content://"
                + LauncherProvider.AUTHORITY + "/" + TABLE_NAME);

        /**
         * The content:// style URL for "favorites_bakup" table
         */
        public static final Uri BACKUP_CONTENT_URI = Uri.parse("content://"
                + LauncherProvider.AUTHORITY + "/" + BACKUP_TABLE_NAME);

        /**
         * The content:// style URL for "favorites_preview" table
         */
+2 −118
Original line number Diff line number Diff line
@@ -15,18 +15,12 @@
 */
package com.android.launcher3.model;

import static com.android.launcher3.LauncherSettings.Favorites.BACKUP_TABLE_NAME;
import static com.android.launcher3.provider.LauncherDbUtils.dropTable;
import static com.android.launcher3.provider.LauncherDbUtils.tableExists;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Process;
import android.util.Log;

import androidx.annotation.IntDef;

import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.pm.UserCache;
@@ -36,50 +30,13 @@ import com.android.launcher3.pm.UserCache;
 * within the same data base.
 */
public class GridBackupTable {
    private static final String TAG = "GridBackupTable";

    private static final int ID_PROPERTY = -1;

    private static final String KEY_HOTSEAT_SIZE = Favorites.SCREEN;
    private static final String KEY_GRID_X_SIZE = Favorites.SPANX;
    private static final String KEY_GRID_Y_SIZE = Favorites.SPANY;
    private static final String KEY_DB_VERSION = Favorites.RANK;

    public static final int OPTION_REQUIRES_SANITIZATION = 1;

    /** STATE_NOT_FOUND indicates backup doesn't exist in the db. */
    private static final int STATE_NOT_FOUND = 0;
    /**
     *  STATE_RAW indicates the backup has not yet been sanitized. This implies it might still
     *  posses app info that doesn't exist in the workspace and needed to be sanitized before
     *  put into use.
     */
    private static final int STATE_RAW = 1;
    /** STATE_SANITIZED indicates the backup has already been sanitized, thus can be used as-is. */
    private static final int STATE_SANITIZED = 2;

    private final Context mContext;
    private final SQLiteDatabase mDb;

    private final int mOldHotseatSize;
    private final int mOldGridX;
    private final int mOldGridY;

    private int mRestoredHotseatSize;
    private int mRestoredGridX;
    private int mRestoredGridY;

    @IntDef({STATE_NOT_FOUND, STATE_RAW, STATE_SANITIZED})
    private @interface BackupState { }

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

        mOldHotseatSize = hotseatSize;
        mOldGridX = gridX;
        mOldGridY = gridY;
    }

    /**
@@ -89,7 +46,6 @@ public class GridBackupTable {
        long profileId = UserCache.INSTANCE.get(mContext).getSerialNumberForUser(
                Process.myUserHandle());
        copyTable(mDb, Favorites.TABLE_NAME, tableName, profileId);
        encodeDBProperties(0);
    }

    /**
@@ -114,78 +70,6 @@ public class GridBackupTable {
    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, 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);
        mDb.insert(BACKUP_TABLE_NAME, null, values);
    }

    /**
     * Load DB properties from grid backup table.
     */
    public @BackupState int loadDBProperties() {
        try (Cursor c = mDb.query(BACKUP_TABLE_NAME, new String[] {
                KEY_DB_VERSION,     // 0
                KEY_GRID_X_SIZE,    // 1
                KEY_GRID_Y_SIZE,    // 2
                KEY_HOTSEAT_SIZE,   // 3
                Favorites.OPTIONS}, // 4
                "_id=" + ID_PROPERTY, null, null, null, null)) {
            if (!c.moveToNext()) {
                Log.e(TAG, "Meta data not found in backup table");
                return STATE_NOT_FOUND;
            }
            if (!validateDBVersion(mDb.getVersion(), c.getInt(0))) {
                return STATE_NOT_FOUND;
            }

            mRestoredGridX = c.getInt(1);
            mRestoredGridY = c.getInt(2);
            mRestoredHotseatSize = c.getInt(3);
            boolean isSanitized = (c.getInt(4) & OPTION_REQUIRES_SANITIZATION) == 0;
            return isSanitized ? STATE_SANITIZED : STATE_RAW;
        }
    }

    /**
     * Restore workspace from raw backup if available.
     */
    public boolean restoreFromRawBackupIfAvailable(long oldProfileId) {
        if (!tableExists(mDb, Favorites.BACKUP_TABLE_NAME)
                || loadDBProperties() != STATE_RAW
                || mOldHotseatSize != mRestoredHotseatSize
                || mOldGridX != mRestoredGridX
                || mOldGridY != mRestoredGridY) {
            // skip restore if dimensions in backup table differs from current setup.
            return false;
        }
        copyTable(mDb, Favorites.BACKUP_TABLE_NAME, Favorites.TABLE_NAME, oldProfileId);
        Log.d(TAG, "Backup restored");
        return true;
    }

    /**
     * Performs a backup on the workspace layout.
     */
    public void doBackup(long profileId, int options) {
        copyTable(mDb, Favorites.TABLE_NAME, Favorites.BACKUP_TABLE_NAME, profileId);
        encodeDBProperties(options);
    }

    private static boolean validateDBVersion(int expected, int actual) {
        if (expected != actual) {
            Log.e(TAG, String.format("Launcher.db version mismatch, expecting %d but %d was found",
                    expected, actual));
            return false;
        }
        return true;
        db.execSQL("INSERT INTO " + to + " SELECT * FROM " + from);
    }
}
+7 −53
Original line number Diff line number Diff line
@@ -39,21 +39,19 @@ import android.util.LongSparseArray;
import android.util.SparseLongArray;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import com.android.launcher3.AppWidgetsRestoredReceiver;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherPrefs;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.Utilities;
import com.android.launcher3.logging.FileLog;
import com.android.launcher3.model.DatabaseHelper;
import com.android.launcher3.model.DeviceGridState;
import com.android.launcher3.model.GridBackupTable;
import com.android.launcher3.model.data.AppInfo;
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.model.data.WorkspaceItemInfo;
import com.android.launcher3.pm.UserCache;
import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
import com.android.launcher3.uioverrides.ApiWrapper;
import com.android.launcher3.util.IntArray;
@@ -108,7 +106,6 @@ public class RestoreDbTask {
        SQLiteDatabase db = helper.getWritableDatabase();
        try (SQLiteTransaction t = new SQLiteTransaction(db)) {
            RestoreDbTask task = new RestoreDbTask();
            task.backupWorkspace(context, db);
            task.sanitizeDB(context, helper, db, new BackupManager(context));
            task.restoreAppWidgetIdsIfExists(context);
            t.commit();
@@ -119,49 +116,6 @@ public class RestoreDbTask {
        }
    }

    /**
     * Restore the workspace if backup is available.
     */
    public static boolean restoreIfPossible(@NonNull Context context,
            @NonNull DatabaseHelper helper, @NonNull BackupManager backupManager) {
        final SQLiteDatabase db = helper.getWritableDatabase();
        try (SQLiteTransaction t = new SQLiteTransaction(db)) {
            RestoreDbTask task = new RestoreDbTask();
            task.restoreWorkspace(context, db, helper, backupManager);
            t.commit();
            return true;
        } catch (Exception e) {
            FileLog.e(TAG, "Failed to restore db", e);
            return false;
        }
    }

    /**
     * Backup the workspace so that if things go south in restore, we can recover these entries.
     */
    private void backupWorkspace(Context context, SQLiteDatabase db) throws Exception {
        InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
        new GridBackupTable(context, db, idp.numDatabaseHotseatIcons, idp.numColumns, idp.numRows)
                .doBackup(getDefaultProfileId(db), GridBackupTable.OPTION_REQUIRES_SANITIZATION);
    }

    private void restoreWorkspace(@NonNull Context context, @NonNull SQLiteDatabase db,
            @NonNull DatabaseHelper helper, @NonNull BackupManager backupManager)
            throws Exception {
        final InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
        GridBackupTable backupTable = new GridBackupTable(context, db, idp.numDatabaseHotseatIcons,
                idp.numColumns, idp.numRows);
        if (backupTable.restoreFromRawBackupIfAvailable(getDefaultProfileId(db))) {
            int itemsDeleted = sanitizeDB(context, helper, db, backupManager);
            LauncherAppState.getInstance(context).getModel().forceReload();
            restoreAppWidgetIdsIfExists(context);
            if (itemsDeleted == 0) {
                // all the items are restored, we no longer need the backup table
                dropTable(db, Favorites.BACKUP_TABLE_NAME);
            }
        }
    }

    /**
     * Makes the following changes in the provider DB.
     *   1. Removes all entries belonging to any profiles that were not restored.
@@ -174,7 +128,8 @@ public class RestoreDbTask {
     *
     * @return number of items deleted.
     */
    private int sanitizeDB(Context context, DatabaseHelper helper, SQLiteDatabase db,
    @VisibleForTesting
    protected int sanitizeDB(Context context, DatabaseHelper helper, SQLiteDatabase db,
            BackupManager backupManager) throws Exception {
        // Primary user ids
        long myProfileId = helper.getDefaultUserSerial();
@@ -258,7 +213,7 @@ public class RestoreDbTask {
        }

        // Override shortcuts
        maybeOverrideShortcuts(context, db, myProfileId);
        maybeOverrideShortcuts(context, helper, db, myProfileId);

        return itemsDeleted;
    }
@@ -388,8 +343,8 @@ public class RestoreDbTask {
                APP_WIDGET_IDS.to(IntArray.wrap(newIds).toConcatString()));
    }

    protected static void maybeOverrideShortcuts(Context context, SQLiteDatabase db,
            long currentUser) {
    protected static void maybeOverrideShortcuts(Context context, DatabaseHelper helper,
            SQLiteDatabase db, long currentUser) {
        Map<String, LauncherActivityInfo> activityOverrides = ApiWrapper.getActivityOverrides(
                context);

@@ -412,8 +367,7 @@ public class RestoreDbTask {
                if (override != null) {
                    ContentValues values = new ContentValues();
                    values.put(Favorites.PROFILE_ID,
                            UserCache.INSTANCE.get(context).getSerialNumberForUser(
                                    override.getUser()));
                            helper.getSerialNumberForUser(override.getUser()));
                    values.put(Favorites.INTENT, AppInfo.makeLaunchIntent(override).toUri(0));
                    db.update(Favorites.TABLE_NAME, values, String.format("%s=?", Favorites._ID),
                            new String[]{String.valueOf(c.getInt(idIndex))});
Loading