Working around incorrect wallpaper offsets being calculated in RTL.

- When launcher starts up, onCreate() triggers the launcher model loader
  to start, which calls bindScreens() to add the workspace pages.  
  However, layout does not happen until the device is unlocked, which 
  means that even though the default screen index and children are there
  the page scrolls are calculated incorrectly, and even in RTL, the 
  page scroll for the 0th screen is zero (it should be at the right
  most edge of the workspace).  This CL works around this by deferring 
  until the first layout after bindScreens() to unlock the wallpaper
  offset from its default bounds.  The workaround is only applied when 
  the launcher activity is first created.

Bug: 28795125
Change-Id: I33da0d7f934f5337d26e69f068f579a32897a837
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 3f12abf..aa6f62e 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -1372,6 +1372,9 @@
         mWorkspace.setHapticFeedbackEnabled(false);
         mWorkspace.setOnLongClickListener(this);
         mWorkspace.setup(mDragController);
+        // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
+        // default state, otherwise we will update to the wrong offsets in RTL
+        mWorkspace.lockWallpaperToDefaultPage();
         mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
         mDragController.addDragListener(mWorkspace);
 
@@ -3667,6 +3670,11 @@
             mWorkspace.createCustomContentContainer();
             populateCustomContentContainer();
         }
+
+        // After we have added all the screens, if the wallpaper was locked to the default state,
+        // then notify to indicate that it can be released and a proper wallpaper offset can be
+        // computed before the next layout
+        mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
     }
 
     private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index 3c057e6..264da04 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -90,7 +90,6 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * The workspace is a wide area with a wallpaper and a finite number of pages.
@@ -253,6 +252,7 @@
     private boolean mWorkspaceFadeInAdjacentScreens;
 
     final WallpaperOffsetInterpolator mWallpaperOffset;
+    private boolean mUnlockWallpaperFromDefaultPageOnLayout;
 
     @Thunk Runnable mDelayedResizeRunnable;
     private Runnable mDelayedSnapToPageRunnable;
@@ -1616,6 +1616,17 @@
         });
     }
 
+    public void lockWallpaperToDefaultPage() {
+        mWallpaperOffset.setLockToDefaultPage(true);
+    }
+
+    public void unlockWallpaperFromDefaultPageOnNextLayout() {
+        if (mWallpaperOffset.isLockedToDefaultPage()) {
+            mUnlockWallpaperFromDefaultPageOnLayout = true;
+            requestLayout();
+        }
+    }
+
     protected void snapToPage(int whichPage, Runnable r) {
         snapToPage(whichPage, SLOW_PAGE_SNAP_ANIMATION_DURATION, r);
     }
@@ -1797,6 +1808,10 @@
 
     @Override
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+        if (mUnlockWallpaperFromDefaultPageOnLayout) {
+            mWallpaperOffset.setLockToDefaultPage(false);
+            mUnlockWallpaperFromDefaultPageOnLayout = false;
+        }
         if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
             mWallpaperOffset.syncWithScroll();
             mWallpaperOffset.jumpToFinal();
diff --git a/src/com/android/launcher3/util/WallpaperOffsetInterpolator.java b/src/com/android/launcher3/util/WallpaperOffsetInterpolator.java
index 1568beb..d7b3914 100644
--- a/src/com/android/launcher3/util/WallpaperOffsetInterpolator.java
+++ b/src/com/android/launcher3/util/WallpaperOffsetInterpolator.java
@@ -33,6 +33,7 @@
     private float mFinalOffset = 0.0f;
     private float mCurrentOffset = 0.5f; // to force an initial update
     private boolean mWaitingForUpdate;
+    private boolean mLockedToDefaultPage;
 
     private boolean mAnimating;
     private long mAnimationStartTime;
@@ -68,6 +69,17 @@
         }
     }
 
+    /**
+     * Locks the wallpaper offset to the offset in the default state of Launcher.
+     */
+    public void setLockToDefaultPage(boolean lockToDefaultPage) {
+        mLockedToDefaultPage = lockToDefaultPage;
+    }
+
+    public boolean isLockedToDefaultPage() {
+        return mLockedToDefaultPage;
+    }
+
     public boolean computeScrollOffset() {
         final float oldOffset = mCurrentOffset;
         if (mAnimating) {
@@ -97,7 +109,7 @@
         // To match the default wallpaper behavior in the system, we default to either the left
         // or right edge on initialization
         int numScrollingPages = getNumScreensExcludingEmptyAndCustom();
-        if (numScrollingPages <= 1) {
+        if (mLockedToDefaultPage || numScrollingPages <= 1) {
             return mIsRtl ? 1f : 0f;
         }
 
@@ -194,7 +206,7 @@
 
     public void setFinalX(float x) {
         scheduleUpdate();
-        mFinalOffset = Math.max(0f, Math.min(x, 1.0f));
+        mFinalOffset = Math.max(0f, Math.min(x, 1f));
         if (getNumScreensExcludingEmptyAndCustom() != mNumScreens) {
             if (mNumScreens > 0) {
                 // Don't animate if we're going from 0 screens