Fixing some drag and drop issues.

- When opening recents while there is a task docked, initialize the
  stack scroll to the front of the stack
- When undocking a task while recents is open, scroll to the front of
  the stack
- Fixing offset when adjusting the task stack size due to top inset 
  being double applied in some dock states
- Fixing issue with task view not scaling when picking up from the 
  freeform workspace

Change-Id: I15436ac21f05ff521492d8ae1c682a414503101a
diff --git a/packages/SystemUI/src/com/android/systemui/recents/events/activity/TaskStackUpdatedEvent.java b/packages/SystemUI/src/com/android/systemui/recents/events/activity/TaskStackUpdatedEvent.java
index 7579cd8..f87f6de 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/events/activity/TaskStackUpdatedEvent.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/events/activity/TaskStackUpdatedEvent.java
@@ -22,7 +22,7 @@
 /**
  * This is sent by the activity whenever the task stach has changed.
  */
-public class TaskStackUpdatedEvent extends EventBus.Event {
+public class TaskStackUpdatedEvent extends EventBus.AnimatedEvent {
 
     /**
      * A new TaskStack instance representing the latest stack state.
diff --git a/packages/SystemUI/src/com/android/systemui/recents/events/ui/dragndrop/DragStartInitializeDropTargetsEvent.java b/packages/SystemUI/src/com/android/systemui/recents/events/ui/dragndrop/DragStartInitializeDropTargetsEvent.java
index b450b1f..7030729 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/events/ui/dragndrop/DragStartInitializeDropTargetsEvent.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/events/ui/dragndrop/DragStartInitializeDropTargetsEvent.java
@@ -19,6 +19,7 @@
 import com.android.systemui.recents.events.EventBus;
 import com.android.systemui.recents.model.Task;
 import com.android.systemui.recents.views.RecentsViewTouchHandler;
+import com.android.systemui.recents.views.TaskView;
 
 /**
  * This event is sent by the drag manager when it requires drop targets to register themselves for
@@ -27,10 +28,13 @@
 public class DragStartInitializeDropTargetsEvent extends EventBus.Event {
 
     public final Task task;
+    public final TaskView taskView;
     public final RecentsViewTouchHandler handler;
 
-    public DragStartInitializeDropTargetsEvent(Task task, RecentsViewTouchHandler handler) {
+    public DragStartInitializeDropTargetsEvent(Task task, TaskView taskView,
+            RecentsViewTouchHandler handler) {
         this.task = task;
+        this.taskView = taskView;
         this.handler = handler;
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java b/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java
index 9bf48bb..aa8efa7 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java
@@ -400,7 +400,12 @@
             // Calculate the task stack bounds from the new window bounds
             Rect searchBarSpaceBounds = new Rect();
             Rect taskStackBounds = new Rect();
-            config.getTaskStackBounds(newWindowBounds, insets.top, insets.right,
+            // If the task stack bounds is specifically under the dock area, then ignore the top
+            // inset
+            int top = dockArea.bottom < 1f
+                    ? 0
+                    : insets.top;
+            config.getTaskStackBounds(newWindowBounds, top, insets.right,
                     searchBarSpaceBounds, taskStackBounds);
             return taskStackBounds;
         }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/tv/RecentsTvActivity.java b/packages/SystemUI/src/com/android/systemui/recents/tv/RecentsTvActivity.java
index b4ee42b..fb86214 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/tv/RecentsTvActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/tv/RecentsTvActivity.java
@@ -320,22 +320,6 @@
     }
 
     @Override
-    public void onMultiWindowModeChanged(boolean multiWindowMode) {
-        super.onMultiWindowModeChanged(multiWindowMode);
-        if (!multiWindowMode) {
-            RecentsTaskLoader loader = Recents.getTaskLoader();
-            RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
-            launchOpts.loadIcons = false;
-            launchOpts.loadThumbnails = false;
-            launchOpts.onlyLoadForCache = true;
-            RecentsTaskLoadPlan loadPlan = loader.createLoadPlan(this);
-            loader.preloadTasks(loadPlan, -1, false);
-            loader.loadTasks(this, loadPlan, launchOpts);
-            EventBus.getDefault().send(new TaskStackUpdatedEvent(loadPlan.getTaskStack()));
-        }
-    }
-
-    @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         switch (keyCode) {
             case KeyEvent.KEYCODE_DPAD_UP: {
diff --git a/packages/SystemUI/src/com/android/systemui/recents/tv/views/RecentsTvView.java b/packages/SystemUI/src/com/android/systemui/recents/tv/views/RecentsTvView.java
index 9193114..b175855 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/tv/views/RecentsTvView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/tv/views/RecentsTvView.java
@@ -227,12 +227,6 @@
         EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
     }
 
-    public final void onBusEvent(TaskStackUpdatedEvent event) {
-        mStack.setTasks(event.stack.computeAllTasksList(), true /* notifyStackChanges */);
-        mStack.createAffiliatedGroupings(getContext());
-    }
-
-
     public final void onBusEvent(RecentsVisibilityChangedEvent event) {
         if (!event.visible) {
             // Reset the view state
@@ -240,7 +234,6 @@
         }
     }
 
-
     public void setTaskStackViewAdapter(TaskStackHorizontalViewAdapter taskStackViewAdapter) {
         if(mTaskStackHorizontalView != null) {
             mTaskStackHorizontalView.setAdapter(taskStackViewAdapter);
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsViewTouchHandler.java b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsViewTouchHandler.java
index 288fcab..346ce16 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsViewTouchHandler.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsViewTouchHandler.java
@@ -150,7 +150,8 @@
         }
 
         // Request other drop targets to register themselves
-        EventBus.getDefault().send(new DragStartInitializeDropTargetsEvent(event.task, this));
+        EventBus.getDefault().send(new DragStartInitializeDropTargetsEvent(event.task,
+                event.taskView, this));
     }
 
     public final void onBusEvent(DragEndEvent event) {
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
index 264cede..7079ff44 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
@@ -59,6 +59,7 @@
 import com.android.systemui.recents.events.activity.PackagesChangedEvent;
 import com.android.systemui.recents.events.activity.ShowHistoryButtonEvent;
 import com.android.systemui.recents.events.activity.ShowHistoryEvent;
+import com.android.systemui.recents.events.activity.TaskStackUpdatedEvent;
 import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
 import com.android.systemui.recents.events.ui.AllTaskViewsDismissedEvent;
 import com.android.systemui.recents.events.ui.DeleteTaskDataEvent;
@@ -142,6 +143,7 @@
     Rect mStableStackBounds = new Rect();
     // The current stack bounds are dynamic and may change as the user drags and drops
     Rect mStackBounds = new Rect();
+
     int[] mTmpVisibleRange = new int[2];
     Rect mTmpRect = new Rect();
     ArrayMap<Task.TaskKey, TaskView> mTmpTaskViewMap = new ArrayMap<>();
@@ -860,8 +862,7 @@
                 // TODO: Center the newly focused task view, only if not freeform
                 float newScroll = mLayoutAlgorithm.getStackScrollForTask(newFocusedTask);
                 if (Float.compare(newScroll, mStackScroller.getStackScroll()) != 0) {
-                    mStackScroller.animateScroll(mStackScroller.getStackScroll(), newScroll,
-                            focusTaskRunnable);
+                    mStackScroller.animateScroll(newScroll, focusTaskRunnable);
                     willScroll = true;
                 } else {
                     focusTaskRunnable.run();
@@ -1586,8 +1587,7 @@
     public final void onBusEvent(DragStartEvent event) {
         if (event.task.isFreeformTask()) {
             // Animate to the front of the stack
-            mStackScroller.animateScroll(mStackScroller.getStackScroll(),
-                    mLayoutAlgorithm.mInitialScrollP, null);
+            mStackScroller.animateScroll(mLayoutAlgorithm.mInitialScrollP, null);
         }
 
         // Enlarge the dragged view slightly
@@ -1613,7 +1613,6 @@
         if (event.dropTarget instanceof TaskStack.DockState) {
             // Calculate the new task stack bounds that matches the window size that Recents will
             // have after the drop
-            addIgnoreTask(event.task);
             final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
             mStackBounds.set(dockState.getDockedTaskStackBounds(getMeasuredWidth(),
                     getMeasuredHeight(), mDividerSize, mLayoutAlgorithm.mSystemInsets,
@@ -1775,6 +1774,17 @@
         mAnimationHelper.startHideHistoryAnimation();
     }
 
+    public final void onBusEvent(TaskStackUpdatedEvent event) {
+        // Scroll the stack to the front after it has been updated
+        event.addPostAnimationCallback(new Runnable() {
+            @Override
+            public void run() {
+                mStackScroller.animateScroll(mLayoutAlgorithm.mMaxScrollP,
+                        null /* postScrollRunnable */);
+            }
+        });
+    }
+
     /**
      * Removes the task from the stack, and updates the focus to the next task in the stack if the
      * removed TaskView was focused.
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackViewScroller.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackViewScroller.java
index 7884bd3..ced5d4b 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackViewScroller.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackViewScroller.java
@@ -27,6 +27,8 @@
 
 import com.android.systemui.Interpolators;
 import com.android.systemui.R;
+import com.android.systemui.recents.Recents;
+import com.android.systemui.recents.misc.SystemServicesProxy;
 import com.android.systemui.recents.misc.Utilities;
 
 /* The scrolling logic for a TaskStackView */
@@ -111,8 +113,13 @@
      * @return whether the stack progress changed.
      */
     public boolean setStackScrollToInitialState() {
+        SystemServicesProxy ssp = Recents.getSystemServices();
         float prevStackScrollP = mStackScrollP;
-        setStackScroll(getBoundedStackScroll(mLayoutAlgorithm.mInitialScrollP));
+        if (ssp.hasDockedTask()) {
+            setStackScroll(mLayoutAlgorithm.mMaxScrollP);
+        } else {
+            setStackScroll(mLayoutAlgorithm.mInitialScrollP);
+        }
         return Float.compare(prevStackScrollP, mStackScrollP) != 0;
     }
 
@@ -169,13 +176,13 @@
         float newScroll = getBoundedStackScroll(curScroll);
         if (Float.compare(newScroll, curScroll) != 0) {
             // Start a new scroll animation
-            animateScroll(curScroll, newScroll, null);
+            animateScroll(newScroll, null /* postScrollRunnable */);
         }
         return mScrollAnimator;
     }
 
     /** Animates the stack scroll */
-    void animateScroll(float curScroll, float newScroll, final Runnable postRunnable) {
+    void animateScroll(float newScroll, final Runnable postRunnable) {
         // Finish any current scrolling animations
         if (mScrollAnimator != null && mScrollAnimator.isRunning()) {
             setStackScroll(mFinalAnimatedScroll);
@@ -185,7 +192,7 @@
         stopBoundScrollAnimation();
 
         mFinalAnimatedScroll = newScroll;
-        mScrollAnimator = ObjectAnimator.ofFloat(this, STACK_SCROLL, curScroll, newScroll);
+        mScrollAnimator = ObjectAnimator.ofFloat(this, STACK_SCROLL, getStackScroll(), newScroll);
         mScrollAnimator.setDuration(mContext.getResources().getInteger(
                 R.integer.recents_animate_task_stack_scroll_duration));
         mScrollAnimator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);