launcher: hide work apps when profile is off at cold start
- Commit
5bdf1de7c9ab9187eac9a3e8fd55f521f5eae99f- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 19:58 UTC (28 days ago)
- Parents
24bfae9
Symptom: restarting the launcher (or the device) while the work profile is in quiet mode permanently deletes every work-profile icon, folder child and hotseat entry from the home screen. Root cause: ReconcileUseCase hides rather than deletes only for packages in the unavailable set, but that set is accumulated from LauncherApps onPackagesUnavailable callbacks received since this process started. A profile already quiet at cold start has sent no callback, its packages are absent from getActivityList, and reconciliation therefore mistook them for uninstalled. Fix: read the live quiet-mode state of every profile from UserManager in LauncherAppsMonitor.unavailableProfileSerials, thread it through LauncherRepository.reconcile, and have ReconcileUseCase hide (never delete) items whose profileSerial is in that set. The profile check runs before the installed check, and already-hidden items produce no redundant write. Verification: new ReconcileUseCaseTest cases cover an app in an off profile with an empty unavailable set (hidden, not deleted) and the already-hidden no-write case. Full module verification runs at the end of the stack.
Diff
launcher/src/main/java/com/vayunmathur/launcher/data/LauncherRepository.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/data/LauncherRepository.kt b/launcher/src/main/java/com/vayunmathur/launcher/data/LauncherRepository.ktindex 420a8d5b14..6f9e7427bb 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/data/LauncherRepository.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/data/LauncherRepository.kt@@ -303,6 +303,7 @@ class LauncherRepository private constructor(context: Context) : installed: Set<PackageKey>, unavailable: Set<PackageKey>, boundWidgetIds: Set<Int>,+ unavailableProfiles: Set<Long> = emptySet(), ): List<Int> = db.withWriteTransaction { val all = dao.getAll() val actions = ReconcileUseCase.reconcile(@@ -310,6 +311,7 @@ class LauncherRepository private constructor(context: Context) : installed = installed, unavailable = unavailable, boundWidgetIds = boundWidgetIds,+ unavailableProfiles = unavailableProfiles, ) val byId = all.associateBy { it.id } val orphanedWidgetIds = mutableListOf<Int>()
launcher/src/main/java/com/vayunmathur/launcher/domain/ReconcileUseCase.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/domain/ReconcileUseCase.kt b/launcher/src/main/java/com/vayunmathur/launcher/domain/ReconcileUseCase.ktindex 88b14f1caf..daf61ac24e 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/domain/ReconcileUseCase.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/domain/ReconcileUseCase.kt@@ -47,6 +47,7 @@ object ReconcileUseCase { installed: Set<PackageKey>, unavailable: Set<PackageKey>, boundWidgetIds: Set<Int>,+ unavailableProfiles: Set<Long> = emptySet(), ): List<Action> = items.mapNotNull { item -> when (item.type) { // A folder's existence depends on its children, which FolderRules owns.@@ -63,6 +64,14 @@ object ReconcileUseCase { val key = item.key when { key == null -> Action.Delete(item.id)+ // A profile that is off (quiet mode) at cold start has sent+ // no onPackagesUnavailable callback yet, and its packages+ // cannot be enumerated to fill [unavailable]. The profile+ // itself is the only reliable signal, so hide by profile:+ // deleting here wipes every work-profile icon whenever+ // the launcher restarts while the profile is paused.+ item.profileSerial in unavailableProfiles ->+ Action.SetHidden(item.id, true).takeUnless { item.hidden } key in unavailable -> Action.SetHidden(item.id, true).takeUnless { item.hidden } key in installed -> Action.SetHidden(item.id, false).takeIf { item.hidden } else -> Action.Delete(item.id)
launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.kt b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.ktindex 293c208fdc..bf0fb06c7d 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.kt@@ -177,6 +177,21 @@ class LauncherAppsMonitor(context: Context, private val scope: CoroutineScope) { /** Packages present and usable right now, for reconciliation. */ fun installedKeys(): Set<PackageKey> = _apps.value.map { it.packageKey }.toSet() + /**+ * Profiles that exist but are currently off (quiet mode), by serial.+ *+ * Read live rather than accumulated from callbacks: a profile that was+ * already quiet when this process started has delivered no+ * `onPackagesUnavailable`, so the callback-built [unavailable] set is+ * empty for it and reconciliation would mistake its apps for uninstalled.+ */+ fun unavailableProfileSerials(): Set<Long> = runCatching {+ launcherApps.profiles+ .filter { userManager.isQuietModeEnabled(it) }+ .map { serialFor(it) }+ .toSet()+ }.getOrDefault(emptySet())+ fun icon(entry: AppEntry, densityDpi: Int): Drawable? = runCatching { val info = launcherApps.resolveActivity( android.content.Intent().setComponent(entry.componentName),
launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherLifecycleOps.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherLifecycleOps.kt b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherLifecycleOps.ktindex edfb72d157..cce1d7565b 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherLifecycleOps.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherLifecycleOps.kt@@ -50,6 +50,7 @@ internal fun LauncherViewModel.reconcileNowImpl() { installed = installed, unavailable = appsMonitor.unavailable.value, boundWidgetIds = widgetHost.boundIds(),+ unavailableProfiles = appsMonitor.unavailableProfileSerials(), ) orphaned.forEach(widgets::release) widgets.releaseOrphans(repository.usedWidgetIds())
launcher/src/test/java/com/vayunmathur/launcher/domain/LauncherDomainTest.kt
diff --git a/launcher/src/test/java/com/vayunmathur/launcher/domain/LauncherDomainTest.kt b/launcher/src/test/java/com/vayunmathur/launcher/domain/LauncherDomainTest.ktindex 3a9a8d3c85..a16e196f8d 100644--- a/launcher/src/test/java/com/vayunmathur/launcher/domain/LauncherDomainTest.kt+++ b/launcher/src/test/java/com/vayunmathur/launcher/domain/LauncherDomainTest.kt@@ -255,6 +255,34 @@ class ReconcileUseCaseTest { assertTrue(actions.isEmpty()) } + @Test+ fun `an app in a profile that is off is hidden even with no unavailable callback`() {+ // Cold start with the work profile already in quiet mode: no+ // onPackagesUnavailable has been received, so `unavailable` is empty+ // and the package is absent from `installed`. Only the profile+ // signal stands between this row and deletion.+ val actions = ReconcileUseCase.reconcile(+ items = listOf(app(1, work)),+ installed = setOf(here),+ unavailable = emptySet(),+ boundWidgetIds = emptySet(),+ unavailableProfiles = setOf(work.profileSerial),+ )+ assertEquals(listOf(ReconcileUseCase.Action.SetHidden(1, true)), actions)+ }++ @Test+ fun `an already-hidden app in an off profile produces no redundant write`() {+ val actions = ReconcileUseCase.reconcile(+ items = listOf(app(1, work, hidden = true)),+ installed = setOf(here),+ unavailable = emptySet(),+ boundWidgetIds = emptySet(),+ unavailableProfiles = setOf(work.profileSerial),+ )+ assertTrue(actions.isEmpty())+ }+ @Test fun `a widget whose id the host has forgotten is deleted`() { val widget = ReconcileUseCase.Item(