launcher: predictions no longer merge an app's personal and work copies
- Commit
ea54fddc0430d1c6c29437b550f8e54e17dce8a5- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 02:10 UTC (5 days ago)
- Parents
a979197
Launcher identity includes the user profile everywhere (ComponentKey, IconCache, persisted rows) — except LauncherPredictions, which keyed launch counts and prediction lookups by component alone. With a personal app and its work -profile twin installed, whichever copy was listed last won the merged count, and the personal-only profile filter then dropped it: launching the personal copy surfaced no prediction at all, and launching the work copy promoted the personal copy into the personal-only row. Counts are now keyed by the full ComponentKey and looked up the same way. The persisted format gains the serial as a third tab-separated field; legacy two-field lines load against serial 0 (the primary profile — the only copy the merged counts could ever have displayed), malformed lines skip instead of failing the load. Two regression tests failed pre-fix in both profile directions; a third pins the persistence/legacy parse. Grid/folder/reconcile math, widget lifecycle, search ranking and Wear sync were audited and cleared. Verified: the ViewModel ops class passes incl. the new tests; full-suite failures are the documented pre-existing drawer failure plus timing-sensitive seed tests passing in isolation, and the lint error is a pre-existing finding on an untouched file.
Diff
launcher/src/androidTest/java/com/vayunmathur/launcher/platform/LauncherViewModelExtraDeviceTest.kt
diff --git a/launcher/src/androidTest/java/com/vayunmathur/launcher/platform/LauncherViewModelExtraDeviceTest.kt b/launcher/src/androidTest/java/com/vayunmathur/launcher/platform/LauncherViewModelExtraDeviceTest.ktindex 3972b3eafa..1fb7fd5f94 100644--- a/launcher/src/androidTest/java/com/vayunmathur/launcher/platform/LauncherViewModelExtraDeviceTest.kt+++ b/launcher/src/androidTest/java/com/vayunmathur/launcher/platform/LauncherViewModelExtraDeviceTest.kt@@ -99,9 +99,9 @@ class LauncherViewModelExtraDeviceTest { assertTrue(apps.isNotEmpty()) val entry = apps.first() val key = ComponentKey(entry.componentName, entry.profileSerial)- val before = vm.launchCounts[key.componentName.flattenToShortString()] ?: 0L+ val before = vm.launchCounts[key] ?: 0L vm.launchApp(key, 0, 0, 10, 10)- val after = vm.launchCounts[key.componentName.flattenToShortString()] ?: 0L+ val after = vm.launchCounts[key] ?: 0L assertEquals(before + 1, after) back() }
launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherPredictions.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherPredictions.kt b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherPredictions.ktindex 33bff18af3..577d689565 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherPredictions.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherPredictions.kt@@ -1,5 +1,6 @@ package com.vayunmathur.launcher.platform +import android.content.ComponentName import androidx.lifecycle.viewModelScope import kotlinx.coroutines.launch @@ -26,7 +27,13 @@ internal const val KEY_LAUNCH_COUNTS = "launcher_launch_counts" */ internal fun LauncherViewModel.predictedApps(): List<DrawerApp> { if (launchCounts.isEmpty()) return emptyList()- val byKey = allApps.associateBy { it.key.componentName.flattenToShortString() }+ // Keyed by the full ComponentKey, profile included: the same activity in+ // the personal and work profiles is two different apps here, and keying+ // by flattened component alone lets whichever copy is listed last win+ // the slot. The winners then get dropped by the work-profile filter+ // below (losing the personal copy's prediction entirely), or, with the+ // listing order reversed, a work launch promotes its personal twin.+ val byKey = allApps.associateBy { it.key } return launchCounts.entries .sortedByDescending { it.value } .mapNotNull { byKey[it.key] }@@ -41,23 +48,50 @@ internal fun LauncherViewModel.predictedApps(): List<DrawerApp> { * read whole and written whole, and a Room table for it would be a migration for nothing. */ internal fun LauncherViewModel.countLaunch(key: ComponentKey) {- val flattened = key.componentName.flattenToShortString()- launchCounts[flattened] = (launchCounts[flattened] ?: 0) + 1+ launchCounts[key] = (launchCounts[key] ?: 0) + 1 drawerState.value = drawerState.value.copy(predictions = predictedApps()) viewModelScope.launch {- ds.setString(- KEY_LAUNCH_COUNTS,- launchCounts.entries.joinToString("\n") { "${it.key}\t${it.value}" },- )+ ds.setString(KEY_LAUNCH_COUNTS, serializeLaunchCounts(launchCounts)) } } internal fun LauncherViewModel.loadLaunchCounts() {- val stored = ds.getString(KEY_LAUNCH_COUNTS).orEmpty()+ launchCounts.putAll(parseLaunchCounts(ds.getString(KEY_LAUNCH_COUNTS).orEmpty()))+}++/**+ * One counter per line: flattened component, profile serial, count.+ *+ * The serial is part of the key because the count belongs to one profile's+ * copy of the activity (see [predictedApps]).+ */+internal fun serializeLaunchCounts(counts: Map<ComponentKey, Long>): String =+ counts.entries.joinToString("\n") { (key, count) ->+ "${key.componentName.flattenToShortString()}\t${key.profileSerial}\t$count"+ }++/**+ * Parses [serializeLaunchCounts] output, plus the legacy two-field lines+ * (`component<TAB>count`) written before the serial was part of the key.+ * Legacy counts were merged across profiles by construction, so they are+ * attributed to the primary profile (serial 0): that is the copy the+ * predictions row could ever show for them. Malformed lines are skipped+ * rather than failing the whole load.+ */+internal fun parseLaunchCounts(stored: String): MutableMap<ComponentKey, Long> {+ val counts = mutableMapOf<ComponentKey, Long>() stored.lineSequence().forEach { line ->- val (flattened, count) = line.split('\t').takeIf { it.size == 2 } ?: return@forEach- count.toLongOrNull()?.let { launchCounts[flattened] = it }+ val parts = line.split('\t')+ val (flattened, serial, count) = when (parts.size) {+ 3 -> Triple(parts[0], parts[1].toLongOrNull(), parts[2].toLongOrNull())+ 2 -> Triple(parts[0], 0L, parts[1].toLongOrNull())+ else -> return@forEach+ }+ val component = ComponentName.unflattenFromString(flattened) ?: return@forEach+ if (serial == null || count == null) return@forEach+ counts[ComponentKey(component, serial)] = count }+ return counts } internal fun LauncherViewModel.applyDrawerQuery(query: String) {
launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherViewModel.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherViewModel.kt b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherViewModel.ktindex 76621f98be..caa5b67cec 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherViewModel.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherViewModel.kt@@ -86,8 +86,8 @@ class LauncherViewModel(app: Application) : AndroidViewModel(app), /** Cache of the last committed layout, so a drop can look up its neighbours. */ internal var savedItems: List<LauncherItemEntity> = emptyList() - /** Launch counts by flattened component, which is what the predictions row is ordered by. */- internal val launchCounts = mutableMapOf<String, Long>()+ /** Launch counts by activity-in-profile, which is what the predictions row is ordered by. */+ internal val launchCounts = mutableMapOf<ComponentKey, Long>() /** * Whether each package is a system app, cached for the life of the process.
launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherViewModelOpsTest.kt
diff --git a/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherViewModelOpsTest.kt b/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherViewModelOpsTest.ktindex b63a8206dd..08802445d2 100644--- a/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherViewModelOpsTest.kt+++ b/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherViewModelOpsTest.kt@@ -156,8 +156,13 @@ class LauncherViewModelOpsTest { isWorkProfile = work, ) - private fun drawerApp(label: String, pkg: String, work: Boolean = false) = DrawerApp(- key = ComponentKey(ComponentName(pkg, "$pkg.Main"), 0),+ private fun drawerApp(+ label: String,+ pkg: String,+ work: Boolean = false,+ profileSerial: Long = 0,+ ) = DrawerApp(+ key = ComponentKey(ComponentName(pkg, "$pkg.Main"), profileSerial), label = label, isWorkProfile = work, )@@ -228,25 +233,62 @@ class LauncherViewModelOpsTest { listOf(drawerApp("WorkApp", "com.example.work", work = true)) vm.launchCounts.clear() for (i in 0 until 7) {- vm.launchCounts["com.example.a$i/.Main"] = (i + 1).toLong()+ vm.launchCounts[vm.allApps[i].key] = (i + 1).toLong() }- vm.launchCounts["com.example.work/.Main"] = 100L+ vm.launchCounts[vm.allApps.last().key] = 100L val predicted = vm.predictedApps() assertEquals(5, predicted.size) assertEquals("App6", predicted.first().label) assertTrue(predicted.none { it.isWorkProfile }) } + @Test fun predictionsKeepPersonalTwinWhenWorkCopyIsListedLast() {+ // The same activity installed in both profiles: two DrawerApps that+ // differ only by profile serial and the work flag. The monitor lists+ // the personal copy first (profiles in order, stable label sort), so+ // any lookup keyed only by component ends up holding the work copy.+ val personal = drawerApp("Mail", "com.example.mail")+ val work = drawerApp("Mail", "com.example.mail", work = true, profileSerial = 10)+ vm.allApps = listOf(personal, work)+ vm.launchCounts.clear()+ vm.countLaunch(personal.key)+ vm.countLaunch(personal.key)+ assertEquals(listOf(personal), vm.predictedApps())+ }++ @Test fun workLaunchDoesNotPromotePersonalTwin() {+ // The mirror image: with the personal copy listed last, a launch of+ // the work copy must not surface the personal copy as a prediction+ // (predictions are personal-only by design).+ val personal = drawerApp("Mail", "com.example.mail")+ val work = drawerApp("Mail", "com.example.mail", work = true, profileSerial = 10)+ vm.allApps = listOf(work, personal)+ vm.launchCounts.clear()+ vm.countLaunch(work.key)+ assertEquals(emptyList(), vm.predictedApps())+ }+ @Test fun countLaunchIncrementsAndRefreshesPredictions() { val app = drawerApp("Calendar", "com.example.calendar") vm.allApps = listOf(app) vm.launchCounts.clear() vm.countLaunch(app.key) vm.countLaunch(app.key)- assertEquals(2L, vm.launchCounts["com.example.calendar/.Main"])+ assertEquals(2L, vm.launchCounts[app.key]) assertEquals(listOf(app), vm.drawer.value.predictions) } + @Test fun launchCountsPersistPerProfileAndLegacyLinesLoadAsPersonal() {+ val personal = ComponentKey(ComponentName("com.example.mail", "com.example.mail.Main"), 0)+ val work = ComponentKey(ComponentName("com.example.mail", "com.example.mail.Main"), 10)+ val counts = mapOf(personal to 3L, work to 9L)+ assertEquals(counts, parseLaunchCounts(serializeLaunchCounts(counts)))+ // Lines written before the serial joined the key merge both profiles;+ // they load against the primary profile, and junk lines are skipped.+ val legacy = parseLaunchCounts("com.example.mail/.Main\t7\nnot-a-count\n\n")+ assertEquals(mapOf(personal to 7L), legacy)+ }+ // NOTE: a dedicated loadLaunchCounts parse test would need to seed the // persisted string first, but DataStore edits to the default store hang // under Robolectric in this suite (a zombie sandbox instance holds the