Fix stuck status bars.

An odd little bit of logic (attempting to defend against
the status bar getting stuck on orientation change) was
ironically causing it to get stuck if the bar was closed
twice in very rapid succession (which can happen if you
manage to click the settings button as the panel is about to
close).

Other tweaks to help defend against this sort of problem in
the future:

- When the screen goes off, immediately collapse the
  notification panel (without animation)
- When completing panel collapse, force the height of the
  expanded view to 0 (in case it ended up some other way by
  this point).
- Reduce a weird little glitch when you start
  animateCollapse() in the middle of a reveal animation.
  (The panel would jump to full height.)

Bug: 6765842
Change-Id: I233467c73e130f64401333319943289cbf3daa56
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 b9e88a4..c337ebe 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -203,7 +203,6 @@
 
     // the tracker view
     int mTrackingPosition; // the position of the top of the tracking view.
-    private boolean mPanelSlightlyVisible;
 
     // ticker
     private Ticker mTicker;
@@ -1235,6 +1234,7 @@
                     + " mExpandedVisible=" + mExpandedVisible
                     + " mExpanded=" + mExpanded
                     + " mAnimating=" + mAnimating
+                    + " mAnimatingReveal=" + mAnimatingReveal
                     + " mAnimY=" + mAnimY
                     + " mAnimVel=" + mAnimVel
                     + " flags=" + flags);
@@ -1255,7 +1255,7 @@
         }
 
         int y;
-        if (mAnimating) {
+        if (mAnimating || mAnimatingReveal) {
             y = (int)mAnimY;
         } else {
             y = getExpandedViewMaxHeight()-1;
@@ -1290,6 +1290,10 @@
         if (!mExpandedVisible) {
             return;
         }
+
+        // Ensure the panel is fully collapsed (just in case; bug 6765842)
+        updateExpandedViewPos(0);
+
         mExpandedVisible = false;
         mPile.setLayoutTransitionsEnabled(false);
         if (mNavigationBarView != null)
@@ -2050,9 +2054,14 @@
     @Override
     protected void updateExpandedViewPos(int expandedPosition) {
         if (SPEW) {
-            Slog.d(TAG, "updateExpandedViewPos before expandedPosition=" + expandedPosition
+            Slog.d(TAG, "updateExpandedViewPos: expandedPosition=" + expandedPosition
                     //+ " mTrackingParams.y=" + ((mTrackingParams == null) ? "?" : mTrackingParams.y)
+                    + " mTracking=" + mTracking
                     + " mTrackingPosition=" + mTrackingPosition
+                    + " mExpandedVisible=" + mExpandedVisible
+                    + " mAnimating=" + mAnimating
+                    + " mAnimatingReveal=" + mAnimatingReveal
+                    + " mClosing=" + mClosing
                     + " gravity=" + mNotificationPanelGravity);
         }
         int panelh = 0;
@@ -2061,6 +2070,7 @@
         // If the expanded view is not visible, make sure they're still off screen.
         // Maybe the view was resized.
         if (!mExpandedVisible) {
+            if (SPEW) Slog.d(TAG, "updateExpandedViewPos: view not visible, bailing");
             updateExpandedInvisiblePosition();
             return;
         }
@@ -2082,13 +2092,21 @@
         }
 
         // catch orientation changes and other peculiar cases
-        if (panelh > disph || (panelh < disph && !mTracking && !mAnimating)) {
+        if (panelh > 0 &&
+                ((panelh > disph) ||
+                 (panelh < disph && !mTracking && !mAnimating))) {
+            if (SPEW) Slog.d(TAG, "updateExpandedViewPos: orientation change?");
             panelh = disph;
         } else if (panelh < 0) {
             panelh = 0;
         }
 
-        if (panelh == mTrackingPosition) return;
+        if (SPEW) Slog.d(TAG, "updateExpandedViewPos: adjusting size to panelh=" + panelh);
+
+        if (panelh == mTrackingPosition) {
+            if (SPEW) Slog.d(TAG, "updateExpandedViewPos: panelh == mTrackingPosition, bailing");
+            return;
+        }
 
         mTrackingPosition = panelh;
 
@@ -2248,8 +2266,7 @@
     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
-            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
-                    || Intent.ACTION_SCREEN_OFF.equals(action)) {
+            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                 int flags = CommandQueue.FLAG_EXCLUDE_NONE;
                 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                     String reason = intent.getStringExtra("reason");
@@ -2259,6 +2276,10 @@
                 }
                 animateCollapse(flags);
             }
+            else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
+                // no waiting!
+                performCollapse();
+            }
             else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
                 updateResources();
                 repositionNavigationBar();