From e43a10f257a109b533326bf251ecc167a47fe769 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Wed, 20 Oct 2021 22:48:19 -0700 Subject: [PATCH] Add RemoteInputConnectionImpl#finishComposingTextFromImm() This is a preparation CL to make InputConnection tasks cancellable. Currently RemoteInputConnectionImpl#finishComposingText() is the only method that can be called by both RemoteInputConnection as a Binder call and InputMethodManager as a direct invocation. This is not convenient because a common header data structure is going to be added to every IPC defined in IInputContext in order to implement InputConnection task cancelation. Thus, this CL introduces RemoteInputConnectionImpl#finishComposingTextFromImm() to decouple this code path from subsequent CLs. There should be no observable behavior change yet in this CL. Bug: 195115071 Test: presubmit Change-Id: I07290af2595c7a556cb5daa8a4c0f9c4a7daa49c --- .../view/inputmethod/InputMethodManager.java | 4 +-- .../RemoteInputConnectionImpl.java | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 009afceebf2c6..e023ed5a82696 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -696,7 +696,7 @@ public final class InputMethodManager { @Override public void finishComposingText() { if (mServedInputConnection != null) { - mServedInputConnection.finishComposingText(); + mServedInputConnection.finishComposingTextFromImm(); } } @@ -919,7 +919,7 @@ public final class InputMethodManager { mRestartOnNextWindowFocus = true; // Note that finishComposingText() is allowed to run // even when we are not active. - mFallbackInputConnection.finishComposingText(); + mFallbackInputConnection.finishComposingTextFromImm(); } // Check focus again in case that "onWindowFocus" is called before // handling this message. diff --git a/core/java/com/android/internal/inputmethod/RemoteInputConnectionImpl.java b/core/java/com/android/internal/inputmethod/RemoteInputConnectionImpl.java index 7a668fb74cb76..788e3e4e6682c 100644 --- a/core/java/com/android/internal/inputmethod/RemoteInputConnectionImpl.java +++ b/core/java/com/android/internal/inputmethod/RemoteInputConnectionImpl.java @@ -430,6 +430,34 @@ public final class RemoteInputConnectionImpl extends IInputContext.Stub { }); } + /** + * Dispatches {@link InputConnection#finishComposingText()}. + * + *

This method is intended to be called only from {@link InputMethodManager}.

+ */ + public void finishComposingTextFromImm() { + dispatchWithTracing("finishComposingTextFromImm", () -> { + if (isFinished()) { + // In this case, #finishComposingText() is guaranteed to be called already. + // There should be no negative impact if we ignore this call silently. + if (DEBUG) { + Log.w(TAG, "Bug 35301295: Redundant finishComposingTextFromImm."); + } + return; + } + InputConnection ic = getInputConnection(); + // Note we do NOT check isActive() here, because this is safe + // for an IME to call at any time, and we need to allow it + // through to clean up our state after the IME has switched to + // another client. + if (ic == null) { + Log.w(TAG, "finishComposingTextFromImm on inactive InputConnection"); + return; + } + ic.finishComposingText(); + }); + } + @Override public void finishComposingText() { dispatchWithTracing("finishComposingText", () -> {