Move all db calls for grid migration inside a SQLiteTransaction

Bug: 388564911
Flag: com.android.launcher3.one_grid_specs
Test: GridSizeMigrationTest
Change-Id: I22a373ea5b71a0713eeba37e8d09e09130eae525
This commit is contained in:
fbaron
2025-01-08 13:44:34 -08:00
parent 2dbb255975
commit a94ecdfac0
2 changed files with 61 additions and 42 deletions
@@ -126,33 +126,43 @@ public class GridSizeMigrationDBController {
return true;
}
if (isDestNewDb
boolean shouldMigrateToStrictlyTallerGrid = isDestNewDb
&& srcDeviceState.getColumns().equals(destDeviceState.getColumns())
&& srcDeviceState.getRows() < destDeviceState.getRows()) {
// Only use this strategy when comparing the previous grid to the new grid and the
// columns are the same and the destination has more rows
&& srcDeviceState.getRows() < destDeviceState.getRows();
if (shouldMigrateToStrictlyTallerGrid) {
copyTable(source, TABLE_NAME, target.getWritableDatabase(), TABLE_NAME, context);
if (oneGridSpecs()) {
DbReader destReader = new DbReader(
target.getWritableDatabase(), TABLE_NAME, context);
boolean shouldShiftCells = shouldShiftCells(destReader, srcDeviceState.getRows());
if (shouldShiftCells) {
shiftTableByXCells(
target.getWritableDatabase(),
(destDeviceState.getRows() - srcDeviceState.getRows()),
TABLE_NAME);
}
}
// Save current configuration, so that the migration does not run again.
destDeviceState.writeToPrefs(context);
return true;
} else {
copyTable(source, TABLE_NAME, target.getWritableDatabase(), TMP_TABLE, context);
}
copyTable(source, TABLE_NAME, target.getWritableDatabase(), TMP_TABLE, context);
long migrationStartTime = System.currentTimeMillis();
try (SQLiteTransaction t = new SQLiteTransaction(target.getWritableDatabase())) {
if (shouldMigrateToStrictlyTallerGrid) {
// This is a special case where if the grid is the same amount of columns but a
// larger amount of rows we simply copy over the source grid to the destination
// grid, rather than undergoing the general grid migration. If there are more icons
// on the bottom of the first page then we shift the icons down to the bottom of the
// grid so that the icons remain bottom-anchored.
if (oneGridSpecs()) {
DbReader destReader = new DbReader(
target.getWritableDatabase(), TABLE_NAME, context);
boolean shouldShiftCells =
shouldShiftCells(destReader, srcDeviceState.getRows());
if (shouldShiftCells) {
shiftTableByXCells(
target.getWritableDatabase(),
(destDeviceState.getRows() - srcDeviceState.getRows()),
TABLE_NAME);
}
}
// Save current configuration, so that the migration does not run again.
destDeviceState.writeToPrefs(context);
t.commit();
return true;
}
DbReader srcReader = new DbReader(t.getDb(), TMP_TABLE, context);
DbReader destReader = new DbReader(t.getDb(), TABLE_NAME, context);
@@ -65,33 +65,42 @@ class GridSizeMigrationLogic {
"$srcDeviceState\ndestDeviceState: $destDeviceState\nisDestNewDb: $isDestNewDb",
)
// This is a special case where if the grid is the same amount of columns but a larger
// amount of rows we simply copy over the source grid to the destination grid, rather
// than undergoing the general grid migration.
if (shouldMigrateToStrictlyTallerGrid(isDestNewDb, srcDeviceState, destDeviceState)) {
Log.d(TAG, "Migrating to strictly taller grid")
val shouldMigrateToStrtictlyTallerGrid =
shouldMigrateToStrictlyTallerGrid(isDestNewDb, srcDeviceState, destDeviceState)
if (shouldMigrateToStrtictlyTallerGrid) {
copyTable(source, TABLE_NAME, target.writableDatabase, TABLE_NAME, context)
if (oneGridSpecs()) {
val destReader = DbReader(target.writableDatabase, TABLE_NAME, context)
val shouldShiftCells = shouldShiftCells(destReader, srcDeviceState.rows)
if (shouldShiftCells) {
shiftTableByXCells(
target.writableDatabase,
(destDeviceState.rows - srcDeviceState.rows),
TABLE_NAME,
)
}
}
// Save current configuration, so that the migration does not run again.
destDeviceState.writeToPrefs(context)
return
} else {
copyTable(source, TABLE_NAME, target.writableDatabase, TMP_TABLE, context)
}
copyTable(source, TABLE_NAME, target.writableDatabase, TMP_TABLE, context)
val migrationStartTime = System.currentTimeMillis()
try {
SQLiteTransaction(target.writableDatabase).use { t ->
// This is a special case where if the grid is the same amount of columns but a
// larger amount of rows we simply copy over the source grid to the destination
// grid, rather than undergoing the general grid migration. If there are more icons
// on the bottom of the first page then we shift the icons down to the bottom of the
// grid so that the icons remain bottom-anchored.
if (shouldMigrateToStrtictlyTallerGrid) {
Log.d(TAG, "Migrating to strictly taller grid")
if (oneGridSpecs()) {
val destReader = DbReader(target.writableDatabase, TABLE_NAME, context)
val shouldShiftCells = shouldShiftCells(destReader, srcDeviceState.rows)
if (shouldShiftCells) {
Log.i("TAGTAG", "should shift cells")
shiftTableByXCells(
target.writableDatabase,
(destDeviceState.rows - srcDeviceState.rows),
TABLE_NAME,
)
}
}
// Save current configuration, so that the migration does not run again.
destDeviceState.writeToPrefs(context)
t.commit()
return
}
val srcReader = DbReader(t.db, TMP_TABLE, context)
val destReader = DbReader(t.db, TABLE_NAME, context)