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/ActivatableNotificationView.java b/packages/SystemUI/src/com/android/systemui/statusbar/ActivatableNotificationView.java
index 99487ff..ca290e4 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/ActivatableNotificationView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/ActivatableNotificationView.java
@@ -662,7 +662,6 @@
     }
 
     public void reset() {
-        super.reset();
         setTintColor(0);
         setShowingLegacyBackground(false);
         setBelowSpeedBump(false);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
index f4857eb..dc7d9f4 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
@@ -1737,6 +1737,9 @@
                 : null;
 
         // Reapply the RemoteViews
+        if (entry.row != null) {
+            entry.row.resetHeight();
+        }
         contentView.reapply(mContext, entry.expanded, mOnClickHandler);
         if (bigContentView != null && entry.getBigContentView() != null) {
             bigContentView.reapply(mContext, entry.getBigContentView(),
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java b/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java
index 43b7707..9ac20a6 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java
@@ -88,9 +88,14 @@
         mExpansionDisabled = false;
         mPublicLayout.reset();
         mPrivateLayout.reset();
+        resetHeight();
+        logExpansionEvent(false, wasExpanded);
+    }
+
+    public void resetHeight() {
         mMaxExpandHeight = 0;
         mWasReset = true;
-        logExpansionEvent(false, wasExpanded);
+        onHeightReset();
     }
 
     @Override
@@ -178,20 +183,26 @@
      * @param expand whether the system wants this notification to be expanded.
      */
     public void setSystemExpanded(boolean expand) {
-        final boolean wasExpanded = isExpanded();
-        mIsSystemExpanded = expand;
-        notifyHeightChanged();
-        logExpansionEvent(false, wasExpanded);
+        if (expand != mIsSystemExpanded) {
+            final boolean wasExpanded = isExpanded();
+            mIsSystemExpanded = expand;
+            notifyHeightChanged();
+            logExpansionEvent(false, wasExpanded);
+        }
     }
 
     /**
      * @param expansionDisabled whether to prevent notification expansion
      */
     public void setExpansionDisabled(boolean expansionDisabled) {
-        final boolean wasExpanded = isExpanded();
-        mExpansionDisabled = expansionDisabled;
-        logExpansionEvent(false, wasExpanded);
-        notifyHeightChanged();
+        if (expansionDisabled != mExpansionDisabled) {
+            final boolean wasExpanded = isExpanded();
+            mExpansionDisabled = expansionDisabled;
+            logExpansionEvent(false, wasExpanded);
+            if (wasExpanded != isExpanded()) {
+                notifyHeightChanged();
+            }
+        }
     }
 
     /**
@@ -368,9 +379,8 @@
         mPrivateLayout.notifyContentUpdated();
     }
 
-    public boolean isShowingLayoutLayouted() {
-        NotificationContentView showingLayout = getShowingLayout();
-        return showingLayout.getWidth() != 0;
+    public boolean isMaxExpandHeightInitialized() {
+        return mMaxExpandHeight != 0;
     }
 
     private NotificationContentView getShowingLayout() {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableView.java b/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableView.java
index 5b0bf03..127ff6c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableView.java
@@ -255,7 +255,7 @@
     public void setBelowSpeedBump(boolean below) {
     }
 
-    public void reset() {
+    public void onHeightReset() {
         if (mOnHeightChangedListener != null) {
             mOnHeightChangedListener.onReset(this);
         }
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);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java
index 2c5bcb79..f381b05 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java
@@ -122,6 +122,7 @@
             MotionEvent cancellation = MotionEvent.obtain(ev);
             cancellation.setAction(MotionEvent.ACTION_CANCEL);
             mStackScrollLayout.onInterceptTouchEvent(cancellation);
+            mNotificationPanel.onInterceptTouchEvent(cancellation);
             cancellation.recycle();
         }
         return intercept;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java b/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java
index 462452b..eadf951 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java
@@ -1914,6 +1914,7 @@
     @Override
     public void onReset(ExpandableView view) {
         mRequestViewResizeAnimationOnLayout = true;
+        mStackScrollAlgorithm.onReset(view);
     }
 
     private void updateScrollPositionOnExpandInBottom(ExpandableView view) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/stack/StackScrollAlgorithm.java b/packages/SystemUI/src/com/android/systemui/statusbar/stack/StackScrollAlgorithm.java
index f984339b3..dc6a98d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/stack/StackScrollAlgorithm.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/stack/StackScrollAlgorithm.java
@@ -751,39 +751,42 @@
                 // current height.
                 mFirstChildMaxHeight = mFirstChildWhileExpanding.getActualHeight();
             } else {
-
-                // We are expanding the shade, expand it to its full height.
-                if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) {
-
-                    // This child was not layouted yet, wait for a layout pass
-                    mFirstChildWhileExpanding
-                            .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
-                                @Override
-                                public void onLayoutChange(View v, int left, int top, int right,
-                                        int bottom, int oldLeft, int oldTop, int oldRight,
-                                        int oldBottom) {
-                                    if (mFirstChildWhileExpanding != null) {
-                                        mFirstChildMaxHeight = getMaxAllowedChildHeight(
-                                                mFirstChildWhileExpanding);
-                                    } else {
-                                        mFirstChildMaxHeight = 0;
-                                    }
-                                    v.removeOnLayoutChangeListener(this);
-                                }
-                            });
-                } else {
-                    mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding);
-                }
+                updateFirstChildMaxSizeToMaxHeight();
             }
         } else {
             mFirstChildMaxHeight = 0;
         }
     }
 
+    private void updateFirstChildMaxSizeToMaxHeight() {
+        // We are expanding the shade, expand it to its full height.
+        if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) {
+
+            // This child was not layouted yet, wait for a layout pass
+            mFirstChildWhileExpanding
+                    .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
+                        @Override
+                        public void onLayoutChange(View v, int left, int top, int right,
+                                int bottom, int oldLeft, int oldTop, int oldRight,
+                                int oldBottom) {
+                            if (mFirstChildWhileExpanding != null) {
+                                mFirstChildMaxHeight = getMaxAllowedChildHeight(
+                                        mFirstChildWhileExpanding);
+                            } else {
+                                mFirstChildMaxHeight = 0;
+                            }
+                            v.removeOnLayoutChangeListener(this);
+                        }
+                    });
+        } else {
+            mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding);
+        }
+    }
+
     private boolean isMaxSizeInitialized(ExpandableView child) {
         if (child instanceof ExpandableNotificationRow) {
             ExpandableNotificationRow row = (ExpandableNotificationRow) child;
-            return row.isShowingLayoutLayouted();
+            return row.isMaxExpandHeightInitialized();
         }
         return child == null || child.getWidth() != 0;
     }
@@ -818,6 +821,12 @@
         updatePadding(dimmed);
     }
 
+    public void onReset(ExpandableView view) {
+        if (view.equals(mFirstChildWhileExpanding)) {
+            updateFirstChildMaxSizeToMaxHeight();
+        }
+    }
+
     class StackScrollAlgorithmState {
 
         /**