From d4b0e8a107069037137a2066240128ea368d8378 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Wed, 2 Nov 2022 10:26:49 -0700 Subject: [PATCH] Add @hide IMM#setRequestCursorUpdateDisplayIdCheck() This is a follow up CL to our previous CL [1], which re-introduced InputMethodManager#reportVirtualDisplayGeometry() for some special form factors. For apps that only aim to bypass display ID mismatch check in RemoteInputConnectionImpl#requestCursorUpdatesInternal(), this CL gives a much simpler and easier-to-maintain @hide method InputMethodManager#setRequestCursorUpdateDisplayIdCheck(). [1]: I35b491da5a340844e17542d7a6198f8152bbeda3 708c957fa32d25499960eea050db7a23f1353262 Bug: 224424149 Bug: 256926300 Test: presubmit Change-Id: I9e1f62e3d5d2a385dce8a9633b5b73f8e7f0ccef --- .../view/inputmethod/InputMethodManager.java | 27 +++++++++++++++++++ .../RemoteInputConnectionImpl.java | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 9106ce27c27c4..0c1b8b8e9418d 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -128,6 +128,7 @@ import java.util.Map; import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; /** @@ -3621,6 +3622,32 @@ public final class InputMethodManager { return IInputMethodManagerGlobalInvoker.getInputMethodWindowVisibleHeight(mClient); } + /** + * {@code true} means that + * {@link RemoteInputConnectionImpl#requestCursorUpdatesInternal(int, int, int)} returns + * {@code false} when the IME client and the IME run in different displays. + */ + final AtomicBoolean mRequestCursorUpdateDisplayIdCheck = new AtomicBoolean(true); + + /** + * Controls the display ID mismatch validation in + * {@link RemoteInputConnectionImpl#requestCursorUpdatesInternal(int, int, int)}. + * + *

{@link #updateCursorAnchorInfo(View, CursorAnchorInfo)} is not guaranteed to work + * correctly when the IME client and the IME run in different displays. This is why + * {@link RemoteInputConnectionImpl#requestCursorUpdatesInternal(int, int, int)} returns + * {@code false} by default when the display ID does not match. This method allows special apps + * to override this behavior when they are sure that it should work.

+ * + *

By default the validation is enabled.

+ * + * @param enabled {@code false} to disable the display ID validation. + * @hide + */ + public void setRequestCursorUpdateDisplayIdCheck(boolean enabled) { + mRequestCursorUpdateDisplayIdCheck.set(enabled); + } + /** * An internal API for {@link android.hardware.display.VirtualDisplay} to report where its * embedded virtual display is placed. diff --git a/core/java/android/view/inputmethod/RemoteInputConnectionImpl.java b/core/java/android/view/inputmethod/RemoteInputConnectionImpl.java index ead79245609a3..a9277658aaa7c 100644 --- a/core/java/android/view/inputmethod/RemoteInputConnectionImpl.java +++ b/core/java/android/view/inputmethod/RemoteInputConnectionImpl.java @@ -1071,7 +1071,8 @@ final class RemoteInputConnectionImpl extends IRemoteInputConnection.Stub { Log.w(TAG, "requestCursorAnchorInfo on inactive InputConnection"); return false; } - if (mParentInputMethodManager.getDisplayId() != imeDisplayId + if (mParentInputMethodManager.mRequestCursorUpdateDisplayIdCheck.get() + && mParentInputMethodManager.getDisplayId() != imeDisplayId && !mParentInputMethodManager.hasVirtualDisplayToScreenMatrix()) { // requestCursorUpdates() is not currently supported across displays. return false;