Improve multi-crop forward compatibility logic

Make sure we first adapt the existing crop for the new device before
generating missing crops. This is important if there is a B&R from a
similar device with slightly different screen dimensions: we don't want
to calculate the missing crops based on the crops of a previous device;
we first want to adjust the existing crops for the new device before
calculating other crops for missing orientations.

Flag: aconfig com.android.window.flags.multi_crop DEVELOPMENT
Bug: NA (let's not spam bugs with small fixes)
Test: atest WallpaperManagerTest WallpaperCropperTest
Change-Id: I199d6b2c00314c00ca6c240b942e5b0c4e902be9
diff --git a/services/core/java/com/android/server/wallpaper/WallpaperCropper.java b/services/core/java/com/android/server/wallpaper/WallpaperCropper.java
index 37f3825..601c7f4 100644
--- a/services/core/java/com/android/server/wallpaper/WallpaperCropper.java
+++ b/services/core/java/com/android/server/wallpaper/WallpaperCropper.java
@@ -357,15 +357,30 @@
      * Given some suggested crops, find cropHints for all orientations of the default display.
      */
     SparseArray<Rect> getDefaultCrops(SparseArray<Rect> suggestedCrops, Point bitmapSize) {
-        SparseArray<Rect> result = new SparseArray<>();
-        // add missing cropHints for all orientation of the default display
+
         SparseArray<Point> defaultDisplaySizes = mWallpaperDisplayHelper.getDefaultDisplaySizes();
         boolean rtl = TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())
                 == View.LAYOUT_DIRECTION_RTL;
+
+        // adjust existing entries for the default display
+        SparseArray<Rect> adjustedSuggestedCrops = new SparseArray<>();
         for (int i = 0; i < defaultDisplaySizes.size(); i++) {
             int orientation = defaultDisplaySizes.keyAt(i);
             Point displaySize = defaultDisplaySizes.valueAt(i);
-            Rect newCrop = getCrop(displaySize, bitmapSize, suggestedCrops, rtl);
+            Rect suggestedCrop = suggestedCrops.get(orientation);
+            if (suggestedCrop != null) {
+                adjustedSuggestedCrops.put(orientation,
+                        getCrop(displaySize, bitmapSize, suggestedCrops, rtl));
+            }
+        }
+
+        // add missing cropHints for all orientation of the default display
+        SparseArray<Rect> result = adjustedSuggestedCrops.clone();
+        for (int i = 0; i < defaultDisplaySizes.size(); i++) {
+            int orientation = defaultDisplaySizes.keyAt(i);
+            if (result.contains(orientation)) continue;
+            Point displaySize = defaultDisplaySizes.valueAt(i);
+            Rect newCrop = getCrop(displaySize, bitmapSize, adjustedSuggestedCrops, rtl);
             result.put(orientation, newCrop);
         }
         return result;