From f155a9cf457f013dccb5304c89af15683c5e6d95 Mon Sep 17 00:00:00 2001 From: Shan Huang Date: Fri, 25 Mar 2022 17:30:04 +0000 Subject: [PATCH] Dispatch KEYCODE_BACK to the full input pipeline in compat callback. Also fix an issue that back callback is invoked both on ACTION_DOWN and ACTION_UP. Bug: 222408115 Fixes: 227161441 Fixes: 222408115 Fixes: 226301000 Test: Bring up IME from an EditText (e.g. Settings search bar), try swipe back, make sure IME is dismissed and not the activity. Change-Id: I7e5eeab276f70665d1fc37a0ef333f506fe305ec --- core/java/android/view/ViewRootImpl.java | 37 ++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index a3e6945617315..346fecb96a9a3 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -6446,6 +6446,24 @@ public final class ViewRootImpl implements ViewParent, return FINISH_HANDLED; } + // If the new back dispatch is enabled, intercept KEYCODE_BACK before it reaches the + // view tree and invoke the appropriate {@link OnBackInvokedCallback}. + if (isBack(event) + && mContext != null + && WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mContext)) { + OnBackInvokedCallback topCallback = + getOnBackInvokedDispatcher().getTopCallback(); + if (event.getAction() == KeyEvent.ACTION_UP) { + if (topCallback != null) { + topCallback.onBackInvoked(); + return FINISH_HANDLED; + } + } else { + // Drop other actions such as {@link KeyEvent.ACTION_DOWN}. + return FINISH_NOT_HANDLED; + } + } + // Deliver the key to the view hierarchy. if (mView.dispatchKeyEvent(event)) { return FINISH_HANDLED; @@ -6455,19 +6473,6 @@ public final class ViewRootImpl implements ViewParent, return FINISH_NOT_HANDLED; } - if (isBack(event) - && mContext != null - && WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mContext)) { - // Invoke the appropriate {@link OnBackInvokedCallback} if the new back - // navigation should be used, and the key event is not handled by anything else. - OnBackInvokedCallback topCallback = - getOnBackInvokedDispatcher().getTopCallback(); - if (topCallback != null) { - topCallback.onBackInvoked(); - return FINISH_HANDLED; - } - } - // This dispatch is for windows that don't have a Window.Callback. Otherwise, // the Window.Callback usually will have already called this (see // DecorView.superDispatchKeyEvent) leaving this call a no-op. @@ -10839,11 +10844,7 @@ public final class ViewRootImpl implements ViewParent, KeyCharacterMap.VIRTUAL_KEYBOARD, 0 /* scancode */, KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, InputDevice.SOURCE_KEYBOARD); - - ev.setDisplayId(mContext.getDisplay().getDisplayId()); - if (mView != null) { - mView.dispatchKeyEvent(ev); - } + enqueueInputEvent(ev); } private void registerCompatOnBackInvokedCallback() {