Merge "Pass in dbFile from idpGridState for grid migration" into tm-dev am: deae9343be

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/17123550

Change-Id: Id3eab5daa3be86fa8c463ea29e59a9c7e5700097
This commit is contained in:
TreeHugger Robot
2022-03-14 21:59:53 +00:00
committed by Automerger Merge Worker
4 changed files with 48 additions and 24 deletions
@@ -162,6 +162,7 @@ public class LauncherProvider extends ContentProvider {
private synchronized boolean prepForMigration(String dbFile, String targetTableName, private synchronized boolean prepForMigration(String dbFile, String targetTableName,
Supplier<DatabaseHelper> src, Supplier<DatabaseHelper> dst) { Supplier<DatabaseHelper> src, Supplier<DatabaseHelper> dst) {
if (TextUtils.equals(dbFile, mOpenHelper.getDatabaseName())) { if (TextUtils.equals(dbFile, mOpenHelper.getDatabaseName())) {
Log.e("b/198965093", "prepForMigration - target db is same as current: " + dbFile);
return false; return false;
} }
@@ -439,7 +440,7 @@ public class LauncherProvider extends ContentProvider {
Bundle result = new Bundle(); Bundle result = new Bundle();
result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
prepForMigration( prepForMigration(
InvariantDeviceProfile.INSTANCE.get(getContext()).dbFile, arg /* dbFile */,
Favorites.TMP_TABLE, Favorites.TMP_TABLE,
() -> mOpenHelper, () -> mOpenHelper,
() -> DatabaseHelper.createDatabaseHelper( () -> DatabaseHelper.createDatabaseHelper(
@@ -43,15 +43,18 @@ public class DeviceGridState implements Comparable<DeviceGridState> {
public static final String KEY_WORKSPACE_SIZE = "migration_src_workspace_size"; public static final String KEY_WORKSPACE_SIZE = "migration_src_workspace_size";
public static final String KEY_HOTSEAT_COUNT = "migration_src_hotseat_count"; public static final String KEY_HOTSEAT_COUNT = "migration_src_hotseat_count";
public static final String KEY_DEVICE_TYPE = "migration_src_device_type"; public static final String KEY_DEVICE_TYPE = "migration_src_device_type";
public static final String KEY_DB_FILE = "migration_src_db_file";
private final String mGridSizeString; private final String mGridSizeString;
private final int mNumHotseat; private final int mNumHotseat;
private final @DeviceType int mDeviceType; private final @DeviceType int mDeviceType;
private final String mDbFile;
public DeviceGridState(InvariantDeviceProfile idp) { public DeviceGridState(InvariantDeviceProfile idp) {
mGridSizeString = String.format(Locale.ENGLISH, "%d,%d", idp.numColumns, idp.numRows); mGridSizeString = String.format(Locale.ENGLISH, "%d,%d", idp.numColumns, idp.numRows);
mNumHotseat = idp.numDatabaseHotseatIcons; mNumHotseat = idp.numDatabaseHotseatIcons;
mDeviceType = idp.deviceType; mDeviceType = idp.deviceType;
mDbFile = idp.dbFile;
} }
public DeviceGridState(Context context) { public DeviceGridState(Context context) {
@@ -59,6 +62,7 @@ public class DeviceGridState implements Comparable<DeviceGridState> {
mGridSizeString = prefs.getString(KEY_WORKSPACE_SIZE, ""); mGridSizeString = prefs.getString(KEY_WORKSPACE_SIZE, "");
mNumHotseat = prefs.getInt(KEY_HOTSEAT_COUNT, -1); mNumHotseat = prefs.getInt(KEY_HOTSEAT_COUNT, -1);
mDeviceType = prefs.getInt(KEY_DEVICE_TYPE, TYPE_PHONE); mDeviceType = prefs.getInt(KEY_DEVICE_TYPE, TYPE_PHONE);
mDbFile = prefs.getString(KEY_DB_FILE, "");
} }
/** /**
@@ -68,6 +72,20 @@ public class DeviceGridState implements Comparable<DeviceGridState> {
return mDeviceType; return mDeviceType;
} }
/**
* Returns the databaseFile for the grid.
*/
public String getDbFile() {
return mDbFile;
}
/**
* Returns the number of hotseat icons.
*/
public int getNumHotseat() {
return mNumHotseat;
}
/** /**
* Stores the device state to shared preferences * Stores the device state to shared preferences
*/ */
@@ -76,6 +94,7 @@ public class DeviceGridState implements Comparable<DeviceGridState> {
.putString(KEY_WORKSPACE_SIZE, mGridSizeString) .putString(KEY_WORKSPACE_SIZE, mGridSizeString)
.putInt(KEY_HOTSEAT_COUNT, mNumHotseat) .putInt(KEY_HOTSEAT_COUNT, mNumHotseat)
.putInt(KEY_DEVICE_TYPE, mDeviceType) .putInt(KEY_DEVICE_TYPE, mDeviceType)
.putString(KEY_DB_FILE, mDbFile)
.apply(); .apply();
} }
@@ -106,6 +125,7 @@ public class DeviceGridState implements Comparable<DeviceGridState> {
+ "mGridSizeString='" + mGridSizeString + '\'' + "mGridSizeString='" + mGridSizeString + '\''
+ ", mNumHotseat=" + mNumHotseat + ", mNumHotseat=" + mNumHotseat
+ ", mDeviceType=" + mDeviceType + ", mDeviceType=" + mDeviceType
+ ", mDbFile=" + mDbFile
+ '}'; + '}';
} }
@@ -22,7 +22,6 @@ import android.content.ComponentName;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
@@ -104,13 +103,16 @@ public class GridSizeMigrationTaskV2 {
* Check given a new IDP, if migration is necessary. * Check given a new IDP, if migration is necessary.
*/ */
public static boolean needsToMigrate(Context context, InvariantDeviceProfile idp) { public static boolean needsToMigrate(Context context, InvariantDeviceProfile idp) {
DeviceGridState idpGridState = new DeviceGridState(idp); return needsToMigrate(new DeviceGridState(context), new DeviceGridState(idp));
DeviceGridState contextGridState = new DeviceGridState(context); }
boolean needsToMigrate = !idpGridState.isCompatible(contextGridState);
private static boolean needsToMigrate(
DeviceGridState srcDeviceState, DeviceGridState destDeviceState) {
boolean needsToMigrate = !destDeviceState.isCompatible(srcDeviceState);
// TODO(b/198965093): Revert this change after bug is fixed // TODO(b/198965093): Revert this change after bug is fixed
if (needsToMigrate) { if (needsToMigrate) {
Log.d("b/198965093", "Migration is needed. idpGridState: " + idpGridState Log.d("b/198965093", "Migration is needed. destDeviceState: " + destDeviceState
+ ", contextGridState: " + contextGridState); + ", srcDeviceState: " + srcDeviceState);
} }
return needsToMigrate; return needsToMigrate;
} }
@@ -143,23 +145,26 @@ public class GridSizeMigrationTaskV2 {
idp = LauncherAppState.getIDP(context); idp = LauncherAppState.getIDP(context);
} }
if (!needsToMigrate(context, idp)) { DeviceGridState srcDeviceState = new DeviceGridState(context);
DeviceGridState destDeviceState = new DeviceGridState(idp);
if (!needsToMigrate(srcDeviceState, destDeviceState)) {
return true; return true;
} }
SharedPreferences prefs = Utilities.getPrefs(context);
HashSet<String> validPackages = getValidPackages(context); HashSet<String> validPackages = getValidPackages(context);
if (migrateForPreview) { if (migrateForPreview) {
if (!LauncherSettings.Settings.call( if (!LauncherSettings.Settings.call(
context.getContentResolver(), context.getContentResolver(),
LauncherSettings.Settings.METHOD_PREP_FOR_PREVIEW, idp.dbFile).getBoolean( LauncherSettings.Settings.METHOD_PREP_FOR_PREVIEW,
destDeviceState.getDbFile()).getBoolean(
LauncherSettings.Settings.EXTRA_VALUE)) { LauncherSettings.Settings.EXTRA_VALUE)) {
return false; return false;
} }
} else if (!LauncherSettings.Settings.call( } else if (!LauncherSettings.Settings.call(
context.getContentResolver(), context.getContentResolver(),
LauncherSettings.Settings.METHOD_UPDATE_CURRENT_OPEN_HELPER).getBoolean( LauncherSettings.Settings.METHOD_UPDATE_CURRENT_OPEN_HELPER,
destDeviceState.getDbFile()).getBoolean(
LauncherSettings.Settings.EXTRA_VALUE)) { LauncherSettings.Settings.EXTRA_VALUE)) {
return false; return false;
} }
@@ -179,10 +184,10 @@ public class GridSizeMigrationTaskV2 {
: LauncherSettings.Favorites.TABLE_NAME, : LauncherSettings.Favorites.TABLE_NAME,
context, validPackages); context, validPackages);
Point targetSize = new Point(idp.numColumns, idp.numRows); Point targetSize = new Point(destDeviceState.getColumns(), destDeviceState.getRows());
GridSizeMigrationTaskV2 task = new GridSizeMigrationTaskV2(context, t.getDb(), GridSizeMigrationTaskV2 task = new GridSizeMigrationTaskV2(context, t.getDb(),
srcReader, destReader, idp.numDatabaseHotseatIcons, targetSize); srcReader, destReader, destDeviceState.getNumHotseat(), targetSize);
task.migrate(idp); task.migrate(srcDeviceState, destDeviceState);
if (!migrateForPreview) { if (!migrateForPreview) {
dropTable(t.getDb(), LauncherSettings.Favorites.TMP_TABLE); dropTable(t.getDb(), LauncherSettings.Favorites.TMP_TABLE);
@@ -200,13 +205,13 @@ public class GridSizeMigrationTaskV2 {
if (!migrateForPreview) { if (!migrateForPreview) {
// Save current configuration, so that the migration does not run again. // Save current configuration, so that the migration does not run again.
new DeviceGridState(idp).writeToPrefs(context); destDeviceState.writeToPrefs(context);
} }
} }
} }
@VisibleForTesting @VisibleForTesting
protected boolean migrate(InvariantDeviceProfile idp) { protected boolean migrate(DeviceGridState srcDeviceState, DeviceGridState destDeviceState) {
if (mHotseatDiff.isEmpty() && mWorkspaceDiff.isEmpty()) { if (mHotseatDiff.isEmpty() && mWorkspaceDiff.isEmpty()) {
return false; return false;
} }
@@ -228,8 +233,6 @@ public class GridSizeMigrationTaskV2 {
boolean preservePages = false; boolean preservePages = false;
if (screens.isEmpty() && FeatureFlags.ENABLE_NEW_MIGRATION_LOGIC.get()) { if (screens.isEmpty() && FeatureFlags.ENABLE_NEW_MIGRATION_LOGIC.get()) {
DeviceGridState srcDeviceState = new DeviceGridState(mContext);
DeviceGridState destDeviceState = new DeviceGridState(idp);
preservePages = destDeviceState.compareTo(srcDeviceState) >= 0 preservePages = destDeviceState.compareTo(srcDeviceState) >= 0
&& destDeviceState.getColumns() - srcDeviceState.getColumns() <= 2; && destDeviceState.getColumns() - srcDeviceState.getColumns() <= 2;
} }
@@ -125,7 +125,7 @@ class GridSizeMigrationTaskV2Test {
idp.numDatabaseHotseatIcons, idp.numDatabaseHotseatIcons,
Point(idp.numColumns, idp.numRows) Point(idp.numColumns, idp.numRows)
) )
task.migrate(idp) task.migrate(DeviceGridState(context), DeviceGridState(idp))
// Check hotseat items // Check hotseat items
var c = context.contentResolver.query( var c = context.contentResolver.query(
@@ -205,7 +205,7 @@ class GridSizeMigrationTaskV2Test {
idp.numDatabaseHotseatIcons, idp.numDatabaseHotseatIcons,
Point(idp.numColumns, idp.numRows) Point(idp.numColumns, idp.numRows)
) )
task.migrate(idp) task.migrate(DeviceGridState(context), DeviceGridState(idp))
// Check hotseat items // Check hotseat items
val c = context.contentResolver.query( val c = context.contentResolver.query(
@@ -260,7 +260,7 @@ class GridSizeMigrationTaskV2Test {
idp.numDatabaseHotseatIcons, idp.numDatabaseHotseatIcons,
Point(idp.numColumns, idp.numRows) Point(idp.numColumns, idp.numRows)
) )
task.migrate(idp) task.migrate(DeviceGridState(context), DeviceGridState(idp))
// Check hotseat items // Check hotseat items
val c = context.contentResolver.query( val c = context.contentResolver.query(
@@ -325,7 +325,7 @@ class GridSizeMigrationTaskV2Test {
idp.numDatabaseHotseatIcons, idp.numDatabaseHotseatIcons,
Point(idp.numColumns, idp.numRows) Point(idp.numColumns, idp.numRows)
) )
task.migrate(idp) task.migrate(DeviceGridState(context), DeviceGridState(idp))
// Get workspace items // Get workspace items
val c = context.contentResolver.query( val c = context.contentResolver.query(
@@ -385,7 +385,7 @@ class GridSizeMigrationTaskV2Test {
idp.numDatabaseHotseatIcons, idp.numDatabaseHotseatIcons,
Point(idp.numColumns, idp.numRows) Point(idp.numColumns, idp.numRows)
) )
task.migrate(idp) task.migrate(DeviceGridState(context), DeviceGridState(idp))
// Get workspace items // Get workspace items
val c = context.contentResolver.query( val c = context.contentResolver.query(
@@ -446,7 +446,7 @@ class GridSizeMigrationTaskV2Test {
idp.numDatabaseHotseatIcons, idp.numDatabaseHotseatIcons,
Point(idp.numColumns, idp.numRows) Point(idp.numColumns, idp.numRows)
) )
task.migrate(idp) task.migrate(DeviceGridState(context), DeviceGridState(idp))
// Get workspace items // Get workspace items
val c = context.contentResolver.query( val c = context.contentResolver.query(