From dc1b3660fe104793014c9bacc3546e884cf2ce1e Mon Sep 17 00:00:00 2001 From: Yein Jo Date: Mon, 12 Sep 2022 23:09:16 +0000 Subject: [PATCH] Migrate Launcher to use the new OnBackInvokedCallback Bug: b/238475344 Test: Tapl & manual Change-Id: Ieab329873f82890d95136e490086cc2cffff7ba8 --- src/com/android/launcher3/Launcher.java | 14 ++++++++++++++ .../launcher3/tapl/LauncherInstrumentation.java | 15 +++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index 4a52d3e6a2..0d0b2248cf 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -113,6 +113,7 @@ import android.view.accessibility.AccessibilityEvent; import android.view.animation.OvershootInterpolator; import android.widget.ImageView; import android.widget.Toast; +import android.window.OnBackInvokedDispatcher; import androidx.annotation.CallSuper; import androidx.annotation.NonNull; @@ -545,6 +546,8 @@ public class Launcher extends StatefulActivity getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_NOTHING); } setTitle(R.string.home_screen); + + registerOnBackInvokedCallback(); } protected LauncherOverlayManager getDefaultOverlay() { @@ -2085,6 +2088,17 @@ public class Launcher extends StatefulActivity mStateManager.getState().onBackPressed(this); } + private void registerOnBackInvokedCallback() { + if (Utilities.ATLEAST_T) { + getOnBackInvokedDispatcher().registerOnBackInvokedCallback( + OnBackInvokedDispatcher.PRIORITY_DEFAULT, + () -> { + onBackPressed(); + TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onBackInvoked"); + }); + } + } + protected void onScreenOff() { // Reset AllApps to its initial state only if we are not in the middle of // processing a multi-step drop diff --git a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java index fa7e8e93bb..dd2da14946 100644 --- a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java +++ b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java @@ -111,8 +111,11 @@ public final class LauncherInstrumentation { static final Pattern EVENT_TOUCH_UP_TIS = getTouchEventPatternTIS("ACTION_UP"); static final Pattern EVENT_TOUCH_CANCEL_TIS = getTouchEventPatternTIS("ACTION_CANCEL"); - static final Pattern EVENT_KEY_BACK_DOWN = getKeyEventPattern("ACTION_DOWN", "KEYCODE_BACK"); - static final Pattern EVENT_KEY_BACK_UP = getKeyEventPattern("ACTION_UP", "KEYCODE_BACK"); + private static final Pattern EVENT_KEY_BACK_DOWN = + getKeyEventPattern("ACTION_DOWN", "KEYCODE_BACK"); + private static final Pattern EVENT_KEY_BACK_UP = + getKeyEventPattern("ACTION_UP", "KEYCODE_BACK"); + private static final Pattern EVENT_ON_BACK_INVOKED = Pattern.compile("onBackInvoked"); private final String mLauncherPackage; private Boolean mIsLauncher3; @@ -984,8 +987,12 @@ public final class LauncherInstrumentation { } } if (launcherVisible) { - expectEvent(TestProtocol.SEQUENCE_MAIN, EVENT_KEY_BACK_DOWN); - expectEvent(TestProtocol.SEQUENCE_MAIN, EVENT_KEY_BACK_UP); + if (getContext().getApplicationInfo().isOnBackInvokedCallbackEnabled()) { + expectEvent(TestProtocol.SEQUENCE_MAIN, EVENT_ON_BACK_INVOKED); + } else { + expectEvent(TestProtocol.SEQUENCE_MAIN, EVENT_KEY_BACK_DOWN); + expectEvent(TestProtocol.SEQUENCE_MAIN, EVENT_KEY_BACK_UP); + } } } }