From b633b9aa2b3da3032f1d6ffef5e5b83c58d843ea Mon Sep 17 00:00:00 2001 From: fbaron Date: Mon, 23 Sep 2024 18:35:29 -0700 Subject: [PATCH] Fix for bug where we don't use default grid on comet, and migrate normally if not in a B&R case -The grid migration where we just copy the grid and move everything one row up should only occur in a B&R scenario, so now we add that restriction -We should default to 4x5 in comet if migrating from a 4x4 grid if the device we're migrating from does not have any other grids saved -if we have other grids saved, then we'll try using the saved grid if possible. If not possible, we use the default grid for the new device Bug: 360462379 Test: GridSizeMigrationUtilTest Flag: EXEMPT bugfix Change-Id: Ia905081046431c08dc058bd61b2b4ab42dee0506 --- .../android/launcher3/InvariantDeviceProfile.java | 2 +- .../launcher3/model/GridSizeMigrationUtil.java | 4 +++- .../android/launcher3/provider/RestoreDbTask.java | 15 ++++++++------- .../BackupAndRestoreDBSelectionTest.kt | 10 ++++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/com/android/launcher3/InvariantDeviceProfile.java b/src/com/android/launcher3/InvariantDeviceProfile.java index 54aea38ef3..5ea7bd9fbb 100644 --- a/src/com/android/launcher3/InvariantDeviceProfile.java +++ b/src/com/android/launcher3/InvariantDeviceProfile.java @@ -354,7 +354,7 @@ public class InvariantDeviceProfile implements SafeCloseable { */ @Deprecated public void reset(Context context) { - initGrid(context, getCurrentGridName(context)); + initGrid(context, getDefaultGridName(context)); } @VisibleForTesting diff --git a/src/com/android/launcher3/model/GridSizeMigrationUtil.java b/src/com/android/launcher3/model/GridSizeMigrationUtil.java index 8d2a7f92d4..62ab4a4f32 100644 --- a/src/com/android/launcher3/model/GridSizeMigrationUtil.java +++ b/src/com/android/launcher3/model/GridSizeMigrationUtil.java @@ -17,6 +17,7 @@ package com.android.launcher3.model; import static com.android.launcher3.Flags.enableSmartspaceRemovalToggle; +import static com.android.launcher3.LauncherPrefs.IS_FIRST_LOAD_AFTER_RESTORE; import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME; import static com.android.launcher3.LauncherSettings.Favorites.TMP_TABLE; import static com.android.launcher3.Utilities.SHOULD_SHOW_FIRST_PAGE_WIDGET; @@ -132,7 +133,8 @@ public class GridSizeMigrationUtil { return true; } - if (Flags.enableGridMigrationFix() + if (LauncherPrefs.get(context).get(IS_FIRST_LOAD_AFTER_RESTORE) + && Flags.enableGridMigrationFix() && srcDeviceState.getColumns().equals(destDeviceState.getColumns()) && srcDeviceState.getRows() < destDeviceState.getRows()) { Log.i("b/360462379", "Grid migration fix entry point."); diff --git a/src/com/android/launcher3/provider/RestoreDbTask.java b/src/com/android/launcher3/provider/RestoreDbTask.java index 21897bffb8..775d248290 100644 --- a/src/com/android/launcher3/provider/RestoreDbTask.java +++ b/src/com/android/launcher3/provider/RestoreDbTask.java @@ -75,7 +75,6 @@ import com.android.launcher3.util.ContentWriter; import com.android.launcher3.util.IntArray; import com.android.launcher3.util.LogConfig; -import java.io.File; import java.io.InvalidObjectException; import java.util.Arrays; import java.util.Collection; @@ -127,12 +126,12 @@ public class RestoreDbTask { if (Flags.enableNarrowGridRestore()) { String oldPhoneFileName = idp.dbFile; - List previousDbs = existingDbs(); + List previousDbs = existingDbs(context); removeOldDBs(context, oldPhoneFileName); // The idp before this contains data about the old phone, after this it becomes the idp // of the current phone. idp.reset(context); - trySettingPreviousGidAsCurrent(context, idp, oldPhoneFileName, previousDbs); + trySettingPreviousGridAsCurrent(context, idp, oldPhoneFileName, previousDbs); } else { idp.reinitializeAfterRestore(context); } @@ -143,7 +142,7 @@ public class RestoreDbTask { * Try setting the gird used in the previous phone to the new one. If the current device doesn't * support the previous grid option it will not be set. */ - private static void trySettingPreviousGidAsCurrent(Context context, InvariantDeviceProfile idp, + private static void trySettingPreviousGridAsCurrent(Context context, InvariantDeviceProfile idp, String oldPhoneDbFileName, List previousDbs) { InvariantDeviceProfile.GridOption oldPhoneGridOption = idp.getGridOptionFromFileName( context, oldPhoneDbFileName); @@ -166,17 +165,19 @@ public class RestoreDbTask { /** * Returns a list of paths of the existing launcher dbs. */ - private static List existingDbs() { + @VisibleForTesting + public static List existingDbs(Context context) { // At this point idp.dbFile contains the name of the dbFile from the previous phone return LauncherFiles.GRID_DB_FILES.stream() - .filter(dbName -> new File(dbName).exists()) + .filter(dbName -> context.getDatabasePath(dbName).exists()) .toList(); } /** * Only keep the last database used on the previous device. */ - private static void removeOldDBs(Context context, String oldPhoneDbFileName) { + @VisibleForTesting + public static void removeOldDBs(Context context, String oldPhoneDbFileName) { // At this point idp.dbFile contains the name of the dbFile from the previous phone LauncherFiles.GRID_DB_FILES.stream() .filter(dbName -> !dbName.equals(oldPhoneDbFileName)) diff --git a/tests/src/com/android/launcher3/backuprestore/BackupAndRestoreDBSelectionTest.kt b/tests/src/com/android/launcher3/backuprestore/BackupAndRestoreDBSelectionTest.kt index 479b201f61..35ac0a1027 100644 --- a/tests/src/com/android/launcher3/backuprestore/BackupAndRestoreDBSelectionTest.kt +++ b/tests/src/com/android/launcher3/backuprestore/BackupAndRestoreDBSelectionTest.kt @@ -23,6 +23,7 @@ import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation import com.android.launcher3.Flags import com.android.launcher3.LauncherPrefs import com.android.launcher3.model.ModelDbController +import com.android.launcher3.provider.RestoreDbTask import com.android.launcher3.util.Executors.MODEL_EXECUTOR import com.android.launcher3.util.TestUtil import com.android.launcher3.util.rule.BackAndRestoreRule @@ -67,4 +68,13 @@ class BackupAndRestoreDBSelectionTest { } } } + + @Test + fun testExistingDbsAndRemovingDbs() { + var existingDbs = RestoreDbTask.existingDbs(getInstrumentation().targetContext) + assert(existingDbs.size == 4) + RestoreDbTask.removeOldDBs(getInstrumentation().targetContext, "launcher_4_by_4.db") + existingDbs = RestoreDbTask.existingDbs(getInstrumentation().targetContext) + assert(existingDbs.size == 1) + } }