QS Detail: Fix up open/close animations

 - CP from CL below to handle removing callbacks correctly
 - Make correct QSPanel handle detail callbacks (quick/full)
 - Move expanding/collapsing panel to QSDetail
 - Collapse to the point expansion was from

Fixes: 28770315
Fixes: 29057236
Change-Id: Ib70c29cbc45dd8a58a751de0ea9e4f8a25057468
(based on cherry pick from commit d319f04986e44a7e9eab5f2822f1a6510282f803)
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
index 6945176..2c874e5 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
@@ -23,6 +23,7 @@
 import android.os.Handler;
 import android.os.Message;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.ImageView;
@@ -56,7 +57,7 @@
 
     private int mPanelPaddingBottom;
     private int mBrightnessPaddingTop;
-    private boolean mExpanded;
+    protected boolean mExpanded;
     protected boolean mListening;
 
     private Callback mCallback;
@@ -70,7 +71,6 @@
 
     private QSCustomizer mCustomizePanel;
     private Record mDetailRecord;
-    private boolean mTriggeredExpand;
 
     public QSPanel(Context context) {
         this(context, null);
@@ -221,7 +221,6 @@
         }
         MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
         if (!mExpanded) {
-            mTriggeredExpand = false;
             closeDetail();
         } else {
             logTiles();
@@ -279,6 +278,7 @@
     public void setTiles(Collection<QSTile<?>> tiles, boolean collapsedView) {
         for (TileRecord record : mRecords) {
             mTileLayout.removeTile(record);
+            record.tile.removeCallback(record.callback);
         }
         mRecords.clear();
         for (QSTile<?> tile : tiles) {
@@ -294,6 +294,10 @@
         return new QSTileView(mContext, tile.createTileView(mContext), collapsedView);
     }
 
+    protected boolean shouldShowDetail() {
+        return mExpanded;
+    }
+
     protected void addTile(final QSTile<?> tile, boolean collapsedView) {
         final TileRecord r = new TileRecord();
         r.tile = tile;
@@ -306,7 +310,11 @@
 
             @Override
             public void onShowDetail(boolean show) {
-                QSPanel.this.showDetail(show, r);
+                // Both the collapsed and full QS panels get this callback, this check determines
+                // which one should handle showing the detail.
+                if (shouldShowDetail()) {
+                    QSPanel.this.showDetail(show, r);
+                }
             }
 
             @Override
@@ -330,6 +338,7 @@
             }
         };
         r.tile.addCallback(callback);
+        r.callback = callback;
         final View.OnClickListener click = new View.OnClickListener() {
             @Override
             public void onClick(View v) {
@@ -390,17 +399,6 @@
     }
 
     protected void handleShowDetail(Record r, boolean show) {
-        if (show) {
-            if (!mExpanded) {
-                mTriggeredExpand = true;
-                mHost.animateToggleQSExpansion();
-            } else {
-                mTriggeredExpand = false;
-            }
-        } else if (mTriggeredExpand) {
-            mHost.animateToggleQSExpansion();
-            mTriggeredExpand = false;
-        }
         if (r instanceof TileRecord) {
             handleShowDetailTile((TileRecord) r, show);
         } else {
@@ -520,6 +518,7 @@
         public QSTile<?> tile;
         public QSTileBaseView tileView;
         public boolean scanState;
+        public QSTile.Callback callback;
     }
 
     public interface Callback {