Fix small computation error.

The case "BALANCE" is usually used to find a rotated crop, i.e. in cases
where the reference crop is for display "x, y" and we're looking for
"y, x". The previous computation for widthToAdd worked only in this
specific case (when screenRatio = crop.height/crop.width), but the
formulae was globally wrong. Fix that and add a note.

Flag: aconfig com.android.window.flags.multi_crop TEAMFOOD
Bug: 281648899
Test: atest WallpaperCropperTest
Change-Id: I70d1b1a18645ac0a161645d7a2d016a47b61c5c8
diff --git a/services/core/java/com/android/server/wallpaper/WallpaperCropper.java b/services/core/java/com/android/server/wallpaper/WallpaperCropper.java
index b19bc7d..dd3d512 100644
--- a/services/core/java/com/android/server/wallpaper/WallpaperCropper.java
+++ b/services/core/java/com/android/server/wallpaper/WallpaperCropper.java
@@ -313,10 +313,14 @@
                 adjustedCrop.right -= widthToRemove / 2 + widthToRemove % 2;
             }
         } else {
-            // TODO (b/281648899) the third case is not always correct, fix that.
+            // Note: the third case when MODE == BALANCE, -W + sqrt(W * H * R), is the width to add
+            // so that, when removing the appropriate height, we get a bitmap of aspect ratio R and
+            // total surface of W * H. In other words it is the width to add to get the desired
+            // aspect ratio R, while preserving the total number of pixels W * H.
             int widthToAdd = mode == REMOVE ? 0
                     : mode == ADD ? (int) (0.5 + crop.height() * screenRatio - crop.width())
-                    : (int) (0.5 + crop.height() - crop.width());
+                    : (int) (0.5 - crop.width()
+                            + Math.sqrt(crop.width() * crop.height() * screenRatio));
             int availableWidth = bitmapSize.x - crop.width();
             if (availableWidth >= widthToAdd) {
                 int widthToAddLeft = widthToAdd / 2;