keyboard: honour clear-history when IME was not running
- Commit
7204d558120daf15fa6fe222a07b94e597a1cc8f- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:06 UTC (29 days ago)
- Parents
1f6a665
Symptom: tap 'clear clipboard history' in the app settings while
the keyboard service is not running (never selected, or its process
was killed), then open the keyboard: the old history is restored from
disk and the clear is silently lost.
Root cause: the wipe watcher's first flow emission was treated as
the baseline ('the stored value, not a request'), because a counter
alone cannot tell a fresh request from an applied one. A clear that
landed while nobody was collecting became exactly such a first
emission, and the startup restore brought the clips back.
Fix: persist what has been carried out under kb_clips_wipe_applied.
Each wipe emission is compared against that marker: a counter ahead
of the marker is a real, still-outstanding request and wipes (even
on the first emission after a cold start), and the marker is then
advanced. The marker's first write simply adopts the current counter,
so an upgrade never re-wipes a history that legitimately grew after
an old, already-applied clear.
Verification: no service-level regression test (the watcher is a
DataStore flow raced against the service's async disk restore, which
Robolectric cannot drive deterministically); full
:keyboard:testDevUnitTest and :keyboard:compileDevKotlin pass.Diff
keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardServiceSetupOps.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardServiceSetupOps.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardServiceSetupOps.ktindex 49e9856e6a..3a42692f2b 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardServiceSetupOps.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardServiceSetupOps.kt@@ -29,19 +29,18 @@ internal fun KeyboardService.observeSettings() { } // Settings wipes the history by bumping the wipe counter; a counter (not a blank // string) means the one-time legacy migration below can clear the old key without- // looking like a wipe request. The first emission is the stored value, not a request.+ // looking like a wipe request. What has actually been carried out is persisted in+ // CLIPS_WIPE_APPLIED, so a clear tapped while this service was not running is+ // still honoured on the next start instead of being read as the baseline, while+ // an already-applied wipe (the marker's first write) never fires twice. scope.launch {- var first = true- var seen = 0L- ds.longFlow(keys.CLIPS_WIPE, 0L).collectLatest {- if (first) {- seen = it- first = false- return@collectLatest- }- if (it > seen) {- seen = it+ ds.longFlow(keys.CLIPS_WIPE, 0L).collectLatest { wipe ->+ val applied = ds.getLong(keys.CLIPS_WIPE_APPLIED)+ if (applied == null) {+ ds.setLong(keys.CLIPS_WIPE_APPLIED, wipe)+ } else if (wipe > applied) { forgetClips()+ ds.setLong(keys.CLIPS_WIPE_APPLIED, wipe) } } }
keyboard/src/main/java/com/vayunmathur/keyboard/util/KeyboardPrefs.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/util/KeyboardPrefs.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/util/KeyboardPrefs.ktindex b308e84228..ef7981c6d1 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/util/KeyboardPrefs.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/util/KeyboardPrefs.kt@@ -43,6 +43,13 @@ data class KeyboardSettings( * startup migration clearing the legacy key looked like a wipe request. */ const val CLIPS_WIPE = "kb_clips_wipe"+ /**+ * The wipe-counter value the IME has actually carried out. A clear requested+ * while the service is not running has to survive until its next start, which+ * a "first flow emission is the baseline" rule cannot distinguish from an+ * already-applied wipe; comparing against this marker can.+ */+ const val CLIPS_WIPE_APPLIED = "kb_clips_wipe_applied" const val EMOJI_RECENTS = "kb_emoji_recents" const val KEY_HEIGHT = "kb_key_height" const val ACTIVE_LAYOUT = "kb_active_layout"