From 3fd0b400ee46089d3a6eb5afafe67d6231f68cf5 Mon Sep 17 00:00:00 2001 From: Shan Huang Date: Fri, 24 Feb 2023 18:53:09 -0800 Subject: [PATCH] Do not forward & register IME compat back callback in the app process. IME window's ViewRootImpl registers its compat callback when the first child view is added. If the app dispatches start input and sets a forwarding dispatcher on the IME's WindowOnBackInvokedDispatcher, before IME ViewRootImpl receives its first child, the compat callback would be forwarded to app process and registered on the app window. We should prevent this from happening because the IME window never has focus and the compat callback will just get stuck and break back nav. Test: m -j. Test: for run in {1..500}; do adb shell input tap 164 1950; sleep 1; adb shell input swipe 50 1000 200 1000; sleep 1; done Bug: 269821450 Change-Id: I9157d8d9c85edc29a5d004709c2cd3e25764b56a --- core/java/android/view/ViewRootImpl.java | 4 ++++ core/java/android/window/WindowOnBackInvokedDispatcher.java | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index f430ec300b5bf..2ff9ad346a71a 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -11382,6 +11382,10 @@ public final class ViewRootImpl implements ViewParent, sendBackKeyEvent(KeyEvent.ACTION_DOWN); sendBackKeyEvent(KeyEvent.ACTION_UP); }; + if (mOnBackInvokedDispatcher.hasImeOnBackInvokedDispatcher()) { + Log.d(TAG, "Skip registering CompatOnBackInvokedCallback on IME dispatcher"); + return; + } mOnBackInvokedDispatcher.registerOnBackInvokedCallback( OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCompatOnBackInvokedCallback); } diff --git a/core/java/android/window/WindowOnBackInvokedDispatcher.java b/core/java/android/window/WindowOnBackInvokedDispatcher.java index a8c2b2f28df4a..8066f5085a01b 100644 --- a/core/java/android/window/WindowOnBackInvokedDispatcher.java +++ b/core/java/android/window/WindowOnBackInvokedDispatcher.java @@ -357,6 +357,11 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher { mImeDispatcher = imeDispatcher; } + /** Returns true if a non-null {@link ImeOnBackInvokedDispatcher} has been set. **/ + public boolean hasImeOnBackInvokedDispatcher() { + return mImeDispatcher != null; + } + /** * Class used to check whether a callback can be registered or not. This is meant to be * shared with {@link ProxyOnBackInvokedDispatcher} which needs to do the same checks.