Avoid unwanted wakeAndUnLock mode for face

Test: press power repeatedly on lock screen
Bug: 132846921
Fixes: 130766403
Bug: 130327302
Change-Id: Ic11914ad220a0c8f81f0d28d5f999fcf970ccdeb
(cherry picked from commit 9bae9c2dc6d2e507594a0195d1c74a7ed57cb8e8)
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
index 9feca3d..3c119a6 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
@@ -748,6 +748,10 @@
     private void handleFaceAuthenticated(int authUserId) {
         Trace.beginSection("KeyGuardUpdateMonitor#handlerFaceAuthenticated");
         try {
+            if (mGoingToSleep) {
+                Log.d(TAG, "Aborted successful auth because device is going to sleep.");
+                return;
+            }
             final int userId;
             try {
                 userId = ActivityManager.getService().getCurrentUser().id;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java
index d677952..aa78a5d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java
@@ -365,16 +365,23 @@
     private int calculateMode(BiometricSourceType biometricSourceType) {
         boolean unlockingAllowed = mUpdateMonitor.isUnlockingWithBiometricAllowed();
         boolean deviceDreaming = mUpdateMonitor.isDreaming();
-        boolean faceStayingOnKeyguard = biometricSourceType == BiometricSourceType.FACE
-                && !mKeyguardBypassController.getBypassEnabled();
+        boolean face = biometricSourceType == BiometricSourceType.FACE;
+        boolean faceStayingOnKeyguard = face && !mKeyguardBypassController.getBypassEnabled();
 
         if (!mUpdateMonitor.isDeviceInteractive()) {
             if (!mStatusBarKeyguardViewManager.isShowing()) {
                 return MODE_ONLY_WAKE;
             } else if (mDozeScrimController.isPulsing() && unlockingAllowed) {
                 return faceStayingOnKeyguard ? MODE_NONE : MODE_WAKE_AND_UNLOCK_PULSING;
-            } else if (unlockingAllowed || !mUnlockMethodCache.isMethodSecure()) {
+            } else if (!face && (unlockingAllowed || !mUnlockMethodCache.isMethodSecure())) {
                 return MODE_WAKE_AND_UNLOCK;
+            } else if (face) {
+                if (!(mDozeScrimController.isPulsing() && !unlockingAllowed)) {
+                    Log.wtf(TAG, "Face somehow arrived when the device was not interactive");
+                }
+                // We could theoretically return MODE_NONE, but this means that the device
+                // would be not interactive, unlocked, and the user would not see the device state.
+                return MODE_ONLY_WAKE;
             } else {
                 return MODE_SHOW_BOUNCER;
             }
@@ -389,6 +396,8 @@
                 return MODE_DISMISS_BOUNCER;
             } else if (unlockingAllowed) {
                 return faceStayingOnKeyguard ? MODE_ONLY_WAKE : MODE_UNLOCK;
+            } else if (face) {
+                return MODE_NONE;
             } else if (!mStatusBarKeyguardViewManager.isBouncerShowing()) {
                 return MODE_SHOW_BOUNCER;
             }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBypassController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBypassController.kt
index c9dd461..124206a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBypassController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBypassController.kt
@@ -19,7 +19,6 @@
 import android.content.Context
 import android.hardware.face.FaceManager
 import android.provider.Settings
-import com.android.internal.annotations.VisibleForTesting
 import com.android.keyguard.KeyguardUpdateMonitor
 import com.android.systemui.tuner.TunerService
 
@@ -59,10 +58,4 @@
             }
         }, Settings.Secure.FACE_UNLOCK_DISMISSES_KEYGUARD)
     }
-
-    @VisibleForTesting
-    constructor(bypassEnabled: Boolean, unlockMethodCache: UnlockMethodCache) {
-        this.bypassEnabled = bypassEnabled
-        this.unlockMethodCache = unlockMethodCache
-    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/BiometricsUnlockControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/BiometricsUnlockControllerTest.java
index 9b8d09e..eb3f56a 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/BiometricsUnlockControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/BiometricsUnlockControllerTest.java
@@ -71,6 +71,8 @@
     private UnlockMethodCache mUnlockMethodCache;
     @Mock
     private Handler mHandler;
+    @Mock
+    private KeyguardBypassController mKeyguardBypassController;
     private BiometricUnlockController mBiometricUnlockController;
 
     @Before
@@ -83,8 +85,9 @@
         mDependency.injectTestDependency(NotificationMediaManager.class, mMediaManager);
         mDependency.injectTestDependency(StatusBarWindowController.class,
                 mStatusBarWindowController);
-        mBiometricUnlockController = new TestableBiometricUnlockController(
-                false /* faceDismissesKeyguard */);
+        mBiometricUnlockController = new BiometricUnlockController(mContext, mDozeScrimController,
+                mKeyguardViewMediator, mScrimController, mStatusBar, mUnlockMethodCache,
+                mHandler, mUpdateMonitor, 0 /* wakeUpDelay */, mKeyguardBypassController);
         mBiometricUnlockController.setStatusBarKeyguardViewManager(mStatusBarKeyguardViewManager);
     }
 
@@ -139,9 +142,8 @@
     }
 
     @Test
-    public void onBiometricAuthenticated_whenFace_dismissingKeyguard() {
-        mBiometricUnlockController = new TestableBiometricUnlockController(
-                true /* faceDismissesKeyguard */);
+    public void onBiometricAuthenticated_whenFace_andBypass_dismissKeyguard() {
+        when(mKeyguardBypassController.getBypassEnabled()).thenReturn(true);
         mBiometricUnlockController.setStatusBarKeyguardViewManager(mStatusBarKeyguardViewManager);
 
         when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true);
@@ -184,14 +186,4 @@
         mBiometricUnlockController.onFinishedGoingToSleep(-1);
         verify(mHandler).post(any());
     }
-
-    private class TestableBiometricUnlockController extends BiometricUnlockController {
-
-        TestableBiometricUnlockController(boolean faceDismissesKeyguard) {
-            super(mContext, mDozeScrimController,
-                    mKeyguardViewMediator, mScrimController, mStatusBar, mUnlockMethodCache,
-                    mHandler, mUpdateMonitor, 0 /* wakeUpDelay */,
-                    new KeyguardBypassController(faceDismissesKeyguard, mUnlockMethodCache));
-        }
-    }
 }