Some launcher bugs:

-> Allowing folders to be specified in xml (issue 5032559)
-> Mostly fixing issue where you can lose items when dragging
   out of a folder to a full screen (issue 5052078)

Change-Id: I3228dd568b2b8919c14594ee518f86173430481e
diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java
index 428d7b3..83f580c 100644
--- a/src/com/android/launcher2/Folder.java
+++ b/src/com/android/launcher2/Folder.java
@@ -98,6 +98,10 @@
     private Rect mTempRect = new Rect();
     private boolean mFirstOpen = true;
 
+    // Internal variable to track whether the folder was destroyed due to only a single
+    // item remaining
+    private boolean mDestroyed = false;
+
     private boolean mIsEditingName = false;
     private InputMethodManager mInputMethodManager;
 
@@ -511,7 +515,8 @@
         // 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.
-        if (mContent.getChildAt(item.cellX, item.cellY) != null) {
+        if (mContent.getChildAt(item.cellX, item.cellY) != null ||
+                item.cellX < 0 || item.cellY < 0) {
             if (!findAndSetEmptyCells(item)) {
                 return false;
             }
@@ -655,17 +660,16 @@
         mCurrentDragInfo = null;
         mCurrentDragView = null;
         mSuppressOnAdd = false;
+        if (target != this) {
+            mOnExitAlarm.cancelAlarm();
+            completeDragExit();
+        }
+
         if (!success) {
-            if (d.dragView != null) {
-                if (target instanceof CellLayout) {
-                    // TODO: we should animate the item back to the folder in this case
-                }
-            }
-            // TODO: if the drag fails, we need to re-add the item
-        } else {
-            if (target != this) {
-                mOnExitAlarm.cancelAlarm();
-                completeDragExit();
+            if (!mDestroyed) {
+                mFolderIcon.onDrop(d);
+            } else {
+                // TODO: if the folder was removed, recreate it
             }
         }
     }
@@ -839,6 +843,7 @@
     private void replaceFolderWithFinalItem() {
         ItemInfo finalItem = null;
 
+        mDestroyed = true;
         if (getItemCount() == 1) {
             finalItem = mInfo.contents.get(0);
         }