Prevent edge gradient for screenshots in correct orientation

- Due to rounding issues, the width could be a pixel too small, causing
  a gradient on the right edge
- On tall devices, the height ratio of the task thumbnail might be
  larger than the width ratio, causing the screenshot to be too short
  upon swiping up, and thus a gradient on the bottom edge

Bug: 73540853
Change-Id: I1330cec3d2ff8c98eb1b354f0e7f40c6ea49ad7c
diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java
index db82286..d5c0ff8 100644
--- a/quickstep/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsView.java
@@ -323,11 +323,31 @@
         padding.bottom = profile.availableHeightPx - padding.top - sTempStableInsets.top
                 - Math.round(overviewHeight);
         padding.left = padding.right = (int) ((profile.availableWidthPx - overviewWidth) / 2);
+
+        // If the height ratio is larger than the width ratio, the screenshot will get cropped
+        // at the bottom when swiping up. In this case, increase the top/bottom padding to make it
+        // the same aspect ratio.
+        Rect pageRect = new Rect();
+        getPageRect(profile, context, pageRect, padding);
+        float widthRatio = (float) pageRect.width() / taskWidth;
+        float heightRatio = (float) pageRect.height() / taskHeight;
+        if (heightRatio > widthRatio) {
+            float additionalVerticalPadding = pageRect.height() - widthRatio * taskHeight;
+            additionalVerticalPadding = Math.round(additionalVerticalPadding);
+            padding.top += additionalVerticalPadding / 2;
+            padding.bottom += additionalVerticalPadding / 2;
+        }
+
         return padding;
     }
 
     public static void getPageRect(DeviceProfile grid, Context context, Rect outRect) {
         Rect targetPadding = getPadding(grid, context);
+        getPageRect(grid, context, outRect, targetPadding);
+    }
+
+    private static void getPageRect(DeviceProfile grid, Context context, Rect outRect,
+            Rect targetPadding) {
         Rect insets = grid.getInsets();
         outRect.set(
                 targetPadding.left + insets.left,
diff --git a/quickstep/src/com/android/quickstep/views/TaskThumbnailView.java b/quickstep/src/com/android/quickstep/views/TaskThumbnailView.java
index 87bb53b..6f8878c 100644
--- a/quickstep/src/com/android/quickstep/views/TaskThumbnailView.java
+++ b/quickstep/src/com/android/quickstep/views/TaskThumbnailView.java
@@ -175,7 +175,7 @@
 
             float bitmapHeight = Math.max(thumbnailHeight * thumbnailScale, 0);
             Shader shader = mBitmapShader;
-            if (bitmapHeight < getMeasuredHeight()) {
+            if (Math.round(bitmapHeight) < getMeasuredHeight()) {
                 int color = mPaint.getColor();
                 LinearGradient fade = new LinearGradient(
                         0, bitmapHeight - mFadeLength, 0, bitmapHeight,
@@ -184,7 +184,7 @@
             }
 
             float bitmapWidth = Math.max(thumbnailWidth * thumbnailScale, 0);
-            if (bitmapWidth < getMeasuredWidth()) {
+            if (Math.round(bitmapWidth) < getMeasuredWidth()) {
                 int color = mPaint.getColor();
                 LinearGradient fade = new LinearGradient(
                         bitmapWidth - mFadeLength, 0, bitmapWidth, 0,