From 2e376ce96c580589873d12f0192a0c7265f42ad3 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Wed, 26 Apr 2023 20:59:41 -0700 Subject: [PATCH] Implement InputConnectionWrapper#takeSnapshot() This is a follow up CL to my previous CL [1], which forgot to implement InputConnectionWrapper#takeSnapshot(). As a result, it has alreays returned null even if the wrapped InputConnection does implement InputConnection#takeSnapshot(). This means that InputMethodManager#invalidateInput() had always fallen back to slow InputMethodManager#restartInput() path if an app returned a wrapped InputConnection for example. With this CL, wrapped InputConnection can also start taking advantage of fast restart-input mechanism unless it's further overridden by the app developer to return null for some reasons. [1]: I8e00dacce5ca915e276fdd8288ffc9167eb01a26 dae47962355ebca330f8fe57e3aa48f4d20a573a Test: atest CtsInputMethodTestCases:InputConnectionWrapperTest Fix: 263677142 Change-Id: I9064a4008d688f2e1188c8d78337f0454c101465 --- .../inputmethod/InputConnectionWrapper.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/core/java/android/view/inputmethod/InputConnectionWrapper.java b/core/java/android/view/inputmethod/InputConnectionWrapper.java index 8f270f5c5eeb6..62f3c909dd4fe 100644 --- a/core/java/android/view/inputmethod/InputConnectionWrapper.java +++ b/core/java/android/view/inputmethod/InputConnectionWrapper.java @@ -412,4 +412,32 @@ public class InputConnectionWrapper implements InputConnection { public boolean setImeConsumesInput(boolean imeConsumesInput) { return mTarget.setImeConsumesInput(imeConsumesInput); } + + /** + * Called by the system when it needs to take a snapshot of multiple text-related data in an + * atomic manner. + * + *

Editor authors: Supporting this method is strongly encouraged. Atomically + * taken {@link TextSnapshot} is going to be really helpful for the system when optimizing IPCs + * in a safe and deterministic manner. Return {@code null} if an atomically taken + * {@link TextSnapshot} is unavailable. The system continues supporting such a scenario + * gracefully.

+ * + *

IME authors: Currently IMEs cannot call this method directly and always + * receive {@code null} as the result.

+ * + *

Beware that there is a bug that this method was not overridden in + * {@link InputConnectionWrapper}, which ended up always returning {@code null} when gets + * called even if the wrapped {@link InputConnection} implements this method. The bug was + * fixed in {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE}.

+ * + * @return {@code null} if {@link TextSnapshot} is unavailable and/or this API is called from + * IMEs. Beware the bug in older devices mentioned above. + * @throws NullPointerException if the target is {@code null}. + */ + @Nullable + @Override + public TextSnapshot takeSnapshot() { + return mTarget.takeSnapshot(); + } }