Apply minimal task dimension to all task bounds change.

We only used to apply the minimal dimension on resizeTask in
ActivityStackSuperior which is mostly used for freeform and
missed resizes that went through other channels like resizeStack
which is used for split-screen.
We now apply it on TaskRecord.updateOverrideConfiguration which
all task bounds changes go through.

Bug: 27220870
Change-Id: I856948c371f5f5f144e61029ced4a467f7ebe33a
diff --git a/services/core/java/com/android/server/am/TaskRecord.java b/services/core/java/com/android/server/am/TaskRecord.java
index add1b96..37a549a 100644
--- a/services/core/java/com/android/server/am/TaskRecord.java
+++ b/services/core/java/com/android/server/am/TaskRecord.java
@@ -245,6 +245,7 @@
     Rect mBounds = null;
     private final Rect mTmpStableBounds = new Rect();
     private final Rect mTmpNonDecorBounds = new Rect();
+    private final Rect mTmpRect = new Rect();
     private final Rect mTmpRect2 = new Rect();
 
     // Last non-fullscreen bounds the task was launched in or resized to.
@@ -1314,6 +1315,38 @@
         return task;
     }
 
+    private void adjustForMinimalTaskDimensions(Rect bounds) {
+        if (bounds == null) {
+            return;
+        }
+        final int minimalSize = mMinimalSize == -1
+                ? mService.mStackSupervisor.mDefaultMinimalSizeOfResizeableTask : mMinimalSize;
+        final boolean adjustWidth = minimalSize > bounds.width();
+        final boolean adjustHeight = minimalSize > bounds.height();
+        if (!(adjustWidth || adjustHeight)) {
+            return;
+        }
+
+        if (adjustWidth) {
+            if (mBounds != null && bounds.right == mBounds.right) {
+                bounds.left = bounds.right - minimalSize;
+            } else {
+                // Either left bounds match, or neither match, or the previous bounds were
+                // fullscreen and we default to keeping left.
+                bounds.right = bounds.left + minimalSize;
+            }
+        }
+        if (adjustHeight) {
+            if (mBounds != null && bounds.bottom == mBounds.bottom) {
+                bounds.top = bounds.bottom - minimalSize;
+            } else {
+                // Either top bounds match, or neither match, or the previous bounds were
+                // fullscreen and we default to keeping top.
+                bounds.bottom = bounds.top + minimalSize;
+            }
+        }
+    }
+
     /**
      * Update task's override configuration based on the bounds.
      * @param bounds The bounds of the task.
@@ -1346,15 +1379,17 @@
             mBounds = null;
             mOverrideConfig = Configuration.EMPTY;
         } else {
+            mTmpRect.set(bounds);
+            adjustForMinimalTaskDimensions(mTmpRect);
             if (mBounds == null) {
-                mBounds = new Rect(bounds);
+                mBounds = new Rect(mTmpRect);
             } else {
-                mBounds.set(bounds);
+                mBounds.set(mTmpRect);
             }
             if (stack == null || StackId.persistTaskBounds(stack.mStackId)) {
                 mLastNonFullscreenBounds = mBounds;
             }
-            mOverrideConfig = calculateOverrideConfig(bounds, insetBounds);
+            mOverrideConfig = calculateOverrideConfig(mTmpRect, insetBounds);
         }
 
         if (mFullscreen != oldFullscreen) {