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:
parent
742a3ed883
commit
7a00f0ee3a
3 changed files with 22 additions and 22 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue