From bf5dc9ecbf0f2bb8bb04e367de049d13e89759dc Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Tue, 18 Oct 2022 07:58:51 -0700 Subject: [PATCH] Make IMM#hasActiveInputConnection() more predictable This is a follow up CL to our previous CL [1], which introduced InputMethodManager#hasActiveInputConnection(View) as a test API. As this remains to be a test API, we should be able to tweak its behavior as long as existing use cases continue working. The main motivations of this CL is to make it more predictable and easier to understand. Even with simplification, ImeAwareEditText and CTS tests should continue working as expected. Also there must be no app compatibility concern in this CL. [1]: I466a93e2b4706ae809ac50dc16dc8e4ffa7c2f1c 8bcce88c8608d77a2fb760e932fed8f08e3645fe Bug: 176697749 Bug: 182259171 Test: presubmit Change-Id: I7fe8b18ffbb450fd1e272e15f06f2629518f320c --- .../view/inputmethod/InputMethodManager.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 867b8260ec5e1..18b3e212d6f40 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -873,12 +873,25 @@ public final class InputMethodManager { /** * Checks whether the active input connection (if any) is for the given view. * + *

Note that {@code view} parameter does not take + * {@link View#checkInputConnectionProxy(View)} into account. This method returns {@code true} + * when and only when the specified {@code view} is the actual {@link View} instance that is + * connected to the IME.

+ * + * @param view {@link View} to be checked. + * @return {@code true} if {@code view} is currently interacting with IME. * @hide - * @see #hasActiveInputConnectionInternal(View)} */ @TestApi public boolean hasActiveInputConnection(@Nullable View view) { - return hasActiveInputConnectionInternal(view); + synchronized (mH) { + return mCurRootView != null + && view != null + && mServedView == view + && mServedInputConnection != null + && mServedInputConnection.isAssociatedWith(view) + && isImeSessionAvailableLocked(); + } } /**