From 7a00f0ee3af37bfa74633e28052313dfa482f03b Mon Sep 17 00:00:00 2001 From: sspanak Date: Thu, 6 Mar 2025 19:10:12 +0200 Subject: [PATCH] a more general approach for handling messaging applications that send messages without notifying us, like Viber and the standard SMS app --- .../io/github/sspanak/tt9/hacks/AppHacks.java | 12 ++++++++++-- .../github/sspanak/tt9/hacks/InputType.java | 13 ------------- .../sspanak/tt9/ime/helpers/TextField.java | 19 ++++++++++++------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/github/sspanak/tt9/hacks/AppHacks.java b/app/src/main/java/io/github/sspanak/tt9/hacks/AppHacks.java index 54e23262..84294cc4 100644 --- a/app/src/main/java/io/github/sspanak/tt9/hacks/AppHacks.java +++ b/app/src/main/java/io/github/sspanak/tt9/hacks/AppHacks.java @@ -85,7 +85,15 @@ public class AppHacks { /** - * Performs extra operations when the cursor moves and returns "true" if the selection was handled, "false" otherwise. + * Performs extra operations when the cursor moves and returns "true" if the selection was handled, + * "false" otherwise. + * + * CURSOR RESET + * When sending messages using the Viber or the SMS app SEND button, it does so and clears the text + * field, but without notifying the keyboard. This means, after sending the message, the InputMode + * still holds the old text, while the text field is empty. Attempting to type a new word then + * results in appending to the old word. We use this hack to detect Viber and reset the InputMode + * upon sending a message. */ public boolean onUpdateSelection( @NonNull InputMode inputMode, @@ -97,7 +105,7 @@ public class AppHacks { int candidatesStart, int candidatesEnd ) { - if (inputType.isViber() && CursorOps.isInputReset(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd)) { + if (CursorOps.isInputReset(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd) && textField.isEmpty()) { inputMode.onAcceptSuggestion(suggestionOps.acceptIncomplete()); inputMode.reset(); return true; diff --git a/app/src/main/java/io/github/sspanak/tt9/hacks/InputType.java b/app/src/main/java/io/github/sspanak/tt9/hacks/InputType.java index 2ecb53b4..3644e048 100644 --- a/app/src/main/java/io/github/sspanak/tt9/hacks/InputType.java +++ b/app/src/main/java/io/github/sspanak/tt9/hacks/InputType.java @@ -154,19 +154,6 @@ public class InputType extends StandardInputType { } - /** - * isViber - * When sending messages using the Viber's SEND button, it does so and clears the text field, - * but without notifying the keyboard. This means, after sending the message, the InputMode still - * holds the old text, while the text field is empty. Attempting to type a new word then results - * in appending to the old word. We use this hack to detect Viber and reset the InputMode upon - * sending a message. - */ - public boolean isViber() { - return isAppField("com.viber.voip", EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE); - } - - public boolean isNotUs(Context context) { return !isAppField(context.getPackageName(), EditorInfo.TYPE_NULL); } diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/helpers/TextField.java b/app/src/main/java/io/github/sspanak/tt9/ime/helpers/TextField.java index db4560fd..1dee0c99 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/helpers/TextField.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/helpers/TextField.java @@ -36,13 +36,18 @@ public class TextField extends InputField { } - public String getStringAfterCursor(int numberOfChars) { + public boolean isEmpty() { + return getStringBeforeCursor(1).isEmpty() && getStringAfterCursor(1).isEmpty(); + } + + + @NonNull public String getStringAfterCursor(int numberOfChars) { CharSequence chars = connection != null ? connection.getTextAfterCursor(numberOfChars, 0) : null; return chars != null ? chars.toString() : ""; } - public String getStringBeforeCursor(int numberOfChars) { + @NonNull public String getStringBeforeCursor(int numberOfChars) { CharSequence chars = connection != null ? connection.getTextBeforeCursor(numberOfChars, 0) : null; return chars != null ? chars.toString() : ""; } @@ -52,17 +57,17 @@ public class TextField extends InputField { * getStringBeforeCursor * A simplified helper that return up to 50 characters before the cursor and "just works". */ - public String getStringBeforeCursor() { + @NonNull public String getStringBeforeCursor() { return getStringBeforeCursor(50); } - public Text getTextAfterCursor(int numberOfChars) { + @NonNull public Text getTextAfterCursor(int numberOfChars) { return new Text(getStringAfterCursor(numberOfChars)); } - public Text getTextBeforeCursor() { + @NonNull public Text getTextBeforeCursor() { return new Text(getStringBeforeCursor()); } @@ -207,7 +212,7 @@ public class TextField extends InputField { connection.beginBatchEdit(); String beforeText = getStringBeforeCursor(searchText.length()); - if (beforeText == null || !beforeText.equals(searchText)) { + if (!beforeText.equals(searchText)) { connection.endBatchEdit(); return; } @@ -230,7 +235,7 @@ public class TextField extends InputField { connection.beginBatchEdit(); String beforeText = getStringBeforeCursor(word.length()); - if (beforeText == null || !beforeText.equals(word)) { + if (!beforeText.equals(word)) { connection.endBatchEdit(); return; }