Adjust the wallpaper preview parallax according to the crop that will be saved.

To get the crop, the wallpaper is first scaled down as much as possible (i.e.
until either the width or height matches that of the device screen), then a
center crop is performed (the excess width or height is chopped off). The
preview's parallax was previously incorrect if the width was the victim of this
cropping, because in that case the parallax started and ended at a different
point than in the actual wallpaper. This is fixed by adjusting the parallax of
the preview to match the final crop.

The end result is that all wallpaper previews match the actual wallpaper upon
setting, except for the default wallpaper because it follows a different flow.

Bug: 23568800
Change-Id: I82b7ba506d51ee4b3812af5fbdf95d3303b37aef
diff --git a/WallpaperPicker/src/com/android/launcher3/CropView.java b/WallpaperPicker/src/com/android/launcher3/CropView.java
index 0ead365..4770a71 100644
--- a/WallpaperPicker/src/com/android/launcher3/CropView.java
+++ b/WallpaperPicker/src/com/android/launcher3/CropView.java
@@ -193,10 +193,10 @@
      * Offsets wallpaper preview according to the state it will be displayed in upon returning home.
      * @param offset Ranges from 0 to 1, where 0 is the leftmost parallax and 1 is the rightmost.
      */
-    public void setParallaxOffset(float offset) {
+    public void setParallaxOffset(float offset, RectF crop) {
         offset = Math.max(0, Math.min(offset, 1)); // Make sure the offset is in the correct range.
         float screenWidth = getWidth() / mRenderer.scale;
-        mCenterX = screenWidth / 2 + offset * (getSourceDimensions().x - screenWidth);
+        mCenterX = screenWidth / 2 + offset * (crop.width() - screenWidth) + crop.left;
         updateCenter();
     }
 
diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java
index 871eef2..efa1181 100644
--- a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java
+++ b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java
@@ -250,8 +250,13 @@
                 mCropView.moveToLeft();
             }
             if (req.scaleAndOffsetProvider != null) {
-                mCropView.setScale(req.scaleAndOffsetProvider.getScale(req.result));
-                mCropView.setParallaxOffset(req.scaleAndOffsetProvider.getParallaxOffset());
+                TileSource src = req.result;
+                Point wallpaperSize = WallpaperUtils.getDefaultWallpaperSize(
+                        getResources(), getWindowManager());
+                RectF crop = Utils.getMaxCropRect(src.getImageWidth(), src.getImageHeight(),
+                        wallpaperSize.x, wallpaperSize.y, false /* leftAligned */);
+                mCropView.setScale(req.scaleAndOffsetProvider.getScale(wallpaperSize, crop));
+                mCropView.setParallaxOffset(req.scaleAndOffsetProvider.getParallaxOffset(), crop);
             }
 
             // Free last image
@@ -443,7 +448,7 @@
     }
 
     public interface CropViewScaleAndOffsetProvider {
-        float getScale(TileSource src);
+        float getScale(Point wallpaperSize, RectF crop);
         float getParallaxOffset();
     }
 }
diff --git a/WallpaperPicker/src/com/android/launcher3/wallpapertileinfo/DefaultWallpaperInfo.java b/WallpaperPicker/src/com/android/launcher3/wallpapertileinfo/DefaultWallpaperInfo.java
index 2b508f1..7ede260 100644
--- a/WallpaperPicker/src/com/android/launcher3/wallpapertileinfo/DefaultWallpaperInfo.java
+++ b/WallpaperPicker/src/com/android/launcher3/wallpapertileinfo/DefaultWallpaperInfo.java
@@ -9,6 +9,7 @@
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Point;
+import android.graphics.RectF;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.os.Build;
@@ -19,7 +20,6 @@
 import com.android.launcher3.Utilities;
 import com.android.launcher3.WallpaperCropActivity.CropViewScaleAndOffsetProvider;
 import com.android.launcher3.WallpaperPickerActivity;
-import com.android.photos.views.TiledImageRenderer.TileSource;
 
 import java.io.File;
 import java.io.FileOutputStream;
@@ -38,7 +38,7 @@
         a.setCropViewTileSource(null, false, false, new CropViewScaleAndOffsetProvider() {
 
             @Override
-            public float getScale(TileSource src) {
+            public float getScale(Point wallpaperSize, RectF crop) {
                 return 1f;
             }
 
diff --git a/WallpaperPicker/src/com/android/launcher3/wallpapertileinfo/ResourceWallpaperInfo.java b/WallpaperPicker/src/com/android/launcher3/wallpapertileinfo/ResourceWallpaperInfo.java
index 9180c46..a5becf1 100644
--- a/WallpaperPicker/src/com/android/launcher3/wallpapertileinfo/ResourceWallpaperInfo.java
+++ b/WallpaperPicker/src/com/android/launcher3/wallpapertileinfo/ResourceWallpaperInfo.java
@@ -4,13 +4,11 @@
 import android.graphics.Point;
 import android.graphics.RectF;
 import android.graphics.drawable.Drawable;
-import com.android.gallery3d.common.Utils;
+
 import com.android.launcher3.WallpaperCropActivity.CropViewScaleAndOffsetProvider;
 import com.android.launcher3.WallpaperPickerActivity;
-import com.android.launcher3.util.WallpaperUtils;
 import com.android.photos.BitmapRegionTileSource;
 import com.android.photos.BitmapRegionTileSource.BitmapSource;
-import com.android.photos.views.TiledImageRenderer.TileSource;
 
 public class ResourceWallpaperInfo extends DrawableThumbWallpaperInfo {
 
@@ -28,16 +26,11 @@
         a.setWallpaperButtonEnabled(false);
         final BitmapRegionTileSource.ResourceBitmapSource bitmapSource =
                 new BitmapRegionTileSource.ResourceBitmapSource(mResources, mResId, a);
-        a.setCropViewTileSource(bitmapSource, false, true, new CropViewScaleAndOffsetProvider() {
+        a.setCropViewTileSource(bitmapSource, false, false, new CropViewScaleAndOffsetProvider() {
 
             @Override
-            public float getScale(TileSource src) {
-                Point wallpaperSize = WallpaperUtils.getDefaultWallpaperSize(
-                        a.getResources(), a.getWindowManager());
-                RectF crop = Utils.getMaxCropRect(
-                        src.getImageWidth(), src.getImageHeight(),
-                        wallpaperSize.x, wallpaperSize.y, false);
-                return wallpaperSize.x / crop.width();
+            public float getScale(Point wallpaperSize, RectF crop) {
+                return wallpaperSize.x /crop.width();
             }
 
             @Override