TypeScript
fix(mobile): Android terminal Backspace deletes on the first press (#24983)
Victor0d8958baaf64
diff --git a/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.ktindex 5646ab8c588..14f91964dca 100644--- a/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt+++ b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt@@ -10,6 +10,7 @@ import android.view.KeyEventimport android.view.Viewimport android.view.ViewGroupimport android.view.inputmethod.EditorInfo+import android.view.inputmethod.InputConnectionimport android.view.inputmethod.InputMethodManagerimport android.widget.EditTextimport android.widget.FrameLayout@@ -21,11 +22,19 @@ import kotlin.math.maxclass T3TerminalView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {private val container = FrameLayout(context)private val terminalCanvas = TerminalCanvasView(context)+ private val sentText = TerminalSentText()// Newer Android can request autofill even with importantForAutofill=no.// Reporting no autofill type keeps terminal input out of credential autofill.private val inputView = object : EditText(context) {override fun getAutofillType(): Int = View.AUTOFILL_TYPE_NONE++ override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? =+ super.onCreateInputConnection(outAttrs)?.let { connection ->+ TerminalInputConnection(connection, sentText, { !readOnly }) { count ->+ if (!readOnly) onInput(mapOf("data" to "\u007F".repeat(count)))+ }+ }}private val onInput by EventDispatcher()private val onResize by EventDispatcher()@@ -282,6 +291,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contexif (event.action != KeyEvent.ACTION_DOWN) return@setOnKeyListener falsewhen {keyCode == KeyEvent.KEYCODE_DEL -> {+ sentText.removeCodePoints(1)onInput(mapOf("data" to "\u007F"))true}@@ -304,6 +314,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contexval end = (start + count).coerceAtMost(s.length)if (start < end) {val insertedText = s.subSequence(start, end).toString()+ sentText.append(insertedText)onInput(mapOf("data" to insertedText))}}diff --git a/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalInputConnection.kt b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalInputConnection.ktnew file mode 100644index 00000000000..9d73e08cc84--- /dev/null+++ b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalInputConnection.kt@@ -0,0 +1,70 @@+package expo.modules.t3terminal++import android.view.inputmethod.InputConnection+import android.view.inputmethod.InputConnectionWrapper++private const val MAX_DELETE_LENGTH = 1024++// Text the terminal already received from the hidden input. The input is empty+// by the time an IME deletes, so this is what its UTF-16 lengths refer to.+internal class TerminalSentText {+ private val text = StringBuilder()+ private var deletedHighSurrogate = false++ fun append(sent: CharSequence) {+ discardDeletedSurrogate()+ text.append(sent)+ if (text.length > MAX_DELETE_LENGTH) text.delete(0, text.length - MAX_DELETE_LENGTH)+ }++ fun removeUnits(units: Int): Int {+ val start = (text.length - units).coerceAtLeast(0)+ val removed = units - (text.length - start) + text.codePointCount(start, text.length) -+ if (deletedHighSurrogate && units > 0) 1 else 0+ // The low half already sent DEL; retain the high half only for IME unit accounting.+ deletedHighSurrogate = start > 0 && start < text.length &&+ Character.isHighSurrogate(text[start - 1]) && Character.isLowSurrogate(text[start])+ text.setLength(start)+ return removed+ }++ fun removeCodePoints(count: Int): Int {+ discardDeletedSurrogate()+ val known = count.coerceAtMost(text.codePointCount(0, text.length))+ text.setLength(text.offsetByCodePoints(text.length, -known))+ return count+ }++ private fun discardDeletedSurrogate() {+ if (deletedHighSurrogate) text.setLength(text.length - 1)+ deletedHighSurrogate = false+ }+}++// The hidden input clears after every change, so text an IME deletes was+// already sent to the terminal. Some keyboards delete through these calls+// instead of KEYCODE_DEL, so forward them as one DEL per code point.+internal class TerminalInputConnection(+ target: InputConnection,+ private val sentText: TerminalSentText,+ private val canTrackDeletes: () -> Boolean,+ private val sendDeletes: (count: Int) -> Unit+) : InputConnectionWrapper(target, true) {+ override fun deleteSurroundingText(beforeLength: Int, afterLength: Int): Boolean {+ if (beforeLength > 0 && canTrackDeletes()) {+ forwardDeletes(sentText.removeUnits(beforeLength))+ }+ return super.deleteSurroundingText(beforeLength, afterLength)+ }++ override fun deleteSurroundingTextInCodePoints(beforeLength: Int, afterLength: Int): Boolean {+ if (beforeLength > 0 && canTrackDeletes()) {+ forwardDeletes(sentText.removeCodePoints(beforeLength))+ }+ return super.deleteSurroundingTextInCodePoints(beforeLength, afterLength)+ }++ private fun forwardDeletes(count: Int) {+ sendDeletes(count.coerceAtMost(MAX_DELETE_LENGTH))+ }+}diff --git a/apps/mobile/modules/t3-terminal/android/src/test/java/expo/modules/t3terminal/TerminalInputConnectionTest.kt b/apps/mobile/modules/t3-terminal/android/src/test/java/expo/modules/t3terminal/TerminalInputConnectionTest.ktnew file mode 100644index 00000000000..83cf850ca10--- /dev/null+++ b/apps/mobile/modules/t3-terminal/android/src/test/java/expo/modules/t3terminal/TerminalInputConnectionTest.kt@@ -0,0 +1,97 @@+package expo.modules.t3terminal++import android.view.inputmethod.EditorInfo+import android.widget.EditText+import org.junit.Assert.assertEquals+import org.junit.Test+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.RuntimeEnvironment+import org.robolectric.annotation.Config++@RunWith(RobolectricTestRunner::class)+@Config(sdk = [36], manifest = Config.NONE)+class TerminalInputConnectionTest {+ private val input = EditText(RuntimeEnvironment.getApplication())+ private val sentText = TerminalSentText()+ private val deletes = mutableListOf<Int>()+ private var writable = true+ private val connection = TerminalInputConnection(+ input.onCreateInputConnection(EditorInfo())!!,+ sentText,+ { writable },+ ) { if (writable) deletes += it }++ private fun type(text: String) {+ connection.commitText(text, 1)+ sentText.append(input.text)+ input.text.clear()+ }++ @Test+ fun imeDeletionReachesTheTerminalAfterTheInputClears() {+ type("abc")+ connection.deleteSurroundingText(1, 0)+ connection.deleteSurroundingTextInCodePoints(2, 0)+ connection.deleteSurroundingText(Int.MAX_VALUE, 0)++ assertEquals(listOf(1, 2, 1024), deletes)+ }++ @Test+ fun utf16DeletionOfAnEmojiSendsOneDelete() {+ type("aπ")+ connection.deleteSurroundingText(2, 0)+ connection.deleteSurroundingText(1, 0)++ assertEquals(listOf(1, 1), deletes)++ for (codePoints in listOf(false, true)) {+ deletes.clear()+ type("aπ")+ writable = false+ if (codePoints) {+ connection.deleteSurroundingTextInCodePoints(1, 0)+ } else {+ connection.deleteSurroundingText(2, 0)+ }+ writable = true+ connection.deleteSurroundingText(2, 0)+ connection.deleteSurroundingText(1, 0)++ assertEquals(listOf(1, 1), deletes)+ }++ deletes.clear()+ type("aπ")+ connection.deleteSurroundingText(1, 0)+ connection.deleteSurroundingText(1, 0)+ connection.deleteSurroundingText(1, 0)++ assertEquals(listOf(1, 0, 1), deletes)++ deletes.clear()+ type("aπ")+ connection.deleteSurroundingText(1, 0)+ connection.deleteSurroundingTextInCodePoints(1, 0)++ assertEquals(listOf(1, 1), deletes)++ deletes.clear()+ type("aπ")+ connection.deleteSurroundingText(1, 0)+ type("b")+ connection.deleteSurroundingText(1, 0)+ connection.deleteSurroundingText(1, 0)++ assertEquals(listOf(1, 1, 1), deletes)++ deletes.clear()+ type("xπa")+ sentText.removeCodePoints(1)+ connection.deleteSurroundingText(2, 0)+ connection.deleteSurroundingText(1, 0)++ assertEquals(listOf(1, 1), deletes)+ }+}