Make home stack move like another stack.

Previously the home stack was either positioned at the top or bottom
of the stack list. This type of movement was okay when there was mainly
2 stacks (home and app), but doesn't work in all cases where we have
multiple stacks.
For example:
1. Launch any activity in the freeform stack
2. Press the home button
3. Launch a translucent or dialog activity
In this case you would except the home stack to be seen behind the
translucent activity since that is the stack the user is coming from,
but you see the freeform stack instead since the home stack was moved
to the bottom of all stacks when the translucent activity was launched.
We no longer move the home stack to the bottom when a new
activity/task/stack comes to the front.

Bug: 23626353
Change-Id: Ic506acc8528e63b8e7f999dd88a450bee6fb4552
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index ee4dbd4..b6d5d89 100644
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -528,21 +528,15 @@
      * */
     void moveToFront(String reason, TaskRecord task) {
         if (isAttached()) {
-            final boolean homeStack = isHomeStack()
-                    || (mActivityContainer.mParentActivity != null
-                        && mActivityContainer.mParentActivity.isHomeActivity());
-            ActivityStack lastFocusStack = null;
-            if (!homeStack) {
-                // Need to move this stack to the front before calling
-                // {@link ActivityStackSupervisor#moveHomeStack} below.
-                lastFocusStack = mStacks.get(mStacks.size() - 1);
-                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.
+            final ActivityStack lastFocusStack = mStacks.get(mStacks.size() - 1);
+            // Need to move this stack to the front before calling
+            // {@link ActivityStackSupervisor#setFocusStack} below.
+            mStacks.remove(this);
+            mStacks.add(this);
+
+            // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
             if (isOnHomeDisplay()) {
-                mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
+                mStackSupervisor.setFocusStack(reason, lastFocusStack);
             }
             if (task != null) {
                 insertTaskAtTop(task, null);
@@ -4536,18 +4530,17 @@
 
         if (mTaskHistory.isEmpty()) {
             if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
-            final boolean notHomeStack = !isHomeStack();
             if (isOnHomeDisplay()) {
                 String myReason = reason + " leftTaskHistoryEmpty";
                 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
-                    mStackSupervisor.moveHomeStack(notHomeStack, myReason);
+                    mStackSupervisor.moveHomeStackToFront(myReason);
                 }
             }
             if (mStacks != null) {
                 mStacks.remove(this);
                 mStacks.add(0, this);
             }
-            if (notHomeStack) {
+            if (!isHomeStack()) {
                 mActivityContainer.onTaskListEmptyLocked();
             }
         }
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index f546c1d..7f7638b 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -464,35 +464,22 @@
         return stack == mFocusedStack;
     }
 
-    void moveHomeStack(boolean toFront, String reason) {
-        moveHomeStack(toFront, reason, null);
-    }
-
-    void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
+    void setFocusStack(String reason, ActivityStack lastFocusedStack) {
         ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
         final int topNdx = stacks.size() - 1;
         if (topNdx <= 0) {
             return;
         }
 
-        // The home stack should either be at the top or bottom of the stack list.
-        if ((toFront && (stacks.get(topNdx) != mHomeStack))
-                || (!toFront && (stacks.get(0) != mHomeStack))) {
-            if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
-                    + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
-                    + " new=" + mFocusedStack);
-            stacks.remove(mHomeStack);
-            stacks.add(toFront ? topNdx : 0, mHomeStack);
-        }
-
+        final ActivityStack topStack = stacks.get(topNdx);
+        mFocusedStack = topStack;
         if (lastFocusedStack != null) {
             mLastFocusedStack = lastFocusedStack;
         }
-        mFocusedStack = stacks.get(topNdx);
 
-        EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
-                mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
-                mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
+        EventLogTags.writeAmFocusedStack(
+                mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
+                mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
 
         if (mService.mBooting || !mService.mBooted) {
             final ActivityRecord r = topRunningActivityLocked();
@@ -502,6 +489,10 @@
         }
     }
 
+    void moveHomeStackToFront(String reason) {
+        mHomeStack.moveToFront(reason);
+    }
+
     /** Returns true if the focus activity was adjusted to the home stack top activity. */
     boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
         if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
@@ -3650,11 +3641,7 @@
         }
         final boolean homeInFront = stack.isHomeStack();
         if (stack.isOnHomeDisplay()) {
-            moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
-            TaskRecord task = stack.topTask();
-            if (task != null) {
-                mWindowManager.moveTaskToTop(task.taskId);
-            }
+            stack.moveToFront("switchUserOnHomeDisplay");
         } else {
             // Stack was moved to another display while user was swapped out.
             resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
diff --git a/services/core/java/com/android/server/am/EventLogTags.logtags b/services/core/java/com/android/server/am/EventLogTags.logtags
index 9a645df..78b5f33 100644
--- a/services/core/java/com/android/server/am/EventLogTags.logtags
+++ b/services/core/java/com/android/server/am/EventLogTags.logtags
@@ -93,8 +93,8 @@
 # Activity focused
 30043 am_focused_activity (User|1|5),(Component Name|3)
 
-# Home Stack brought to front or rear
-30044 am_home_stack_moved (User|1|5),(To Front|1|5),(Top Stack Id|1|5),(Focused Stack Id|1|5),(Reason|3)
+# Stack focus
+30044 am_focused_stack (User|1|5),(Focused Stack Id|1|5),(Last Focused Stack Id|1|5),(Reason|3)
 
 # Running pre boot receiver
 30045 am_pre_boot (User|1|5),(Package|3)