Getting RTL up to a reasonable state in Launcher3

-> Fixing random page jumps from one edge of page view to another (issue 10846070)
-> Fixing screen -1 overlap (issue 10861684)
-> Fixing callbacks / transition state related to Custom Content in RTL (issue 10858355)
-> Overview mode now works in RTL
-> Making visible page range code RTL-LTR independent

Change-Id: I4dcc6127a02bf6669f5a1e8e1b62b340b60a5edc
diff --git a/src/com/android/launcher3/PagedView.java b/src/com/android/launcher3/PagedView.java
index 31a9797..6ccd32f 100644
--- a/src/com/android/launcher3/PagedView.java
+++ b/src/com/android/launcher3/PagedView.java
@@ -911,8 +911,9 @@
                 mPageScrolls[i] = childLeft - scrollOffset - offsetX;
 
                 if (i != endIndex - delta) {
+                    childLeft += childWidth + scrollOffset;
                     int nextScrollOffset = (getViewportWidth() - getChildWidth(i + delta)) / 2;
-                    childLeft += childWidth + nextScrollOffset;
+                    childLeft += nextScrollOffset;
                 }
             }
         }
@@ -1039,9 +1040,6 @@
     protected int getChildOffset(int index) {
         if (index < 0 || index > getChildCount() - 1) return 0;
 
-        final boolean isRtl = isLayoutRtl();
-
-        if (isRtl) index = getChildCount() - index - 1;
         int offset = getPageAt(index).getLeft() - getViewportOffsetX();
 
         return offset;
@@ -1056,33 +1054,43 @@
         final int pageCount = getChildCount();
         mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
 
+        range[0] = -1;
+        range[1] = -1;
+
         if (pageCount > 0) {
             int viewportWidth = getViewportWidth();
-            int leftScreen = 0;
-            int rightScreen = 0;
+            int curScreen = 0;
 
-            for (leftScreen = getNextPage(); leftScreen >= 0; --leftScreen) {
-                View currPage = getPageAt(leftScreen);
+            int count = getChildCount();
+            for (int i = 0; i < count; i++) {
+                View currPage = getPageAt(i);
 
-                // Check if the right edge of the page is in the viewport
-                mTmpIntPoint[0] = currPage.getMeasuredWidth();
-                Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
-                if (mTmpIntPoint[0] < 0) {
-                    break;
-                }
-            }
-            for (rightScreen = getNextPage(); rightScreen < getChildCount(); ++rightScreen) {
-                View currPage = getPageAt(rightScreen);
-
-                // Check if the left edge of the page is in the viewport
                 mTmpIntPoint[0] = 0;
                 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
-                if (mTmpIntPoint[0] >= viewportWidth) {
-                    break;
+                if (mTmpIntPoint[0] > viewportWidth) {
+                    if (range[0] == -1) {
+                        continue;
+                    } else {
+                        break;
+                    }
+                }
+
+                mTmpIntPoint[0] = currPage.getMeasuredWidth();;
+                Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
+                if (mTmpIntPoint[0] < 0) {
+                    if (range[0] == -1) {
+                        continue;
+                    } else {
+                        break;
+                    }
+                }
+                curScreen = i;
+                if (range[0] < 0) {
+                    range[0] = curScreen;
                 }
             }
-            range[0] = Math.max(0, leftScreen);
-            range[1] = Math.min(rightScreen, getChildCount() - 1);
+
+            range[1] = curScreen;
         } else {
             range[0] = -1;
             range[1] = -1;
@@ -1558,8 +1566,13 @@
         if (!mFreeScroll) {
             snapToPage(snapPage);
         } else {
-            mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
-            mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
+            if (isLayoutRtl()) {
+                mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
+                mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
+            } else {
+                mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
+                mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
+            }
 
             if (getCurrentPage() < mTempVisiblePagesRange[0]) {
                 setCurrentPage(mTempVisiblePagesRange[0]);
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index aab0a63..53a6b68 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -933,8 +933,9 @@
         boolean passRightSwipesToCustomContent =
                 (mTouchDownTime - mCustomContentShowTime) > CUSTOM_CONTENT_GESTURE_DELAY;
 
-        if (deltaX > 0 && getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID
-                && passRightSwipesToCustomContent) {
+        boolean swipeInIgnoreDirection = isLayoutRtl() ? deltaX < 0 : deltaX > 0;
+        if (swipeInIgnoreDirection && getScreenIdForPageIndex(getCurrentPage()) ==
+                CUSTOM_CONTENT_SCREEN_ID && passRightSwipesToCustomContent) {
             // Pass swipes to the right to the custom content page.
             return;
         }
@@ -1363,9 +1364,15 @@
         if (hasCustomContent()) {
             int index = mScreenOrder.indexOf(CUSTOM_CONTENT_SCREEN_ID);
             int scrollDelta = getScrollForPage(index + 1) - getScrollX();
-            translationX = Math.max(scrollDelta, 0);
+            translationX = scrollDelta;
             progress = (1.0f * scrollDelta) /
                     (getScrollForPage(index + 1) - getScrollForPage(index));
+
+            if (isLayoutRtl()) {
+                translationX = Math.min(0, translationX);
+            } else {
+                translationX = Math.max(0, translationX);
+            }
             progress = Math.max(0, progress);
         }
 
@@ -1396,7 +1403,10 @@
         updateStateForCustomContent(screenCenter);
         enableHwLayersOnVisiblePages();
 
-        if ((mOverScrollX < 0 && !hasCustomContent()) || mOverScrollX > mMaxScrollX) {
+        boolean shouldOverScroll = (mOverScrollX < 0 && (!hasCustomContent() || isLayoutRtl())) ||
+                (mOverScrollX > mMaxScrollX && (!hasCustomContent() || !isLayoutRtl()));
+
+        if (shouldOverScroll) {
             int index = 0;
             float pivotX = 0f;
             final float leftBiasedPivot = 0.25f;
@@ -1857,7 +1867,7 @@
             } else if (stateIsOverview) {
                 mNewScale = mOverviewModeShrinkFactor;
             } else if (stateIsSmall){
-                mNewScale = mOverviewModeShrinkFactor - 0.1f;
+                mNewScale = mOverviewModeShrinkFactor - 0.3f;
             }
             if (oldStateIsNormal && stateIsSmall) {
                 zoomIn = false;