From 1edaf75ad9bf927b907f6884ba669871c38eebda Mon Sep 17 00:00:00 2001 From: Sebastian Franco Date: Wed, 10 Apr 2024 10:22:49 -0700 Subject: [PATCH] If the user only had the default db, then migrate to the new default We also check if the new default is bigger than or equal to the previous one. Flag: ACONFIG enable_grid_migration_fix enabled Flag: ACONFIG enable_narrow_grid_restore enabled Bug: 325286145 Bug: 325285743 Test: BackupAndRestoreDBSelectionTest.kt Test: GridMigrationTest.kt (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:12678a536b747279bb7ce04557696aae6725ce33) Merged-In: I98315a4e4c112ca96a74200dd76016803dfaf820 Change-Id: I98315a4e4c112ca96a74200dd76016803dfaf820 --- .../launcher3/provider/RestoreDbTask.java | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/com/android/launcher3/provider/RestoreDbTask.java b/src/com/android/launcher3/provider/RestoreDbTask.java index 3a65f17800..e6ce337485 100644 --- a/src/com/android/launcher3/provider/RestoreDbTask.java +++ b/src/com/android/launcher3/provider/RestoreDbTask.java @@ -75,9 +75,11 @@ 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; +import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -125,37 +127,52 @@ public class RestoreDbTask { if (Flags.enableNarrowGridRestore()) { String oldPhoneFileName = idp.dbFile; + List previousDbs = existingDbs(); 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); + trySettingPreviousGidAsCurrent(context, idp, oldPhoneFileName, previousDbs); } else { idp.reinitializeAfterRestore(context); } } + /** * 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, - String oldPhoneDbFileName) { - InvariantDeviceProfile.GridOption gridOption = idp.getGridOptionFromFileName(context, - oldPhoneDbFileName); - if (gridOption != null) { + String oldPhoneDbFileName, List previousDbs) { + InvariantDeviceProfile.GridOption oldPhoneGridOption = idp.getGridOptionFromFileName( + context, oldPhoneDbFileName); + // The grid option could be null if current phone doesn't support the previous db. + if (oldPhoneGridOption != null) { + /* If the user only used the default db on the previous phone and the new default db is + * bigger than or equal to the previous one, then keep the new default db */ + if (previousDbs.size() == 1 && oldPhoneGridOption.numColumns <= idp.numColumns + && oldPhoneGridOption.numRows <= idp.numRows) { + /* Keep the user in default grid */ + return; + } /* - * We do this because in some cases different devices have different names for grid - * options, in one device the grid option "normal" can be 4x4 while in other it - * could be "practical". Calling this changes the current device grid to the same - * we had in the other phone, in the case the current phone doesn't support the grid - * option we use the default and migrate the db to the default. Migration occurs on - * {@code GridSizeMigrationUtil#migrateGridIfNeeded} + * Here we are setting the previous db as the current one. */ - idp.setCurrentGrid(context, gridOption.name); + idp.setCurrentGrid(context, oldPhoneGridOption.name); } } + /** + * Returns a list of paths of the existing launcher dbs. + */ + private static List existingDbs() { + // 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()) + .toList(); + } + /** * Only keep the last database used on the previous device. */