Fixed focusing order for the notification panel and QS

Also fixed multiple bugs regarding focusability, where
some elements were focusable even though invisible.
The buttons, QS-tiles, QS-detail buttons, QS Header
icons and other elements now have the correct focusability
state.
The rect indicating accessibility focus is now also
correct for dual label tiles, instead of just the whole
button.
Also fixes an ordering issue where notifications were above
the camera circle when launching.
In addition the focus order of the notifications now work
correctly.

Bug: 15569922
Bug: 15682123
Bug: 17159249
Bug: 15690386
Change-Id: Ie9f7ae73397b41ce2e9a4060699301fdef3a0d01
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
index c1fd509..a2136d2 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
@@ -71,6 +71,7 @@
     private QSTileHost mHost;
 
     private QSFooter mFooter;
+    private boolean mGridContentVisible = true;
 
     public QSPanel(Context context) {
         this(context, null);
@@ -189,13 +190,13 @@
         mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
     }
 
-    private void setTileVisibility(View v, boolean visible) {
-        mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visible ? 1 : 0, 0, v).sendToTarget();
+    private void setTileVisibility(View v, int visibility) {
+        mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visibility, 0, v).sendToTarget();
     }
 
-    private void handleSetTileVisibility(View v, boolean visible) {
-        if (visible == (v.getVisibility() == VISIBLE)) return;
-        v.setVisibility(visible ? VISIBLE : GONE);
+    private void handleSetTileVisibility(View v, int visibility) {
+        if (visibility == v.getVisibility()) return;
+        v.setVisibility(visibility);
     }
 
     public void setTiles(Collection<QSTile<?>> tiles) {
@@ -219,7 +220,14 @@
         final QSTile.Callback callback = new QSTile.Callback() {
             @Override
             public void onStateChanged(QSTile.State state) {
-                setTileVisibility(r.tileView, state.visible);
+                int visibility = state.visible ? VISIBLE : GONE;
+                if (state.visible && !mGridContentVisible) {
+
+                    // We don't want to show it if the content is hidden,
+                    // then we just set it to invisible, to ensure that it gets visible again
+                    visibility = INVISIBLE;
+                }
+                setTileVisibility(r.tileView, visibility);
                 r.tileView.onStateChanged(state);
             }
             @Override
@@ -317,7 +325,9 @@
             mDetail.bringToFront();
             mDetailContent.addView(r.detailView);
             setDetailRecord(r);
+            listener = mHideGridContentWhenDone;
         } else {
+            setGridContentVisibility(true);
             listener = mTeardownDetailWhenDone;
             fireScanStateChanged(false);
         }
@@ -325,6 +335,18 @@
         mClipper.animateCircularClip(x, y, show, listener);
     }
 
+    private void setGridContentVisibility(boolean visible) {
+        int newVis = visible ? VISIBLE : INVISIBLE;
+        for (int i = 0; i < mRecords.size(); i++) {
+            TileRecord tileRecord = mRecords.get(i);
+            if (tileRecord.tileView.getVisibility() != GONE) {
+                tileRecord.tileView.setVisibility(newVis);
+            }
+        }
+        mBrightnessView.setVisibility(newVis);
+        mGridContentVisible = visible;
+    }
+
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         final int width = MeasureSpec.getSize(widthMeasureSpec);
@@ -448,7 +470,7 @@
             if (msg.what == SHOW_DETAIL) {
                 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
             } else if (msg.what == SET_TILE_VISIBILITY) {
-                handleSetTileVisibility((View)msg.obj, msg.arg1 != 0);
+                handleSetTileVisibility((View)msg.obj, msg.arg1);
             }
         }
     }
@@ -473,6 +495,13 @@
         };
     };
 
+    private final AnimatorListenerAdapter mHideGridContentWhenDone = new AnimatorListenerAdapter() {
+        @Override
+        public void onAnimationEnd(Animator animation) {
+            setGridContentVisibility(false);
+        }
+    };
+
     public interface Callback {
         void onShowingDetail(QSTile.DetailAdapter detail);
         void onToggleStateChanged(boolean state);