Modern Apps git

← Modern-Apps.git

launcher: a deliberately cleared home screen is no longer reseeded

Commit
ec51f81b05f1d4498176f9b6b54eeead01bcb6bb
Author
Vayun Mathur <[email protected]>
When
2026-11-30 21:15 UTC (5 days ago)
Parents
6e471f3
Removing every home/hotseat item silently brought the defaults
back on the next app-list refresh: seedIfEmpty decided 'first
run' solely from the table being empty, a state identical to
'user cleared everything' — despite two comments claiming the
opposite. The next package event or relaunch resurrected the
default hotseat and first row.

A persisted launcher_seeded flag now distinguishes the two
states: seeding sets it, a non-empty table also sets it (a
hand-built layout counts as seeded), and empty + flag set means
deliberately cleared — never reseeded. The check goes through
getBooleanAwait so a cold-start mirror race can't resurrect the
guard, and the repository doc no longer repeats the conflation.
New regression tests cover cleared-screen and hand-built-layout
cases (failed pre-fix). Install/uninstall reconcile, folders,
search, widgets and wear sync were audited and cleared. Verified:
:launcher:testDevUnitTest 395 tests (one pre-existing drawer
loading failure, untouched); lint findings pre-existing.

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 1855b37a7d..f8c5631ac3 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/data/LauncherRepository.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/data/LauncherRepository.kt@@ -89,8 +89,10 @@ class LauncherRepository private constructor(context: Context) :     /**      * Seeds the first run.      *-     * Called only when the table is empty, so a user who deliberately empties their home-     * screen does not get it refilled on the next launch.+     * The transaction keeps this a no-op when rows already exist. Whether an empty+     * table is a first run or a home screen the user deliberately emptied is the+     * caller's call (the seeded flag in `seedIfEmptyImpl`): the row count cannot+     * tell the two states apart.      */     suspend fun seed(spec: GridSpec, apps: List<LauncherItemEntity>, hotseat: List<LauncherItemEntity>) {         db.withWriteTransaction {
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 0827572ea3..73e5a5d653 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherLifecycleOps.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherLifecycleOps.kt@@ -215,12 +215,19 @@ internal fun LauncherViewModel.labelForImpl(entity: LauncherItemEntity): String  * Seeds a first run: the hotseat from the apps that answer the obvious intents, and one  * row on the first page from what is left.  *- * Guarded on the table being empty rather than on a "seeded" flag, so a user who- * deliberately clears their home screen does not have it refilled on the next launch.+ * An empty table cannot tell a first run from a user who removed every item, so the+ * decision rides on the persisted [KEY_SEEDED] flag: seeding sets it, and a non-empty+ * table sets it too, because a layout existing at all means the first run is over.+ * Without the flag, the next package event (or the next launch) would resurrect the+ * seeded layout over a home screen the user deliberately cleared.  */ internal fun LauncherViewModel.seedIfEmptyImpl() {     viewModelScope.launch {-        if (!repository.isEmpty()) return@launch+        if (!repository.isEmpty()) {+            if (!ds.getBooleanAwait(KEY_SEEDED, false)) ds.setBoolean(KEY_SEEDED, true)+            return@launch+        }+        if (ds.getBooleanAwait(KEY_SEEDED, false)) return@launch         val apps = appsMonitor.apps.value         if (apps.isEmpty()) return@launch @@ -239,6 +246,7 @@ internal fun LauncherViewModel.seedIfEmptyImpl() {             apps = firstRow.map(::entityForAppImpl),             hotseat = hotseatApps.map(::entityForAppImpl),         )+        ds.setBoolean(KEY_SEEDED, true)     } } 
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 8d115057aa..76621f98be 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherViewModel.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherViewModel.kt@@ -149,6 +149,7 @@ internal const val KEY_HOTSEAT = "launcher_hotseat_slots" internal const val KEY_SHOW_LABELS = "launcher_show_labels" internal const val KEY_ICON_SCALE = "launcher_icon_scale" internal const val KEY_DRAWER_LIST_LAYOUT = "launcher_drawer_list_layout"+internal const val KEY_SEEDED = "launcher_seeded"  /** Launcher3's own all-apps blur, in pixels; density-independent enough not to be a dp. */ internal const val WALLPAPER_BLUR_PX = 60
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 3e611a1155..b63a8206dd 100644--- a/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherViewModelOpsTest.kt+++ b/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherViewModelOpsTest.kt@@ -6,6 +6,7 @@ import android.content.ComponentName import android.os.Bundle import android.os.Looper import android.os.Process+import androidx.lifecycle.viewModelScope import androidx.test.core.app.ApplicationProvider import com.vayunmathur.launcher.data.LauncherItemEntity import com.vayunmathur.launcher.data.LauncherRepository@@ -26,6 +27,7 @@ import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue import kotlin.test.fail+import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner@@ -69,6 +71,24 @@ class LauncherViewModelOpsTest {         // test classes' ViewModels collect from it after this class ends.     } +    /**+     * Clears the persisted seeded flag through the ViewModel's own scope:+     * a suspending DataStore write issued straight from this (paused main)+     * thread never gets the looper turns it needs to complete.+     */+    private fun resetSeededFlag() {+        vm.viewModelScope.launch { vm.ds.setBoolean(KEY_SEEDED, false) }+        flushDataStoreWrites()+    }++    /** Gives queued DataStore writes time to land (idle turns plus sleeps). */+    private fun flushDataStoreWrites() {+        repeat(300) {+            shadowOf(Looper.getMainLooper()).idle()+            Thread.sleep(10)+        }+    }+     private fun await(description: String, cond: () -> Boolean) {         repeat(200) {             shadowOf(Looper.getMainLooper()).idle()@@ -476,6 +496,9 @@ class LauncherViewModelOpsTest {     }      @Test fun seedIfEmptySeedsFromMonitorApps() {+        // The seeded flag lives in the shared DataStore, so each seed test+        // starts from a genuine first run regardless of test order.+        resetSeededFlag()         setMonitorApps((0 until 7).map { appEntry("com.example.s$it", label = "Seed$it") })         vm.seedIfEmptyImpl()         await("seeded rows") { all().isNotEmpty() }@@ -484,6 +507,54 @@ class LauncherViewModelOpsTest {         assertTrue(all().all { it.itemType == LauncherItemType.APPLICATION })     } +    @Test fun clearedHomeScreenIsNotReseeded() {+        resetSeededFlag()+        setMonitorApps((0 until 7).map { appEntry("com.example.s$it", label = "Seed$it") })+        vm.seedIfEmptyImpl()+        await("seeded rows") { all().isNotEmpty() }+        flushDataStoreWrites()+        // The user removes every item: the table is empty again, which is+        // exactly the state a first run starts in. Empty alone cannot tell+        // the two apart.+        all().map { it.id }.forEach { vm.remove(it) }+        await("home cleared") { all().isEmpty() }+        // The next app-list refresh (any package event, or a relaunch)+        // must not resurrect the seeded layout over the deliberate clear.+        vm.seedIfEmptyImpl()+        repeat(100) {+            shadowOf(Looper.getMainLooper()).idle()+            if (all().isNotEmpty()) fail("cleared home screen was reseeded: ${all()}")+            Thread.sleep(10)+        }+        assertTrue(all().isEmpty())+    }++    @Test fun existingLayoutCountsAsSeeded() {+        resetSeededFlag()+        setMonitorApps((0 until 7).map { appEntry("com.example.s$it", label = "Seed$it") })+        // A layout the user built by hand: no seeding, but its existence+        // records that the first run is over.+        vm.addPendingToHome(+            ComponentKey(ComponentName("com.example.manual", "com.example.manual.Main"), 0),+            screen = 0,+            rect = CellRect(0, 0),+        )+        await("manual row added") { all().isNotEmpty() }+        vm.seedIfEmptyImpl()+        // The recording write is asynchronous; give it time to land before+        // the clear below, or the second call could still read it as unset.+        flushDataStoreWrites()+        all().map { it.id }.forEach { vm.remove(it) }+        await("home cleared") { all().isEmpty() }+        vm.seedIfEmptyImpl()+        repeat(100) {+            shadowOf(Looper.getMainLooper()).idle()+            if (all().isNotEmpty()) fail("hand-built layout was reseeded after a clear: ${all()}")+            Thread.sleep(10)+        }+        assertTrue(all().isEmpty())+    }+     @Test fun reconcileNowDeletesRowsForUninstalledApps() = runTest {         val gone = holder.repository.upsert(entity("com.example.gone", title = "Gone"))         holder.repository.upsert(entity("com.example.kept", title = "Kept", cellX = 1))