Add windows to window tokens in expected z-order

Decouple the logic for adding window to a position in the parent
window token from the position we are adding the window to in the
window list. The window token now adds the windows in order based
on the rules the rest of the system is using which makes the code
a little more straightforward to follow.

Test: bit FrameworksServicesTests:com.android.server.wm.AppWindowTokenTests
Change-Id: Ic9b724fba02279a0c4e92508d39e5e35171b6d8d
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index d46b535..a3f7ac6 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -676,6 +676,34 @@
         mPendingRelaunchCount = 0;
     }
 
+    /**
+     * Returns true if the new child window we are adding to this token is considered greater than
+     * the existing child window in this token in terms of z-order.
+     */
+    @Override
+    protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
+            WindowState existingWindow) {
+        final int type1 = newWindow.mAttrs.type;
+        final int type2 = existingWindow.mAttrs.type;
+
+        // Base application windows should be z-ordered BELOW all other windows in the app token.
+        if (type1 == TYPE_BASE_APPLICATION && type2 != TYPE_BASE_APPLICATION) {
+            return false;
+        } else if (type1 != TYPE_BASE_APPLICATION && type2 == TYPE_BASE_APPLICATION) {
+            return true;
+        }
+
+        // Starting windows should be z-ordered ABOVE all other windows in the app token.
+        if (type1 == TYPE_APPLICATION_STARTING && type2 != TYPE_APPLICATION_STARTING) {
+            return true;
+        } else if (type1 != TYPE_APPLICATION_STARTING && type2 == TYPE_APPLICATION_STARTING) {
+            return false;
+        }
+
+        // Otherwise the new window is greater than the existing window.
+        return true;
+    }
+
     @Override
     void addWindow(WindowState w) {
         super.addWindow(w);
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index 7cd9971..0b39d65 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -1085,12 +1085,13 @@
         return null;
     }
 
-    int addAppWindowToWindowList(final WindowState win) {
+    void addAppWindowToWindowList(final WindowState win) {
         final IWindow client = win.mClient;
 
         WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
         if (!tokenWindowList.isEmpty()) {
-            return addAppWindowExisting(win, tokenWindowList);
+            addAppWindowExisting(win, tokenWindowList);
+            return;
         }
 
         // No windows from this token on this display
@@ -1128,7 +1129,7 @@
                 }
             }
             addWindowToListBefore(win, pos);
-            return 0;
+            return;
         }
 
         // Continue looking down until we find the first token that has windows on this display.
@@ -1154,7 +1155,7 @@
                 }
             }
             addWindowToListAfter(win, pos);
-            return 0;
+            return;
         }
 
         // Just search for the start of this layer.
@@ -1175,7 +1176,6 @@
                 + mWindows.size());
         mWindows.add(i + 1, win);
         mService.mWindowsChanged = true;
-        return 0;
     }
 
     /** Adds this non-app window to the window list. */
@@ -1778,23 +1778,20 @@
         return mLayoutNeeded;
     }
 
-    private int addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
+    private void addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
 
-        int tokenWindowsPos;
         // If this application has existing windows, we simply place the new window on top of
         // them... but keep the starting window on top.
         if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
             // Base windows go behind everything else.
             final WindowState lowestWindow = tokenWindowList.get(0);
             addWindowToListBefore(win, lowestWindow);
-            tokenWindowsPos = win.mToken.getWindowIndex(lowestWindow);
         } else {
             final AppWindowToken atoken = win.mAppToken;
             final int windowListPos = tokenWindowList.size();
             final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
             if (atoken != null && lastWindow == atoken.startingWindow) {
                 addWindowToListBefore(win, lastWindow);
-                tokenWindowsPos = win.mToken.getWindowIndex(lastWindow);
             } else {
                 int newIdx = findIdxBasedOnAppTokens(win);
                 // There is a window above this one associated with the same apptoken note that the
@@ -1804,16 +1801,9 @@
                         "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
                                 + mWindows.size());
                 mWindows.add(newIdx + 1, win);
-                if (newIdx < 0) {
-                    // No window from token found on win's display.
-                    tokenWindowsPos = 0;
-                } else {
-                    tokenWindowsPos = win.mToken.getWindowIndex(mWindows.get(newIdx)) + 1;
-                }
                 mService.mWindowsChanged = true;
             }
         }
-        return tokenWindowsPos;
     }
 
     /** Places the first input window after the second input window in the window list. */
diff --git a/services/core/java/com/android/server/wm/WindowToken.java b/services/core/java/com/android/server/wm/WindowToken.java
index afcdc41..38225cc 100644
--- a/services/core/java/com/android/server/wm/WindowToken.java
+++ b/services/core/java/com/android/server/wm/WindowToken.java
@@ -24,6 +24,7 @@
 import android.view.DisplayInfo;
 
 import java.io.PrintWriter;
+import java.util.Comparator;
 
 import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
@@ -82,6 +83,26 @@
     // The display this token is on.
     private DisplayContent mDisplayContent;
 
+    /**
+     * Compares two child window of this token and returns -1 if the first is lesser than the
+     * second in terms of z-order and 1 otherwise.
+     */
+    private final Comparator<WindowState> mWindowComparator =
+            (WindowState newWindow, WindowState existingWindow) -> {
+        final WindowToken token = WindowToken.this;
+        if (newWindow.mToken != token) {
+            throw new IllegalArgumentException("newWindow=" + newWindow
+                    + " is not a child of token=" + token);
+        }
+
+        if (existingWindow.mToken != token) {
+            throw new IllegalArgumentException("existingWindow=" + existingWindow
+                    + " is not a child of token=" + token);
+        }
+
+        return isFirstChildWindowGreaterThanSecond(newWindow, existingWindow) ? 1 : -1;
+    };
+
     WindowToken(WindowManagerService service, IBinder _token, int type, boolean _explicit,
             DisplayContent dc) {
         mService = service;
@@ -168,19 +189,31 @@
         return -1;
     }
 
+    /**
+     * Returns true if the new window is considered greater than the existing window in terms of
+     * z-order.
+     */
+    protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
+            WindowState existingWindow) {
+        // By default the first window isn't greater than the second to preserve existing logic of
+        // how new windows are added to the token
+        return false;
+    }
+
     void addWindow(final WindowState win) {
-        if (DEBUG_FOCUS) Slog.d(TAG_WM, "addWindow: win=" + win + " Callers=" + Debug.getCallers(5));
+        if (DEBUG_FOCUS) Slog.d(TAG_WM,
+                "addWindow: win=" + win + " Callers=" + Debug.getCallers(5));
 
         if (!win.isChildWindow()) {
-            int tokenWindowsPos = 0;
             if (asAppWindowToken() != null) {
-                tokenWindowsPos = mDisplayContent.addAppWindowToWindowList(win);
+                mDisplayContent.addAppWindowToWindowList(win);
             } else {
                 mDisplayContent.addNonAppWindowToWindowList(win);
             }
+
             if (!mChildren.contains(win)) {
                 if (DEBUG_ADD_REMOVE) Slog.v(TAG_WM, "Adding " + win + " to " + this);
-                addChild(win, tokenWindowsPos);
+                addChild(win, mWindowComparator);
             }
         } else {
             mDisplayContent.addChildWindowToWindowList(win);