Use larger crop surface size to allow for parallax scrolling

In ImagePreviewFragment, we were always limiting the width of the wallpaper
to match the hostViewSize, so the wallpaper ended up cropped to one screen
size.
The current solution still forces one screen size for wallpapers whose
size actually matches the screen, but we need a better longer term solution

Fixes: 160111671
Change-Id: Ic5e6547569eb40ca3a7799bf5519ca32dcedc169
diff --git a/src/com/android/wallpaper/asset/CurrentWallpaperAssetVN.java b/src/com/android/wallpaper/asset/CurrentWallpaperAssetVN.java
index 9b06e45..d7460d6 100755
--- a/src/com/android/wallpaper/asset/CurrentWallpaperAssetVN.java
+++ b/src/com/android/wallpaper/asset/CurrentWallpaperAssetVN.java
@@ -101,6 +101,7 @@
 
     @Override
     protected void adjustCropRect(Context context, Point assetDimensions, Rect cropRect) {
+        cropRect.offsetTo(0, 0);
         WallpaperCropUtils.adjustCurrentWallpaperCropRect(context, assetDimensions, cropRect);
     }
 
diff --git a/src/com/android/wallpaper/picker/ImagePreviewFragment.java b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
index 5c2a904..41df2ba 100755
--- a/src/com/android/wallpaper/picker/ImagePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
@@ -26,6 +26,7 @@
 import android.animation.AnimatorListenerAdapter;
 import android.app.Activity;
 import android.content.Context;
+import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.Color;
 import android.graphics.Point;
@@ -397,9 +398,20 @@
 
         int cropWidth = mWorkspaceSurface.getMeasuredWidth();
         int cropHeight = mWorkspaceSurface.getMeasuredHeight();
+        int maxCrop = Math.max(cropWidth, cropHeight);
+        int minCrop = Math.min(cropWidth, cropHeight);
         Point hostViewSize = new Point(cropWidth, cropHeight);
+
+        // Workaround for now to force wallpapers designed to fit one screen size to not adjust for
+        // parallax scrolling (with an extra 1 pixel to account for rounding)
+        // TODO (santie): implement a better solution
+        boolean shouldForceScreenSize = mRawWallpaperSize.x - mScreenSize.x <= 1;
+        Resources res = context.getResources();
+        Point cropSurfaceSize = shouldForceScreenSize ? hostViewSize :
+                WallpaperCropUtils.calculateCropSurfaceSize(res, maxCrop, minCrop);
+
         Rect cropRect = WallpaperCropUtils.calculateCropRect(context, hostViewSize,
-                hostViewSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom);
+                cropSurfaceSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom);
         WallpaperCropUtils.adjustCropRect(context, cropRect, false /* zoomIn */);
         return cropRect;
     }
diff --git a/src/com/android/wallpaper/util/WallpaperCropUtils.java b/src/com/android/wallpaper/util/WallpaperCropUtils.java
index 0e1d95f..920037b 100755
--- a/src/com/android/wallpaper/util/WallpaperCropUtils.java
+++ b/src/com/android/wallpaper/util/WallpaperCropUtils.java
@@ -84,6 +84,14 @@
             minDim = Math.min(realSize.x, realSize.y);
         }
 
+        return calculateCropSurfaceSize(resources, maxDim, minDim);
+    }
+
+    /**
+     * Calculates ideal crop surface size for a surface of dimensions maxDim x minDim such that
+     * there is room for parallax in both* landscape and portrait screen orientations.
+     */
+    public static Point calculateCropSurfaceSize(Resources resources, int maxDim, int minDim) {
         final int defaultWidth, defaultHeight;
         if (resources.getConfiguration().smallestScreenWidthDp >= 720) {
             defaultWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim));