QS Edit: Try to simplify drag logic

 - Don't forcibly move around items during dragging, instead let the
ItemTouchHelper handle where things can and cannot be dragged.
 - Try to trigger notify calls only when needed.
 - Tiles will sit in the first slot after being removed until the
   edit panel is closed.

Change-Id: I4780e64bc39292ed85e961ac089c69834245dfb4
Fixes: 28067638
diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizer.java b/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizer.java
index 8a0079d..0de1e30 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizer.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizer.java
@@ -233,6 +233,7 @@
                 setVisibility(View.GONE);
             }
             mNotifQsContainer.setCustomizerAnimating(false);
+            mRecyclerView.setAdapter(mTileAdapter);
         }
 
         @Override
diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java b/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java
index f060502..8e4ed91 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java
@@ -112,6 +112,9 @@
     }
 
     public void setTileSpecs(List<String> currentSpecs) {
+        if (currentSpecs.equals(mCurrentSpecs)) {
+            return;
+        }
         mCurrentSpecs = currentSpecs;
         recalcSpecs();
     }
@@ -257,7 +260,7 @@
         }
         holder.mTileView.onStateChanged(info.state);
         holder.mTileView.setAppLabel(info.appLabel);
-        holder.mTileView.setShowAppLabel(mTileDividerIndex > -1 && position > mTileDividerIndex);
+        holder.mTileView.setShowAppLabel(position > mEditIndex && !info.isSystem);
 
         if (mAccessibilityManager.isTouchExplorationEnabled()) {
             final boolean selectable = !mAccessibilityMoving || position < mEditIndex;
@@ -292,13 +295,11 @@
         mTiles.remove(mEditIndex--);
         notifyItemRemoved(mEditIndex - 1);
         move(mAccessibilityFromIndex, position, v);
-        updateDividerLocations();
         notifyDataSetChanged();
-        saveSpecs(mHost);
     }
 
     private void showAccessibilityDialog(final int position, final View v) {
-        TileInfo info = mTiles.get(position);
+        final TileInfo info = mTiles.get(position);
         CharSequence[] options = new CharSequence[] {
                 mContext.getString(R.string.accessibility_qs_edit_move_tile, info.state.label),
                 mContext.getString(R.string.accessibility_qs_edit_remove_tile, info.state.label),
@@ -310,7 +311,9 @@
                         if (which == 0) {
                             startAccessibleDrag(position);
                         } else {
-                            move(position, mEditIndex, v);
+                            move(position, info.isSystem ? mEditIndex : mTileDividerIndex, v);
+                            notifyItemChanged(mTileDividerIndex);
+                            notifyDataSetChanged();
                         }
                     }
                 }).setNegativeButton(android.R.string.cancel, null)
@@ -334,40 +337,12 @@
     }
 
     private boolean move(int from, int to, View v) {
-        if (to >= mEditIndex) {
-            if (from < mEditIndex) {
-                // Removing a tile.
-                // Sort tiles into system/non-system groups.
-                TileInfo tile = mTiles.get(from);
-                if (tile.isSystem) {
-                    if (to > mTileDividerIndex) {
-                        to = mTileDividerIndex;
-                    }
-                } else {
-                    if (mTileDividerIndex == mTiles.size() - 1) {
-                        notifyItemChanged(mTileDividerIndex);
-                    }
-                    if (to <= mTileDividerIndex) {
-                        to = mTileDividerIndex;
-                    }
-                }
-            } else {
-                if (to > mEditIndex) {
-                    // Don't allow tiles to be dragged around when they aren't added.
-                    to = from;
-                }
-                // Allow the case where to == mEditIndex to fall through and swap which
-                // side the tile is currently on.
-                // This lets the the cases where all tiles are on one side of the line
-                // work.
-            }
+        if (to == from) {
+            return true;
         }
         CharSequence fromLabel = mTiles.get(from).state.label;
         move(from, to, mTiles);
         updateDividerLocations();
-        if (to == from) {
-            return true;
-        }
         CharSequence announcement;
         if (to >= mEditIndex) {
             MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_QS_EDIT_REMOVE_SPEC,
@@ -427,7 +402,6 @@
     private <T> void move(int from, int to, List<T> list) {
         list.add(to, list.remove(from));
         notifyItemMoved(from, to);
-        notifyItemChanged(to);
     }
 
     public class Holder extends ViewHolder {
@@ -499,7 +473,7 @@
             for (int i = 0; i < childCount; i++) {
                 final View child = parent.getChildAt(i);
                 final ViewHolder holder = parent.getChildViewHolder(child);
-                if (holder.getAdapterPosition() < mEditIndex) {
+                if (holder.getAdapterPosition() < mEditIndex && !(child instanceof TextView)) {
                     continue;
                 }
 
@@ -530,7 +504,15 @@
         @Override
         public void onSelectedChanged(ViewHolder viewHolder, int actionState) {
             super.onSelectedChanged(viewHolder, actionState);
+            if (actionState != ItemTouchHelper.ACTION_STATE_DRAG) {
+                viewHolder = null;
+            }
+            if (viewHolder == mCurrentDrag) return;
             if (mCurrentDrag != null) {
+                int position = mCurrentDrag.getAdapterPosition();
+                TileInfo info = mTiles.get(position);
+                mCurrentDrag.mTileView.setShowAppLabel(
+                        position > mEditIndex && !info.isSystem);
                 mCurrentDrag.stopDrag();
                 mCurrentDrag = null;
             }
@@ -547,6 +529,12 @@
         }
 
         @Override
+        public boolean canDropOver(RecyclerView recyclerView, ViewHolder current,
+                ViewHolder target) {
+            return target.getAdapterPosition() <= mEditIndex + 1;
+        }
+
+        @Override
         public int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder) {
             if (viewHolder.getItemViewType() == TYPE_EDIT) {
                 return makeMovementFlags(0, 0);