Merge "Updating focus helper to account for change in Folder layout hierarchy. (Bug 9626867)" into jb-ub-gel-agar
diff --git a/src/com/android/launcher3/CellLayout.java b/src/com/android/launcher3/CellLayout.java
index ac41a2b..86bc1b0 100644
--- a/src/com/android/launcher3/CellLayout.java
+++ b/src/com/android/launcher3/CellLayout.java
@@ -3231,6 +3231,11 @@
         public boolean isLockedToGrid = true;
 
         /**
+         * Indicates that this item should use the full extents of its parent.
+         */
+        public boolean isFullscreen = false;
+
+        /**
          * Indicates whether this item can be reordered. Always true except in the case of the
          * the AllApps button.
          */
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 3592045..321c4e7 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -990,7 +990,7 @@
 
         int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
         if (currentScreen > -1) {
-            mWorkspace.setCurrentPage(currentScreen);
+            mWorkspace.setRestorePage(currentScreen);
         }
 
         final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index 28530e6..eaa2fd2 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -1728,7 +1728,9 @@
                         }
                     }
                 } finally {
-                    c.close();
+                    if (c != null) {
+                        c.close();
+                    }
                 }
 
                 if (itemsToRemove.size() > 0) {
diff --git a/src/com/android/launcher3/PagedView.java b/src/com/android/launcher3/PagedView.java
index aaff588..9a851ac 100644
--- a/src/com/android/launcher3/PagedView.java
+++ b/src/com/android/launcher3/PagedView.java
@@ -104,6 +104,7 @@
     protected boolean mFirstLayout = true;
 
     protected int mCurrentPage;
+    protected int mRestorePage = -1;
     protected int mChildCountOnLastLayout;
 
     protected int mNextPage = INVALID_PAGE;
@@ -506,6 +507,14 @@
         invalidate();
     }
 
+    /**
+     * The restore page will be set in place of the current page at the next (likely first)
+     * layout.
+     */
+    void setRestorePage(int restorePage) {
+        mRestorePage = restorePage;
+    }
+
     protected void notifyPageSwitchListener() {
         if (mPageSwitchListener != null) {
             mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
@@ -824,6 +833,7 @@
         }
 
         for (int i = startIndex; i != endIndex; i += delta) {
+
             final View child = getPageAt(i);
             LayoutParams lp = (LayoutParams) child.getLayoutParams();
             int childTop;
@@ -847,10 +857,13 @@
 
                 // We assume the left and right padding are equal, and hence center the pages
                 // horizontally
-                int scrollOffset = false ? 0 : (getViewportWidth() - childWidth) / 2;
+                int scrollOffset = (getViewportWidth() - childWidth) / 2;
                 mPageScrolls[i] = childLeft - scrollOffset - offsetX;
 
-                childLeft += childWidth + mPageSpacing;
+                if (i != endIndex - delta) {
+                    int nextScrollOffset = (getViewportWidth() - getChildWidth(i + delta)) / 2;
+                    childLeft += childWidth + nextScrollOffset;
+                }
             }
         }
 
@@ -870,7 +883,12 @@
 
         if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
                 !mDeferringForDelete) {
-            setCurrentPage(getNextPage());
+            if (mRestorePage > -1) {
+                setCurrentPage(mRestorePage);
+                mRestorePage = -1;
+            } else {
+                setCurrentPage(getNextPage());
+            }
         }
         mChildCountOnLastLayout = getChildCount();
     }
diff --git a/src/com/android/launcher3/ShortcutAndWidgetContainer.java b/src/com/android/launcher3/ShortcutAndWidgetContainer.java
index 18b9399..64a87ef 100644
--- a/src/com/android/launcher3/ShortcutAndWidgetContainer.java
+++ b/src/com/android/launcher3/ShortcutAndWidgetContainer.java
@@ -92,13 +92,15 @@
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         int count = getChildCount();
+
+        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
+        int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
+        setMeasuredDimension(widthSpecSize, heightSpecSize);
+
         for (int i = 0; i < count; i++) {
             View child = getChildAt(i);
             measureChild(child);
         }
-        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
-        int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
-        setMeasuredDimension(widthSpecSize, heightSpecSize);
     }
 
     public void setupLp(CellLayout.LayoutParams lp) {
@@ -115,8 +117,15 @@
         final int cellWidth = mCellWidth;
         final int cellHeight = mCellHeight;
         CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
-
-        lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, invertLayoutHorizontally(), mCountX);
+        if (!lp.isFullscreen) {
+            lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, invertLayoutHorizontally(),
+                    mCountX);
+        } else {
+            lp.x = 0;
+            lp.y = 0;
+            lp.width = getMeasuredWidth();
+            lp.height = getMeasuredHeight();
+        }
         int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
         int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
                 MeasureSpec.EXACTLY);
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index edf3721..91f5396 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -537,6 +537,7 @@
 
         CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, spanX, spanY);
         lp.canReorder  = false;
+        lp.isFullscreen = true;
 
         customScreen.addViewToCellLayout(customContent, 0, 0, lp, true);
 
@@ -929,25 +930,25 @@
             stripEmptyScreens();
             mStripScreensOnPageStopMoving = false;
         }
-    }
 
-    @Override
-    protected void notifyPageSwitchListener() {
-        super.notifyPageSwitchListener();
-        Launcher.setScreen(mCurrentPage);
-
-        if (hasCustomContent() && mCurrentPage == 0) {
+        if (hasCustomContent() && getNextPage() == 0 && !mCustomContentShowing) {
             mCustomContentShowing = true;
             if (mCustomContentCallbacks != null) {
                 mCustomContentCallbacks.onShow();
             }
-        } else if (hasCustomContent() && mCustomContentShowing) {
+        } else if (hasCustomContent() && getNextPage() != 0 && mCustomContentShowing) {
             mCustomContentShowing = false;
             if (mCustomContentCallbacks != null) {
                 mCustomContentCallbacks.onHide();
                 mLauncher.resetQSBScroll();
             }
         }
+    }
+
+    @Override
+    protected void notifyPageSwitchListener() {
+        super.notifyPageSwitchListener();
+        Launcher.setScreen(mCurrentPage);
     };
 
     // As a ratio of screen height, the total distance we want the parallax effect to span
@@ -1302,11 +1303,9 @@
 
     private void updateStateForCustomContent(int screenCenter) {
         if (hasCustomContent()) {
-            CellLayout customContent = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
             int index = mScreenOrder.indexOf(CUSTOM_CONTENT_SCREEN_ID);
 
             int scrollDelta = getScrollForPage(index + 1) - getScrollX();
-            float translationX = Math.max(scrollDelta, 0);
 
             float progress = (1.0f * scrollDelta) /
                     (getScrollForPage(index + 1) - getScrollForPage(index));
@@ -1317,6 +1316,7 @@
 
             if (mLauncher.getHotseat() != null) {
                 mLauncher.getHotseat().setTranslationY(transY);
+                mLauncher.getHotseat().setAlpha(1 - progress);
             }
             if (getPageIndicator() != null) {
                 getPageIndicator().setAlpha(1 - progress);