From 2bcf7edb8674b4aacf740dbf4f29aaf9ec596610 Mon Sep 17 00:00:00 2001 From: Taran Singh Date: Wed, 19 Jul 2023 18:12:01 +0000 Subject: [PATCH] Refactor InkWindow lifetime Refactor InkWindow instance to live longer with a timer. This should help with faster handwritiing intiiaiton if handwriting is intiiated again shortly after the new window gets bound to IME. Also refactored to finish handwriting on idle timeout rather than just removing the window. Bug: 291946496 Test: atest StylusHandwritingTest Change-Id: I29a1eec9502b9558f8a7992308ec894f160dd4b7 --- .../InputMethodService.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/core/java/android/inputmethodservice/InputMethodService.java b/core/java/android/inputmethodservice/InputMethodService.java index e472a40617ee0..481f803e227e9 100644 --- a/core/java/android/inputmethodservice/InputMethodService.java +++ b/core/java/android/inputmethodservice/InputMethodService.java @@ -806,9 +806,11 @@ public class InputMethodService extends AbstractInputMethodService { onUnbindInput(); mInputBinding = null; mInputConnection = null; - // free-up cached InkWindow surface on detaching from current client. + if (mInkWindow != null) { - removeHandwritingInkWindow(); + finishStylusHandwriting(); + // free-up InkWindow surface after timeout. + scheduleStylusWindowIdleTimeout(); } } @@ -1018,6 +1020,7 @@ public class InputMethodService extends AbstractInputMethodService { mOnPreparedStylusHwCalled = true; } if (onStartStylusHandwriting()) { + cancelStylusWindowIdleTimeout(); mPrivOps.onStylusHandwritingReady(requestId, Process.myPid()); } else { Log.i(TAG, "IME is not ready. Can't start Stylus Handwriting"); @@ -1107,7 +1110,7 @@ public class InputMethodService extends AbstractInputMethodService { */ @Override public void removeStylusHandwritingWindow() { - InputMethodService.this.removeStylusHandwritingWindow(); + InputMethodService.this.finishAndRemoveStylusHandwritingWindow(); } /** @@ -2664,21 +2667,15 @@ public class InputMethodService extends AbstractInputMethodService { * Typically, this is called when {@link InkWindow} should no longer be holding a surface in * memory. */ - private void removeStylusHandwritingWindow() { + private void finishAndRemoveStylusHandwritingWindow() { + cancelStylusWindowIdleTimeout(); + mOnPreparedStylusHwCalled = false; + mStylusWindowIdleTimeoutRunnable = null; if (mInkWindow != null) { if (mHandwritingRequestId.isPresent()) { // if handwriting session is still ongoing. This shouldn't happen. finishStylusHandwriting(); } - removeHandwritingInkWindow(); - } - } - - private void removeHandwritingInkWindow() { - cancelStylusWindowIdleTimeout(); - mOnPreparedStylusHwCalled = false; - mStylusWindowIdleTimeoutRunnable = null; - if (mInkWindow != null) { mInkWindow.hide(true /* remove */); mInkWindow.destroy(); mInkWindow = null; @@ -2704,7 +2701,7 @@ public class InputMethodService extends AbstractInputMethodService { private Runnable getStylusWindowIdleTimeoutRunnable() { if (mStylusWindowIdleTimeoutRunnable == null) { mStylusWindowIdleTimeoutRunnable = () -> { - removeHandwritingInkWindow(); + finishAndRemoveStylusHandwritingWindow(); mStylusWindowIdleTimeoutRunnable = null; }; }