Pushing folders up to DragLayer

-> Increasing max num of items in Folders to match the grid
   size of the workspace.
-> Had to push folders up to prevent clipping

Change-Id: I7aae7920cff940b8e88e9bef82162c6ab5ff11e9
diff --git a/src/com/android/launcher2/DragLayer.java b/src/com/android/launcher2/DragLayer.java
index 101ed5d..0cdfd28 100644
--- a/src/com/android/launcher2/DragLayer.java
+++ b/src/com/android/launcher2/DragLayer.java
@@ -146,7 +146,7 @@
         return mDragController.onTouchEvent(ev);
     }
 
-    private void getDescendantRectRelativeToSelf(View descendant, Rect r) {
+    public void getDescendantRectRelativeToSelf(View descendant, Rect r) {
         descendant.getHitRect(r);
 
         ViewParent viewParent = descendant.getParent();
diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java
index 80428fa..d682a6b 100644
--- a/src/com/android/launcher2/Folder.java
+++ b/src/com/android/launcher2/Folder.java
@@ -100,6 +100,7 @@
     private TextView mFolderName;
     private int mFolderNameHeight;
     private Rect mHitRect = new Rect();
+    private Rect mTempRect = new Rect();
 
     private boolean mIsEditingName = false;
     private InputMethodManager mInputMethodManager;
@@ -118,8 +119,8 @@
         setAlwaysDrawnWithCacheEnabled(false);
         mInflater = LayoutInflater.from(context);
         mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
-        mMaxCountX = LauncherModel.getCellCountX() - 1;
-        mMaxCountY = LauncherModel.getCellCountY() - 1;
+        mMaxCountX = LauncherModel.getCellCountX();
+        mMaxCountY = LauncherModel.getCellCountY();
 
         mInputMethodManager = (InputMethodManager)
                 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
@@ -319,9 +320,8 @@
     }
 
     void onClose() {
-        CellLayoutChildren clc = (CellLayoutChildren) getParent();
-        final CellLayout cellLayout = (CellLayout) clc.getParent();
-        cellLayout.removeViewWithoutMarkingCells(Folder.this);
+        DragLayer parent = (DragLayer) getParent();
+        parent.removeView(Folder.this);
         clearFocus();
     }
 
@@ -373,8 +373,8 @@
     private void positionAndSizeAsIcon() {
         if (!(getParent() instanceof CellLayoutChildren)) return;
 
-        CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams();
-        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
+        DragLayer.LayoutParams iconLp = (DragLayer.LayoutParams) mFolderIcon.getLayoutParams();
+        DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
 
         if (mMode == PARTIAL_GROW) {
             setScaleX(0.8f);
@@ -394,10 +394,10 @@
         if (mState != STATE_SMALL) {
             positionAndSizeAsIcon();
         }
-        if (!(getParent() instanceof CellLayoutChildren)) return;
+        if (!(getParent() instanceof DragLayer)) return;
 
         ObjectAnimator oa;
-        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
+        DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
 
         centerAboutIcon();
         if (mMode == PARTIAL_GROW) {
@@ -439,7 +439,7 @@
     }
 
     public void animateClosed() {
-        if (!(getParent() instanceof CellLayoutChildren)) return;
+        if (!(getParent() instanceof DragLayer)) return;
 
         ObjectAnimator oa;
         if (mMode == PARTIAL_GROW) {
@@ -448,8 +448,8 @@
             PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
             oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
         } else {
-            CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams();
-            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
+            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);
@@ -720,25 +720,26 @@
     }
 
     private void centerAboutIcon() {
-        CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams();
-        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
+        DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
 
         int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
         // Technically there is no padding at the bottom, but we add space equal to the padding
         // and have to account for that here.
         int height = getPaddingTop() + mContent.getDesiredHeight() + mFolderNameHeight;
+        DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
 
-        int centerX = iconLp.x + iconLp.width / 2;
-        int centerY = iconLp.y + iconLp.height / 2;
+        parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
+
+        int centerX = mTempRect.centerX();
+        int centerY = mTempRect.centerY();
         int centeredLeft = centerX - width / 2;
         int centeredTop = centerY - height / 2;
 
-        CellLayoutChildren clc = (CellLayoutChildren) getParent();
         int parentWidth = 0;
         int parentHeight = 0;
-        if (clc != null) {
-            parentWidth = clc.getMeasuredWidth();
-            parentHeight = clc.getMeasuredHeight();
+        if (parent != null) {
+            parentWidth = parent.getMeasuredWidth();
+            parentHeight = parent.getMeasuredHeight();
         }
 
         int left = Math.min(Math.max(0, centeredLeft), parentWidth - width);
@@ -768,10 +769,10 @@
     private void setupContentForNumItems(int count) {
         setupContentDimension(count);
 
-        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
+        DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
         if (lp == null) {
-            lp = new CellLayout.LayoutParams(0, 0, -1, -1);
-            lp.isLockedToGrid = false;
+            lp = new DragLayer.LayoutParams(0, 0);
+            lp.customPosition = true;
             setLayoutParams(lp);
         }
         centerAboutIcon();
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 3f4a47c..b52a4f7 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -1297,14 +1297,10 @@
     protected void onSaveInstanceState(Bundle outState) {
         outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
 
-        final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
-        if (folders.size() > 0) {
-            final int count = folders.size();
-            long[] ids = new long[count];
-            for (int i = 0; i < count; i++) {
-                final FolderInfo info = folders.get(i).getInfo();
-                ids[i] = info.id;
-            }
+        final Folder folder = mWorkspace.getOpenFolder();
+        if (folder != null) {
+            long[] ids = new long[1];
+            ids[0] = folder.getInfo().id;
             outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
         } else {
             super.onSaveInstanceState(outState);
@@ -1712,7 +1708,7 @@
 
         ViewGroup parent = (ViewGroup) folder.getParent().getParent();
         if (parent != null) {
-            CellLayout cl = (CellLayout) parent;
+            CellLayout cl = (CellLayout) mWorkspace.getChildAt(folder.mInfo.screen);
             FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
             shrinkAndFadeInFolderIcon(fi);
             mDragController.removeDropTarget((DropTarget)folder);
@@ -1945,7 +1941,9 @@
         growAndFadeOutFolderIcon(folderIcon);
         info.opened = true;
 
-        mWorkspace.addInFullScreen(folder, info.screen);
+        mDragLayer.addView(folder);
+        mDragController.addDropTarget((DropTarget) folder);
+
         folder.animateOpen();
         folder.onOpen();
     }
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index a4e162f..4eea5c3 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -415,10 +415,10 @@
      * @return The open folder on the current screen, or null if there is none
      */
     Folder getOpenFolder() {
-        ViewGroup currentPage = ((CellLayout) getChildAt(mCurrentPage)).getChildrenLayout();
-        int count = currentPage.getChildCount();
+        DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
+        int count = dragLayer.getChildCount();
         for (int i = 0; i < count; i++) {
-            View child = currentPage.getChildAt(i);
+            View child = dragLayer.getChildAt(i);
             if (child instanceof Folder) {
                 Folder folder = (Folder) child;
                 if (folder.getInfo().opened)
@@ -428,26 +428,6 @@
         return null;
     }
 
-    ArrayList<Folder> getOpenFolders() {
-        final int screenCount = getChildCount();
-        ArrayList<Folder> folders = new ArrayList<Folder>(screenCount);
-
-        for (int screen = 0; screen < screenCount; screen++) {
-            ViewGroup currentPage = ((CellLayout) getChildAt(screen)).getChildrenLayout();
-            int count = currentPage.getChildCount();
-            for (int i = 0; i < count; i++) {
-                View child = currentPage.getChildAt(i);
-                if (child instanceof Folder) {
-                    Folder folder = (Folder) child;
-                    if (folder.getInfo().opened)
-                        folders.add(folder);
-                    break;
-                }
-            }
-        }
-        return folders;
-    }
-
     boolean isTouchActive() {
         return mTouchState != TOUCH_STATE_REST;
     }