Show bouncer immediately when MOTION_UP is received

Bug: 15163546
Change-Id: Ie50905b78352e54298cb7e0e3f13152e6c2becd0
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBouncer.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBouncer.java
index 2bb80bf..f72aff4 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBouncer.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBouncer.java
@@ -56,6 +56,9 @@
 
     public void show() {
         ensureView();
+        if (mRoot.getVisibility() == View.VISIBLE) {
+            return;
+        }
 
         // Try to dismiss the Keyguard. If no security pattern is set, this will dismiss the whole
         // Keyguard. If we need to authenticate, show the bouncer.
@@ -109,6 +112,10 @@
         return mRoot != null && mRoot.getVisibility() == View.VISIBLE;
     }
 
+    public void prepare() {
+        ensureView();
+    }
+
     private void ensureView() {
         if (mRoot == null) {
             inflateView();
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardIndicationTextView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardIndicationTextView.java
index 769b1b1..c5c3fff 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardIndicationTextView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardIndicationTextView.java
@@ -17,7 +17,9 @@
 package com.android.systemui.statusbar.phone;
 
 import android.content.Context;
+import android.text.TextUtils;
 import android.util.AttributeSet;
+import android.view.View;
 import android.widget.TextView;
 
 /**
@@ -50,7 +52,12 @@
     public void switchIndication(CharSequence text) {
 
         // TODO: Animation, make sure that we will show one indication long enough.
-        setText(text);
+        if (TextUtils.isEmpty(text)) {
+            setVisibility(View.INVISIBLE);
+        } else {
+            setVisibility(View.VISIBLE);
+            setText(text);
+        }
     }
 
     /**
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
index f5252a3..52688df 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
@@ -664,8 +664,8 @@
     }
 
     @Override
-    protected void onTrackingStopped() {
-        super.onTrackingStopped();
+    protected void onTrackingStopped(boolean expand) {
+        super.onTrackingStopped(expand);
         mOverExpansion = 0.0f;
         mNotificationStackScroller.setOverScrolledPixels(0.0f, true /* onTop */,
                 true /* animate */);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelBar.java
index 8800625..1015d5b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelBar.java
@@ -222,7 +222,11 @@
         }
     }
 
-    public void onTrackingStopped(PanelView panel) {
+    public void onTrackingStopped(PanelView panel, boolean expand) {
         mTracking = false;
     }
+
+    public void onExpandingFinished() {
+
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
index 7c1f2cf..7500c10 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
@@ -67,6 +67,7 @@
     private float mInitialTouchX;
 
     protected void onExpandingFinished() {
+        mBar.onExpandingFinished();
     }
 
     protected void onExpandingStarted() {
@@ -184,9 +185,9 @@
             case MotionEvent.ACTION_CANCEL:
                 mTracking = false;
                 mTrackingPointer = -1;
-                onTrackingStopped();
                 trackMovement(event);
-                flingWithCurrentVelocity();
+                boolean expand = flingWithCurrentVelocity();
+                onTrackingStopped(expand);
                 if (mVelocityTracker != null) {
                     mVelocityTracker.recycle();
                     mVelocityTracker = null;
@@ -196,8 +197,8 @@
         return true;
     }
 
-    protected void onTrackingStopped() {
-        mBar.onTrackingStopped(PanelView.this);
+    protected void onTrackingStopped(boolean expand) {
+        mBar.onTrackingStopped(PanelView.this, expand);
     }
 
     protected void onTrackingStarted() {
@@ -303,7 +304,10 @@
         mMaxPanelHeight = -1;
     }
 
-    private void flingWithCurrentVelocity() {
+    /**
+     * @return whether the panel will be expanded after the animation
+     */
+    private boolean flingWithCurrentVelocity() {
         float vel = getCurrentVelocity();
         boolean expand;
         if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
@@ -312,11 +316,16 @@
             expand = vel > 0;
         }
         fling(vel, expand);
+        return expand;
     }
 
     protected void fling(float vel, boolean expand) {
         cancelPeek();
         float target = expand ? getMaxPanelHeight() : 0.0f;
+        if (target == mExpandedHeight) {
+            onExpandingFinished();
+            return;
+        }
         ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, target);
         if (expand) {
             mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
index b36c2ef..e799ecb 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -209,6 +209,7 @@
     PhoneStatusBarView mStatusBarView;
     private int mStatusBarWindowState = WINDOW_STATE_SHOWING;
     private StatusBarWindowManager mStatusBarWindowManager;
+    private UnlockMethodCache mUnlockMethodCache;
 
     int mPixelFormat;
     Object mQueueLock = new Object();
@@ -523,6 +524,7 @@
                     Settings.Global.getUriFor(SETTING_HEADS_UP_TICKER), true,
                     mHeadsUpObserver);
         }
+        mUnlockMethodCache = UnlockMethodCache.getInstance(mContext);
         startKeyguard();
     }
 
@@ -2882,10 +2884,15 @@
         }
     }
 
-    public void onTrackingStopped() {
+    public void onTrackingStopped(boolean expand) {
         if (mState == StatusBarState.KEYGUARD) {
             mKeyguardIndicationTextView.switchIndication(mKeyguardHotwordPhrase);
         }
+        if (mState == StatusBarState.KEYGUARD || mState == StatusBarState.SHADE_LOCKED) {
+            if (!expand && !mUnlockMethodCache.isMethodInsecure()) {
+                showBouncer();
+            }
+        }
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
index 5fdf5bf..910d88c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
@@ -148,9 +148,15 @@
     }
 
     @Override
-    public void onTrackingStopped(PanelView panel) {
-        super.onTrackingStopped(panel);
-        mBar.onTrackingStopped();
+    public void onTrackingStopped(PanelView panel, boolean expand) {
+        super.onTrackingStopped(panel, expand);
+        mBar.onTrackingStopped(expand);
+    }
+
+    @Override
+    public void onExpandingFinished() {
+        super.onExpandingFinished();
+        mScrimController.onExpandingFinished();
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
index 95255d5..6156fc3 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
@@ -52,6 +52,7 @@
     private boolean mBouncerShowing;
     private boolean mAnimateChange;
     private boolean mUpdatePending;
+    private boolean mExpanding;
 
     private final Interpolator mInterpolator = new DecelerateInterpolator();
 
@@ -67,9 +68,14 @@
     }
 
     public void onTrackingStarted() {
+        mExpanding = true;
         mDarkenWhileDragging = !mUnlockMethodCache.isMethodInsecure();
     }
 
+    public void onExpandingFinished() {
+        mExpanding = false;
+    }
+
     public void setPanelExpansion(float fraction) {
         mFraction = fraction;
         scheduleUpdate();
@@ -77,7 +83,7 @@
 
     public void setBouncerShowing(boolean showing) {
         mBouncerShowing = showing;
-        mAnimateChange = true;
+        mAnimateChange = !mExpanding;
         scheduleUpdate();
     }
 
@@ -98,14 +104,14 @@
     }
 
     private void updateScrimKeyguard() {
-        if (mBouncerShowing) {
-            setScrimInFrontColor(SCRIM_IN_FRONT_ALPHA);
-            setScrimBehindColor(0f);
-        } else if (mDarkenWhileDragging) {
+        if (mExpanding && mDarkenWhileDragging) {
             float behindFraction = Math.max(0, Math.min(mFraction, 1));
             float fraction = 1 - behindFraction;
             setScrimInFrontColor(fraction * SCRIM_IN_FRONT_ALPHA);
             setScrimBehindColor(behindFraction * SCRIM_BEHIND_ALPHA_KEYGUARD);
+        } else if (mBouncerShowing) {
+            setScrimInFrontColor(SCRIM_IN_FRONT_ALPHA);
+            setScrimBehindColor(0f);
         } else {
             setScrimInFrontColor(0f);
             setScrimBehindColor(SCRIM_BEHIND_ALPHA_KEYGUARD);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java
index c3430c3..d5551b8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java
@@ -102,6 +102,7 @@
         } else {
             mPhoneStatusBar.showKeyguard();
             mBouncer.hide(false /* destroyView */);
+            mBouncer.prepare();
         }
     }