@devries-cameron20 / t3code-1 / commits / f4cf7a95b2
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): Android keyboard clipboard pastes fold into attachments (#24482)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Victor committed Nov 8, 2026 at 23:18 UTC f4cf7a95b21d64d2fa425817640dbe1933da16e7
2 files changed +150 -6
apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt
+66 -6
@@ -25,11 +25,14 @@ import android.view.MotionEvent
25 import android.view.KeyEvent
26 import android.view.View
27 import android.view.ViewGroup
28 +import android.view.inputmethod.BaseInputConnection
29 import android.view.inputmethod.EditorInfo
30 import android.view.inputmethod.InputConnection
31 import android.view.inputmethod.InputConnectionWrapper
32 import android.view.inputmethod.InputMethodManager
33 +import android.view.inputmethod.TextAttribute
34 import android.widget.EditText
35 +import androidx.annotation.RequiresApi
36 import com.facebook.react.common.assets.ReactFontManager
37 import expo.modules.kotlin.AppContext
38 import expo.modules.kotlin.viewevent.EventDispatcher
@@ -680,8 +683,57 @@ internal class SelectionAwareEditText(
683 afterLength: Int
684 ): Boolean = deleteAdjacentChip(beforeLength, afterLength) ||
685 super.deleteSurroundingTextInCodePoints(beforeLength, afterLength)
686 + override fun commitText(text: CharSequence?, newCursorPosition: Int): Boolean =
687 + commitUnlessPasted(text) { super.commitText(text, newCursorPosition) }
688 +
689 + // InputConnectionWrapper sends this overload straight to its target, not through the
690 + // two-argument commitText.
691 + @RequiresApi(Build.VERSION_CODES.TIRAMISU)
692 + override fun commitText(
693 + text: CharSequence,
694 + newCursorPosition: Int,
695 + textAttribute: TextAttribute?
696 + ): Boolean = commitUnlessPasted(text) {
697 + super.commitText(text, newCursorPosition, textAttribute)
698 + }
699 +
700 + private fun commitUnlessPasted(text: CharSequence?, commit: () -> Boolean): Boolean =
701 + if (interceptKeyboardPaste(text)) {
702 + // The keyboard now treats its composing text as replaced; a span left behind would
703 + // let its next word overwrite text the paste did not remove.
704 + finishComposingText()
705 + true
706 + } else {
707 + commit()
708 + }
709 + }
710 + }
711 +
712 + // IME clipboard inserts replace the composing range, just like native commitText.
713 + private fun interceptKeyboardPaste(text: CharSequence?): Boolean {
714 + if (readOnly || textPasteThresholdBytes <= 0 || text == null) return false
715 + val value = text.toString()
716 + val composingStart = BaseInputConnection.getComposingSpanStart(editableText)
717 + val composingEnd = BaseInputConnection.getComposingSpanEnd(editableText)
718 + return when {
719 + Character.codePointCount(text, 0, text.length) < 2 -> false
720 + composingStart < 0 -> interceptPaste(value)
721 + else -> {
722 + val continuesWord = value.startsWith(editableText.subSequence(composingStart, composingEnd))
723 + val exceedsInputLimit =
724 + length().toLong() - (composingEnd - composingStart) + value.length > maxInputChars
725 + val keepTyping = if (continuesWord && exceedsInputLimit && !shouldFoldText(value)) {
726 + val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
727 + // commitText has no paste flag; inspect the clipboard only for ambiguous overflow commits.
728 + clipboard?.primaryClip?.plainText() != value
729 + } else {
730 + false
731 + }
732 + !keepTyping && interceptPaste(value, start = composingStart, end = composingEnd)
733 + }
734 }
735 }
736 +
737 override fun onSelectionChanged(selStart: Int, selEnd: Int) {
738 super.onSelectionChanged(selStart, selEnd)
739 selectionListener?.invoke(selStart, selEnd)
@@ -793,13 +845,17 @@ internal class SelectionAwareEditText(
845 private fun pasteInterceptedText(clip: ClipData?, foldLargeText: Boolean = true): Boolean {
846 val text = if (textPasteThresholdBytes > 0) clip?.plainText() else null
847 if (text.isNullOrEmpty()) return false
796 - val start = minOf(selectionStart, selectionEnd).coerceIn(0, length())
797 - val end = maxOf(selectionStart, selectionEnd).coerceIn(start, length())
848 + return interceptPaste(text, foldLargeText)
849 + }
850 +
851 + private fun interceptPaste(
852 + text: String,
853 + foldLargeText: Boolean = true,
854 + start: Int = minOf(selectionStart, selectionEnd).coerceIn(0, length()),
855 + end: Int = maxOf(selectionStart, selectionEnd).coerceIn(start, length())
856 + ): Boolean {
857 val exceedsInputLimit = length().toLong() - (end - start) + text.length > maxInputChars
799 - val shouldFold = foldLargeText && (
800 - text.length >= textPasteThresholdBytes ||
801 - text.toByteArray(Charsets.UTF_8).size >= textPasteThresholdBytes
802 - )
858 + val shouldFold = foldLargeText && shouldFoldText(text)
859 val shouldIntercept = exceedsInputLimit || shouldFold
860 if (shouldIntercept) {
861 pasteTextListener?.invoke(text, start, end)
@@ -808,6 +864,10 @@ internal class SelectionAwareEditText(
864 return shouldIntercept
865 }
866
867 + private fun shouldFoldText(text: String): Boolean =
868 + text.length >= textPasteThresholdBytes ||
869 + text.toByteArray(Charsets.UTF_8).size >= textPasteThresholdBytes
870 +
871 // Match native paste's newlines, including empty items.
872 // URI-backed clips stay native: coerceToText can read a provider synchronously.
873 private fun ClipData.plainText(): String? {
apps/mobile/modules/t3-composer-editor/android/src/test/java/expo/modules/t3composereditor/ComposerPasteTest.kt
+84
@@ -13,6 +13,8 @@ import android.net.Uri
13 import android.view.DragEvent
14 import android.view.View
15 import android.view.KeyEvent
16 +import android.view.inputmethod.BaseInputConnection
17 +import android.view.inputmethod.EditorInfo
18 import org.junit.Assert.assertEquals
19 import org.junit.Assert.assertFalse
20 import org.junit.Assert.assertNull
@@ -103,6 +105,88 @@ class ComposerPasteTest {
105 assertEquals("old", editor.text.toString())
106 }
107
108 + @Test
109 + fun keyboardClipboardInsertFoldsAtTheUtf8Threshold() {
110 + val pasted = "é".repeat(16 * 1024)
111 + editor.setText("before old after")
112 + editor.setSelection(10, 7)
113 + var intercepted: Triple<String, Int, Int>? = null
114 + editor.pasteTextListener = { text, start, end -> intercepted = Triple(text, start, end) }
115 + val connection = editor.onCreateInputConnection(EditorInfo())!!
116 +
117 + assertTrue(connection.commitText(pasted, 1))
118 + assertEquals(Triple(pasted, 7, 10), intercepted)
119 + assertEquals("before old after", editor.text.toString())
120 +
121 + intercepted = null
122 + assertTrue(connection.commitText(pasted, 1, null))
123 + assertEquals(Triple(pasted, 7, 10), intercepted)
124 + assertEquals("before old after", editor.text.toString())
125 + }
126 +
127 + @Test
128 + fun keyboardClipboardInsertWhileComposingReplacesTheComposingWord() {
129 + val pasted = "wor" + "é".repeat(16 * 1024)
130 + editor.setText("before ")
131 + editor.setSelection(editor.length())
132 + var intercepted: Triple<String, Int, Int>? = null
133 + editor.pasteTextListener = { text, start, end -> intercepted = Triple(text, start, end) }
134 + val connection = editor.onCreateInputConnection(EditorInfo())!!
135 +
136 + for (withTextAttribute in listOf(false, true)) {
137 + editor.setText("before ")
138 + editor.setSelection(editor.length())
139 + intercepted = null
140 + assertTrue(connection.setComposingText("wor", 1))
141 + val committed = if (withTextAttribute) {
142 + connection.commitText(pasted, 1, null)
143 + } else {
144 + connection.commitText(pasted, 1)
145 + }
146 + assertTrue(committed)
147 + assertEquals(Triple(pasted, 7, 10), intercepted)
148 + assertEquals("before wor", editor.text.toString())
149 + assertEquals(-1, BaseInputConnection.getComposingSpanStart(editor.text))
150 + }
151 + }
152 +
153 + @Test
154 + fun keyboardClipboardPrefixInterceptsInputLimitOverflow() {
155 + val pasted = "word from clipboard"
156 + clipboard.setPrimaryClip(ClipData.newPlainText("test", pasted))
157 + editor.setText("x".repeat(119_997))
158 + editor.setSelection(editor.length())
159 + var intercepted: Triple<String, Int, Int>? = null
160 + editor.pasteTextListener = { text, start, end -> intercepted = Triple(text, start, end) }
161 + val connection = editor.onCreateInputConnection(EditorInfo())!!
162 +
163 + assertTrue(connection.setComposingText("wor", 1))
164 + assertTrue(connection.commitText(pasted, 1))
165 + assertEquals(Triple(pasted, 119_997, 120_000), intercepted)
166 + assertEquals(120_000, editor.length())
167 + assertEquals(-1, BaseInputConnection.getComposingSpanStart(editor.text))
168 + }
169 +
170 + @Test
171 + fun keyboardCommitsAtTheInputLimitInterceptOnlyClipboardInserts() {
172 + clipboard.setPrimaryClip(ClipData.newPlainText("test", "unrelated clipboard"))
173 + val intercepted = mutableListOf<String>()
174 + editor.pasteTextListener = { text, _, _ -> intercepted.add(text) }
175 + editor.setText("x".repeat(120_000))
176 + editor.setSelection(editor.length())
177 + val connection = editor.onCreateInputConnection(EditorInfo())!!
178 +
179 + assertTrue(connection.commitText("hello", 1))
180 + assertEquals(listOf("hello"), intercepted)
181 + assertEquals(120_000, editor.length())
182 +
183 + assertTrue(connection.commitText("a", 1))
184 + assertTrue(connection.setComposingText("wor", 1))
185 + assertTrue(connection.commitText("word", 1))
186 + assertEquals(listOf("hello"), intercepted)
187 + assertEquals("x".repeat(120_000) + "aword", editor.text.toString())
188 + }
189 +
190 @Test
191 fun regularPasteMeasuresAndInterceptsEveryTextItemJoinedWithNewlines() {
192 // Only the complete UTF-8 payload, including both newlines, reaches 32 KiB.