From a35ebcf88e0a9458f243df3ab4ed85e7ecccddc5 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Tue, 27 Apr 2021 19:20:16 -0700 Subject: Fix stale EditorInfo upon EditText#setText() This is a follow up to our previous CL [1], which added initial surrounding text support into EditorInfo and let TextView support it. What we overlooked was that calling TextView#setText() would trigger TextView#onCreateInputConnection() before TextView#mText is updated to the new value. As a result, EditorInfo is initialized with a stale surrounding text information. With this CL, EditorInfo will correctly be initialized with the new text specified to TextView#setText(). One complicated thing in this CL is InputMethodManager also has some issues about how to drop spurious InputMethodService#onUpdateSelection() while InputConnection is being replaced with a new instance. I ended up having to introduce a hacky boolean Editor#mHasPendingRestartInputForSetText to work around this in the TextView/EditText. This workaround is expected to be removed/revisited as part of Bug 186582769. [1]: Ie04f2349b1157408aa8ed9044aea12ce99132cb4 c486acc4d29a0e441ddcd05b3fc0c919aee7fbd0 Fix: 161330778 Test: atest CtsInputMethodTestCases:EditTextImeSupportTest Change-Id: Iee3b754eea319861b2eb955d6cc95ba13006b55e --- core/java/android/widget/TextView.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'core/java/android/widget/TextView.java') diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 07a9a5fad6e2..1a37b595287d 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -6285,11 +6285,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener || needEditableForNotification) { createEditorIfNeeded(); mEditor.forgetUndoRedo(); + mEditor.scheduleRestartInputForSetText(); Editable t = mEditableFactory.newEditable(text); text = t; setFilters(t, mFilters); - InputMethodManager imm = getInputMethodManager(); - if (imm != null) imm.restartInput(this); } else if (precomputed != null) { if (mTextDir == null) { mTextDir = getTextDirectionHeuristic(); @@ -6408,8 +6407,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener notifyListeningManagersAfterTextChanged(); } - // SelectionModifierCursorController depends on textCanBeSelected, which depends on text - if (mEditor != null) mEditor.prepareCursorControllers(); + if (mEditor != null) { + // SelectionModifierCursorController depends on textCanBeSelected, which depends on text + mEditor.prepareCursorControllers(); + + mEditor.maybeFireScheduledRestartInputForSetText(); + } } /** -- cgit v1.2.3