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

Commit f076eae0 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Adding support for migrating the grid between any two valid screens sizes.

The grid is migrated in steps where each step consists of at max one column change and at max one row change.
Adding some unit tests for GridMigrationLogic

Bug: 25958224
Change-Id: Ie54e872ea0925cc4c463edbba0a7201d62b373a0
parent 5743d870
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ public class InvariantDeviceProfile {
    DeviceProfile landscapeProfile;
    DeviceProfile portraitProfile;

    InvariantDeviceProfile() {
    public InvariantDeviceProfile() {
    }

    public InvariantDeviceProfile(InvariantDeviceProfile p) {
+1 −4
Original line number Diff line number Diff line
@@ -101,12 +101,9 @@ public class LauncherBackupAgentHelper extends BackupAgentHelper {
                        LauncherSettings.Settings.METHOD_UPDATE_FOLDER_ITEMS_RANK);
            }

            // TODO: Update this logic to handle grid difference of 2. as well as hotseat difference
            if (GridSizeMigrationTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) {
                GridSizeMigrationTask.markForMigration(getApplicationContext(),
                        (int) mHelper.migrationCompatibleProfileData.desktopCols,
                        (int) mHelper.migrationCompatibleProfileData.desktopRows,
                        mHelper.widgetSizes);
                        mHelper.widgetSizes, mHelper.migrationCompatibleProfileData);
            }

            LauncherSettings.Settings.call(getContentResolver(),
+4 −5
Original line number Diff line number Diff line
@@ -315,14 +315,13 @@ public class LauncherBackupHelper implements BackupHelper {
            return true;
        }

        if (GridSizeMigrationTask.ENABLED &&
                (oldProfile.desktopCols - currentProfile.desktopCols <= 1) &&
                (oldProfile.desktopRows - currentProfile.desktopRows <= 1)) {
            // Allow desktop migration when row and/or column count contracts by 1.

        if (GridSizeMigrationTask.ENABLED) {
            // One time migrate the workspace when launcher starts.
            migrationCompatibleProfileData = initDeviceProfileData(mIdp);
            migrationCompatibleProfileData.desktopCols = oldProfile.desktopCols;
            migrationCompatibleProfileData.desktopRows = oldProfile.desktopRows;
            migrationCompatibleProfileData.hotseatCount = oldProfile.hotseatCount;
            migrationCompatibleProfileData.allappsRank = oldProfile.allappsRank;
            return true;
        }
        return false;
+4 −19
Original line number Diff line number Diff line
@@ -1651,26 +1651,11 @@ public class LauncherModel extends BroadcastReceiver
            int countX = profile.numColumns;
            int countY = profile.numRows;

            if (GridSizeMigrationTask.ENABLED && GridSizeMigrationTask.shouldRunTask(mContext)) {
                long migrationStartTime = System.currentTimeMillis();
                Log.v(TAG, "Starting workspace migration after restore");
                try {
                    GridSizeMigrationTask task = new GridSizeMigrationTask(mContext);
                    // Clear the flags before starting the task, so that we do not run the task
                    // again, in case there was an uncaught error.
                    GridSizeMigrationTask.clearFlags(mContext);
                    task.execute();
                } catch (Exception e) {
                    Log.e(TAG, "Error during grid migration", e);

                    // Clear workspace.
            if (GridSizeMigrationTask.ENABLED &&
                    !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
                // Migration failed. Clear workspace.
                mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
            }
                Log.v(TAG, "Workspace migration completed in "
                        + (System.currentTimeMillis() - migrationStartTime));

                GridSizeMigrationTask.saveCurrentConfig(mContext);
            }

            if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
                Log.d(TAG, "loadWorkspace: resetting launcher database");
+43 −15
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ public class LauncherProvider extends ContentProvider {

    private static final Object LISTENER_LOCK = new Object();
    @Thunk LauncherProviderChangeListener mListener;
    @Thunk DatabaseHelper mOpenHelper;
    protected DatabaseHelper mOpenHelper;

    @Override
    public boolean onCreate() {
@@ -104,7 +104,10 @@ public class LauncherProvider extends ContentProvider {
        }
    }

    private synchronized void createDbIfNotExists() {
    /**
     * Overridden in tests
     */
    protected synchronized void createDbIfNotExists() {
        if (mOpenHelper == null) {
            mOpenHelper = new DatabaseHelper(getContext(), this);
        }
@@ -364,7 +367,10 @@ public class LauncherProvider extends ContentProvider {
        return folderIds;
    }

    private void notifyListeners() {
    /**
     * Overridden in tests
     */
    protected void notifyListeners() {
        // always notify the backup agent
        LauncherBackupAgentHelper.dataChanged(getContext());
        synchronized (LISTENER_LOCK) {
@@ -501,7 +507,10 @@ public class LauncherProvider extends ContentProvider {
        });
    }

    private static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
    /**
     * The class is subclassed in tests to create an in-memory db.
     */
    protected static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
        private final LauncherProvider mProvider;
        private final Context mContext;
        @Thunk final AppWidgetHost mAppWidgetHost;
@@ -535,6 +544,19 @@ public class LauncherProvider extends ContentProvider {
            }
        }

        /**
         * Constructor used only in tests.
         */
        public DatabaseHelper(Context context, LauncherProvider provider, String tableName) {
            super(context, tableName, null, DATABASE_VERSION);
            mContext = context;
            mProvider = provider;

            mAppWidgetHost = null;
            mMaxItemId = initializeMaxItemId(getWritableDatabase());
            mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
        }

        private boolean tableExists(String tableName) {
            Cursor c = getReadableDatabase().query(
                    true, "sqlite_master", new String[] {"tbl_name"},
@@ -565,18 +587,28 @@ public class LauncherProvider extends ContentProvider {

            // Fresh and clean launcher DB.
            mMaxItemId = initializeMaxItemId(db);
            setFlagEmptyDbCreated();
            onEmptyDbCreated();
        }

        /**
         * Overriden in tests.
         */
        protected void onEmptyDbCreated() {
            // Set the flag for empty DB
            Utilities.getPrefs(mContext).edit().putBoolean(EMPTY_DATABASE_CREATED, true).commit();

            // When a new DB is created, remove all previously stored managed profile information.
            ManagedProfileHeuristic.processAllUsers(Collections.<UserHandleCompat>emptyList(), mContext);
            ManagedProfileHeuristic.processAllUsers(Collections.<UserHandleCompat>emptyList(),
                    mContext);
        }

        private void addFavoritesTable(SQLiteDatabase db, boolean optional) {
            UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
            long userSerialNumber = userManager.getSerialNumberForUser(
        protected long getDefaultUserSerial() {
            return UserManagerCompat.getInstance(mContext).getSerialNumberForUser(
                    UserHandleCompat.myUserHandle());
            String ifNotExists = optional ? " IF NOT EXISTS " : "";
        }

        private void addFavoritesTable(SQLiteDatabase db, boolean optional) {
            String ifNotExists = optional ? " IF NOT EXISTS " : "";
            db.execSQL("CREATE TABLE " + ifNotExists + TABLE_FAVORITES + " (" +
                    "_id INTEGER PRIMARY KEY," +
                    "title TEXT," +
@@ -599,7 +631,7 @@ public class LauncherProvider extends ContentProvider {
                    "appWidgetProvider TEXT," +
                    "modified INTEGER NOT NULL DEFAULT 0," +
                    "restored INTEGER NOT NULL DEFAULT 0," +
                    "profileId INTEGER DEFAULT " + userSerialNumber + "," +
                    "profileId INTEGER DEFAULT " + getDefaultUserSerial() + "," +
                    "rank INTEGER NOT NULL DEFAULT 0," +
                    "options INTEGER NOT NULL DEFAULT 0" +
                    ");");
@@ -649,10 +681,6 @@ public class LauncherProvider extends ContentProvider {
            Utilities.getPrefs(mContext).edit().putBoolean(EMPTY_DATABASE_CREATED, false).commit();
        }

        private void setFlagEmptyDbCreated() {
            Utilities.getPrefs(mContext).edit().putBoolean(EMPTY_DATABASE_CREATED, true).commit();
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Loading