Unify the cropping mechanism in Preview and in Rotation

Move most of the scaling and cropping logic from ImagePreviewFragment
into WallpaperCropUtils and reuse it in DefaultWallpaperPersister when
setting a wallpaper in rotation.

This is just porting ag/11742259 into the new model.
Also we now center the wallpaper preview so that it matches the
thumbnail and previous behavior.

Fixes: 155943055
Change-Id: I7adb3a312491736109d29d4b488509f0037bbfaf
diff --git a/src/com/android/wallpaper/picker/ImagePreviewFragment.java b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
index 7cfc80a..f6d119b 100755
--- a/src/com/android/wallpaper/picker/ImagePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
@@ -33,7 +33,6 @@
 import android.graphics.Rect;
 import android.os.Bundle;
 import android.os.Handler;
-import android.view.Display;
 import android.view.LayoutInflater;
 import android.view.Surface;
 import android.view.SurfaceControlViewHost;
@@ -364,8 +363,10 @@
         Point crop = new Point(cropWidth, cropHeight);
         Rect visibleRawWallpaperRect =
                 WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop);
-        WallpaperCropUtils.adjustCurrentWallpaperCropRect(getContext(), mRawWallpaperSize,
-                visibleRawWallpaperRect);
+
+        final PointF centerPosition = WallpaperCropUtils.calculateDefaultCenter(requireContext(),
+                mRawWallpaperSize, visibleRawWallpaperRect);
+
         Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(),
                 visibleRawWallpaperRect.height());
 
@@ -373,8 +374,6 @@
                 visibleRawWallpaperSize, crop);
         final float minWallpaperZoom = defaultWallpaperZoom;
 
-        final PointF centerPosition = new PointF(visibleRawWallpaperRect.centerX(),
-                visibleRawWallpaperRect.centerY());
 
         // Set min wallpaper zoom and max zoom on MosaicView widget.
         mFullResImageView.setMaxScale(Math.max(DEFAULT_WALLPAPER_MAX_ZOOM, defaultWallpaperZoom));
@@ -387,7 +386,6 @@
     private Rect calculateCropRect() {
         float wallpaperZoom = mFullResImageView.getScale();
         Context context = requireContext().getApplicationContext();
-        Display defaultDisplay = requireActivity().getWindowManager().getDefaultDisplay();
 
         Rect visibleFileRect = new Rect();
         mFullResImageView.visibleFileRect(visibleFileRect);