Modern Apps git

← Modern-Apps.git

keyboard: detekt clean (190 tests)

Commit
de92d125fa80448631a7945eec45a684cccd7f2c
Author
Vayun Mathur <[email protected]>
When
2026-11-14 23:05 UTC (21 days ago)
Parents
68c7f82
Fix all detekt findings in keyboard by fixing code, not config
(detekt.yml untouched, no baselines added):
- UndocumentedPublicClass: KDoc for 8 types in ime/util.
- CyclomaticComplexMethod/ReturnCount in Clipboard.capture:
  extracted the image branch into captureImage() unchanged.
- LongMethod: split the configureForEditor test into
  field-kinds and enter-actions tests.
Verified: ./gradlew :keyboard:detekt :keyboard:testDevUnitTest :keyboard:lint
--max-workers=32 all pass (190 unit tests).

Diff

keyboard/src/main/java/com/vayunmathur/keyboard/ime/HanComposer.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/HanComposer.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/HanComposer.ktindex e7afd3a9f0..64cae07203 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/HanComposer.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/HanComposer.kt@@ -18,6 +18,7 @@ interface SpellingScheme {     /** The pinyin the buffer spells, or null while it spells nothing yet. */     fun toPinyin(buffer: String): String? +    /** Singleton holding pinyin state and operations. */     object Pinyin : SpellingScheme {         override fun accepts(c: Char) = c in 'a'..'z' || c in 'A'..'Z'         override fun toPinyin(buffer: String) = buffer.lowercase()
keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardState.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardState.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardState.ktindex 11a37729f0..5e88b8f897 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardState.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardState.kt@@ -188,6 +188,7 @@ interface ImeActions {     /** Open this app's system settings, the only way back from a blocked microphone. */     fun openVoiceSettings() +    /** Shared constants and helpers for ImeActions. */     companion object {         /**          * Does nothing. Lets `src/screenshotTest` render [
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 57e494f40b..4c1900cc5f 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt@@ -135,32 +135,7 @@ class ClipboardStore(private val imageDir: File) {         val uri = entry.uri         val mime = uri?.let { context.contentResolver.getType(it) }         if (uri != null && mime != null && mime.startsWith("image/")) {-            // Sensitive images are not recorded at all: bytes on disk would outlive-            // the session while the history entry (excluded from serialization)-            // does not, leaving orphaned credential-adjacent files.-            if (flagged) return null-            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,-                mimeType = mime,-                sensitive = false,-            )+            return captureImage(context, uri, mime, flagged)         }          val text = entry.coerceToText(context).toString().trim()@@ -175,6 +150,36 @@ class ClipboardStore(private val imageDir: File) {         )     } +    /** Image branch of [capture]: copies the image, de-duplicating against history by bytes. */+    private suspend fun captureImage(context: Context, uri: Uri, mime: String, flagged: Boolean): ClipItem? {+        // Sensitive images are not recorded at all: bytes on disk would outlive+        // the session while the history entry (excluded from serialization)+        // does not, leaving orphaned credential-adjacent files.+        if (flagged) return null+        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,+            mimeType = mime,+            sensitive = false,+        )+    }+     private fun copyImage(context: Context, uri: Uri, id: Long, mime: String): File? {         val file = File(imageDir, "$id.${mime.substringAfterLast('/', "png")}")         val ok = runCatching {@@ -204,6 +209,7 @@ class ClipboardStore(private val imageDir: File) {         runCatching { item.imageFile?.delete() }     } +    /** Shared constants and helpers for ClipboardStore. */     companion object {         /** Roughly a day of copying; beyond this the history is an archive, not a clipboard. */         const val MAX_ITEMS = 30
keyboard/src/main/java/com/vayunmathur/keyboard/util/Dictionary.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/util/Dictionary.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/util/Dictionary.ktindex 82c268e7e3..55b7b68106 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/util/Dictionary.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/util/Dictionary.kt@@ -169,6 +169,7 @@ class Dictionary private constructor(         return lo     } +    /** Shared constants and helpers for Dictionary. */     companion object {         /** An empty dictionary used before loading completes. */         val EMPTY = Dictionary(emptyList(), IntArray(0), emptyMap(), emptyMap())
keyboard/src/main/java/com/vayunmathur/keyboard/util/Emojis.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/util/Emojis.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/util/Emojis.ktindex 0eb3fce7b7..edf061a369 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/util/Emojis.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/util/Emojis.kt@@ -99,6 +99,7 @@ class EmojiData(         }     } +    /** Shared constants and helpers for EmojiData. */     companion object {         /** Minimum TSV columns for a valid emoji row (emoji, group, name). */         private const val MIN_PARTS = 3
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 ef7981c6d1..8759559e78 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/util/KeyboardPrefs.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/util/KeyboardPrefs.kt@@ -27,6 +27,7 @@ data class KeyboardSettings(     val activeLayout: KeyboardLayout         get() = KeyboardLayouts.byId(activeLayoutId) ?: KeyboardLayouts.DEFAULT +    /** Singleton holding keys state and operations. */     object Keys {         const val HAPTIC = "kb_haptic"         const val SOUND = "kb_sound"@@ -55,6 +56,7 @@ data class KeyboardSettings(         const val ACTIVE_LAYOUT = "kb_active_layout"     } +    /** Shared constants and helpers for KeyboardSettings. */     companion object {         /** Read the current persisted snapshot (defaults applied for unset keys). */         fun load(ds: DataStoreUtils): KeyboardSettings = KeyboardSettings(
keyboard/src/main/java/com/vayunmathur/keyboard/util/PinyinDictionary.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/util/PinyinDictionary.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/util/PinyinDictionary.ktindex 898e253204..2ce778fed8 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/util/PinyinDictionary.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/util/PinyinDictionary.kt@@ -60,6 +60,7 @@ class PinyinDictionary private constructor(         return lo     } +    /** Shared constants and helpers for PinyinDictionary. */     companion object {         /** How many characters a merely-prefix-matching syllable may contribute. */         private const val PREFIX_CANDIDATES = 3
keyboard/src/test/java/com/vayunmathur/keyboard/ime/ServiceOpsTest.kt
diff --git a/keyboard/src/test/java/com/vayunmathur/keyboard/ime/ServiceOpsTest.kt b/keyboard/src/test/java/com/vayunmathur/keyboard/ime/ServiceOpsTest.ktindex 695cfec852..75d69b50d8 100644--- a/keyboard/src/test/java/com/vayunmathur/keyboard/ime/ServiceOpsTest.kt+++ b/keyboard/src/test/java/com/vayunmathur/keyboard/ime/ServiceOpsTest.kt@@ -350,7 +350,15 @@ class ServiceOpsTest {             info(InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD),         )         assertTrue(service.kbState.passwordField)-        // enter actions+    }++    @Test+    fun `configureForEditor maps enter actions`() {+        fun info(inputType: Int, imeOptions: Int = 0, actionLabel: CharSequence? = null, actionId: Int = 0) =+            EditorInfo().apply {+                this.inputType = inputType; this.imeOptions = imeOptions+                this.actionLabel = actionLabel; this.actionId = actionId+            }         service.configureForEditor(info(InputType.TYPE_CLASS_TEXT, EditorInfo.IME_ACTION_SEARCH))         assertEquals(EnterAction.SEARCH, service.kbState.enterAction)         assertTrue(service.enterSendsAction)