Fix null context IllegalStateException received from recalculating colors

Fixes: 189061825
Test: Manually
Change-Id: I9d1ccb118a09330485972cb52fb9d5899ba3ef2c
diff --git a/src/com/android/wallpaper/picker/ImagePreviewFragment.java b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
index 2becd49..d4c8ef5 100755
--- a/src/com/android/wallpaper/picker/ImagePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
@@ -386,9 +386,16 @@
     }
 
     private void recalculateColors() {
+        Context context = getContext();
+        if (context == null) {
+            Log.e(TAG, "Got null context, skip recalculating colors");
+            return;
+        }
+
         BitmapCropper bitmapCropper = InjectorProvider.getInjector().getBitmapCropper();
         bitmapCropper.cropAndScaleBitmap(mWallpaperAsset, mFullResImageView.getScale(),
-                calculateCropRect(), /* adjustForRtl= */ false, new BitmapCropper.Callback() {
+                calculateCropRect(context), /* adjustForRtl= */ false,
+                new BitmapCropper.Callback() {
                     @Override
                     public void onBitmapCropped(Bitmap croppedBitmap) {
                         onWallpaperColorsChanged(WallpaperColors.fromBitmap(croppedBitmap));
@@ -495,9 +502,9 @@
         mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition);
     }
 
-    private Rect calculateCropRect() {
+    private Rect calculateCropRect(Context context) {
         float wallpaperZoom = mFullResImageView.getScale();
-        Context context = requireContext().getApplicationContext();
+        Context appContext = context.getApplicationContext();
 
         Rect visibleFileRect = new Rect();
         mFullResImageView.visibleFileRect(visibleFileRect);
@@ -508,10 +515,10 @@
         int minCrop = Math.min(cropWidth, cropHeight);
         Point hostViewSize = new Point(cropWidth, cropHeight);
 
-        Resources res = context.getResources();
+        Resources res = appContext.getResources();
         Point cropSurfaceSize = WallpaperCropUtils.calculateCropSurfaceSize(res, maxCrop, minCrop);
 
-        Rect cropRect = WallpaperCropUtils.calculateCropRect(context, hostViewSize,
+        Rect cropRect = WallpaperCropUtils.calculateCropRect(appContext, hostViewSize,
                 cropSurfaceSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom);
         return cropRect;
     }
@@ -519,7 +526,7 @@
     @Override
     protected void setCurrentWallpaper(@Destination int destination) {
         mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset,
-                destination, mFullResImageView.getScale(), calculateCropRect(),
+                destination, mFullResImageView.getScale(), calculateCropRect(getContext()),
                 new SetWallpaperCallback() {
                     @Override
                     public void onSuccess(WallpaperInfo wallpaperInfo) {