1
0
Fork 0

a more general approach for handling messaging applications that send messages without notifying us, like Viber and the standard SMS app

This commit is contained in:
sspanak 2025-03-06 19:10:12 +02:00 committed by Dimo Karaivanov
parent 742a3ed883
commit 7a00f0ee3a
3 changed files with 22 additions and 22 deletions

View file

@ -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( public boolean onUpdateSelection(
@NonNull InputMode inputMode, @NonNull InputMode inputMode,
@ -97,7 +105,7 @@ public class AppHacks {
int candidatesStart, int candidatesStart,
int candidatesEnd 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.onAcceptSuggestion(suggestionOps.acceptIncomplete());
inputMode.reset(); inputMode.reset();
return true; return true;

View file

@ -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) { public boolean isNotUs(Context context) {
return !isAppField(context.getPackageName(), EditorInfo.TYPE_NULL); return !isAppField(context.getPackageName(), EditorInfo.TYPE_NULL);
} }

View file

@ -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; CharSequence chars = connection != null ? connection.getTextAfterCursor(numberOfChars, 0) : null;
return chars != null ? chars.toString() : ""; 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; CharSequence chars = connection != null ? connection.getTextBeforeCursor(numberOfChars, 0) : null;
return chars != null ? chars.toString() : ""; return chars != null ? chars.toString() : "";
} }
@ -52,17 +57,17 @@ public class TextField extends InputField {
* getStringBeforeCursor * getStringBeforeCursor
* A simplified helper that return up to 50 characters before the cursor and "just works". * 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); return getStringBeforeCursor(50);
} }
public Text getTextAfterCursor(int numberOfChars) { @NonNull public Text getTextAfterCursor(int numberOfChars) {
return new Text(getStringAfterCursor(numberOfChars)); return new Text(getStringAfterCursor(numberOfChars));
} }
public Text getTextBeforeCursor() { @NonNull public Text getTextBeforeCursor() {
return new Text(getStringBeforeCursor()); return new Text(getStringBeforeCursor());
} }
@ -207,7 +212,7 @@ public class TextField extends InputField {
connection.beginBatchEdit(); connection.beginBatchEdit();
String beforeText = getStringBeforeCursor(searchText.length()); String beforeText = getStringBeforeCursor(searchText.length());
if (beforeText == null || !beforeText.equals(searchText)) { if (!beforeText.equals(searchText)) {
connection.endBatchEdit(); connection.endBatchEdit();
return; return;
} }
@ -230,7 +235,7 @@ public class TextField extends InputField {
connection.beginBatchEdit(); connection.beginBatchEdit();
String beforeText = getStringBeforeCursor(word.length()); String beforeText = getStringBeforeCursor(word.length());
if (beforeText == null || !beforeText.equals(word)) { if (!beforeText.equals(word)) {
connection.endBatchEdit(); connection.endBatchEdit();
return; return;
} }