diff --git a/CHANGELOG.md b/CHANGELOG.md index 913b087f6..61434122a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## [Unreleased] -- Recover unfinished practices and data backups; add photos reliably and reset break timers after time away. +- Backspace and text corrections work again; recover unfinished practices and backups, add photos reliably and reset break timers after time away. - Switch practices smoothly, choose an astral exercise, and claim daily coins beside the Habits balance. - Read Tao Te Ching art on its own page; find Photos and Radio in Apps, with clearer feedback and appearance controls. diff --git a/droid/app/src/main/java/xyz/waozi/inbe/TextInputView.java b/droid/app/src/main/java/xyz/waozi/inbe/TextInputView.java index 2328f0bd7..8ae16ea9f 100644 --- a/droid/app/src/main/java/xyz/waozi/inbe/TextInputView.java +++ b/droid/app/src/main/java/xyz/waozi/inbe/TextInputView.java @@ -47,13 +47,27 @@ final class TextInputView extends EditText { @Override public void onTextChanged(CharSequence text, int start, int before, int count) {} @Override public void afterTextChanged(Editable text) { publish(); } }); - setOnEditorActionListener((view, action, event) -> { - if (!multiline && action != EditorInfo.IME_NULL) { - listener.enter(); - return true; - } + setOnEditorActionListener((view, action, event) -> editorAction(action, event)); + } + + private boolean editorAction(int action, KeyEvent event) { + if (multiline) { return false; - }); + } + // An IME Enter key reaches this editor directly. Android 14 reports + // the field's action for both its key down and key up, so submit only + // on the first key down and consume the repeats and the key up. + if (event != null) { + if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { + listener.enter(); + } + return true; + } + if (action == EditorInfo.IME_NULL) { + return false; + } + listener.enter(); + return true; } private static int byteOffset(String value, int characterOffset) { @@ -198,8 +212,18 @@ final class TextInputView extends EditText { } @Override public boolean sendKeyEvent(KeyEvent event) { - // EditText owns text keys; Activity must not also queue them. - return current() && super.sendKeyEvent(event); + if (!current()) { + return false; + } + // BaseInputConnection sends keys through the window's native + // input queue. Keep IME edits in this editor, like commitText + // and deleteSurroundingText, so Kryon receives one final state. + beginBatchEdit(); + try { + return TextInputView.this.dispatchKeyEvent(event); + } finally { + endBatchEdit(); + } } @Override public boolean commitText(CharSequence text, int cursor) { diff --git a/tests/android-text-selection/TextInputEditingTest.java b/tests/android-text-selection/TextInputEditingTest.java new file mode 100644 index 000000000..15a48ac4f --- /dev/null +++ b/tests/android-text-selection/TextInputEditingTest.java @@ -0,0 +1,128 @@ +package xyz.waozi.inbe; + +import static org.junit.Assert.*; + +import android.app.Activity; +import android.os.Looper; +import android.view.KeyEvent; +import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputConnection; +import java.nio.charset.StandardCharsets; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.Shadows; +import org.robolectric.annotation.Config; +import org.robolectric.annotation.GraphicsMode; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {31, 32, 34, 36}, manifest = Config.NONE) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +public class TextInputEditingTest { + private static final class Changes implements TextInputView.Listener { + String value = ""; + long revision; + int cursor; + int anchor; + int count; + int enters; + + @Override public void changed(long target, long revision, byte[] value, int cursor, int anchor) { + this.value = new String(value, StandardCharsets.UTF_8); + this.revision = revision; + this.cursor = cursor; + this.anchor = anchor; + count++; + } + @Override public void enter() { + enters++; + } + } + + private static byte[] bytes(String value) { + return value.getBytes(StandardCharsets.UTF_8); + } + + private static void key(InputConnection connection, int code) { + connection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, code)); + connection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, code)); + Shadows.shadowOf(Looper.getMainLooper()).idle(); + } + + @Test public void imeDeleteKeysRemoveTextAndSelections() { + try (var activity = Robolectric.buildActivity(Activity.class).setup().visible()) { + Changes changes = new Changes(); + TextInputView editor = new TextInputView(activity.get(), changes); + activity.get().setContentView(editor); + editor.update(1, 0, bytes(""), 0, 0, false, false, true); + assertTrue(editor.requestFocus()); + InputConnection connection = editor.onCreateInputConnection(new EditorInfo()); + assertNotNull(connection); + assertTrue(connection.commitText("alpha 水😀 omega", 1)); + int before = changes.count; + key(connection, KeyEvent.KEYCODE_DEL); + assertEquals("alpha 水😀 omeg", changes.value); + assertEquals(before + 1, changes.count); + editor.update(1, changes.revision, bytes(changes.value), changes.cursor, changes.anchor, + false, false, true); + assertTrue(connection.setSelection(6, 9)); + key(connection, KeyEvent.KEYCODE_DEL); + assertEquals("alpha omeg", changes.value); + assertEquals(6, changes.cursor); + assertEquals(6, changes.anchor); + key(connection, KeyEvent.KEYCODE_FORWARD_DEL); + assertEquals("alpha omeg", changes.value); + assertTrue(connection.setSelection(0, editor.length())); + key(connection, KeyEvent.KEYCODE_DEL); + assertEquals("", changes.value); + assertEquals(0, changes.cursor); + assertEquals(0, changes.anchor); + editor.update(2, 0, bytes("next"), 4, 4, false, false, true); + key(connection, KeyEvent.KEYCODE_DEL); + assertEquals("next", editor.getText().toString()); + } + } + + @Test public void imeEnterKeySubmitsSingleLineAndBreaksMultiline() { + try (var activity = Robolectric.buildActivity(Activity.class).setup().visible()) { + Changes changes = new Changes(); + TextInputView editor = new TextInputView(activity.get(), changes); + activity.get().setContentView(editor); + editor.update(1, 0, bytes("name"), 4, 4, false, false, false); + assertTrue(editor.requestFocus()); + InputConnection connection = editor.onCreateInputConnection(new EditorInfo()); + assertNotNull(connection); + key(connection, KeyEvent.KEYCODE_ENTER); + assertEquals(1, changes.enters); + assertEquals("name", editor.getText().toString()); + editor.update(2, 0, bytes("line"), 4, 4, false, false, true); + connection = editor.onCreateInputConnection(new EditorInfo()); + assertNotNull(connection); + key(connection, KeyEvent.KEYCODE_ENTER); + assertEquals(1, changes.enters); + assertEquals("line\n", changes.value); + assertEquals(5, changes.cursor); + } + } + + @Test public void imeSurroundingDeleteAndCorrectionsKeepUnicodeAndCursor() { + Changes changes = new Changes(); + TextInputView editor = new TextInputView(org.robolectric.RuntimeEnvironment.getApplication(), changes); + editor.update(1, 0, bytes(""), 0, 0, false, false, true); + InputConnection connection = editor.onCreateInputConnection(new EditorInfo()); + assertNotNull(connection); + assertTrue(connection.commitText("hello 水😀", 1)); + assertTrue(connection.deleteSurroundingTextInCodePoints(1, 0)); + assertEquals("hello 水", changes.value); + assertEquals(9, changes.cursor); + assertTrue(connection.deleteSurroundingText(1, 0)); + assertEquals("hello ", changes.value); + assertTrue(connection.setSelection(0, 5)); + assertTrue(connection.setComposingText("calm", 1)); + assertTrue(connection.commitText("calmer", 1)); + assertEquals("calmer ", changes.value); + assertEquals(6, changes.cursor); + assertEquals(6, changes.anchor); + } +}