Folder auto-destroys itself when there is only one item left.

Change-Id: I4ea41e0730e3dc474a518eba0a04c11167b5c1df
diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java
index 029b89f..1324a08 100644
--- a/src/com/android/launcher2/Folder.java
+++ b/src/com/android/launcher2/Folder.java
@@ -329,7 +329,7 @@
         mItemsInvalidated = true;
         mInfo.addListener(this);
 
-        if (sDefaultFolderName != mInfo.title) {
+        if (!sDefaultFolderName.contentEquals(mInfo.title)) {
             mFolderName.setText(mInfo.title);
         } else {
             mFolderName.setText("");
@@ -784,6 +784,42 @@
             setupContentForNumItems(getItemCount());
             mRearrangeOnClose = false;
         }
+        if (getItemCount() <= 1) {
+            replaceFolderWithFinalItem();
+        }
+    }
+
+    private void replaceFolderWithFinalItem() {
+        ItemInfo finalItem = null;
+
+        if (getItemCount() == 1) {
+            finalItem = mInfo.contents.get(0);
+        }
+
+        // Remove the folder completely
+        final CellLayout cellLayout = (CellLayout)
+                mLauncher.getWorkspace().getChildAt(mInfo.screen);
+        cellLayout.removeView(mFolderIcon);
+        if (mFolderIcon instanceof DropTarget) {
+            mDragController.removeDropTarget((DropTarget) mFolderIcon);
+        }
+        mLauncher.removeFolder(mInfo);
+
+        if (finalItem != null) {
+            LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem,
+                    LauncherSettings.Favorites.CONTAINER_DESKTOP, mInfo.screen,
+                    mInfo.cellX, mInfo.cellY);
+        }
+        LauncherModel.deleteFolderContentsFromDatabase(mLauncher, mInfo, true);
+
+        // Add the last remaining child to the workspace in place of the folder
+        if (finalItem != null) {
+            View child = mLauncher.createShortcut(R.layout.application, cellLayout,
+                    (ShortcutInfo) finalItem);
+
+            mLauncher.getWorkspace().addInScreen(child, mInfo.screen, mInfo.cellX, mInfo.cellY, 
+                    mInfo.spanX, mInfo.spanY);
+        }
     }
 
     public void onDrop(DragObject d) {
@@ -833,6 +869,9 @@
         } else {
             setupContentForNumItems(getItemCount());
         }
+        if (getItemCount() <= 1) {
+            replaceFolderWithFinalItem();
+        }
     }
 
     public void onItemsChanged() {