keyboard: de-duplicate re-captured image clips by bytes
- Commit
e65e9feeb8fee252a40e42ab8fdf2584fc95f701- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:04 UTC (28 days ago)
- Parents
df561d3
Symptom: copy an image, then keep using the keyboard. Every onStartInputView sweeps the current system clip (to catch copies made while the keyboard was away), and each sweep added another copy of the same image: a new cached file and another history entry, until the 10-image cap filled the clipboard page with duplicates and text history was crowded out. Root cause: capture() copies image bytes into a fresh file under a fresh id, and ClipItem.sameContentAs deliberately returns false for images (it has no content to compare), so ClipboardStore.add could never recognise the re-capture. Text clips were immune because they de-duplicate on the string itself. Fix: after copying the bytes, capture() compares the new file against the images already in the history (on the IO dispatcher, length first, then bytes). On a match it deletes the fresh file and returns the existing entry, so add() simply moves it to the front and no chip is offered, exactly like a text re-copy. Verification: new ClipboardTest coverage for the byte comparison (same/different/longer/missing files); ClipboardTest and PlatformRoboTest pass.
Diff
keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.ktindex b623522a75..57e494f40b 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt@@ -142,6 +142,19 @@ class ClipboardStore(private val imageDir: File) { val id = nextId() val file = withContext(Dispatchers.IO) { copyImage(context, uri, id, mime) } ?: return null+ // The service re-sweeps the system clip on every field start, and an+ // image cannot be content-compared the way text is (sameContentAs),+ // so without this the same image is re-copied into a new file and a+ // duplicate history entry every time the keyboard opens, crowding+ // out the history up to the image cap. Same bytes = same clip:+ // drop the fresh copy and hand back the entry already kept.+ val duplicate = withContext(Dispatchers.IO) {+ items.firstOrNull { it.isImage && sameFileBytes(it.imageFile, file) }+ }+ if (duplicate != null) {+ withContext(Dispatchers.IO) { runCatching { file.delete() } }+ return duplicate+ } return ClipItem( id = id, imagePath = file.path,@@ -238,6 +251,15 @@ class ClipboardStore(private val imageDir: File) { private val json = Json { ignoreUnknownKeys = true } + /** True when both files exist with identical bytes; a cheap length check first. */+ internal fun sameFileBytes(a: File?, b: File?): Boolean {+ if (a == null || b == null) return false+ return runCatching {+ a.exists() && b.exists() && a.length() == b.length() &&+ a.readBytes().contentEquals(b.readBytes())+ }.getOrDefault(false)+ }+ /** * Trim to the caps, newest first, handing every dropped clip to [onDropped] so its * cached image file goes with it.
keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.kt
diff --git a/keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.kt b/keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.ktindex 71e01265a8..3926d5f74d 100644--- a/keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.kt+++ b/keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.kt@@ -115,6 +115,25 @@ class ClipboardTest { assertTrue(ids.zipWithNext().all { (a, b) -> b > a }, "ids must be monotonic") } + /** Image de-duplication compares cached bytes, not ids, names or timestamps. */+ @Test+ fun `sameFileBytes compares image file contents`() {+ val dir = kotlin.io.path.createTempDirectory("clips").toFile()+ try {+ val a = java.io.File(dir, "a.png").apply { writeBytes(byteArrayOf(1, 2, 3)) }+ val same = java.io.File(dir, "b.png").apply { writeBytes(byteArrayOf(1, 2, 3)) }+ val different = java.io.File(dir, "c.png").apply { writeBytes(byteArrayOf(1, 2, 4)) }+ val longer = java.io.File(dir, "d.png").apply { writeBytes(byteArrayOf(1, 2, 3, 4)) }+ assertTrue(ClipboardStore.sameFileBytes(a, same))+ assertFalse(ClipboardStore.sameFileBytes(a, different))+ assertFalse(ClipboardStore.sameFileBytes(a, longer))+ assertFalse(ClipboardStore.sameFileBytes(a, java.io.File(dir, "missing.png")))+ assertFalse(ClipboardStore.sameFileBytes(null, a))+ } finally {+ dir.deleteRecursively()+ }+ }+ // --- persistence --- @Test