From 5f82653beef9b557f7fd3027cb0c146486c60753 Mon Sep 17 00:00:00 2001 From: Lan Wei Date: Thu, 11 Mar 2021 12:14:46 +0800 Subject: [PATCH] Update the fallback logic in BaseInputConnection#getSurroundingText() Update the implementation of BaseInputConnection#getSurroundingText() to fallback to use previous APIs for retrieving surrounding when there's no valid editable. This will make the API when editor provides custom InputConnection exends from BaseInputConnection (e.g. WebView & Google Docs). Also, update the fallback implementation to return null instead of empty string when getTextBeforeCursor() or getTextAfterCursor() returns null, to make IME is able to distinguish the two cases. Test: atest CtsInputMethodTestCases:BaseInputConnectionTest Test: verified manually Bug: 167947745 Bug: 181303771 Bug: 182426649 Change-Id: I70527eb954a741640b728b4b7bb6c74e0e6496b7 --- .../android/view/inputmethod/BaseInputConnection.java | 7 ++++++- .../java/android/view/inputmethod/InputConnection.java | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/java/android/view/inputmethod/BaseInputConnection.java b/core/java/android/view/inputmethod/BaseInputConnection.java index 37220fe6870be..c5bce28fcee13 100644 --- a/core/java/android/view/inputmethod/BaseInputConnection.java +++ b/core/java/android/view/inputmethod/BaseInputConnection.java @@ -608,7 +608,12 @@ public class BaseInputConnection implements InputConnection { Preconditions.checkArgumentNonnegative(afterLength); final Editable content = getEditable(); - if (content == null) return null; + // If {@link #getEditable()} is null or {@code mEditable} is equal to {@link #getEditable()} + // (a.k.a, a fake editable), it means we cannot get valid content from the editable, so + // fallback to retrieve surrounding text from other APIs. + if (content == null || mEditable == content) { + return InputConnection.super.getSurroundingText(beforeLength, afterLength, flags); + } int selStart = Selection.getSelectionStart(content); int selEnd = Selection.getSelectionEnd(content); diff --git a/core/java/android/view/inputmethod/InputConnection.java b/core/java/android/view/inputmethod/InputConnection.java index 34a60bbe76425..38019c9a34f8a 100644 --- a/core/java/android/view/inputmethod/InputConnection.java +++ b/core/java/android/view/inputmethod/InputConnection.java @@ -325,16 +325,16 @@ public interface InputConnection { CharSequence textBeforeCursor = getTextBeforeCursor(beforeLength, flags); if (textBeforeCursor == null) { - textBeforeCursor = ""; + return null; + } + CharSequence textAfterCursor = getTextAfterCursor(afterLength, flags); + if (textAfterCursor == null) { + return null; } CharSequence selectedText = getSelectedText(flags); if (selectedText == null) { selectedText = ""; } - CharSequence textAfterCursor = getTextAfterCursor(afterLength, flags); - if (textAfterCursor == null) { - textAfterCursor = ""; - } CharSequence surroundingText = TextUtils.concat(textBeforeCursor, selectedText, textAfterCursor); return new SurroundingText(surroundingText, textBeforeCursor.length(),