Fade in/out UDFPS when dialogs show/hide

Test: manually bring up & dismiss power menu on
the lock screen when UDFPS is enrolled, see
UDFPS fades in/out
Test: atest UdfpsKeyguardViewControllerTest
Fixes: 220044615
Fixes: 218806349

Change-Id: I0e44fd36444c57d3286b6f6d2d42aa0dae0289ef
diff --git a/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsAnimationView.java b/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsAnimationView.java
index 7fdb5ea..9281eb8 100644
--- a/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsAnimationView.java
+++ b/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsAnimationView.java
@@ -34,8 +34,10 @@
  * - optionally can override dozeTimeTick to adjust views for burn-in mitigation
  */
 public abstract class UdfpsAnimationView extends FrameLayout {
-    // mAlpha takes into consideration the status bar expansion amount to fade out icon when
-    // the status bar is expanded
+    private float mDialogSuggestedAlpha = 1f;
+    private float mNotificationShadeExpansion = 0f;
+
+    // mAlpha takes into consideration the status bar expansion amount and dialog suggested alpha
     private int mAlpha;
     boolean mPauseAuth;
 
@@ -92,6 +94,10 @@
     }
 
     int calculateAlpha() {
+        int alpha = expansionToAlpha(mNotificationShadeExpansion);
+        alpha *= mDialogSuggestedAlpha;
+        mAlpha = alpha;
+
         return mPauseAuth ? mAlpha : 255;
     }
 
@@ -111,8 +117,26 @@
         return (int) ((1 - percent) * 255);
     }
 
+    /**
+     * Set the suggested alpha based on whether a dialog was recently shown or hidden.
+     * @param dialogSuggestedAlpha value from 0f to 1f.
+     */
+    public void setDialogSuggestedAlpha(float dialogSuggestedAlpha) {
+        mDialogSuggestedAlpha = dialogSuggestedAlpha;
+        updateAlpha();
+    }
+
+    public float getDialogSuggestedAlpha() {
+        return mDialogSuggestedAlpha;
+    }
+
+    /**
+     * Sets the amount the notification shade is expanded. This will influence the opacity of the
+     * this visual affordance.
+     * @param expansion amount the shade has expanded from 0f to 1f.
+     */
     public void onExpansionChanged(float expansion) {
-        mAlpha = expansionToAlpha(expansion);
+        mNotificationShadeExpansion = expansion;
         updateAlpha();
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsAnimationViewController.kt b/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsAnimationViewController.kt
index c33cd8d..27b0592 100644
--- a/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsAnimationViewController.kt
+++ b/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsAnimationViewController.kt
@@ -15,10 +15,12 @@
  */
 package com.android.systemui.biometrics
 
+import android.animation.ValueAnimator
 import android.graphics.PointF
 import android.graphics.RectF
 import com.android.systemui.Dumpable
 import com.android.systemui.dump.DumpManager
+import com.android.systemui.animation.Interpolators
 import com.android.systemui.plugins.statusbar.StatusBarStateController
 import com.android.systemui.statusbar.phone.SystemUIDialogManager
 import com.android.systemui.statusbar.phone.panelstate.PanelExpansionListener
@@ -50,7 +52,8 @@
     private val view: T
         get() = mView!!
 
-    private val dialogListener = SystemUIDialogManager.Listener { updatePauseAuth() }
+    private var dialogAlphaAnimator: ValueAnimator? = null
+    private val dialogListener = SystemUIDialogManager.Listener { runDialogAlphaAnimator() }
 
     private val panelExpansionListener =
         PanelExpansionListener { fraction, expanded, tracking ->
@@ -83,6 +86,29 @@
      */
     open val paddingY: Int = 0
 
+    open fun updateAlpha() {
+        view.updateAlpha()
+    }
+
+    fun runDialogAlphaAnimator() {
+        val hideAffordance = dialogManager.shouldHideAffordance()
+        dialogAlphaAnimator?.cancel()
+        dialogAlphaAnimator = ValueAnimator.ofFloat(
+                view.calculateAlpha() / 255f,
+                if (hideAffordance) 0f else 1f)
+                .apply {
+            duration = if (hideAffordance) 83L else 200L
+            interpolator = if (hideAffordance) Interpolators.LINEAR else Interpolators.ALPHA_IN
+
+            addUpdateListener { animatedValue ->
+                view.setDialogSuggestedAlpha(animatedValue.animatedValue as Float)
+                updateAlpha()
+                updatePauseAuth()
+            }
+            start()
+        }
+    }
+
     override fun onViewAttached() {
         panelExpansionStateManager.addExpansionListener(panelExpansionListener)
         dialogManager.registerListener(dialogListener)
@@ -106,6 +132,7 @@
         pw.println("mNotificationShadeVisible=$notificationShadeVisible")
         pw.println("shouldPauseAuth()=" + shouldPauseAuth())
         pw.println("isPauseAuth=" + view.isPauseAuth)
+        pw.println("dialogSuggestedAlpha=" + view.dialogSuggestedAlpha)
     }
 
     /**
diff --git a/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsKeyguardViewController.java b/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsKeyguardViewController.java
index 5ac21ff..b8334a0 100644
--- a/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsKeyguardViewController.java
+++ b/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsKeyguardViewController.java
@@ -28,6 +28,7 @@
 import com.android.keyguard.KeyguardUpdateMonitor;
 import com.android.systemui.R;
 import com.android.systemui.animation.ActivityLaunchAnimator;
+import com.android.systemui.animation.Interpolators;
 import com.android.systemui.dump.DumpManager;
 import com.android.systemui.plugins.statusbar.StatusBarStateController;
 import com.android.systemui.statusbar.LockscreenShadeTransitionController;
@@ -112,6 +113,7 @@
         mActivityLaunchAnimator = activityLaunchAnimator;
 
         mUnlockedScreenOffDozeAnimator.setDuration(StackStateAnimator.ANIMATION_DURATION_STANDARD);
+        mUnlockedScreenOffDozeAnimator.setInterpolator(Interpolators.ALPHA_IN);
         mUnlockedScreenOffDozeAnimator.addUpdateListener(
                 new ValueAnimator.AnimatorUpdateListener() {
                     @Override
@@ -245,7 +247,7 @@
             return false;
         }
 
-        if (getDialogManager().shouldHideAffordance()) {
+        if (mView.getDialogSuggestedAlpha() == 0f) {
             return true;
         }
 
@@ -321,7 +323,8 @@
      * Update alpha for the UDFPS lock screen affordance. The AoD UDFPS visual affordance's
      * alpha is based on the doze amount.
      */
-    private void updateAlpha() {
+    @Override
+    public void updateAlpha() {
         // fade icon on transitions to showing the status bar, but if mUdfpsRequested, then
         // the keyguard is occluded by some application - so instead use the input bouncer
         // hidden amount to determine the fade
@@ -338,6 +341,10 @@
             if (mIsLaunchingActivity && !mUdfpsRequested) {
                 alpha *= (1.0f - mActivityLaunchProgress);
             }
+
+            // Fade out alpha when a dialog is shown
+            // Fade in alpha when a dialog is hidden
+            alpha *= mView.getDialogSuggestedAlpha();
         }
         mView.setUnpausedAlpha(alpha);
     }
@@ -369,6 +376,7 @@
         public void onStateChanged(int statusBarState) {
             mStatusBarState = statusBarState;
             mView.setStatusBarState(statusBarState);
+            updateAlpha();
             updatePauseAuth();
         }
     };
diff --git a/packages/SystemUI/tests/src/com/android/systemui/biometrics/UdfpsKeyguardViewControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/biometrics/UdfpsKeyguardViewControllerTest.java
index 7fb42b6..186f2bb 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/biometrics/UdfpsKeyguardViewControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/biometrics/UdfpsKeyguardViewControllerTest.java
@@ -124,6 +124,7 @@
         when(mView.getContext()).thenReturn(mResourceContext);
         when(mResourceContext.getString(anyInt())).thenReturn("test string");
         when(mKeyguardViewMediator.isAnimatingScreenOff()).thenReturn(false);
+        when(mView.getDialogSuggestedAlpha()).thenReturn(1f);
         mController = new UdfpsKeyguardViewController(
                 mView,
                 mStatusBarStateController,
@@ -144,7 +145,7 @@
     @Test
     public void testRegistersExpansionChangedListenerOnAttached() {
         mController.onViewAttached();
-        captureExpansionListeners();
+        captureStatusBarExpansionListeners();
     }
 
     @Test
@@ -173,7 +174,7 @@
     public void testListenersUnregisteredOnDetached() {
         mController.onViewAttached();
         captureStatusBarStateListeners();
-        captureExpansionListeners();
+        captureStatusBarExpansionListeners();
         captureKeyguardStateControllerCallback();
         mController.onViewDetached();
 
@@ -200,10 +201,41 @@
     public void testShouldPauseAuthBouncerShowing() {
         mController.onViewAttached();
         captureStatusBarStateListeners();
-
         sendStatusBarStateChanged(StatusBarState.KEYGUARD);
 
-        assertFalse(mController.shouldPauseAuth());
+        captureAltAuthInterceptor();
+        when(mStatusBarKeyguardViewManager.isBouncerShowing()).thenReturn(true);
+        mAltAuthInterceptor.onBouncerVisibilityChanged();
+
+        assertTrue(mController.shouldPauseAuth());
+    }
+
+    @Test
+    public void testShouldPauseAuthDialogSuggestedAlpha0() {
+        mController.onViewAttached();
+        captureStatusBarStateListeners();
+
+        when(mView.getDialogSuggestedAlpha()).thenReturn(0f);
+        sendStatusBarStateChanged(StatusBarState.KEYGUARD);
+
+        assertTrue(mController.shouldPauseAuth());
+    }
+
+    @Test
+    public void testFadeFromDialogSuggestedAlpha() {
+        // GIVEN view is attached and status bar expansion is 1f
+        mController.onViewAttached();
+        captureStatusBarStateListeners();
+        captureStatusBarExpansionListeners();
+        updateStatusBarExpansion(1f, true);
+        reset(mView);
+
+        // WHEN dialog suggested alpha is .6f
+        when(mView.getDialogSuggestedAlpha()).thenReturn(.6f);
+        sendStatusBarStateChanged(StatusBarState.KEYGUARD);
+
+        // THEN alpha is updated based on dialog suggested alpha
+        verify(mView).setUnpausedAlpha((int) (.6f * 255));
     }
 
     @Test
@@ -367,7 +399,7 @@
     public void testFadeInWithStatusBarExpansion() {
         // GIVEN view is attached
         mController.onViewAttached();
-        captureExpansionListeners();
+        captureStatusBarExpansionListeners();
         captureKeyguardStateControllerCallback();
         reset(mView);
 
@@ -382,7 +414,7 @@
     public void testShowUdfpsBouncer() {
         // GIVEN view is attached and status bar expansion is 0
         mController.onViewAttached();
-        captureExpansionListeners();
+        captureStatusBarExpansionListeners();
         captureKeyguardStateControllerCallback();
         captureAltAuthInterceptor();
         updateStatusBarExpansion(0, true);
@@ -401,9 +433,10 @@
     public void testTransitionToFullShadeProgress() {
         // GIVEN view is attached and status bar expansion is 1f
         mController.onViewAttached();
-        captureExpansionListeners();
+        captureStatusBarExpansionListeners();
         updateStatusBarExpansion(1f, true);
         reset(mView);
+        when(mView.getDialogSuggestedAlpha()).thenReturn(1f);
 
         // WHEN we're transitioning to the full shade
         float transitionProgress = .6f;
@@ -417,7 +450,7 @@
     public void testShowUdfpsBouncer_transitionToFullShadeProgress() {
         // GIVEN view is attached and status bar expansion is 1f
         mController.onViewAttached();
-        captureExpansionListeners();
+        captureStatusBarExpansionListeners();
         captureKeyguardStateControllerCallback();
         captureAltAuthInterceptor();
         updateStatusBarExpansion(1f, true);
@@ -440,7 +473,7 @@
         mStatusBarStateListener = mStateListenerCaptor.getValue();
     }
 
-    private void captureExpansionListeners() {
+    private void captureStatusBarExpansionListeners() {
         verify(mPanelExpansionStateManager, times(2))
                 .addExpansionListener(mExpansionListenerCaptor.capture());
         // first (index=0) is from super class, UdfpsAnimationViewController.
@@ -460,8 +493,6 @@
         mAltAuthInterceptor = mAltAuthInterceptorCaptor.getValue();
     }
 
-
-
     private void captureKeyguardStateControllerCallback() {
         verify(mKeyguardStateController).addCallback(
                 mKeyguardStateControllerCallbackCaptor.capture());