Always position home stack either at the top or bottom of the list.

The current implementation was okay since we mostly had
2 stacks (the home stack and 1 application stack), so moving the
application stack to the top meant the home stack was automatically
moved to the bottom. This is no longer the case if you have multiple
application stacks.

Change-Id: I8ec7cfdc3650a73a00c4e8dad869fb2bec2a4eac
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 80664f5..8782950 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -417,13 +417,15 @@
         if (topNdx <= 0) {
             return;
         }
-        ActivityStack topStack = stacks.get(topNdx);
-        final boolean homeInFront = topStack == mHomeStack;
-        if (homeInFront != toFront) {
+
+        // 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, "moveHomeTask: topStack old="
+                    + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
+                    + " new=" + mFocusedStack);
             stacks.remove(mHomeStack);
             stacks.add(toFront ? topNdx : 0, mHomeStack);
-            if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
-                    + mFocusedStack);
         }
 
         if (lastFocusedStack != null) {