Crop snapshot icons for Manage Windows menu.

Crops the provided task snapshot bitmaps to match the aspect ratio of
the icon buttons in the Manage Windows menu to prevent stretching the
bitmaps when they are particularly wide or tall.

Bug: 337915660
Bug: 372299112
Test: Manual
Flag: com.android.window.flags.enable_desktop_windowing_multi_instance_features
Change-Id: I15880d31328d547c547411449418d30602a98d6f
diff --git a/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/desktopmode/ManageWindowsViewContainer.kt b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/desktopmode/ManageWindowsViewContainer.kt
index 79becb0..0e8e904 100644
--- a/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/desktopmode/ManageWindowsViewContainer.kt
+++ b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/desktopmode/ManageWindowsViewContainer.kt
@@ -122,7 +122,8 @@
                     snapshot.hardwareBuffer,
                     snapshot.colorSpace
                 )
-                val scaledSnapshotBitmap = snapshotBitmap?.let {
+                val croppedBitmap = snapshotBitmap?.let { cropBitmap(it) }
+                val scaledSnapshotBitmap = croppedBitmap?.let {
                     Bitmap.createScaledBitmap(
                         it, instanceIconWidth.toInt(), instanceIconHeight.toInt(), true /* filter */
                     )
@@ -160,6 +161,35 @@
             menuHeight += iconMargin.toInt()
         }
 
+        private fun cropBitmap(
+            bitmapToCrop: Bitmap
+        ): Bitmap {
+            val ratioToMatch = ICON_WIDTH_DP / ICON_HEIGHT_DP
+            val bitmapWidth = bitmapToCrop.width
+            val bitmapHeight = bitmapToCrop.height
+            if (bitmapWidth > bitmapHeight * ratioToMatch) {
+                // Crop based on height
+                val newWidth = bitmapHeight * ratioToMatch
+                return Bitmap.createBitmap(
+                    bitmapToCrop,
+                    ((bitmapWidth - newWidth) / 2).toInt(),
+                    0,
+                    newWidth.toInt(),
+                    bitmapHeight
+                )
+            } else {
+                // Crop based on width
+                val newHeight = bitmapWidth / ratioToMatch
+                return Bitmap.createBitmap(
+                    bitmapToCrop,
+                    0,
+                    ((bitmapHeight - newHeight) / 2).toInt(),
+                    bitmapWidth,
+                    newHeight.toInt()
+                )
+            }
+        }
+
         companion object {
             private const val MENU_RADIUS_DP = 26f
             private const val ICON_WIDTH_DP = 204f