Scale wallpaper preview surface instead of preview image

Instead of scaling the actual image when previewing, scale the surface
where the wallpaper preview is rendered (while keeping the extra size
outside of the preview visible area).
This will allow the wallpaper to be rendered similarly to how the system
will do it, thus preventing the user from panning or zooming the image
further than the zoomed out area (eg, panning the image all the way to
the edge, thus not leaving any extra area to zoom out to)

Bug: 171274086
Change-Id: Ifbec1259b38b63e9b91ecd7a45ea8a3fd3130e91
(cherry picked from commit 91bc4642be979b3d960f90fd0fcd1dec29cc38b8)
diff --git a/src/com/android/wallpaper/picker/ImagePreviewFragment.java b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
index c827b5a..1ea49f0 100755
--- a/src/com/android/wallpaper/picker/ImagePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
@@ -40,6 +40,7 @@
 import android.view.SurfaceView;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.ViewGroup.LayoutParams;
 import android.widget.ImageView;
 
 import androidx.annotation.NonNull;
@@ -378,8 +379,8 @@
      */
     private void setDefaultWallpaperZoomAndScroll(boolean offsetToFarLeft) {
         // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface.
-        int cropWidth = mWorkspaceSurface.getMeasuredWidth();
-        int cropHeight = mWorkspaceSurface.getMeasuredHeight();
+        int cropWidth = mWallpaperSurface.getMeasuredWidth();
+        int cropHeight = mWallpaperSurface.getMeasuredHeight();
         Point crop = new Point(cropWidth, cropHeight);
         Rect visibleRawWallpaperRect =
                 WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop);
@@ -387,8 +388,8 @@
             visibleRawWallpaperRect.offsetTo(/* newLeft= */ 0, visibleRawWallpaperRect.top);
         }
 
-        final PointF centerPosition = WallpaperCropUtils.calculateDefaultCenter(requireContext(),
-                mRawWallpaperSize, visibleRawWallpaperRect);
+        final PointF centerPosition = new PointF(visibleRawWallpaperRect.centerX(),
+                visibleRawWallpaperRect.centerY());
 
         Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(),
                 visibleRawWallpaperRect.height());
@@ -413,18 +414,14 @@
         Rect visibleFileRect = new Rect();
         mFullResImageView.visibleFileRect(visibleFileRect);
 
-        int cropWidth = mWorkspaceSurface.getMeasuredWidth();
-        int cropHeight = mWorkspaceSurface.getMeasuredHeight();
+        int cropWidth = mWallpaperSurface.getMeasuredWidth();
+        int cropHeight = mWallpaperSurface.getMeasuredHeight();
         int maxCrop = Math.max(cropWidth, cropHeight);
         int minCrop = Math.min(cropWidth, cropHeight);
         Point hostViewSize = new Point(cropWidth, cropHeight);
 
         Resources res = context.getResources();
         Point cropSurfaceSize = WallpaperCropUtils.calculateCropSurfaceSize(res, maxCrop, minCrop);
-        WallpaperCropUtils.scaleSize(context, hostViewSize);
-        WallpaperCropUtils.scaleSize(context, cropSurfaceSize);
-
-        WallpaperCropUtils.adjustCropRect(context, visibleFileRect, false);
 
         Rect cropRect = WallpaperCropUtils.calculateCropRect(context, hostViewSize,
                 cropSurfaceSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom);
@@ -469,10 +466,34 @@
                 if (mFullResImageView != null) {
                     mFullResImageView.recycle();
                 }
-                View wallpaperPreviewContainer = LayoutInflater.from(getContext()).inflate(
+                Context context = getContext();
+                View wallpaperPreviewContainer = LayoutInflater.from(context).inflate(
                         R.layout.fullscreen_wallpaper_preview, null);
                 mFullResImageView = wallpaperPreviewContainer.findViewById(R.id.full_res_image);
                 mLowResImageView = wallpaperPreviewContainer.findViewById(R.id.low_res_image);
+                // Scale the mWallpaperSurface based on system zoom's scale so that the wallpaper is
+                // rendered in a larger surface than what preview shows, simulating the behavior of
+                // the actual wallpaper surface.
+                float scale = WallpaperCropUtils.getSystemWallpaperMaximumScale(context);
+                int origWidth = mWallpaperSurface.getWidth();
+                int width = (int) (origWidth * scale);
+                int origHeight = mWallpaperSurface.getHeight();
+                int height = (int) (origHeight * scale);
+                int left = (origWidth - width) / 2;
+                int top = (origHeight - height) / 2;
+
+                if (WallpaperCropUtils.isRtl(context)) {
+                    left *= -1;
+                }
+
+                LayoutParams params = mWallpaperSurface.getLayoutParams();
+                params.width = width;
+                params.height = height;
+                mWallpaperSurface.setX(left);
+                mWallpaperSurface.setY(top);
+                mWallpaperSurface.setLayoutParams(params);
+                mWallpaperSurface.requestLayout();
+
                 // Load a low-res placeholder image if there's a thumbnail available from the asset
                 // that can be shown to the user more quickly than the full-sized image.
                 if (mWallpaperAsset.hasLowResDataSource()) {
@@ -482,15 +503,14 @@
                                     activity.getApplicationContext(), isRtl()));
                 }
                 wallpaperPreviewContainer.measure(
-                        makeMeasureSpec(mWallpaperSurface.getWidth(), EXACTLY),
-                        makeMeasureSpec(mWallpaperSurface.getHeight(), EXACTLY));
-                wallpaperPreviewContainer.layout(0, 0, mWallpaperSurface.getWidth(),
-                        mWallpaperSurface.getHeight());
+                        makeMeasureSpec(width, EXACTLY),
+                        makeMeasureSpec(height, EXACTLY));
+                wallpaperPreviewContainer.layout(0, 0, width, height);
                 mTouchForwardingLayout.setTargetView(mFullResImageView);
 
                 cleanUp();
-                mHost = new SurfaceControlViewHost(getContext(),
-                        getContext().getDisplay(), mWallpaperSurface.getHostToken());
+                mHost = new SurfaceControlViewHost(context,
+                        context.getDisplay(), mWallpaperSurface.getHostToken());
                 mHost.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(),
                         wallpaperPreviewContainer.getHeight());
                 mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage());
diff --git a/src/com/android/wallpaper/util/WallpaperCropUtils.java b/src/com/android/wallpaper/util/WallpaperCropUtils.java
index a13ed55..bff7a67 100755
--- a/src/com/android/wallpaper/util/WallpaperCropUtils.java
+++ b/src/com/android/wallpaper/util/WallpaperCropUtils.java
@@ -347,7 +347,7 @@
      * Returns whether layout direction is RTL (or false for LTR). Since native RTL layout support
      * was added in API 17, returns false for versions lower than 17.
      */
-    private static boolean isRtl(Context context) {
+    public static boolean isRtl(Context context) {
         return context.getResources().getConfiguration().getLayoutDirection()
                 == View.LAYOUT_DIRECTION_RTL;
     }