From 627e12ca86125eee82d8b7fd2a79a9a2314cab86 Mon Sep 17 00:00:00 2001 From: Diya Bera Date: Wed, 31 May 2023 15:20:38 -0700 Subject: [PATCH] Don't show biometric prompt if owner is not in foreground Bug: 282739203 Test: Open Capital One, swipe up, biometric prompt should not flicker when app not in focus Change-Id: Ie0a0d23b604259bb7f06d062ef6fb1e7171c5c50 --- .../systemui/biometrics/AuthController.java | 57 +++++++++++-------- .../biometrics/AuthControllerTest.java | 19 +++++++ 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/biometrics/AuthController.java b/packages/SystemUI/src/com/android/systemui/biometrics/AuthController.java index 57f1928fe5454..b2ffea3d050c2 100644 --- a/packages/SystemUI/src/com/android/systemui/biometrics/AuthController.java +++ b/packages/SystemUI/src/com/android/systemui/biometrics/AuthController.java @@ -201,7 +201,9 @@ public class AuthController implements CoreStartable, CommandQueue.Callbacks, final TaskStackListener mTaskStackListener = new TaskStackListener() { @Override public void onTaskStackChanged() { - mHandler.post(AuthController.this::cancelIfOwnerIsNotInForeground); + if (!isOwnerInForeground()) { + mHandler.post(AuthController.this::cancelIfOwnerIsNotInForeground); + } } }; @@ -239,33 +241,37 @@ public class AuthController implements CoreStartable, CommandQueue.Callbacks, } } + private boolean isOwnerInForeground() { + final String clientPackage = mCurrentDialog.getOpPackageName(); + final List runningTasks = + mActivityTaskManager.getTasks(1); + if (!runningTasks.isEmpty()) { + final String topPackage = runningTasks.get(0).topActivity.getPackageName(); + if (!topPackage.contentEquals(clientPackage) + && !Utils.isSystem(mContext, clientPackage)) { + Log.w(TAG, "Evicting client due to: " + topPackage); + return false; + } + } + return true; + } + private void cancelIfOwnerIsNotInForeground() { mExecution.assertIsMainThread(); if (mCurrentDialog != null) { try { - final String clientPackage = mCurrentDialog.getOpPackageName(); - Log.w(TAG, "Task stack changed, current client: " + clientPackage); - final List runningTasks = - mActivityTaskManager.getTasks(1); - if (!runningTasks.isEmpty()) { - final String topPackage = runningTasks.get(0).topActivity.getPackageName(); - if (!topPackage.contentEquals(clientPackage) - && !Utils.isSystem(mContext, clientPackage)) { - Log.e(TAG, "Evicting client due to: " + topPackage); - mCurrentDialog.dismissWithoutCallback(true /* animate */); - mCurrentDialog = null; + mCurrentDialog.dismissWithoutCallback(true /* animate */); + mCurrentDialog = null; - for (Callback cb : mCallbacks) { - cb.onBiometricPromptDismissed(); - } + for (Callback cb : mCallbacks) { + cb.onBiometricPromptDismissed(); + } - if (mReceiver != null) { - mReceiver.onDialogDismissed( - BiometricPrompt.DISMISSED_REASON_USER_CANCEL, - null /* credentialAttestation */); - mReceiver = null; - } - } + if (mReceiver != null) { + mReceiver.onDialogDismissed( + BiometricPrompt.DISMISSED_REASON_USER_CANCEL, + null /* credentialAttestation */); + mReceiver = null; } } catch (RemoteException e) { Log.e(TAG, "Remote exception", e); @@ -1253,10 +1259,11 @@ public class AuthController implements CoreStartable, CommandQueue.Callbacks, cb.onBiometricPromptShown(); } mCurrentDialog = newDialog; - mCurrentDialog.show(mWindowManager, savedState); - if (!promptInfo.isAllowBackgroundAuthentication()) { - mHandler.post(this::cancelIfOwnerIsNotInForeground); + if (!promptInfo.isAllowBackgroundAuthentication() && !isOwnerInForeground()) { + cancelIfOwnerIsNotInForeground(); + } else { + mCurrentDialog.show(mWindowManager, savedState); } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthControllerTest.java index b9f92a064bc8f..b4a4a11a81a18 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthControllerTest.java @@ -954,6 +954,25 @@ public class AuthControllerTest extends SysuiTestCase { eq(null) /* credentialAttestation */); } + @Test + public void testShowDialog_whenOwnerNotInForeground() { + PromptInfo promptInfo = createTestPromptInfo(); + promptInfo.setAllowBackgroundAuthentication(false); + switchTask("other_package"); + mAuthController.showAuthenticationDialog(promptInfo, + mReceiver /* receiver */, + new int[]{1} /* sensorIds */, + false /* credentialAllowed */, + true /* requireConfirmation */, + 0 /* userId */, + 0 /* operationId */, + "testPackage", + REQUEST_ID); + + assertNull(mAuthController.mCurrentDialog); + verify(mDialog1, never()).show(any(), any()); + } + private void showDialog(int[] sensorIds, boolean credentialAllowed) { mAuthController.showAuthenticationDialog(createTestPromptInfo(), mReceiver /* receiver */,