Modern Apps git

← Modern-Apps.git

keyboard: clipboard history no longer duplicates re-captured images

Commit
76b9fef67b26ca0f99d9c5d541a0edd297674318
Author
Vayun Mathur <[email protected]>
When
2026-11-30 21:40 UTC (5 days ago)
Parents
e462ba2
captureImage de-duplicates images by bytes and returns the
existing entry — but add() only recognised duplicates by text
content (sameContentAs is false whenever either side is an
image), so every field start re-sweep added the same entry again:
two history items sharing one id and one backing file, and a
later cap() eviction could delete that file out from under the
surviving copy.

add() now matches by id first, so a re-recorded image is a
move-to-front returning null, matching the documented contract
and the text path. Regression test fails pre-fix and passes now.
Composers, autocorrect, text-mirror arithmetic, layouts and
settings persistence were audited and cleared. Verified:
:keyboard:testDevUnitTest 191 tests and lint (0 errors) pass.

Diff

keyboard/src/main/java/com/vayunmathur/keyboard/data/Clipboard.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/data/Clipboard.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/data/Clipboard.ktindex f25b839848..a83d2762e1 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/data/Clipboard.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/data/Clipboard.kt@@ -96,7 +96,10 @@ class ClipboardStore(private val imageDir: File) {      * pop up a chip for a clip the user is only re-using.      */     fun add(item: ClipItem): ClipItem? {-        val existing = items.firstOrNull { it.sameContentAs(item) }+        // An image re-capture hands back the entry already kept (see captureImage), so+        // identity is a duplicate too: sameContentAs is text-only and would prepend a+        // second entry with the same id and the same image file.+        val existing = items.firstOrNull { it.id == item.id || it.sameContentAs(item) }         if (existing != null) {             // Sensitivity only ever upgrades: the same text re-copied from a             // password field is a credential now, whatever it was before, and
keyboard/src/test/java/com/vayunmathur/keyboard/data/ClipboardTest.kt
diff --git a/keyboard/src/test/java/com/vayunmathur/keyboard/data/ClipboardTest.kt b/keyboard/src/test/java/com/vayunmathur/keyboard/data/ClipboardTest.ktindex 9247f17e0f..fc787d43f5 100644--- a/keyboard/src/test/java/com/vayunmathur/keyboard/data/ClipboardTest.kt+++ b/keyboard/src/test/java/com/vayunmathur/keyboard/data/ClipboardTest.kt@@ -93,6 +93,23 @@ class ClipboardTest {         assertEquals(listOf("one", "two"), store.items.map { it.text })     } +    /**+     * Image capture de-duplicates by bytes and hands back the entry already kept; the+     * service then records that entry again on every field-start re-sweep. Recording it+     * must be a move-to-front, not a second entry with the same id and image file.+     */+    @Test+    fun `re-recording a captured image does not duplicate it`() {+        val store = ClipboardStore(java.io.File("/tmp/clip-test-does-not-exist"))+        val captured = image(1)+        assertEquals(captured, store.add(captured))+        store.add(text(2, "two"))+        val returnedAgain = store.items.first { it.isImage }+        assertEquals(null, store.add(returnedAgain), "a re-captured image is not a new clip")+        assertEquals(listOf(1L, 2L), store.items.map { it.id })+        assertEquals(1, store.items.count { it.isImage })+    }+     /** The same text copied from a password field must not stay plain in the history. */     @Test     fun `re-copying as sensitive upgrades the existing entry`() {