Fixing wallpaper scrolling to be consistent in springloaded

Change-Id: I0aabd541e07c32aaa47068047e5ecb67f89cabab
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 27382e8..c9c690a 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -2144,7 +2144,6 @@
                         // Hide the workspace scrollbar
                         mWorkspace.hideScrollingIndicator(true);
                         mWorkspace.hideDockDivider(true);
-                        mWorkspace.showAllAppsAnimationComplete();
                     }
                     updateWallpaperVisibility(false);
                 }
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index 6f94c93..d191d07 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -113,6 +113,8 @@
     private int mOverScrollPageIndex = -1;
     private AnimatorSet mDividerAnimator;
 
+    private float mWallpaperScrollRatio = 1.0f;
+
     private final WallpaperManager mWallpaperManager;
     private IBinder mWindowToken;
 
@@ -159,7 +161,6 @@
     enum State { NORMAL, SPRING_LOADED, SMALL };
     private State mState = State.NORMAL;
     private boolean mIsSwitchingState = false;
-    boolean mEnableSyncWallpaper = false;
     private boolean mSwitchStateAfterFirstLayout = false;
     private State mStateAfterFirstLayout;
 
@@ -192,7 +193,6 @@
     int mWallpaperHeight;
     WallpaperOffsetInterpolator mWallpaperOffset;
     boolean mUpdateWallpaperOffsetImmediately = false;
-    boolean mSyncWallpaperOffsetWithScroll = true;
     private Runnable mDelayedResizeRunnable;
 
     // Variables relating to the creation of user folders by hovering shortcuts over shortcuts
@@ -754,6 +754,11 @@
         // for all apps/customize)
         mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 1.0f / (3 - 1));
 
+        // For the purposes of computing the scrollRange and overScrollOffset, we ignore
+        // assume that mLayoutScale is 1. This means that when we're in spring-loaded mode,
+        // there's no discrepancy between the wallpaper offset for a given page.
+        float layoutScale = mLayoutScale;
+        mLayoutScale = 1f;
         int scrollRange = getScrollRange();
         float scrollProgressOffset = 0;
 
@@ -767,8 +772,12 @@
             scrollRange += 2 * overscrollOffset;
         }
 
+        // Again, we adjust the wallpaper offset to be consistent between values of mLayoutScale
+        float adjustedScrollX = mWallpaperScrollRatio * mScrollX;
+        mLayoutScale = layoutScale;
+
         float scrollProgress =
-            mScrollX / (float) scrollRange + scrollProgressOffset;
+            adjustedScrollX / (float) scrollRange + scrollProgressOffset;
         float offsetInDips = wallpaperTravelWidth * scrollProgress +
             (mWallpaperWidth - wallpaperTravelWidth) / 2; // center it
         float offset = offsetInDips / (float) mWallpaperWidth;
@@ -807,6 +816,34 @@
         }
     }
 
+    @Override
+    protected void updateCurrentPageScroll() {
+        super.updateCurrentPageScroll();
+        computeWallpaperScrollRatio();
+    }
+
+    @Override
+    protected void snapToPage(int whichPage) {
+        super.snapToPage(whichPage);
+        computeWallpaperScrollRatio();
+    }
+
+    private void computeWallpaperScrollRatio() {
+        // Here, we determine what the desired scroll would be with and without a layout scale,
+        // and compute a ratio between the two. This allows us to adjust the wallpaper offset
+        // as though there is no layout scale.
+        float layoutScale = mLayoutScale;
+        int scaled = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
+        mLayoutScale = 1.0f;
+        float unscaled = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
+        mLayoutScale = layoutScale;
+        if (scaled > 0) {
+            mWallpaperScrollRatio = (1.0f * unscaled) / scaled;
+        } else {
+            mWallpaperScrollRatio = 1f;
+        }
+    }
+
     class WallpaperOffsetInterpolator {
         float mFinalHorizontalWallpaperOffset = 0.0f;
         float mFinalVerticalWallpaperOffset = 0.5f;
@@ -920,9 +957,7 @@
     @Override
     public void computeScroll() {
         super.computeScroll();
-        if (mSyncWallpaperOffsetWithScroll) {
-            syncWallpaperOffsetWithScroll();
-        }
+        syncWallpaperOffsetWithScroll();
     }
 
     void showOutlines() {
@@ -1411,13 +1446,6 @@
         changeState(shrinkState, true);
     }
 
-    void showAllAppsAnimationComplete() {
-        if (mEnableSyncWallpaper) {
-            mSyncWallpaperOffsetWithScroll = true;
-            mEnableSyncWallpaper = true;
-        }
-    }
-
     void changeState(final State state, boolean animated) {
         if (mFirstLayout) {
             // (mFirstLayout == "first layout has not happened yet")
@@ -1440,14 +1468,13 @@
         State oldState = mState;
         mState = state;
         boolean zoomIn = true;
+
         if (state != State.NORMAL) {
             finalScaleFactor = mSpringLoadedShrinkFactor - (state == State.SMALL ? 0.1f : 0);
             finalBackgroundAlpha = 1.0f;
             if (oldState == State.NORMAL && state == State.SMALL) {
                 zoomIn = false;
                 if (animated) {
-                    mEnableSyncWallpaper = true;
-                    mSyncWallpaperOffsetWithScroll = false;
                     hideScrollingIndicator(true);
                 }
                 setLayoutScale(finalScaleFactor);