Added transitions when dropping items onto Folders, fixed bugs

-> Changed the ordering of the FolderIcon preview to show first items first
-> Folder now closes immediately upon drop of an item from folder to workspace

Change-Id: I66361b604dc5ef81da2413b7dda53d0c4691377e
diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java
index 37368b9..13815ad 100644
--- a/src/com/android/launcher2/Folder.java
+++ b/src/com/android/launcher2/Folder.java
@@ -86,6 +86,7 @@
     private int mMaxCountX;
     private int mMaxCountY;
     private Rect mNewSize = new Rect();
+    private Rect mIconRect = new Rect();
     private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
     private Drawable mIconDrawable;
     boolean mItemsInvalidated = false;
@@ -134,6 +135,7 @@
         if (sHintText == null) {
             sHintText = res.getString(R.string.folder_hint_text);
         }
+        setLayerType(LAYER_TYPE_HARDWARE, null);
     }
 
     @Override
@@ -141,7 +143,6 @@
         super.onFinishInflate();
         mContent = (CellLayout) findViewById(R.id.folder_content);
         mContent.setGridSize(0, 0);
-        mContent.enableHardwareLayers();
         mFolderName = (TextView) findViewById(R.id.folder_name);
 
         // We find out how tall the text view wants to be (it is set to wrap_content), so that
@@ -372,9 +373,8 @@
      * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
      */
     private void positionAndSizeAsIcon() {
-        if (!(getParent() instanceof CellLayoutChildren)) return;
+        if (!(getParent() instanceof DragLayer)) return;
 
-        DragLayer.LayoutParams iconLp = (DragLayer.LayoutParams) mFolderIcon.getLayoutParams();
         DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
 
         if (mMode == PARTIAL_GROW) {
@@ -382,19 +382,19 @@
             setScaleY(0.8f);
             setAlpha(0f);
         } else {
-            lp.width = iconLp.width;
-            lp.height = iconLp.height;
-            lp.x = iconLp.x;
-            lp.y = iconLp.y;
+            mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
+            lp.width = mIconRect.width();
+            lp.height = mIconRect.height();
+            lp.x = mIconRect.left;
+            lp.y = mIconRect.top;
             mContent.setAlpha(0);
         }
         mState = STATE_SMALL;
     }
 
     public void animateOpen() {
-        if (mState != STATE_SMALL) {
-            positionAndSizeAsIcon();
-        }
+        positionAndSizeAsIcon();
+
         if (!(getParent() instanceof DragLayer)) return;
 
         ObjectAnimator oa;
@@ -449,13 +449,12 @@
             PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
             oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
         } else {
-            DragLayer.LayoutParams iconLp = (DragLayer.LayoutParams) mFolderIcon.getLayoutParams();
             DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
 
-            PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", iconLp.width);
-            PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", iconLp.height);
-            PropertyValuesHolder x = PropertyValuesHolder.ofInt("x",iconLp.x);
-            PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", iconLp.y);
+            PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
+            PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
+            PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
+            PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
             oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
             oa.addUpdateListener(new AnimatorUpdateListener() {
                 public void onAnimationUpdate(ValueAnimator animation) {
@@ -645,14 +644,18 @@
 
     OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
         public void onAlarm(Alarm alarm) {
-            mLauncher.closeFolder();
-            mCurrentDragInfo = null;
-            mCurrentDragView = null;
-            mSuppressOnAdd = false;
-            mRearrangeOnClose = true;
+            completeDragExit();
         }
     };
 
+    private void completeDragExit() {
+        mLauncher.closeFolder();
+        mCurrentDragInfo = null;
+        mCurrentDragView = null;
+        mSuppressOnAdd = false;
+        mRearrangeOnClose = true;
+    }
+
     public void onDragExit(DragObject d) {
         // We only close the folder if this is a true drag exit, ie. not because a drop
         // has occurred above the folder.
@@ -674,6 +677,11 @@
                 }
             }
             // TODO: if the drag fails, we need to re-add the item
+        } else {
+            if (target != this) {
+                mOnExitAlarm.cancelAlarm();
+                completeDragExit();
+            }
         }
     }