Fixed several bugs with the notification shade

Cleaned up the code around mMaxPanelHeight of the
PanelView which could lead to flickering during
peeking.
Changed the panel opening logic to account for lag
when we need to wait for a layout, which could lead
to inconsistent animations.
Fixed a bug where holes could appear in the shade
when notifications were updating.
This also improved the general updating behaviour
which is now done in a nicer animation.

Bug: 15942322
Bug: 15861506
Bug: 15168335
Change-Id: Ifd7ce51bea6b5e39c9b76fd0d766a7d2c42bf7a4
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 c8e943e..decaeb6 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
@@ -54,6 +54,11 @@
     private float mInitialOffsetOnTouch;
     private float mExpandedFraction = 0;
     protected float mExpandedHeight = 0;
+    private boolean mPanelClosedOnDown;
+    private boolean mHasLayoutedSinceDown;
+    private float mUpdateFlingVelocity;
+    private boolean mUpdateFlingOnLayout;
+    private boolean mTouching;
     private boolean mJustPeeked;
     private boolean mClosing;
     protected boolean mTracking;
@@ -76,7 +81,6 @@
 
     PanelBar mBar;
 
-    protected int mMaxPanelHeight = -1;
     private String mViewName;
     private float mInitialTouchY;
     private float mInitialTouchX;
@@ -226,6 +230,10 @@
                 mInitialOffsetOnTouch = mExpandedHeight;
                 mTouchSlopExceeded = false;
                 mJustPeeked = false;
+                mPanelClosedOnDown = mExpandedHeight == 0.0f;
+                mHasLayoutedSinceDown = false;
+                mUpdateFlingOnLayout = false;
+                mTouching = true;
                 if (mVelocityTracker == null) {
                     initVelocityTracker();
                 }
@@ -316,6 +324,10 @@
                     boolean expand = flingExpands(vel, vectorVel);
                     onTrackingStopped(expand);
                     fling(vel, expand);
+                    mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown;
+                    if (mUpdateFlingOnLayout) {
+                        mUpdateFlingVelocity = vel;
+                    }
                 } else {
                     boolean expands = onEmptySpaceClick(mInitialTouchX);
                     onTrackingStopped(expands);
@@ -325,6 +337,7 @@
                     mVelocityTracker.recycle();
                     mVelocityTracker = null;
                 }
+                mTouching = false;
                 break;
         }
         return !waitForTouchSlop || mTracking;
@@ -383,6 +396,10 @@
                 mInitialTouchX = x;
                 mTouchSlopExceeded = false;
                 mJustPeeked = false;
+                mPanelClosedOnDown = mExpandedHeight == 0.0f;
+                mHasLayoutedSinceDown = false;
+                mUpdateFlingOnLayout = false;
+                mTouching = true;
                 initVelocityTracker();
                 trackMovement(event);
                 break;
@@ -415,6 +432,10 @@
                     }
                 }
                 break;
+            case MotionEvent.ACTION_CANCEL:
+            case MotionEvent.ACTION_UP:
+                mTouching = false;
+                break;
         }
         return false;
     }
@@ -444,7 +465,6 @@
     protected void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
         loadDimens();
-        mMaxPanelHeight = -1;
     }
 
     /**
@@ -524,27 +544,6 @@
         return mViewName;
     }
 
-    @Override
-    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
-        if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
-                widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
-
-        // Did one of our children change size?
-        int newHeight = getMeasuredHeight();
-        if (newHeight > mMaxPanelHeight) {
-            // we only adapt the max height if it's bigger
-            mMaxPanelHeight = newHeight;
-            // If the user isn't actively poking us, let's rubberband to the content
-            if (!mTracking && mHeightAnimator == null
-                    && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
-                    && mMaxPanelHeight > 0 && mPeekAnimator == null) {
-                mExpandedHeight = mMaxPanelHeight;
-            }
-        }
-    }
-
     public void setExpandedHeight(float height) {
         if (DEBUG) logf("setExpandedHeight(%.1f)", height);
         setExpandedHeightInternal(height + getOverExpansionPixels());
@@ -552,10 +551,14 @@
 
     @Override
     protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
-        if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
-                (int)mExpandedHeight, mMaxPanelHeight);
         super.onLayout(changed, left, top, right, bottom);
         requestPanelHeightUpdate();
+        mHasLayoutedSinceDown = true;
+        if (mUpdateFlingOnLayout) {
+            abortAnimations();
+            fling(mUpdateFlingVelocity, true);
+            mUpdateFlingOnLayout = false;
+        }
     }
 
     protected void requestPanelHeightUpdate() {
@@ -567,7 +570,8 @@
                 && mExpandedHeight > 0
                 && currentMaxPanelHeight != mExpandedHeight
                 && !mPeekPending
-                && mPeekAnimator == null) {
+                && mPeekAnimator == null
+                && !mTouching) {
             setExpandedHeight(currentMaxPanelHeight);
         }
     }
@@ -615,10 +619,7 @@
      *
      * @return the default implementation simply returns the maximum height.
      */
-    protected int getMaxPanelHeight() {
-        mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
-        return mMaxPanelHeight;
-    }
+    protected abstract int getMaxPanelHeight();
 
     public void setExpandedFraction(float frac) {
         setExpandedHeight(getMaxPanelHeight() * frac);