Fix a couple of drag and drop issues from transient taskbar

- Don't stash until drag ends, but immediately stash at that point
  (regardless of success or failure, though failure will animate to the
  original icon before stashing)
- Send transient taskbar's bounds to WM Shell via intent extra such that
  they ignore drag events in that region

Test: manual in persistent and transient taskbar
Bug: 269814838
Fixes: 268526633
Fixes: 259645384
Change-Id: I5ded3998046f259ed6e79cb4ed765ad7b0c72e45
diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarBackgroundRenderer.kt b/quickstep/src/com/android/launcher3/taskbar/TaskbarBackgroundRenderer.kt
index c03c916..37d9090 100644
--- a/quickstep/src/com/android/launcher3/taskbar/TaskbarBackgroundRenderer.kt
+++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarBackgroundRenderer.kt
@@ -20,6 +20,7 @@
 import android.graphics.Color
 import android.graphics.Paint
 import android.graphics.Path
+import android.graphics.RectF
 import com.android.launcher3.R
 import com.android.launcher3.Utilities.mapRange
 import com.android.launcher3.Utilities.mapToRange
@@ -30,7 +31,8 @@
 /** Helps draw the taskbar background, made up of a rectangle plus two inverted rounded corners. */
 class TaskbarBackgroundRenderer(context: TaskbarActivityContext) {
 
-    val paint: Paint = Paint()
+    val paint = Paint()
+    val lastDrawnTransientRect = RectF()
     var backgroundHeight = context.deviceProfile.taskbarSize.toFloat()
     var translationYForSwipe = 0f
 
@@ -131,7 +133,11 @@
             val radius = newBackgroundHeight / 2f
             val bottomMarginProgress = bottomMargin * ((1f - progress) / 2f)
 
-            canvas.translate(0f, canvas.height - bottomMargin + bottomMarginProgress)
+            // Aligns the bottom with the bottom of the stashed handle.
+            val bottom =
+                canvas.height - bottomMargin +
+                    bottomMarginProgress +
+                    (-mapRange(1f - progress, 0f, stashedHandleHeight / 2f) + translationYForSwipe)
 
             // Draw shadow.
             val shadowAlpha =
@@ -143,19 +149,14 @@
                 setColorAlphaBound(Color.BLACK, Math.round(shadowAlpha))
             )
 
-            // Aligns the bottom with the bottom of the stashed handle.
-            val bottom =
-                (-mapRange(1f - progress, 0f, stashedHandleHeight / 2f) + translationYForSwipe)
-
-            canvas.drawRoundRect(
+            lastDrawnTransientRect.set(
                 transientBackgroundBounds.left + halfWidthDelta,
                 bottom - newBackgroundHeight,
                 transientBackgroundBounds.right - halfWidthDelta,
-                bottom,
-                radius,
-                radius,
-                paint
+                bottom
             )
+
+            canvas.drawRoundRect(lastDrawnTransientRect, radius, radius, paint)
         }
         canvas.restore()
     }
diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java
index d1fea7b..45df9d6 100644
--- a/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java
+++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java
@@ -33,6 +33,7 @@
 import android.graphics.Canvas;
 import android.graphics.Point;
 import android.graphics.Rect;
+import android.graphics.RectF;
 import android.graphics.drawable.Drawable;
 import android.os.UserHandle;
 import android.util.Pair;
@@ -69,11 +70,13 @@
 import com.android.launcher3.shortcuts.ShortcutDragPreviewProvider;
 import com.android.launcher3.testing.TestLogging;
 import com.android.launcher3.testing.shared.TestProtocol;
+import com.android.launcher3.util.DisplayController;
 import com.android.launcher3.util.IntSet;
 import com.android.launcher3.util.ItemInfoMatcher;
 import com.android.quickstep.util.LogUtils;
 import com.android.quickstep.util.MultiValueUpdateListener;
 import com.android.systemui.shared.recents.model.Task;
+import com.android.wm.shell.draganddrop.DragAndDropConstants;
 
 import java.io.PrintWriter;
 import java.util.Arrays;
@@ -310,9 +313,6 @@
         if (mDisallowGlobalDrag) {
             AbstractFloatingView.closeAllOpenViewsExcept(mActivity, TYPE_TASKBAR_ALL_APPS);
         } else {
-            // stash the transient taskbar
-            mControllers.taskbarStashController.updateAndAnimateTransientTaskbar(true);
-
             AbstractFloatingView.closeAllOpenViews(mActivity);
         }
 
@@ -395,6 +395,15 @@
             com.android.launcher3.logging.InstanceId launcherInstanceId = instanceIds.second;
 
             intent.putExtra(ClipDescription.EXTRA_LOGGING_INSTANCE_ID, internalInstanceId);
+            if (DisplayController.isTransientTaskbar(mActivity)) {
+                // Tell WM Shell to ignore drag events in the provided transient taskbar region.
+                TaskbarDragLayer dragLayer = mControllers.taskbarActivityContext.getDragLayer();
+                int[] locationOnScreen = dragLayer.getLocationOnScreen();
+                RectF disallowExternalDropRegion = new RectF(dragLayer.getLastDrawnTransientRect());
+                disallowExternalDropRegion.offset(locationOnScreen[0], locationOnScreen[1]);
+                intent.putExtra(DragAndDropConstants.EXTRA_DISALLOW_HIT_REGION,
+                        disallowExternalDropRegion);
+            }
 
             ClipData clipData = new ClipData(clipDescription, new ClipData.Item(intent));
             if (btv.startDragAndDrop(clipData, shadowBuilder, null /* localState */,
@@ -421,9 +430,6 @@
                     if (dragEvent.getResult()) {
                         maybeOnDragEnd();
                     } else {
-                        // un-stash the transient taskbar in case drag and drop was canceled
-                        mControllers.taskbarStashController.updateAndAnimateTransientTaskbar(false);
-
                         // This will take care of calling maybeOnDragEnd() after the animation
                         animateGlobalDragViewToOriginalPosition(btv, dragEvent);
                     }
@@ -451,6 +457,9 @@
             mControllers.taskbarAutohideSuspendController.updateFlag(
                     TaskbarAutohideSuspendController.FLAG_AUTOHIDE_SUSPEND_DRAGGING, false);
             mActivity.onDragEnd();
+            // Note, this must be done last to ensure no AutohideSuspendFlags are active, as that
+            // will prevent us from stashing until the timeout.
+            mControllers.taskbarStashController.updateAndAnimateTransientTaskbar(true);
         }
     }
 
diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarDragLayer.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarDragLayer.java
index 7114849..58d6244 100644
--- a/quickstep/src/com/android/launcher3/taskbar/TaskbarDragLayer.java
+++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarDragLayer.java
@@ -20,6 +20,7 @@
 
 import android.content.Context;
 import android.graphics.Canvas;
+import android.graphics.RectF;
 import android.util.AttributeSet;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
@@ -183,6 +184,11 @@
         invalidate();
     }
 
+    /** Returns the bounds in DragLayer coordinates of where the transient background was drawn. */
+    protected RectF getLastDrawnTransientRect() {
+        return mBackgroundRenderer.getLastDrawnTransientRect();
+    }
+
     @Override
     public boolean dispatchTouchEvent(MotionEvent ev) {
         TestLogging.recordMotionEvent(TestProtocol.SEQUENCE_MAIN, "Touch event", ev);