Merge "Refactoring some logic in FolderPagedView" into ub-launcher3-burnaby
diff --git a/src/com/android/launcher3/FolderPagedView.java b/src/com/android/launcher3/FolderPagedView.java
index 60c94e0..c9e825a 100644
--- a/src/com/android/launcher3/FolderPagedView.java
+++ b/src/com/android/launcher3/FolderPagedView.java
@@ -16,6 +16,7 @@
 
 package com.android.launcher3;
 
+import android.annotation.SuppressLint;
 import android.content.Context;
 import android.util.AttributeSet;
 import android.util.Log;
@@ -44,11 +45,9 @@
     private final IconCache mIconCache;
     private final HashMap<View, Runnable> mPageChangingViews = new HashMap<>();
 
-    private final CellLayout mFirstPage;
-
-    final int mMaxCountX;
-    final int mMaxCountY;
-    final int mMaxItemsPerPage;
+    private final int mMaxCountX;
+    private final int mMaxCountY;
+    private final int mMaxItemsPerPage;
 
     private int mAllocatedContentSize;
     private int mGridCountX;
@@ -61,10 +60,6 @@
     public FolderPagedView(Context context, AttributeSet attrs) {
         super(context, attrs);
         LauncherAppState app = LauncherAppState.getInstance();
-
-        mFirstPage = newCellLayout();
-        addFullScreenPage(mFirstPage);
-        setCurrentPage(0);
         setDataIsReady();
 
         DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
@@ -96,8 +91,6 @@
             mGridCountY = mMaxCountY;
             done = true;
         } else {
-            mGridCountX = mFirstPage.getCountX();
-            mGridCountY = mFirstPage.getCountY();
             done = false;
         }
 
@@ -120,50 +113,19 @@
             done = mGridCountX == oldCountX && mGridCountY == oldCountY;
         }
 
-        setGridSize(mGridCountX, mGridCountY);
-    }
-
-    public void setGridSize(int countX, int countY) {
-        mGridCountX = countX;
-        mGridCountY = countY;
-        mFirstPage.setGridSize(mGridCountX, mGridCountY);
-        for (int i = getPageCount() - 1; i > 0; i--) {
+        // Update grid size
+        for (int i = getPageCount() - 1; i >= 0; i--) {
             getPageAt(i).setGridSize(mGridCountX, mGridCountY);
         }
     }
 
     @Override
     public ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> items) {
-        final int count = items.size();
-
-        if (getPageCount() > 1) {
-            Log.d(TAG, "Binding items to an non-empty view");
-            removeAllViews();
-            addView(mFirstPage);
-            mFirstPage.removeAllViews();
-        }
-
-        setupContentDimensions(count);
-        CellLayout page = mFirstPage;
-        int pagePosition = 0;
-        int rank = 0;
-
+        ArrayList<View> icons = new ArrayList<View>();
         for (ShortcutInfo item : items) {
-            if (pagePosition >= mMaxItemsPerPage) {
-                // This page is full, add a new page.
-                pagePosition = 0;
-                page = newCellLayout();
-                addFullScreenPage(page);
-            }
-
-            item.cellX = pagePosition % mGridCountX;
-            item.cellY = pagePosition / mGridCountX;
-            item.rank = rank;
-            addNewView(item, page);
-
-            rank++;
-            pagePosition++;
+            icons.add(createNewView(item));
         }
+        arrangeChildren(icons, icons.size(), false);
         return new ArrayList<ShortcutInfo>();
     }
 
@@ -185,7 +147,7 @@
         // Add a new page if last page is full
         if (getPageAt(getChildCount() - 1).getShortcutsAndWidgets().getChildCount()
                 >= mMaxItemsPerPage) {
-            addFullScreenPage(newCellLayout());
+            createAndAddNewPage();
         }
         setCurrentPage(getChildCount() - 1);
         return rank;
@@ -193,14 +155,20 @@
 
     @Override
     public View createAndAddViewForRank(ShortcutInfo item, int rank) {
-        int pageNo = updateItemXY(item, rank);
-        CellLayout page = getPageAt(pageNo);
-        return addNewView(item, page);
+        View icon = createNewView(item);
+        addViewForRank(createNewView(item), item, rank);
+        return icon;
     }
 
     @Override
     public void addViewForRank(View view, ShortcutInfo item, int rank) {
-        int pageNo = updateItemXY(item, rank);
+        int pagePos = rank % mMaxItemsPerPage;
+        int pageNo = rank / mMaxItemsPerPage;
+
+        item.rank = rank;
+        item.cellX = pagePos % mGridCountX;
+        item.cellY = pagePos / mGridCountX;
+
         CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
         lp.cellX = item.cellX;
         lp.cellY = item.cellY;
@@ -208,32 +176,18 @@
                 view, -1, mFolder.mLauncher.getViewIdForItem(item), lp, true);
     }
 
-    /**
-     * Updates the item cellX and cellY position and return the page number for that item.
-     */
-    private int updateItemXY(ShortcutInfo item, int rank) {
-        item.rank = rank;
-
-        int pagePos = item.rank % mMaxItemsPerPage;
-        item.cellX = pagePos % mGridCountX;
-        item.cellY = pagePos / mGridCountX;
-
-        return item.rank / mMaxItemsPerPage;
-    }
-
-    private View addNewView(ShortcutInfo item, CellLayout target) {
+    @SuppressLint("InflateParams")
+    private View createNewView(ShortcutInfo item) {
         final BubbleTextView textView = (BubbleTextView) mInflater.inflate(
-                R.layout.folder_application, target.getShortcutsAndWidgets(), false);
+                R.layout.folder_application, null, false);
         textView.applyFromShortcutInfo(item, mIconCache, false);
         textView.setOnClickListener(mFolder);
         textView.setOnLongClickListener(mFolder);
         textView.setOnFocusChangeListener(mFocusIndicatorView);
         textView.setOnKeyListener(mKeyListener);
 
-        CellLayout.LayoutParams lp = new CellLayout.LayoutParams(
-                item.cellX, item.cellY, item.spanX, item.spanY);
-        target.addViewToCellLayout(
-                textView, -1, mFolder.mLauncher.getViewIdForItem(item), lp, true);
+        textView.setLayoutParams(new CellLayout.LayoutParams(
+                item.cellX, item.cellY, item.spanX, item.spanY));
         return textView;
     }
 
@@ -252,26 +206,18 @@
         return getPageAt(getNextPage());
     }
 
-    @Override
-    public void addFullScreenPage(View page) {
+    private CellLayout createAndAddNewPage() {
+        DeviceProfile grid = LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile();
+        CellLayout page = new CellLayout(getContext());
+        page.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
+        page.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
+        page.setInvertIfRtl(true);
+        page.setGridSize(mGridCountX, mGridCountY);
+
         LayoutParams lp = generateDefaultLayoutParams();
         lp.isFullScreenPage = true;
-        super.addView(page, -1, lp);
-    }
-
-    private CellLayout newCellLayout() {
-        DeviceProfile grid = LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile();
-
-        CellLayout layout = new CellLayout(getContext());
-        layout.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
-        layout.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
-        layout.setInvertIfRtl(true);
-
-        if (mFirstPage != null) {
-            layout.setGridSize(mFirstPage.getCountX(), mFirstPage.getCountY());
-        }
-
-        return layout;
+        addView(page, -1, lp);
+        return page;
     }
 
     @Override
@@ -300,6 +246,10 @@
      */
     @Override
     public void arrangeChildren(ArrayList<View> list, int itemCount) {
+        arrangeChildren(list, itemCount, true);
+    }
+
+    private void arrangeChildren(ArrayList<View> list, int itemCount, boolean saveChanges) {
         ArrayList<CellLayout> pages = new ArrayList<CellLayout>();
         for (int i = 0; i < getChildCount(); i++) {
             CellLayout page = (CellLayout) getChildAt(i);
@@ -321,8 +271,7 @@
                 if (pageItr.hasNext()) {
                     currentPage = pageItr.next();
                 } else {
-                    currentPage = newCellLayout();
-                    addFullScreenPage(currentPage);
+                    currentPage = createAndAddNewPage();
                 }
                 position = 0;
             }
@@ -335,8 +284,10 @@
                 info.cellX = newX;
                 info.cellY = newY;
                 info.rank = rank;
-                LauncherModel.addOrMoveItemInDatabase(getContext(), info,
-                        mFolder.mInfo.id, 0, info.cellX, info.cellY);
+                if (saveChanges) {
+                    LauncherModel.addOrMoveItemInDatabase(getContext(), info,
+                            mFolder.mInfo.id, 0, info.cellX, info.cellY);
+                }
             }
             lp.cellX = info.cellX;
             lp.cellY = info.cellY;
@@ -346,13 +297,11 @@
                     v, -1, mFolder.mLauncher.getViewIdForItem(info), lp, true);
         }
 
+        // Remove extra views.
         boolean removed = false;
         while (pageItr.hasNext()) {
-            CellLayout layout = pageItr.next();
-            if (layout != mFirstPage) {
-                removeView(layout);
-                removed = true;
-            }
+            removeView(pageItr.next());
+            removed = true;
         }
         if (removed) {
             setCurrentPage(0);
@@ -369,11 +318,11 @@
     public void syncPageItems(int page, boolean immediate) { }
 
     public int getDesiredWidth() {
-        return mFirstPage.getDesiredWidth();
+        return getPageCount() > 0 ? getPageAt(0).getDesiredWidth() : 0;
     }
 
     public int getDesiredHeight()  {
-        return mFirstPage.getDesiredHeight();
+        return  getPageCount() > 0 ? getPageAt(0).getDesiredHeight() : 0;
     }
 
     @Override