Merge "Fix crash on long press of prediction row icons" into tm-qpr-dev
diff --git a/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java b/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java
index ff612f5..a2e88f9 100644
--- a/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java
+++ b/quickstep/src/com/android/quickstep/util/TaskViewSimulator.java
@@ -191,6 +191,7 @@
         mStagePosition = mThumbnailPosition.equals(splitInfo.leftTopBounds) ?
                 STAGE_POSITION_TOP_OR_LEFT :
                 STAGE_POSITION_BOTTOM_OR_RIGHT;
+        mPositionHelper.setSplitBounds(convertSplitBounds(mSplitBounds), mStagePosition);
     }
 
     /**
@@ -418,4 +419,15 @@
         return Math.max(Math.abs(mTempPoint[0]), Math.abs(mTempPoint[1]));
     }
 
+    /**
+     * TODO(b/254378592): Remove this after consolidation of classes
+     */
+    public static com.android.wm.shell.util.SplitBounds convertSplitBounds(SplitBounds bounds) {
+        return new com.android.wm.shell.util.SplitBounds(
+                bounds.leftTopBounds,
+                bounds.rightBottomBounds,
+                bounds.leftTopTaskId,
+                bounds.rightBottomTaskId
+        );
+    }
 }
diff --git a/quickstep/src/com/android/quickstep/views/GroupedTaskView.java b/quickstep/src/com/android/quickstep/views/GroupedTaskView.java
index 71b0c60..7b9d9cd 100644
--- a/quickstep/src/com/android/quickstep/views/GroupedTaskView.java
+++ b/quickstep/src/com/android/quickstep/views/GroupedTaskView.java
@@ -19,6 +19,7 @@
 import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
 import com.android.launcher3.util.RunnableList;
+import com.android.launcher3.util.SplitConfigurationOptions;
 import com.android.launcher3.util.SplitConfigurationOptions.SplitBounds;
 import com.android.launcher3.util.TransformingTouchDelegate;
 import com.android.quickstep.RecentsModel;
@@ -26,8 +27,10 @@
 import com.android.quickstep.TaskThumbnailCache;
 import com.android.quickstep.util.CancellableTask;
 import com.android.quickstep.util.RecentsOrientedState;
+import com.android.quickstep.util.TaskViewSimulator;
 import com.android.systemui.shared.recents.model.Task;
 import com.android.systemui.shared.recents.model.ThumbnailData;
+import com.android.systemui.shared.recents.utilities.PreviewPositionHelper;
 import com.android.systemui.shared.system.InteractionJankMonitorWrapper;
 
 import java.util.HashMap;
@@ -86,9 +89,19 @@
         mTaskIdContainer[1] = secondary.key.id;
         mTaskIdAttributeContainer[1] = new TaskIdAttributeContainer(secondary, mSnapshotView2,
                 mIconView2, STAGE_POSITION_BOTTOM_OR_RIGHT);
-        mTaskIdAttributeContainer[0].setStagePosition(STAGE_POSITION_TOP_OR_LEFT);
+        mTaskIdAttributeContainer[0].setStagePosition(
+                SplitConfigurationOptions.STAGE_POSITION_TOP_OR_LEFT);
         mSnapshotView2.bind(secondary);
         mSplitBoundsConfig = splitBoundsConfig;
+        if (mSplitBoundsConfig == null) {
+            return;
+        }
+        mSnapshotView.getPreviewPositionHelper().setSplitBounds(TaskViewSimulator
+                        .convertSplitBounds(splitBoundsConfig),
+                PreviewPositionHelper.STAGE_POSITION_TOP_OR_LEFT);
+        mSnapshotView2.getPreviewPositionHelper().setSplitBounds(TaskViewSimulator
+                        .convertSplitBounds(splitBoundsConfig),
+                PreviewPositionHelper.STAGE_POSITION_BOTTOM_OR_RIGHT);
     }
 
     @Override
@@ -207,7 +220,8 @@
     @Override
     public void launchTask(@NonNull Consumer<Boolean> callback, boolean freezeTaskList) {
         getRecentsView().getSplitSelectController().launchTasks(mTask.key.id, mSecondaryTask.key.id,
-                STAGE_POSITION_TOP_OR_LEFT, callback, freezeTaskList, getSplitRatio());
+                SplitConfigurationOptions.STAGE_POSITION_TOP_OR_LEFT, callback, freezeTaskList,
+                getSplitRatio());
     }
 
     @Override
diff --git a/src/com/android/launcher3/DeviceProfile.java b/src/com/android/launcher3/DeviceProfile.java
index 7881a26..1c26f04 100644
--- a/src/com/android/launcher3/DeviceProfile.java
+++ b/src/com/android/launcher3/DeviceProfile.java
@@ -218,6 +218,9 @@
     public int overviewRowSpacing;
     public int overviewGridSideMargin;
 
+    // Split staging
+    public int splitPlaceholderInset;
+
     // Widgets
     private final ViewScaleProvider mViewScaleProvider;
 
@@ -459,6 +462,8 @@
         overviewRowSpacing = res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing);
         overviewGridSideMargin = res.getDimensionPixelSize(R.dimen.overview_grid_side_margin);
 
+        splitPlaceholderInset = res.getDimensionPixelSize(R.dimen.split_placeholder_inset);
+
         // Calculate all of the remaining variables.
         extraSpace = updateAvailableDimensions(res);
 
diff --git a/src/com/android/launcher3/touch/LandscapePagedViewHandler.java b/src/com/android/launcher3/touch/LandscapePagedViewHandler.java
index ceebc2e..0300fc9 100644
--- a/src/com/android/launcher3/touch/LandscapePagedViewHandler.java
+++ b/src/com/android/launcher3/touch/LandscapePagedViewHandler.java
@@ -424,8 +424,8 @@
         // In fake land/seascape, the placeholder always needs to go to the "top" of the device,
         // which is the same bounds as 0 rotation.
         int width = dp.widthPx;
-        int insetThickness = dp.getInsets().top;
-        out.set(0, 0, width, placeholderHeight + insetThickness);
+        int insetSizeAdjustment = getPlaceholderSizeAdjustment(dp);
+        out.set(0, 0, width, placeholderHeight + insetSizeAdjustment);
         out.inset(placeholderInset, 0);
 
         // Adjust the top to account for content off screen. This will help to animate the view in
@@ -442,13 +442,21 @@
             float onScreenRectCenterY, float fullscreenScaleX, float fullscreenScaleY,
             int drawableWidth, int drawableHeight, DeviceProfile dp,
             @StagePosition int stagePosition) {
-        float inset = dp.getInsets().top;
+        float insetAdjustment = getPlaceholderSizeAdjustment(dp) / 2f;
         out.setX(Math.round(onScreenRectCenterX / fullscreenScaleX
                 - 1.0f * drawableWidth / 2));
-        out.setY(Math.round((onScreenRectCenterY + (inset / 2f)) / fullscreenScaleY
+        out.setY(Math.round((onScreenRectCenterY + insetAdjustment) / fullscreenScaleY
                 - 1.0f * drawableHeight / 2));
     }
 
+    /**
+     * The split placeholder comes with a default inset to buffer the icon from the top of the
+     * screen. But if the device already has a large inset (from cutouts etc), use that instead.
+     */
+    private int getPlaceholderSizeAdjustment(DeviceProfile dp) {
+        return Math.max(dp.getInsets().top - dp.splitPlaceholderInset, 0);
+    }
+
     @Override
     public void setSplitInstructionsParams(View out, DeviceProfile dp, int splitInstructionsHeight,
             int splitInstructionsWidth, int threeButtonNavShift) {
diff --git a/src/com/android/launcher3/touch/PortraitPagedViewHandler.java b/src/com/android/launcher3/touch/PortraitPagedViewHandler.java
index 5efebaa..89c7cfd 100644
--- a/src/com/android/launcher3/touch/PortraitPagedViewHandler.java
+++ b/src/com/android/launcher3/touch/PortraitPagedViewHandler.java
@@ -445,13 +445,9 @@
         int screenWidth = dp.widthPx;
         int screenHeight = dp.heightPx;
         boolean pinToRight = stagePosition == STAGE_POSITION_BOTTOM_OR_RIGHT;
-        int insetThickness;
-        if (!dp.isLandscape) {
-            insetThickness = dp.getInsets().top;
-        } else {
-            insetThickness = pinToRight ? dp.getInsets().right : dp.getInsets().left;
-        }
-        out.set(0, 0, screenWidth, placeholderHeight + insetThickness);
+        int insetSizeAdjustment = getPlaceholderSizeAdjustment(dp, pinToRight);
+
+        out.set(0, 0, screenWidth, placeholderHeight + insetSizeAdjustment);
         if (!dp.isLandscape) {
             // portrait, phone or tablet - spans width of screen, nothing else to do
             out.inset(placeholderInset, 0);
@@ -496,20 +492,18 @@
             int drawableWidth, int drawableHeight, DeviceProfile dp,
             @StagePosition int stagePosition) {
         boolean pinToRight = stagePosition == STAGE_POSITION_BOTTOM_OR_RIGHT;
+        float insetAdjustment = getPlaceholderSizeAdjustment(dp, pinToRight) / 2f;
         if (!dp.isLandscape) {
-            float inset = dp.getInsets().top;
             out.setX(Math.round(onScreenRectCenterX / fullscreenScaleX
                     - 1.0f * drawableWidth / 2));
-            out.setY(Math.round((onScreenRectCenterY + (inset / 2f)) / fullscreenScaleY
+            out.setY(Math.round((onScreenRectCenterY + insetAdjustment) / fullscreenScaleY
                     - 1.0f * drawableHeight / 2));
         } else {
             if (pinToRight) {
-                float inset = dp.getInsets().right;
-                out.setX(Math.round((onScreenRectCenterX - (inset / 2f)) / fullscreenScaleX
+                out.setX(Math.round((onScreenRectCenterX - insetAdjustment) / fullscreenScaleX
                         - 1.0f * drawableWidth / 2));
             } else {
-                float inset = dp.getInsets().left;
-                out.setX(Math.round((onScreenRectCenterX + (inset / 2f)) / fullscreenScaleX
+                out.setX(Math.round((onScreenRectCenterX + insetAdjustment) / fullscreenScaleX
                         - 1.0f * drawableWidth / 2));
             }
             out.setY(Math.round(onScreenRectCenterY / fullscreenScaleY
@@ -517,6 +511,20 @@
         }
     }
 
+    /**
+     * The split placeholder comes with a default inset to buffer the icon from the top of the
+     * screen. But if the device already has a large inset (from cutouts etc), use that instead.
+     */
+    private int getPlaceholderSizeAdjustment(DeviceProfile dp, boolean pinToRight) {
+        int insetThickness;
+        if (!dp.isLandscape) {
+            insetThickness = dp.getInsets().top;
+        } else {
+            insetThickness = pinToRight ? dp.getInsets().right : dp.getInsets().left;
+        }
+        return Math.max(insetThickness - dp.splitPlaceholderInset, 0);
+    }
+
     @Override
     public void setSplitInstructionsParams(View out, DeviceProfile dp, int splitInstructionsHeight,
             int splitInstructionsWidth, int threeButtonNavShift) {
diff --git a/src/com/android/launcher3/util/SplitConfigurationOptions.java b/src/com/android/launcher3/util/SplitConfigurationOptions.java
index 88e1b22..3eff783 100644
--- a/src/com/android/launcher3/util/SplitConfigurationOptions.java
+++ b/src/com/android/launcher3/util/SplitConfigurationOptions.java
@@ -100,6 +100,7 @@
      * with the same name/functionality in wm.shell.util (which launcher3 cannot be built against)
      *
      * If you make changes here, consider making the same changes there
+     * TODO(b/254378592): We really need to consolidate this
      */
     public static class SplitBounds {
         public final Rect leftTopBounds;