Merge "Fix grid migration tests with grid migration refactor flag on" into main
diff --git a/src/com/android/launcher3/model/GridSizeMigrationDBController.java b/src/com/android/launcher3/model/GridSizeMigrationDBController.java
index 9531d5b..d8ca095 100644
--- a/src/com/android/launcher3/model/GridSizeMigrationDBController.java
+++ b/src/com/android/launcher3/model/GridSizeMigrationDBController.java
@@ -328,10 +328,9 @@
} else {
values.put(LauncherSettings.Favorites.CONTAINER, folderId);
}
- newId = helper.generateNewItemId();
- while (idsInUse.contains(newId)) {
+ do {
newId = helper.generateNewItemId();
- }
+ } while (idsInUse.contains(newId));
values.put(LauncherSettings.Favorites._ID, newId);
helper.getWritableDatabase().insert(destTableName, null, values);
}
diff --git a/src/com/android/launcher3/model/GridSizeMigrationLogic.java b/src/com/android/launcher3/model/GridSizeMigrationLogic.java
index 12a14b2..f8c8f77 100644
--- a/src/com/android/launcher3/model/GridSizeMigrationLogic.java
+++ b/src/com/android/launcher3/model/GridSizeMigrationLogic.java
@@ -35,6 +35,7 @@
import android.util.Log;
import androidx.annotation.NonNull;
+import androidx.annotation.VisibleForTesting;
import com.android.launcher3.Flags;
import com.android.launcher3.LauncherPrefs;
@@ -95,10 +96,17 @@
t.getDb(), TABLE_NAME, context);
Point targetSize = new Point(destDeviceState.getColumns(), destDeviceState.getRows());
+
+ // Here we keep all the DB ids we have in the destination DB such that we don't assign
+ // an item that we want to add to the destination DB the same id as an already existing
+ // item.
+ List<Integer> idsInUse = new ArrayList<>();
+
// Migrate hotseat.
- migrateHotseat(destDeviceState.getNumHotseat(), srcReader, destReader, target);
+ migrateHotseat(destDeviceState.getNumHotseat(), srcReader, destReader, target,
+ idsInUse);
// Migrate workspace.
- migrateWorkspace(srcReader, destReader, target, targetSize);
+ migrateWorkspace(srcReader, destReader, target, targetSize, idsInUse);
dropTable(t.getDb(), TMP_TABLE);
t.commit();
@@ -113,17 +121,22 @@
}
}
- private void migrateHotseat(int destHotseatSize,
+ /**
+ * Handles hotseat migration.
+ */
+ @VisibleForTesting
+ public void migrateHotseat(int destHotseatSize,
GridSizeMigrationDBController.DbReader srcReader,
- GridSizeMigrationDBController.DbReader destReader, DatabaseHelper helper) {
+ GridSizeMigrationDBController.DbReader destReader,
+ DatabaseHelper helper, List<Integer> idsInUse) {
final List<DbEntry> srcHotseatItems =
srcReader.loadHotseatEntries();
final List<DbEntry> dstHotseatItems =
destReader.loadHotseatEntries();
-
final List<DbEntry> hotseatToBeAdded =
getItemsToBeAdded(srcHotseatItems, dstHotseatItems);
+
final IntArray toBeRemoved = new IntArray();
toBeRemoved.addAll(getItemsToBeRemoved(srcHotseatItems, dstHotseatItems));
@@ -144,19 +157,19 @@
removeEntryFromDb(destReader.mDb, destReader.mTableName, toBeRemoved);
}
- placeHotseatItems(
- hotseatToBeAdded, dstHotseatItems, destHotseatSize, helper, srcReader, destReader);
+ placeHotseatItems(hotseatToBeAdded, dstHotseatItems, destHotseatSize, helper, srcReader,
+ destReader, idsInUse);
}
private void placeHotseatItems(List<DbEntry> hotseatToBeAdded,
List<DbEntry> dstHotseatItems, int destHotseatSize,
DatabaseHelper helper, GridSizeMigrationDBController.DbReader srcReader,
- GridSizeMigrationDBController.DbReader destReader) {
+ GridSizeMigrationDBController.DbReader destReader, List<Integer> idsInUse) {
if (hotseatToBeAdded.isEmpty()) {
return;
}
- List<Integer> idsInUse = dstHotseatItems.stream().map(entry -> entry.id).toList();
+ idsInUse.addAll(dstHotseatItems.stream().map(entry -> entry.id).toList());
Collections.sort(hotseatToBeAdded);
@@ -168,11 +181,14 @@
}
}
- private void migrateWorkspace(GridSizeMigrationDBController.DbReader srcReader,
+
+ /**
+ * Handles workspace migration.
+ */
+ @VisibleForTesting
+ public void migrateWorkspace(GridSizeMigrationDBController.DbReader srcReader,
GridSizeMigrationDBController.DbReader destReader, DatabaseHelper helper,
- Point targetSize) {
-
-
+ Point targetSize, List<Integer> idsInUse) {
final List<DbEntry> srcWorkspaceItems =
srcReader.loadAllWorkspaceEntries();
@@ -213,7 +229,7 @@
}
placeWorkspaceItems(workspaceToBeAdded, dstWorkspaceItems, targetSize.x, targetSize.y,
- helper, srcReader, destReader);
+ helper, srcReader, destReader, idsInUse);
}
private void placeWorkspaceItems(
@@ -221,13 +237,12 @@
List<DbEntry> dstWorkspaceItems,
int trgX, int trgY, DatabaseHelper helper,
GridSizeMigrationDBController.DbReader srcReader,
- GridSizeMigrationDBController.DbReader destReader) {
+ GridSizeMigrationDBController.DbReader destReader, List<Integer> idsInUse) {
if (workspaceToBeAdded.isEmpty()) {
return;
}
- List<Integer> idsInUse = dstWorkspaceItems.stream().map(entry -> entry.id).collect(
- Collectors.toList());
+ idsInUse.addAll(dstWorkspaceItems.stream().map(entry -> entry.id).toList());
Collections.sort(workspaceToBeAdded);
diff --git a/src/com/android/launcher3/model/ModelDbController.java b/src/com/android/launcher3/model/ModelDbController.java
index 4f0f162..8c3e860 100644
--- a/src/com/android/launcher3/model/ModelDbController.java
+++ b/src/com/android/launcher3/model/ModelDbController.java
@@ -325,7 +325,7 @@
private boolean isThereExistingDb() {
if (LauncherPrefs.get(mContext).get(getEmptyDbCreatedKey())) {
// If we already have a new DB, ignore migration
- Log.d(TAG, "migrateGridIfNeeded: new DB already created, skipping migration");
+ FileLog.d(TAG, "migrateGridIfNeeded: new DB already created, skipping migration");
return true;
}
return false;
@@ -336,7 +336,7 @@
if (GridSizeMigrationDBController.needsToMigrate(mContext, idp)) {
return true;
}
- Log.d(TAG, "migrateGridIfNeeded: no grid migration needed");
+ FileLog.d(TAG, "migrateGridIfNeeded: no grid migration needed");
return false;
}
@@ -344,7 +344,7 @@
InvariantDeviceProfile idp = LauncherAppState.getIDP(mContext);
String targetDbName = new DeviceGridState(idp).getDbFile();
if (TextUtils.equals(targetDbName, mOpenHelper.getDatabaseName())) {
- Log.e(TAG, "migrateGridIfNeeded: target db is same as current: " + targetDbName);
+ FileLog.e(TAG, "migrateGridIfNeeded: target db is same as current: " + targetDbName);
return true;
}
return false;
@@ -428,7 +428,7 @@
}
InvariantDeviceProfile idp = LauncherAppState.getIDP(mContext);
if (!GridSizeMigrationDBController.needsToMigrate(mContext, idp)) {
- Log.d(TAG, "migrateGridIfNeeded: no grid migration needed");
+ FileLog.d(TAG, "migrateGridIfNeeded: no grid migration needed");
return true;
}
String targetDbName = new DeviceGridState(idp).getDbFile();
diff --git a/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationDBControllerTest.kt b/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationTest.kt
similarity index 94%
rename from tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationDBControllerTest.kt
rename to tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationTest.kt
index c6f291d..7933331 100644
--- a/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationDBControllerTest.kt
+++ b/tests/multivalentTests/src/com/android/launcher3/model/GridSizeMigrationTest.kt
@@ -41,10 +41,10 @@
import org.junit.Test
import org.junit.runner.RunWith
-/** Unit tests for [GridSizeMigrationUtil] */
+/** Unit tests for [GridSizeMigrationDBController, GridSizeMigrationLogic] */
@SmallTest
@RunWith(AndroidJUnit4::class)
-class GridSizeMigrationUtilTest {
+class GridSizeMigrationTest {
private lateinit var modelHelper: LauncherModelHelper
private lateinit var context: Context
@@ -130,13 +130,21 @@
val srcReader = DbReader(db, TMP_TABLE, context)
val destReader = DbReader(db, TABLE_NAME, context)
if (Flags.gridMigrationRefactor()) {
- val gridSizeMigrationLogic = GridSizeMigrationLogic()
- gridSizeMigrationLogic.migrateGrid(
- context,
- DeviceGridState(context),
- DeviceGridState(idp),
+ var gridSizeMigrationLogic = GridSizeMigrationLogic()
+ val idsInUse = mutableListOf<Int>()
+ gridSizeMigrationLogic.migrateHotseat(
+ idp.numDatabaseHotseatIcons,
+ srcReader,
+ destReader,
dbHelper,
- db,
+ idsInUse,
+ )
+ gridSizeMigrationLogic.migrateWorkspace(
+ srcReader,
+ destReader,
+ dbHelper,
+ Point(idp.numColumns, idp.numRows),
+ idsInUse,
)
} else {
GridSizeMigrationDBController.migrate(
@@ -266,12 +274,20 @@
// migrate from A -> B
if (Flags.gridMigrationRefactor()) {
var gridSizeMigrationLogic = GridSizeMigrationLogic()
- gridSizeMigrationLogic.migrateGrid(
- context,
- DeviceGridState(context),
- DeviceGridState(idp),
+ val idsInUse = mutableListOf<Int>()
+ gridSizeMigrationLogic.migrateHotseat(
+ idp.numDatabaseHotseatIcons,
+ readerGridA,
+ readerGridB,
dbHelper,
- db,
+ idsInUse,
+ )
+ gridSizeMigrationLogic.migrateWorkspace(
+ readerGridA,
+ readerGridB,
+ dbHelper,
+ Point(idp.numColumns, idp.numRows),
+ idsInUse,
)
} else {
GridSizeMigrationDBController.migrate(
@@ -445,12 +461,20 @@
) {
if (Flags.gridMigrationRefactor()) {
var gridSizeMigrationLogic = GridSizeMigrationLogic()
- gridSizeMigrationLogic.migrateGrid(
- context,
- DeviceGridState(context),
- DeviceGridState(idp),
+ val idsInUse = mutableListOf<Int>()
+ gridSizeMigrationLogic.migrateHotseat(
+ idp.numDatabaseHotseatIcons,
+ srcReader,
+ destReader,
dbHelper,
- db,
+ idsInUse,
+ )
+ gridSizeMigrationLogic.migrateWorkspace(
+ srcReader,
+ destReader,
+ dbHelper,
+ Point(idp.numColumns, idp.numRows),
+ idsInUse,
)
} else {
GridSizeMigrationDBController.migrate(
diff --git a/tests/src/com/android/launcher3/model/gridmigration/ValidGridMigrationUnitTest.kt b/tests/src/com/android/launcher3/model/gridmigration/ValidGridMigrationUnitTest.kt
index c08237c..b96dbcd 100644
--- a/tests/src/com/android/launcher3/model/gridmigration/ValidGridMigrationUnitTest.kt
+++ b/tests/src/com/android/launcher3/model/gridmigration/ValidGridMigrationUnitTest.kt
@@ -136,12 +136,20 @@
LauncherDbUtils.SQLiteTransaction(dbHelper.writableDatabase).use {
if (Flags.gridMigrationRefactor()) {
val gridSizeMigrationLogic = GridSizeMigrationLogic()
- gridSizeMigrationLogic.migrateGrid(
- context,
- srcGrid.toGridState(),
- dstGrid.toGridState(),
+ val idsInUse = mutableListOf<Int>()
+ gridSizeMigrationLogic.migrateHotseat(
+ dstGrid.size.x,
+ GridSizeMigrationDBController.DbReader(it.db, srcGrid.tableName, context),
+ GridSizeMigrationDBController.DbReader(it.db, dstGrid.tableName, context),
dbHelper,
- it.db,
+ idsInUse,
+ )
+ gridSizeMigrationLogic.migrateWorkspace(
+ GridSizeMigrationDBController.DbReader(it.db, srcGrid.tableName, context),
+ GridSizeMigrationDBController.DbReader(it.db, dstGrid.tableName, context),
+ dbHelper,
+ dstGrid.size,
+ idsInUse,
)
} else {
GridSizeMigrationDBController.migrate(