From ed5f3082b0c9378fed39232d29a50c19e9d0103b Mon Sep 17 00:00:00 2001 From: Tracy Zhou Date: Mon, 20 Apr 2020 01:13:26 -0700 Subject: [PATCH] Special handling when a db for one grid option is not setup yet - Init KEY_MIGRATION_SRC_WORKSPACE_SIZE and KEY_MIGRATION_SRC_HOTSEAT_COUNT - Load default workspace only when default db is created, not when peeking into dbs of other grid options during grid preview / migration Fixes: 154184711 Test: run grid preview and migration right after a cleared cache Pixel Launcher Change-Id: I86c7072b8c4a9da76e289c55ab440071f192fc38 --- .../model/DbDowngradeHelperTest.java | 4 +-- .../launcher3/provider/RestoreDbTaskTest.java | 2 +- .../launcher3/InvariantDeviceProfile.java | 8 ++++++ .../android/launcher3/LauncherProvider.java | 26 ++++++++++++------- .../model/GridSizeMigrationTask.java | 8 +++--- .../model/GridSizeMigrationTaskV2.java | 16 +++--------- 6 files changed, 34 insertions(+), 30 deletions(-) diff --git a/robolectric_tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java b/robolectric_tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java index b7340cf7d0..bbbe21e3f2 100644 --- a/robolectric_tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java +++ b/robolectric_tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java @@ -130,7 +130,7 @@ public class DbDowngradeHelperTest { } helper.close(); - helper = new DatabaseHelper(mContext, DB_FILE) { + helper = new DatabaseHelper(mContext, DB_FILE, false) { @Override public void onOpen(SQLiteDatabase db) { } }; @@ -161,7 +161,7 @@ public class DbDowngradeHelperTest { DbDowngradeHelper.updateSchemaFile(mSchemaFile, LauncherProvider.SCHEMA_VERSION, mContext); - DatabaseHelper dbHelper = new DatabaseHelper(mContext, DB_FILE) { + DatabaseHelper dbHelper = new DatabaseHelper(mContext, DB_FILE, false) { @Override public void onOpen(SQLiteDatabase db) { } }; diff --git a/robolectric_tests/src/com/android/launcher3/provider/RestoreDbTaskTest.java b/robolectric_tests/src/com/android/launcher3/provider/RestoreDbTaskTest.java index 58174c793c..ee73b82bb8 100644 --- a/robolectric_tests/src/com/android/launcher3/provider/RestoreDbTaskTest.java +++ b/robolectric_tests/src/com/android/launcher3/provider/RestoreDbTaskTest.java @@ -95,7 +95,7 @@ public class RestoreDbTaskTest { private final long mProfileId; MyDatabaseHelper(long profileId) { - super(RuntimeEnvironment.application, null); + super(RuntimeEnvironment.application, null, false); mProfileId = profileId; } diff --git a/src/com/android/launcher3/InvariantDeviceProfile.java b/src/com/android/launcher3/InvariantDeviceProfile.java index 0a1fad1d7d..63b90ae4f8 100644 --- a/src/com/android/launcher3/InvariantDeviceProfile.java +++ b/src/com/android/launcher3/InvariantDeviceProfile.java @@ -17,6 +17,7 @@ package com.android.launcher3; import static com.android.launcher3.Utilities.getDevicePrefs; +import static com.android.launcher3.Utilities.getPointString; import static com.android.launcher3.config.FeatureFlags.APPLY_CONFIG_AT_RUNTIME; import static com.android.launcher3.settings.SettingsActivity.GRID_OPTIONS_PREFERENCE_KEY; import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; @@ -69,6 +70,9 @@ public class InvariantDeviceProfile { public static final MainThreadInitializedObject INSTANCE = new MainThreadInitializedObject<>(InvariantDeviceProfile::new); + public static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size"; + public static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count"; + private static final String KEY_IDP_GRID_NAME = "idp_grid_name"; private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48; @@ -165,6 +169,10 @@ public class InvariantDeviceProfile { if (!newGridName.equals(gridName)) { Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply(); } + Utilities.getPrefs(context).edit() + .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numHotseatIcons) + .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows)) + .apply(); mConfigMonitor = new ConfigMonitor(context, APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess); diff --git a/src/com/android/launcher3/LauncherProvider.java b/src/com/android/launcher3/LauncherProvider.java index 8d20bd64df..308d84f77c 100644 --- a/src/com/android/launcher3/LauncherProvider.java +++ b/src/com/android/launcher3/LauncherProvider.java @@ -146,7 +146,8 @@ public class LauncherProvider extends ContentProvider { */ protected synchronized void createDbIfNotExists() { if (mOpenHelper == null) { - mOpenHelper = DatabaseHelper.createDatabaseHelper(getContext()); + mOpenHelper = DatabaseHelper.createDatabaseHelper( + getContext(), false /* forMigration */); if (RestoreDbTask.isPending(getContext())) { if (!RestoreDbTask.performRestore(getContext(), mOpenHelper, @@ -430,7 +431,8 @@ public class LauncherProvider extends ContentProvider { InvariantDeviceProfile.INSTANCE.get(getContext()).dbFile, Favorites.TMP_TABLE, () -> mOpenHelper, - () -> DatabaseHelper.createDatabaseHelper(getContext()))); + () -> DatabaseHelper.createDatabaseHelper( + getContext(), true /* forMigration */))); return result; } } @@ -441,7 +443,8 @@ public class LauncherProvider extends ContentProvider { prepForMigration( arg /* dbFile */, Favorites.PREVIEW_TABLE_NAME, - () -> DatabaseHelper.createDatabaseHelper(getContext(), arg), + () -> DatabaseHelper.createDatabaseHelper( + getContext(), arg, true /* forMigration */), () -> mOpenHelper)); return result; } @@ -609,20 +612,22 @@ public class LauncherProvider extends ContentProvider { public static class DatabaseHelper extends NoLocaleSQLiteHelper implements LayoutParserCallback { private final Context mContext; + private final boolean mForMigration; private int mMaxItemId = -1; private int mMaxScreenId = -1; private boolean mBackupTableExists; - static DatabaseHelper createDatabaseHelper(Context context) { - return createDatabaseHelper(context, null); + static DatabaseHelper createDatabaseHelper(Context context, boolean forMigration) { + return createDatabaseHelper(context, null, forMigration); } - static DatabaseHelper createDatabaseHelper(Context context, String dbName) { + static DatabaseHelper createDatabaseHelper(Context context, String dbName, + boolean forMigration) { if (dbName == null) { dbName = MULTI_DB_GRID_MIRATION_ALGO.get() ? InvariantDeviceProfile.INSTANCE.get( context).dbFile : LauncherFiles.LAUNCHER_DB; } - DatabaseHelper databaseHelper = new DatabaseHelper(context, dbName); + DatabaseHelper databaseHelper = new DatabaseHelper(context, dbName, forMigration); // Table creation sometimes fails silently, which leads to a crash loop. // This way, we will try to create a table every time after crash, so the device // would eventually be able to recover. @@ -643,9 +648,10 @@ public class LauncherProvider extends ContentProvider { /** * Constructor used in tests and for restore. */ - public DatabaseHelper(Context context, String dbName) { + public DatabaseHelper(Context context, String dbName, boolean forMigration) { super(context, dbName, SCHEMA_VERSION); mContext = context; + mForMigration = forMigration; } protected void initIds() { @@ -670,7 +676,9 @@ public class LauncherProvider extends ContentProvider { // Fresh and clean launcher DB. mMaxItemId = initializeMaxItemId(db); - onEmptyDbCreated(); + if (!mForMigration) { + onEmptyDbCreated(); + } } protected void onAddOrDeleteOp(SQLiteDatabase db) { diff --git a/src/com/android/launcher3/model/GridSizeMigrationTask.java b/src/com/android/launcher3/model/GridSizeMigrationTask.java index b27e4ea72e..e8a52bdd54 100644 --- a/src/com/android/launcher3/model/GridSizeMigrationTask.java +++ b/src/com/android/launcher3/model/GridSizeMigrationTask.java @@ -1,5 +1,7 @@ package com.android.launcher3.model; +import static com.android.launcher3.InvariantDeviceProfile.KEY_MIGRATION_SRC_HOTSEAT_COUNT; +import static com.android.launcher3.InvariantDeviceProfile.KEY_MIGRATION_SRC_WORKSPACE_SIZE; import static com.android.launcher3.LauncherSettings.Settings.EXTRA_VALUE; import static com.android.launcher3.Utilities.getPointString; import static com.android.launcher3.Utilities.parsePoint; @@ -53,9 +55,6 @@ public class GridSizeMigrationTask { private static final String TAG = "GridSizeMigrationTask"; private static final boolean DEBUG = true; - private static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size"; - private static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count"; - // These are carefully selected weights for various item types (Math.random?), to allow for // the least absurd migration experience. private static final float WT_SHORTCUT = 1; @@ -894,8 +893,7 @@ public class GridSizeMigrationTask { String gridSizeString = getPointString(idp.numColumns, idp.numRows); return !gridSizeString.equals(prefs.getString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, "")) - || idp.numHotseatIcons != prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, - idp.numHotseatIcons); + || idp.numHotseatIcons != prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, -1); } /** See {@link #migrateGridIfNeeded(Context, InvariantDeviceProfile)} */ diff --git a/src/com/android/launcher3/model/GridSizeMigrationTaskV2.java b/src/com/android/launcher3/model/GridSizeMigrationTaskV2.java index 1c44fc381e..4a28218246 100644 --- a/src/com/android/launcher3/model/GridSizeMigrationTaskV2.java +++ b/src/com/android/launcher3/model/GridSizeMigrationTaskV2.java @@ -16,6 +16,8 @@ package com.android.launcher3.model; +import static com.android.launcher3.InvariantDeviceProfile.KEY_MIGRATION_SRC_HOTSEAT_COUNT; +import static com.android.launcher3.InvariantDeviceProfile.KEY_MIGRATION_SRC_WORKSPACE_SIZE; import static com.android.launcher3.Utilities.getPointString; import static com.android.launcher3.provider.LauncherDbUtils.dropTable; @@ -63,9 +65,6 @@ import java.util.stream.Collectors; */ public class GridSizeMigrationTaskV2 { - public static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size"; - public static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count"; - private static final String TAG = "GridSizeMigrationTaskV2"; private static final boolean DEBUG = true; @@ -110,8 +109,7 @@ public class GridSizeMigrationTaskV2 { String gridSizeString = getPointString(idp.numColumns, idp.numRows); return !gridSizeString.equals(prefs.getString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, "")) - || idp.numHotseatIcons != prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, - idp.numHotseatIcons); + || idp.numHotseatIcons != prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, -1); } /** See {@link #migrateGridIfNeeded(Context, InvariantDeviceProfile)} */ @@ -148,14 +146,6 @@ public class GridSizeMigrationTaskV2 { SharedPreferences prefs = Utilities.getPrefs(context); String gridSizeString = getPointString(idp.numColumns, idp.numRows); - - if (gridSizeString.equals(prefs.getString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, "")) - && idp.numHotseatIcons == prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, - idp.numHotseatIcons)) { - // Skip if workspace and hotseat sizes have not changed. - return true; - } - HashSet validPackages = getValidPackages(context); int srcHotseatCount = prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, idp.numHotseatIcons);