Fixing folder order persistence (issue 6176721)

Change-Id: I5b3e4b7baa4222a883bcb061429113962ea69883
diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java
index 0389264..8632370 100644
--- a/src/com/android/launcher2/Folder.java
+++ b/src/com/android/launcher2/Folder.java
@@ -29,6 +29,7 @@
 import android.text.Selection;
 import android.text.Spannable;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.ActionMode;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
@@ -47,6 +48,8 @@
 import com.android.launcher2.FolderInfo.FolderListener;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 
 /**
  * Represents a set of icons chosen by the user or generated by the system.
@@ -305,11 +308,48 @@
         return mInfo;
     }
 
+    private class GridComparator implements Comparator<ShortcutInfo> {
+        int mNumCols;
+        public GridComparator(int numCols) {
+            mNumCols = numCols;
+        }
+
+        @Override
+        public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
+            int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
+            int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
+            return (lhIndex - rhIndex);
+        }
+
+    }
+
+    private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
+        int maxX = 0;
+        int count = items.size();
+        for (int i = 0; i < count; i++) {
+            ShortcutInfo item = items.get(i);
+            if (item.cellX > maxX) {
+                maxX = item.cellX;
+            }
+        }
+        GridComparator gridComparator = new GridComparator(maxX);
+        Collections.sort(items, gridComparator);
+        final int countX = mContent.getCountX();
+        for (int i = 0; i < count; i++) {
+            int x = i % countX;
+            int y = i / countX;
+            ShortcutInfo item = items.get(i);
+            item.cellX = x;
+            item.cellY = y;
+        }
+    }
+
     void bind(FolderInfo info) {
         mInfo = info;
         ArrayList<ShortcutInfo> children = info.contents;
         ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
         setupContentForNumItems(children.size());
+        placeInReadingOrder(children);
         int count = 0;
         for (int i = 0; i < children.size(); i++) {
             ShortcutInfo child = (ShortcutInfo) children.get(i);
@@ -478,11 +518,11 @@
         textView.setOnLongClickListener(this);
 
         // We need to check here to verify that the given item's location isn't already occupied
-        // by another item. If it is, we need to find the next available slot and assign
-        // it that position. This is an issue when upgrading from the old Folders implementation
-        // which could contain an unlimited number of items.
+        // by another item.
         if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
                 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
+            // This shouldn't happen, log it. 
+            Log.e(TAG, "Folder order not properly persisted during bind");
             if (!findAndSetEmptyCells(item)) {
                 return false;
             }
@@ -777,6 +817,7 @@
                 (1.0f * folderPivotX / width));
         int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
                 (1.0f * folderPivotY / height));
+
         mFolderIcon.setPivotX(folderIconPivotX);
         mFolderIcon.setPivotY(folderIconPivotY);