Fixing multi-window task sizes in Recents activity

> In recents activity, getting the DeviceProfile size from the
root view, instead of the Display (as recents activtiy can be resized)
> The task size in Recents activity is now proportional to the activity
size instead of 50% screen size

Bug: 77875376
Change-Id: Ib417c31fc7ec8569876368134ef021452d60aa12
diff --git a/quickstep/src/com/android/quickstep/ActivityControlHelper.java b/quickstep/src/com/android/quickstep/ActivityControlHelper.java
index 95947d7..f44def2 100644
--- a/quickstep/src/com/android/quickstep/ActivityControlHelper.java
+++ b/quickstep/src/com/android/quickstep/ActivityControlHelper.java
@@ -41,10 +41,9 @@
 import com.android.launcher3.allapps.AllAppsTransitionController;
 import com.android.launcher3.anim.AnimatorPlaybackController;
 import com.android.launcher3.util.ViewOnDrawExecutor;
-import com.android.quickstep.fallback.FallbackRecentsView;
+import com.android.quickstep.util.LayoutUtils;
 import com.android.quickstep.util.RemoteAnimationProvider;
 import com.android.quickstep.views.LauncherLayoutListener;
-import com.android.quickstep.views.LauncherRecentsView;
 import com.android.quickstep.views.RecentsView;
 import com.android.quickstep.views.TaskView;
 
@@ -142,7 +141,7 @@
 
         @Override
         public int getSwipeUpDestinationAndLength(DeviceProfile dp, Context context, Rect outRect) {
-            LauncherRecentsView.getPageRect(dp, context, outRect);
+            LayoutUtils.calculateLauncherTaskSize(context, dp, outRect);
             if (dp.isVerticalBarLayout()) {
                 Rect targetInsets = dp.getInsets();
                 int hotseatInset = dp.isSeascape() ? targetInsets.left : targetInsets.right;
@@ -295,7 +294,7 @@
 
         @Override
         public int getSwipeUpDestinationAndLength(DeviceProfile dp, Context context, Rect outRect) {
-            FallbackRecentsView.getPageRect(dp, context, outRect);
+            LayoutUtils.calculateFallbackTaskSize(context, dp, outRect);
             if (dp.isVerticalBarLayout()) {
                 Rect targetInsets = dp.getInsets();
                 int hotseatInset = dp.isSeascape() ? targetInsets.left : targetInsets.right;
diff --git a/quickstep/src/com/android/quickstep/RecentsActivity.java b/quickstep/src/com/android/quickstep/RecentsActivity.java
index 9ec9f52..0c49f16 100644
--- a/quickstep/src/com/android/quickstep/RecentsActivity.java
+++ b/quickstep/src/com/android/quickstep/RecentsActivity.java
@@ -29,7 +29,6 @@
 import android.animation.AnimatorSet;
 import android.app.ActivityOptions;
 import android.content.Intent;
-import android.content.pm.ActivityInfo;
 import android.content.res.Configuration;
 import android.os.Bundle;
 import android.os.Handler;
@@ -38,6 +37,7 @@
 
 import com.android.launcher3.AbstractFloatingView;
 import com.android.launcher3.BaseDraggingActivity;
+import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.InvariantDeviceProfile;
 import com.android.launcher3.ItemInfo;
 import com.android.launcher3.LauncherAnimationRunner;
@@ -73,12 +73,7 @@
         super.onCreate(savedInstanceState);
 
         mOldConfig = new Configuration(getResources().getConfiguration());
-        // In case we are reusing IDP, create a copy so that we dont conflict with Launcher
-        // activity.
-        LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
-        setDeviceProfile(appState != null
-                ? appState.getInvariantDeviceProfile().getDeviceProfile(this).copy(this)
-                : new InvariantDeviceProfile(this).getDeviceProfile(this));
+        initDeviceProfile();
 
         setContentView(R.layout.fallback_recents_activity);
         mRecentsRootView = findViewById(R.id.drag_layer);
@@ -108,15 +103,15 @@
         super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
     }
 
+    public void onRootViewSizeChanged() {
+        if (isInMultiWindowModeCompat()) {
+            onHandleConfigChanged();
+        }
+    }
+
     private void onHandleConfigChanged() {
         mUserEventDispatcher = null;
-
-        // In case we are reusing IDP, create a copy so that we dont conflict with Launcher
-        // activity.
-        LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
-        setDeviceProfile(appState != null
-                ? appState.getInvariantDeviceProfile().getDeviceProfile(this).copy(this)
-                : new InvariantDeviceProfile(this).getDeviceProfile(this));
+        initDeviceProfile();
 
         AbstractFloatingView.closeOpenViews(this, true,
                 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
@@ -124,7 +119,23 @@
 
         mRecentsRootView.setup();
         mRecentsRootView.dispatchInsets();
-        mRecentsRootView.requestLayout();
+    }
+
+    private void initDeviceProfile() {
+        // In case we are reusing IDP, create a copy so that we dont conflict with Launcher
+        // activity.
+        LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
+        if (isInMultiWindowModeCompat()) {
+            InvariantDeviceProfile idp = appState == null
+                    ? new InvariantDeviceProfile(this) : appState.getInvariantDeviceProfile();
+            DeviceProfile dp = idp.getDeviceProfile(this);
+            mDeviceProfile = dp.getMultiWindowProfile(this, mRecentsRootView.getLastKnownSize());
+        } else {
+            // If we are reusing the Invariant device profile, make a copy.
+            mDeviceProfile = appState == null
+                    ? new InvariantDeviceProfile(this).getDeviceProfile(this)
+                    : appState.getInvariantDeviceProfile().getDeviceProfile(this).copy(this);
+        }
     }
 
     @Override
diff --git a/quickstep/src/com/android/quickstep/fallback/FallbackRecentsView.java b/quickstep/src/com/android/quickstep/fallback/FallbackRecentsView.java
index 89422af..5a8ce16 100644
--- a/quickstep/src/com/android/quickstep/fallback/FallbackRecentsView.java
+++ b/quickstep/src/com/android/quickstep/fallback/FallbackRecentsView.java
@@ -64,11 +64,7 @@
 
     @Override
     protected void getTaskSize(DeviceProfile dp, Rect outRect) {
-        LayoutUtils.calculateTaskSize(getContext(), dp, 0, outRect);
+        LayoutUtils.calculateFallbackTaskSize(getContext(), dp, outRect);
     }
 
-    @AnyThread
-    public static void getPageRect(DeviceProfile grid, Context context, Rect outRect) {
-        LayoutUtils.calculateTaskSize(context, grid, 0, outRect);
-    }
 }
diff --git a/quickstep/src/com/android/quickstep/fallback/RecentsRootView.java b/quickstep/src/com/android/quickstep/fallback/RecentsRootView.java
index 1dc6fcf..ad7d19a 100644
--- a/quickstep/src/com/android/quickstep/fallback/RecentsRootView.java
+++ b/quickstep/src/com/android/quickstep/fallback/RecentsRootView.java
@@ -17,6 +17,7 @@
 
 import android.annotation.TargetApi;
 import android.content.Context;
+import android.graphics.Point;
 import android.graphics.Rect;
 import android.util.AttributeSet;
 
@@ -31,6 +32,8 @@
 
     private final RecentsActivity mActivity;
 
+    private final Point mLastKnownSize = new Point(10, 10);
+
     public RecentsRootView(Context context, AttributeSet attrs) {
         super(context, attrs);
         mActivity = (RecentsActivity) BaseActivity.fromContext(context);
@@ -39,10 +42,27 @@
                 | SYSTEM_UI_FLAG_LAYOUT_STABLE);
     }
 
+    public Point getLastKnownSize() {
+        return mLastKnownSize;
+    }
+
     public void setup() {
         mControllers = new TouchController[] { new RecentsTaskController(mActivity) };
     }
 
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        // Check size changes before the actual measure, to avoid multiple measure calls.
+        int width = MeasureSpec.getSize(widthMeasureSpec);
+        int height = MeasureSpec.getSize(heightMeasureSpec);
+        if (mLastKnownSize.x != width || mLastKnownSize.y != height) {
+            mLastKnownSize.set(width, height);
+            mActivity.onRootViewSizeChanged();
+        }
+
+        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+    }
+
     @TargetApi(23)
     @Override
     protected boolean fitSystemWindows(Rect insets) {
diff --git a/quickstep/src/com/android/quickstep/util/LayoutUtils.java b/quickstep/src/com/android/quickstep/util/LayoutUtils.java
index f29f9e4..aeaebea 100644
--- a/quickstep/src/com/android/quickstep/util/LayoutUtils.java
+++ b/quickstep/src/com/android/quickstep/util/LayoutUtils.java
@@ -15,39 +15,63 @@
  */
 package com.android.quickstep.util;
 
+import static java.lang.annotation.RetentionPolicy.SOURCE;
+
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Rect;
-import android.graphics.RectF;
+import android.support.annotation.AnyThread;
+import android.support.annotation.IntDef;
 
 import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.R;
 
+import java.lang.annotation.Retention;
+
 public class LayoutUtils {
 
+    private static final int MULTI_WINDOW_STRATEGY_HALF_SCREEN = 1;
+    private static final int MULTI_WINDOW_STRATEGY_DEVICE_PROFILE = 2;
+
+    @Retention(SOURCE)
+    @IntDef({MULTI_WINDOW_STRATEGY_HALF_SCREEN, MULTI_WINDOW_STRATEGY_DEVICE_PROFILE})
+    private @interface MultiWindowStrategy {}
+
     public static void calculateLauncherTaskSize(Context context, DeviceProfile dp, Rect outRect) {
         float extraSpace = dp.isVerticalBarLayout() ? 0 : dp.hotseatBarSizePx;
-        calculateTaskSize(context, dp, extraSpace, outRect);
+        calculateTaskSize(context, dp, extraSpace, MULTI_WINDOW_STRATEGY_HALF_SCREEN, outRect);
     }
 
+    public static void calculateFallbackTaskSize(Context context, DeviceProfile dp, Rect outRect) {
+        calculateTaskSize(context, dp, 0, MULTI_WINDOW_STRATEGY_DEVICE_PROFILE, outRect);
+    }
+
+    @AnyThread
     public static void calculateTaskSize(Context context, DeviceProfile dp,
-            float extraVerticalSpace, Rect outRect) {
+            float extraVerticalSpace, @MultiWindowStrategy int multiWindowStrategy, Rect outRect) {
         float taskWidth, taskHeight, paddingHorz;
         Resources res = context.getResources();
         Rect insets = dp.getInsets();
 
         if (dp.isMultiWindowMode) {
-            DeviceProfile fullDp = dp.getFullScreenProfile();
-            // Use availableWidthPx and availableHeightPx instead of widthPx and heightPx to
-            // account for system insets
-            taskWidth = fullDp.availableWidthPx;
-            taskHeight = fullDp.availableHeightPx;
-            float halfDividerSize = res.getDimension(R.dimen.multi_window_task_divider_size) / 2;
+            if (multiWindowStrategy == MULTI_WINDOW_STRATEGY_HALF_SCREEN) {
+                DeviceProfile fullDp = dp.getFullScreenProfile();
+                // Use availableWidthPx and availableHeightPx instead of widthPx and heightPx to
+                // account for system insets
+                taskWidth = fullDp.availableWidthPx;
+                taskHeight = fullDp.availableHeightPx;
+                float halfDividerSize = res.getDimension(R.dimen.multi_window_task_divider_size)
+                        / 2;
 
-            if (fullDp.isLandscape) {
-                taskWidth = taskWidth / 2 - halfDividerSize;
+                if (fullDp.isLandscape) {
+                    taskWidth = taskWidth / 2 - halfDividerSize;
+                } else {
+                    taskHeight = taskHeight / 2 - halfDividerSize;
+                }
             } else {
-                taskHeight = taskHeight / 2 - halfDividerSize;
+                // multiWindowStrategy == MULTI_WINDOW_STRATEGY_DEVICE_PROFILE
+                taskWidth = dp.widthPx;
+                taskHeight = dp.heightPx;
             }
             paddingHorz = res.getDimension(R.dimen.multi_window_task_card_horz_space);
         } else {
diff --git a/quickstep/src/com/android/quickstep/views/LauncherRecentsView.java b/quickstep/src/com/android/quickstep/views/LauncherRecentsView.java
index 4b4af3f..280fd46 100644
--- a/quickstep/src/com/android/quickstep/views/LauncherRecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/LauncherRecentsView.java
@@ -27,7 +27,6 @@
 import android.graphics.Canvas;
 import android.graphics.Rect;
 import android.os.Build;
-import android.support.annotation.AnyThread;
 import android.util.AttributeSet;
 import android.util.FloatProperty;
 import android.view.View;
@@ -131,9 +130,4 @@
     protected void getTaskSize(DeviceProfile dp, Rect outRect) {
         LayoutUtils.calculateLauncherTaskSize(getContext(), dp, outRect);
     }
-
-    @AnyThread
-    public static void getPageRect(DeviceProfile grid, Context context, Rect outRect) {
-        LayoutUtils.calculateLauncherTaskSize(context, grid, outRect);
-    }
 }