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]);