From e92f5e8c2bd89a726cfbcd27e3e5cf47553011e6 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Mon, 12 Jun 2023 16:43:13 -0700 Subject: [PATCH] Make sure to call back View#onCheckIsTextEditor() on the UI thread Calling back View#onCheckIsTextEditor() from a non-UI thread is a well known type of bugs that we have met in the past [1]. This time it turns out that it can still happen when InputMethodManager#isActive() gets called on a background thread. This CL should address such a remaining case. [1]: I098df41f935438bf1889fb030c7a3e7a0645a168 3f733cc7efa6342a849960fdcf1a4cf28d36bae5 Bug: 286016109 Test: atest CtsInputMethodTestCases:FocusHandlingTest#testOnCheckIsTextEditorRunOnUIThreadWithInputMethodManagerIsActive Change-Id: I015603c1b33ecdf4a4f5d5be5d85756a3b2b1615 (cherry picked from commit b9eecc6bc883824c148cff901fbcb0ec2ebfa6c0) --- .../view/inputmethod/InputMethodManager.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 41ef44e1ac1f6..3cac1e5f7d6eb 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -2657,17 +2657,6 @@ public final class InputMethodManager { } } - if (windowGainingFocus == null) { - windowGainingFocus = view.getWindowToken(); - if (windowGainingFocus == null) { - Log.e(TAG, "ABORT input: ServedView must be attached to a Window"); - return false; - } - startInputFlags = getStartInputFlags(view, startInputFlags); - softInputMode = view.getViewRootImpl().mWindowAttributes.softInputMode; - windowFlags = view.getViewRootImpl().mWindowAttributes.flags; - } - // Now we need to get an input connection from the served view. // This is complicated in a couple ways: we can't be holding our lock // when calling out to the view, and we need to make sure we call into @@ -2690,6 +2679,17 @@ public final class InputMethodManager { return false; } + if (windowGainingFocus == null) { + windowGainingFocus = view.getWindowToken(); + if (windowGainingFocus == null) { + Log.e(TAG, "ABORT input: ServedView must be attached to a Window"); + return false; + } + startInputFlags = getStartInputFlags(view, startInputFlags); + softInputMode = view.getViewRootImpl().mWindowAttributes.softInputMode; + windowFlags = view.getViewRootImpl().mWindowAttributes.flags; + } + // Okay we are now ready to call into the served view and have it // do its stuff. // Life is good: let's hook everything up!