Fixing folder order persistence, issue 5422712

Change-Id: I72d99274b581a803f01042e1de979c59faa4684b
diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java
index 8a198f8..019ac70 100644
--- a/src/com/android/launcher2/Folder.java
+++ b/src/com/android/launcher2/Folder.java
@@ -678,6 +678,20 @@
         mCurrentDragInfo = null;
         mCurrentDragView = null;
         mSuppressOnAdd = false;
+
+        // Reordering may have occured, and we need to save the new item locations. We do this once
+        // at the end to prevent unnecessary database operations.
+        updateItemLocationsInDatabase();
+    }
+
+    private void updateItemLocationsInDatabase() {
+        ArrayList<View> list = getItemsInReadingOrder();
+        for (int i = 0; i < list.size(); i++) {
+            View v = list.get(i);
+            ItemInfo info = (ItemInfo) v.getTag();
+            LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
+                        info.cellX, info.cellY);
+        }
     }
 
     public void notifyDrop() {
@@ -694,7 +708,7 @@
         return null;
     }
 
-    private void setupContentDimension(int count) {
+    private void setupContentDimensions(int count) {
         ArrayList<View> list = getItemsInReadingOrder();
 
         int countX = mContent.getCountX();
@@ -783,7 +797,7 @@
     }
 
     private void setupContentForNumItems(int count) {
-        setupContentDimension(count);
+        setupContentDimensions(count);
 
         DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
         if (lp == null) {
@@ -933,7 +947,7 @@
                 mCurrentDragView.setVisibility(VISIBLE);
             }
             mItemsInvalidated = true;
-            setupContentDimension(getItemCount());
+            setupContentDimensions(getItemCount());
             mSuppressOnAdd = true;
         }
         mInfo.add(item);