From 5a2b8d3ffe418e4f7eb12f24aa05f98500d15cf4 Mon Sep 17 00:00:00 2001 From: Nikolas Havrikov Date: Thu, 2 Dec 2021 18:21:54 +0100 Subject: [PATCH] Store displayIdToShowIme in an IMMS field This change allows the IMMS to remember the last display id the IME was requested to bind on. One use case is when we want to prevent binding the IME if there is no currently focused editText, and yet we want to still be able to handle an explicit request to show the IME later. Remembering the display id helps to show it on the correct screen. Bug: 199887357 Bug: 37617707 Test: make Change-Id: I98459c4a44416fd7209150932a2a2520612da818 --- .../InputMethodBindingController.java | 7 +++--- .../InputMethodManagerService.java | 23 ++++++++++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/services/core/java/com/android/server/inputmethod/InputMethodBindingController.java b/services/core/java/com/android/server/inputmethod/InputMethodBindingController.java index 6727a931de880..3d91feef70431 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodBindingController.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodBindingController.java @@ -383,7 +383,7 @@ final class InputMethodBindingController { @GuardedBy("mMethodMap") @NonNull - InputBindResult bindCurrentMethodLocked(int displayIdToShowIme) { + InputBindResult bindCurrentMethodLocked() { InputMethodInfo info = mMethodMap.get(mSelectedMethodId); if (info == null) { throw new IllegalArgumentException("Unknown id: " + mSelectedMethodId); @@ -395,7 +395,7 @@ final class InputMethodBindingController { mCurId = info.getId(); mLastBindTime = SystemClock.uptimeMillis(); - addFreshWindowTokenLocked(displayIdToShowIme); + addFreshWindowTokenLocked(); return new InputBindResult( InputBindResult.ResultCode.SUCCESS_WAITING_IME_BINDING, null, null, mCurId, mCurSeq, false); @@ -420,7 +420,8 @@ final class InputMethodBindingController { } @GuardedBy("mMethodMap") - private void addFreshWindowTokenLocked(int displayIdToShowIme) { + private void addFreshWindowTokenLocked() { + int displayIdToShowIme = mService.getDisplayIdToShowIme(); mCurToken = new Binder(); mService.setCurTokenDisplayId(displayIdToShowIme); diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java index bf4c3670b8a3d..bff4f273e1956 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java @@ -305,6 +305,17 @@ public class InputMethodManagerService extends IInputMethodManager.Stub @GuardedBy("mMethodMap") private int mMethodMapUpdateCount = 0; + /** + * The display id for which the latest startInput was called. + */ + @GuardedBy("mMethodMap") + int getDisplayIdToShowIme() { + return mDisplayIdToShowIme; + } + + @GuardedBy("mMethodMap") + private int mDisplayIdToShowIme = INVALID_DISPLAY; + // Ongoing notification private NotificationManager mNotificationManager; KeyguardManager mKeyguardManager; @@ -2340,10 +2351,10 @@ public class InputMethodManagerService extends IInputMethodManager.Stub } // Compute the final shown display ID with validated cs.selfReportedDisplayId for this // session & other conditions. - final int displayIdToShowIme = computeImeDisplayIdForTarget(cs.selfReportedDisplayId, + mDisplayIdToShowIme = computeImeDisplayIdForTarget(cs.selfReportedDisplayId, mImeDisplayValidator); - if (displayIdToShowIme == INVALID_DISPLAY) { + if (mDisplayIdToShowIme == INVALID_DISPLAY) { mImeHiddenByDisplayPolicy = true; hideCurrentInputLocked(mCurFocusedWindow, 0, null, SoftInputShowHideReason.HIDE_DISPLAY_IME_POLICY_HIDE); @@ -2364,7 +2375,7 @@ public class InputMethodManagerService extends IInputMethodManager.Stub // Check if the input method is changing. // We expect the caller has already verified that the client is allowed to access this // display ID. - if (isSelectedMethodBound(displayIdToShowIme)) { + if (isSelectedMethodBound()) { if (cs.curSession != null) { // Fast case: if we are already connected to the input method, // then just return it. @@ -2380,13 +2391,13 @@ public class InputMethodManagerService extends IInputMethodManager.Stub mBindingController.unbindCurrentMethodLocked(); - return mBindingController.bindCurrentMethodLocked(displayIdToShowIme); + return mBindingController.bindCurrentMethodLocked(); } - private boolean isSelectedMethodBound(int displayIdToShowIme) { + private boolean isSelectedMethodBound() { String curId = getCurId(); return curId != null && curId.equals(getSelectedMethodId()) - && displayIdToShowIme == mCurTokenDisplayId; + && mDisplayIdToShowIme == mCurTokenDisplayId; } @GuardedBy("mMethodMap")