Remove duplicate move of home stack that can cause animation jank.

Each move of the home stack in the ActivityManager ends up calling
into the WindowManager which causes layout to be performed.

Also, fixed issue where ActivityStack.moveTaskToFrontLocked()
could move a task to the front without the focus been adjusted to
the new top activity.

Bug: 19692494
Change-Id: Ib4c999c6dfa1af3fda0fced52b58da614b154d00
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index 8a7e3d3..62c18a8 100644
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -507,6 +507,8 @@
                 mStacks.remove(this);
                 mStacks.add(this);
             }
+            // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
+            // Needs to also work if focus is moving to the non-home display.
             if (isOnHomeDisplay()) {
                 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
             }
@@ -2570,7 +2572,11 @@
                     }
                     // Move the home stack to the top if this stack is fullscreen or there is no
                     // other visible stack.
-                    mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), myReason);
+                    if (mStackSupervisor.moveHomeStackTaskToTop(
+                            task.getTaskToReturnTo(), myReason)) {
+                        // Activity focus was already adjusted. Nothing else to do...
+                        return;
+                    }
                 }
             }
 
@@ -3572,7 +3578,6 @@
                 mTaskHistory.remove(taskNdx);
                 mTaskHistory.add(top, task);
                 updateTaskMovement(task, true);
-                mWindowManager.moveTaskToTop(task.taskId);
                 return;
             }
         }
@@ -3597,12 +3602,14 @@
         // Shift all activities with this task up to the top
         // of the stack, keeping them in the same internal order.
         insertTaskAtTop(tr);
-        moveToFront(reason);
+
+        // Set focus to the top running activity of this stack.
+        ActivityRecord r = topRunningActivityLocked(null);
+        mService.setFocusedActivityLocked(r, reason);
 
         if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
         if (noAnimation) {
             mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
-            ActivityRecord r = topRunningActivityLocked(null);
             if (r != null) {
                 mNoAnimActivities.add(r);
             }