From 61f1075626ba914261bb425e3fa8c1fc15b888cc Mon Sep 17 00:00:00 2001 From: Shan Huang Date: Sat, 10 Sep 2022 01:52:56 +0000 Subject: [PATCH] Migrate AuthContainerView to predictive back Bug: 238358031 Test: atest AuthContainerViewTest Change-Id: Ib64e078c00296c5b6306ba5178f417ce1d32f496 --- .../biometrics/AuthContainerView.java | 20 +++++++++++++++++-- .../biometrics/AuthContainerViewTest.kt | 16 +++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/biometrics/AuthContainerView.java b/packages/SystemUI/src/com/android/systemui/biometrics/AuthContainerView.java index 436b756ea0cb6..8f5cbb76222fc 100644 --- a/packages/SystemUI/src/com/android/systemui/biometrics/AuthContainerView.java +++ b/packages/SystemUI/src/com/android/systemui/biometrics/AuthContainerView.java @@ -54,6 +54,8 @@ import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ScrollView; +import android.window.OnBackInvokedCallback; +import android.window.OnBackInvokedDispatcher; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.jank.InteractionJankMonitor; @@ -127,6 +129,7 @@ public class AuthContainerView extends LinearLayout private final float mTranslationY; @ContainerState private int mContainerState = STATE_UNKNOWN; private final Set mFailedModalities = new HashSet(); + private final OnBackInvokedCallback mBackCallback = this::onBackInvoked; private final @Background DelayableExecutor mBackgroundExecutor; private int mOrientation; @@ -362,8 +365,7 @@ public class AuthContainerView extends LinearLayout return false; } if (event.getAction() == KeyEvent.ACTION_UP) { - sendEarlyUserCanceled(); - animateAway(AuthDialogCallback.DISMISSED_USER_CANCELED); + onBackInvoked(); } return true; }); @@ -373,6 +375,11 @@ public class AuthContainerView extends LinearLayout requestFocus(); } + private void onBackInvoked() { + sendEarlyUserCanceled(); + animateAway(AuthDialogCallback.DISMISSED_USER_CANCELED); + } + void sendEarlyUserCanceled() { mConfig.mCallback.onSystemEvent( BiometricConstants.BIOMETRIC_SYSTEM_EVENT_EARLY_USER_CANCEL, getRequestId()); @@ -520,6 +527,11 @@ public class AuthContainerView extends LinearLayout .start(); }); } + OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher(); + if (dispatcher != null) { + dispatcher.registerOnBackInvokedCallback( + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mBackCallback); + } } private Animator.AnimatorListener getJankListener(View v, String type, long timeout) { @@ -618,6 +630,10 @@ public class AuthContainerView extends LinearLayout @Override public void onDetachedFromWindow() { + OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher(); + if (dispatcher != null) { + findOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(mBackCallback); + } super.onDetachedFromWindow(); mWakefulnessLifecycle.removeObserver(this); } diff --git a/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthContainerViewTest.kt b/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthContainerViewTest.kt index bf3788e4c76a7..4a5b23c02e409 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthContainerViewTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthContainerViewTest.kt @@ -29,6 +29,7 @@ import android.testing.AndroidTestingRunner import android.testing.TestableLooper import android.testing.TestableLooper.RunWithLooper import android.testing.ViewUtils +import android.view.KeyEvent import android.view.View import android.view.WindowInsets import android.view.WindowManager @@ -91,6 +92,21 @@ class AuthContainerViewTest : SysuiTestCase() { verify(callback).onDialogAnimatedIn(authContainer?.requestId ?: 0L) } + @Test + fun testDismissesOnBack() { + val container = initializeFingerprintContainer(addToView = true) + assertThat(container.parent).isNotNull() + val root = container.rootView + + // Simulate back invocation + container.dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)) + container.dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK)) + waitForIdleSync() + + assertThat(container.parent).isNull() + assertThat(root.isAttachedToWindow).isFalse() + } + @Test fun testIgnoresAnimatedInWhenDismissed() { val container = initializeFingerprintContainer(addToView = false)