Convert API refs to StackBox from ActivityStack.

- Removed IActivityManager.getStacks() since getStackBoxes() is better.
- Made createStacks operate relative to StackBox instead of TaskStack.
- Made resizeStack into resizeStackBox.

Change-Id: I7a0e1f4e34f399b4fd1180c60cc3989f9c2433f3
diff --git a/services/java/com/android/server/wm/DisplayContent.java b/services/java/com/android/server/wm/DisplayContent.java
index 8ad2ef1..0647296 100644
--- a/services/java/com/android/server/wm/DisplayContent.java
+++ b/services/java/com/android/server/wm/DisplayContent.java
@@ -203,11 +203,11 @@
     }
 
     /** Refer to {@link WindowManagerService#createStack(int, int, int, float)} */
-    TaskStack createStack(WindowManagerService service, int stackId, int relativeStackId,
+    TaskStack createStack(WindowManagerService service, int stackId, int relativeStackBoxId,
             int position, float weight) {
         TaskStack newStack = null;
-        if (DEBUG_STACK) Slog.d(TAG, "createStack: stackId=" + stackId + " relativeStackId="
-                + relativeStackId + " position=" + position + " weight=" + weight);
+        if (DEBUG_STACK) Slog.d(TAG, "createStack: stackId=" + stackId + " relativeStackBoxId="
+                + relativeStackBoxId + " position=" + position + " weight=" + weight);
         if (mStackBoxes.isEmpty()) {
             if (stackId != HOME_STACK_ID) {
                 throw new IllegalArgumentException("createStack: First stackId not "
@@ -226,7 +226,7 @@
                 if (position == StackBox.TASK_STACK_GOES_OVER
                         || position == StackBox.TASK_STACK_GOES_UNDER) {
                     // Position indicates a new box is added at top level only.
-                    if (box.contains(relativeStackId)) {
+                    if (box.contains(relativeStackBoxId)) {
                         StackBox newBox = new StackBox(service, this, null);
                         newStack = new TaskStack(service, stackId, this);
                         newStack.mStackBox = newBox;
@@ -239,14 +239,14 @@
                     }
                 } else {
                     // Remaining position values indicate a box must be split.
-                    newStack = box.split(stackId, relativeStackId, position, weight);
+                    newStack = box.split(stackId, relativeStackBoxId, position, weight);
                     if (newStack != null) {
                         break;
                     }
                 }
             }
             if (stackBoxNdx < 0) {
-                throw new IllegalArgumentException("createStack: stackId " + relativeStackId
+                throw new IllegalArgumentException("createStack: stackBoxId " + relativeStackBoxId
                         + " not found.");
             }
         }
@@ -256,11 +256,12 @@
         return newStack;
     }
 
-    /** Refer to {@link WindowManagerService#resizeStack(int, float)} */
-    boolean resizeStack(int stackId, float weight) {
+    /** Refer to {@link WindowManagerService#resizeStackBox(int, float)} */
+    boolean resizeStack(int stackBoxId, float weight) {
         for (int stackBoxNdx = mStackBoxes.size() - 1; stackBoxNdx >= 0; --stackBoxNdx) {
             final StackBox box = mStackBoxes.get(stackBoxNdx);
-            if (box.resize(stackId, weight)) {
+            if (box.resize(stackBoxId, weight)) {
+                layoutNeeded = true;
                 return true;
             }
         }
diff --git a/services/java/com/android/server/wm/StackBox.java b/services/java/com/android/server/wm/StackBox.java
index e2fd105e..15f5dff 100644
--- a/services/java/com/android/server/wm/StackBox.java
+++ b/services/java/com/android/server/wm/StackBox.java
@@ -26,18 +26,22 @@
 import java.io.PrintWriter;
 
 public class StackBox {
-    /** Used with {@link WindowManagerService#createStack}. To left of, lower l/r Rect values. */
+    /** Used with {@link WindowManagerService#createStack}. Dependent on Configuration LTR/RTL. */
     public static final int TASK_STACK_GOES_BEFORE = 0;
-    /** Used with {@link WindowManagerService#createStack}. To right of, higher l/r Rect values. */
+    /** Used with {@link WindowManagerService#createStack}. Dependent on Configuration LTR/RTL. */
     public static final int TASK_STACK_GOES_AFTER = 1;
+    /** Used with {@link WindowManagerService#createStack}. Horizontal to left of. */
+    public static final int TASK_STACK_TO_LEFT_OF = 2;
+    /** Used with {@link WindowManagerService#createStack}. Horizontal to right of. */
+    public static final int TASK_STACK_TO_RIGHT_OF = 3;
     /** Used with {@link WindowManagerService#createStack}. Vertical: lower t/b Rect values. */
-    public static final int TASK_STACK_GOES_ABOVE = 2;
+    public static final int TASK_STACK_GOES_ABOVE = 4;
     /** Used with {@link WindowManagerService#createStack}. Vertical: higher t/b Rect values. */
-    public static final int TASK_STACK_GOES_BELOW = 3;
+    public static final int TASK_STACK_GOES_BELOW = 5;
     /** Used with {@link WindowManagerService#createStack}. Put on a higher layer on display. */
-    public static final int TASK_STACK_GOES_OVER = 4;
+    public static final int TASK_STACK_GOES_OVER = 6;
     /** Used with {@link WindowManagerService#createStack}. Put on a lower layer on display. */
-    public static final int TASK_STACK_GOES_UNDER = 5;
+    public static final int TASK_STACK_GOES_UNDER = 7;
 
     static int sCurrentBoxId = 0;
 
@@ -97,15 +101,13 @@
     }
 
     /**
-     * Determine if a particular TaskStack is in this StackBox or any of its descendants.
-     * @param stackId The TaskStack being considered.
-     * @return true if the specified TaskStack is in this box or its descendants. False otherwise.
+     * Determine if a particular StackBox is this one or a descendant of this one.
+     * @param stackBoxId The StackBox being searched for.
+     * @return true if the specified StackBox matches this or one of its descendants.
      */
-    boolean contains(int stackId) {
-        if (mStack != null) {
-            return mStack.mStackId == stackId;
-        }
-        return mFirst.contains(stackId) || mSecond.contains(stackId);
+    boolean contains(int stackBoxId) {
+        return mStackBoxId == stackBoxId || mFirst.contains(stackBoxId)
+                || mSecond.contains(stackBoxId);
     }
 
     /**
@@ -155,37 +157,42 @@
      * the specified TaskStack into two children. The size and position each of the new StackBoxes
      * is determined by the passed parameters.
      * @param stackId The id of the new TaskStack to create.
-     * @param relativeStackId The id of the TaskStack to place the new one next to.
+     * @param relativeStackBoxId The id of the StackBox to place the new TaskStack next to.
      * @param position One of the static TASK_STACK_GOES_xxx positions defined in this class.
      * @param weight The percentage size of the parent StackBox to devote to the new TaskStack.
      * @return The new TaskStack.
      */
-    TaskStack split(int stackId, int relativeStackId, int position, float weight) {
-        if (mStack == null) {
-            // Propagate the split to see if the target task stack is in either sub box.
-            TaskStack stack = mFirst.split(stackId, relativeStackId, position, weight);
+    TaskStack split(int stackId, int relativeStackBoxId, int position, float weight) {
+        if (mStackBoxId != relativeStackBoxId) {
+            // This is not the targeted StackBox.
+            if (mStack != null) {
+                return null;
+            }
+            // Propagate the split to see if the targeted StackBox is in either sub box.
+            TaskStack stack = mFirst.split(stackId, relativeStackBoxId, position, weight);
             if (stack != null) {
                 return stack;
             }
-            return mSecond.split(stackId, relativeStackId, position, weight);
-        }
-
-        // This StackBox contains just a TaskStack.
-        if (mStack.mStackId != relativeStackId) {
-            // Barking down the wrong stack.
-            return null;
+            return mSecond.split(stackId, relativeStackBoxId, position, weight);
         }
 
         // Found it!
         TaskStack stack = new TaskStack(mService, stackId, mDisplayContent);
         TaskStack firstStack;
         TaskStack secondStack;
+        if (position == TASK_STACK_GOES_BEFORE) {
+            // TODO: Test Configuration here for LTR/RTL.
+            position = TASK_STACK_TO_LEFT_OF;
+        } else if (position == TASK_STACK_GOES_AFTER) {
+            // TODO: Test Configuration here for LTR/RTL.
+            position = TASK_STACK_TO_RIGHT_OF;
+        }
         switch (position) {
             default:
-            case TASK_STACK_GOES_AFTER:
-            case TASK_STACK_GOES_BEFORE:
+            case TASK_STACK_TO_LEFT_OF:
+            case TASK_STACK_TO_RIGHT_OF:
                 mVertical = false;
-                if (position == TASK_STACK_GOES_BEFORE) {
+                if (position == TASK_STACK_TO_LEFT_OF) {
                     mWeight = weight;
                     firstStack = stack;
                     secondStack = mStack;
@@ -265,15 +272,16 @@
         return sibling.getStackId();
     }
 
-    boolean resize(int stackId, float weight) {
-        if (mStack == null) {
-            return mFirst.resize(stackId, weight) || mSecond.resize(stackId, weight);
+    boolean resize(int stackBoxId, float weight) {
+        if (mStackBoxId != stackBoxId) {
+            return mStack == null &&
+                    (mFirst.resize(stackBoxId, weight) || mSecond.resize(stackBoxId, weight));
         }
-        if (mStack.mStackId == stackId) {
+        // Don't change weight on topmost stack.
+        if (mParent != null) {
             mParent.mWeight = isFirstChild() ? weight : 1.0f - weight;
-            return true;
         }
-        return false;
+        return true;
     }
 
     /** If this is a terminal StackBox (contains a TaskStack) set the bounds.
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index d19fb2d..7c884c1 100644
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -4782,7 +4782,7 @@
     /**
      * Create a new TaskStack and place it next to an existing stack.
      * @param stackId The unique identifier of the new stack.
-     * @param relativeStackId The existing stack that this stack goes before or after.
+     * @param relativeStackBoxId The existing stack that this stack goes before or after.
      * @param position One of:
      *      {@link StackBox#TASK_STACK_GOES_BEFORE}
      *      {@link StackBox#TASK_STACK_GOES_AFTER}
@@ -4792,7 +4792,7 @@
      *      {@link StackBox#TASK_STACK_GOES_OVER}
      * @param weight Relative weight for determining how big to make the new TaskStack.
      */
-    public void createStack(int stackId, int relativeStackId, int position, float weight) {
+    public void createStack(int stackId, int relativeStackBoxId, int position, float weight) {
         synchronized (mWindowMap) {
             if (position <= StackBox.TASK_STACK_GOES_BELOW &&
                     (weight < STACK_WEIGHT_MIN || weight > STACK_WEIGHT_MAX)) {
@@ -4800,23 +4800,19 @@
                         "createStack: weight must be between " + STACK_WEIGHT_MIN + " and " +
                         STACK_WEIGHT_MAX + ", weight=" + weight);
             }
-            final DisplayContent displayContent;
-            if (stackId != HOME_STACK_ID) {
-                // TODO: What to do for the first stack on a non-default display?
-                final TaskStack relativeStack = mStackIdToStack.get(relativeStackId);
-                if (relativeStack == null) {
-                    throw new IllegalArgumentException("createStack: Invalid relativeStackId=" +
-                            relativeStackId);
+            DisplayContentsIterator iterator = new DisplayContentsIterator();
+            while (iterator.hasNext()) {
+                final DisplayContent displayContent = iterator.next();
+                TaskStack stack = displayContent.createStack(this, stackId, relativeStackBoxId,
+                        position, weight);
+                if (stack != null) {
+                    mStackIdToStack.put(stackId, stack);
+                    displayContent.moveStack(stack, true);
+                    performLayoutAndPlaceSurfacesLocked();
+                    return;
                 }
-                displayContent = relativeStack.getDisplayContent();
-            } else {
-                displayContent = getDefaultDisplayContentLocked();
             }
-            TaskStack stack =
-                    displayContent.createStack(this, stackId, relativeStackId, position, weight);
-            mStackIdToStack.put(stackId, stack);
-            displayContent.moveStack(stack, true);
-            performLayoutAndPlaceSurfacesLocked();
+            Slog.e(TAG, "createStack: Unable to find relativeStackBoxId=" + relativeStackBoxId);
         }
     }
 
@@ -4864,7 +4860,7 @@
         }
     }
 
-    public void resizeStack(int stackId, float weight) {
+    public void resizeStackBox(int stackBoxId, float weight) {
         if (weight < STACK_WEIGHT_MIN || weight > STACK_WEIGHT_MAX) {
             throw new IllegalArgumentException(
                     "resizeStack: weight must be between " + STACK_WEIGHT_MIN + " and " +
@@ -4873,14 +4869,14 @@
         synchronized (mWindowMap) {
             DisplayContentsIterator iterator = new DisplayContentsIterator();
             while (iterator.hasNext()) {
-                if (iterator.next().resizeStack(stackId, weight)) {
-                    break;
-                } else if (!iterator.hasNext()) {
-                    throw new IllegalArgumentException("resizeStack: stackId " + stackId
-                            + " not found.");
+                if (iterator.next().resizeStack(stackBoxId, weight)) {
+                    performLayoutAndPlaceSurfacesLocked();
+                    return;
                 }
             }
         }
+        throw new IllegalArgumentException("resizeStack: stackBoxId " + stackBoxId
+                + " not found.");
     }
 
     public ArrayList<StackBoxInfo> getStackBoxInfos() {