Make sure all overlay panels are visible as user free scrolls.

Previously there was a workaround to ensure that adjacent panels were visible
while in the overview or spring-loaded states, but it incorrectly kept only
those original pages visible even while the user scrolled to other pages. So now
we only use the workaround when first entering the overview or spring-loaded
states, and then fall back to the default getVisiblePages() implementation in
PageView when in free scoll mode.

Bug: 23766408
Change-Id: I692ec00b9cd6d7889c374aee41b85abd0a5d8d3c
diff --git a/src/com/android/launcher3/PagedView.java b/src/com/android/launcher3/PagedView.java
index 5d2e1cc..5f54e1d 100644
--- a/src/com/android/launcher3/PagedView.java
+++ b/src/com/android/launcher3/PagedView.java
@@ -372,7 +372,10 @@
     }
 
     /**
-     * Returns the index of the currently displayed page.
+     * Returns the index of the currently displayed page. When in free scroll mode, this is the page
+     * that the user was on before entering free scroll mode (e.g. the home screen page they
+     * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
+     * to get the page the user is currently scrolling over.
      */
     public int getCurrentPage() {
         return mCurrentPage;
@@ -1039,11 +1042,10 @@
 
         if (pageCount > 0) {
             int viewportWidth = getViewportWidth();
-            int curScreen = 0;
+            int lastVisiblePageIndex = 0;
 
-            int count = getChildCount();
-            for (int i = 0; i < count; i++) {
-                View currPage = getPageAt(i);
+            for (int currPageIndex = 0; currPageIndex < pageCount; currPageIndex++) {
+                View currPage = getPageAt(currPageIndex);
 
                 sTmpIntPoint[0] = 0;
                 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
@@ -1064,13 +1066,13 @@
                         break;
                     }
                 }
-                curScreen = i;
                 if (range[0] < 0) {
-                    range[0] = curScreen;
+                    range[0] = currPageIndex;
                 }
+                lastVisiblePageIndex = currPageIndex;
             }
 
-            range[1] = curScreen;
+            range[1] = lastVisiblePageIndex;
         } else {
             range[0] = -1;
             range[1] = -1;
diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java
index b316016..4e93684 100644
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -726,4 +726,13 @@
             return true;
         }
     }
+
+    /**
+     * Ensures that a value is within given bounds. Specifically:
+     * If value is less than lowerBound, return lowerBound; else if value is greater than upperBound,
+     * return upperBound; else return value unchanged.
+     */
+    public static int boundInRange(int value, int lowerBound, int upperBound) {
+        return Math.max(lowerBound, Math.min(value, upperBound));
+    }
 }
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index f2837a0..5c8c402 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -271,6 +271,7 @@
     boolean mStartedSendingScrollEvents;
     boolean mShouldSendPageSettled;
     int mLastOverlaySroll = 0;
+    private boolean mForceDrawAdjacentPages = false;
 
     // Handles workspace state transitions
     private WorkspaceStateTransitionAnimation mStateTransitionAnimation;
@@ -1854,10 +1855,10 @@
     @Override
     protected void getVisiblePages(int[] range) {
         super.getVisiblePages(range);
-        if (mState == State.OVERVIEW || mState == State.SPRING_LOADED) {
+        if (mForceDrawAdjacentPages) {
             // In overview mode, make sure that the two side pages are visible.
-            range[0] = Math.min(range[0], Math.max(getCurrentPage() - 1, numCustomPages()));
-            range[1] = Math.max(range[0], Math.min(getCurrentPage() + 1, getPageCount() - 1));
+            range[0] = Utilities.boundInRange(getCurrentPage() - 1, numCustomPages(), range[1]);
+            range[1] = Utilities.boundInRange(getCurrentPage() + 1, range[0], getPageCount() - 1);
         }
     }
 
@@ -2015,7 +2016,8 @@
         updateAccessibilityFlags();
         if (mState == State.OVERVIEW || mState == State.SPRING_LOADED) {
             // Redraw pages, as we might want to draw pages which were not visible.
-            invalidate();
+            mForceDrawAdjacentPages = true;
+            invalidate(); // This will call dispatchDraw(), which calls getVisiblePages().
         }
 
         return workspaceAnim;
@@ -2089,6 +2091,7 @@
         mIsSwitchingState = false;
         updateChildrenLayersEnabled(false);
         showCustomContentIfNecessary();
+        mForceDrawAdjacentPages = false;
     }
 
     void updateCustomContentVisibility() {