Replaced use of AppWindowToken.allAppWindows with WindowToken.windows

After ag/1290328 the 2 list now contain the same entries. The only
different is allAppWindows is not sorted and windows is. Removing
allAppWindows and only having one windows list for a token object
reduces confusion in the codebase and also removes a major source
of bugs.

Bug: 30060889
Change-Id: I6e9a3a4b52cfd686788fe18f4fd6c9b0731f10bd
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index 7004e35..c78e94b 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -73,9 +73,6 @@
     // Non-null only for application tokens.
     final IApplicationToken appToken;
 
-    // All of the windows and child windows that are included in this
-    // application token.  Note this list is NOT sorted!
-    private final WindowList allAppWindows = new WindowList();
     @NonNull final AppWindowAnimator mAppAnimator;
 
     final boolean voiceInteraction;
@@ -163,9 +160,9 @@
     }
 
     void sendAppVisibilityToClients() {
-        final int N = allAppWindows.size();
+        final int N = windows.size();
         for (int i=0; i<N; i++) {
-            WindowState win = allAppWindows.get(i);
+            WindowState win = windows.get(i);
             if (win == startingWindow && clientHidden) {
                 // Don't hide the starting window.
                 continue;
@@ -180,8 +177,8 @@
     }
 
     void setVisibleBeforeClientHidden() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             w.setVisibleBeforeClientHidden();
         }
     }
@@ -217,9 +214,9 @@
 
         if (DEBUG_VISIBILITY) Slog.v(TAG,
                 "Update reported visibility: " + this);
-        final int N = allAppWindows.size();
+        final int N = windows.size();
         for (int i=0; i<N; i++) {
-            WindowState win = allAppWindows.get(i);
+            WindowState win = windows.get(i);
             if (win == startingWindow || win.mAppFreezing
                     || win.mViewVisibility != View.VISIBLE
                     || win.mAttrs.type == TYPE_APPLICATION_STARTING
@@ -330,9 +327,9 @@
                 changed = true;
             }
 
-            final int windowsCount = allAppWindows.size();
+            final int windowsCount = windows.size();
             for (int i = 0; i < windowsCount; i++) {
-                final WindowState win = allAppWindows.get(i);
+                final WindowState win = windows.get(i);
                 if (win == startingWindow) {
                     // Starting window that's exiting will be removed when the animation finishes.
                     // Mark all relevant flags for that onExitAnimationDone will proceed all the way
@@ -406,8 +403,8 @@
             delayed = true;
         }
 
-        for (int i = allAppWindows.size() - 1; i >= 0 && !delayed; i--) {
-            if (allAppWindows.get(i).mWinAnimator.isWindowAnimationSet()) {
+        for (int i = windows.size() - 1; i >= 0 && !delayed; i--) {
+            if (windows.get(i).mWinAnimator.isWindowAnimationSet()) {
                 delayed = true;
             }
         }
@@ -434,10 +431,10 @@
 
     WindowState findMainWindow() {
         WindowState candidate = null;
-        int j = allAppWindows.size();
+        int j = windows.size();
         while (j > 0) {
             j--;
-            WindowState win = allAppWindows.get(j);
+            WindowState win = windows.get(j);
             if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
                     || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
                 // In cases where there are multiple windows, we prefer the non-exiting window. This
@@ -458,9 +455,9 @@
     }
 
     boolean isVisible() {
-        final int N = allAppWindows.size();
+        final int N = windows.size();
         for (int i=0; i<N; i++) {
-            WindowState win = allAppWindows.get(i);
+            WindowState win = windows.get(i);
             // If we're animating with a saved surface, we're already visible.
             // Return true so that the alpha doesn't get cleared.
             if (!win.mAppFreezing
@@ -476,8 +473,8 @@
     }
 
     boolean isVisibleForUser() {
-        for (int j = allAppWindows.size() - 1; j >= 0; j--) {
-            final WindowState w = allAppWindows.get(j);
+        for (int j = windows.size() - 1; j >= 0; j--) {
+            final WindowState w = windows.get(j);
             if (!w.isHiddenFromUserLocked()) {
                 return true;
             }
@@ -501,8 +498,8 @@
     }
 
     void clearAnimatingFlags() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState win = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState win = windows.get(i);
             // We don't want to clear it out for windows that get replaced, because the
             // animation depends on the flag to remove the replaced window.
             //
@@ -540,7 +537,7 @@
      */
 
     private void destroySurfaces(boolean cleanupOnResume) {
-        final ArrayList<WindowState> allWindows = (ArrayList<WindowState>) allAppWindows.clone();
+        final ArrayList<WindowState> allWindows = (ArrayList<WindowState>) windows.clone();
         final DisplayContentList displayList = new DisplayContentList();
         for (int i = allWindows.size() - 1; i >= 0; i--) {
             final WindowState win = allWindows.get(i);
@@ -620,8 +617,8 @@
     }
 
     boolean canRestoreSurfaces() {
-        for (int i = allAppWindows.size() -1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() -1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             if (w.canRestoreSurface()) {
                 return true;
             }
@@ -630,8 +627,8 @@
     }
 
     void clearVisibleBeforeClientHidden() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             w.clearVisibleBeforeClientHidden();
         }
     }
@@ -641,8 +638,8 @@
      * animating with saved surface.
      */
     boolean isAnimatingInvisibleWithSavedSurface() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             if (w.isAnimatingInvisibleWithSavedSurface()) {
                 return true;
             }
@@ -655,8 +652,8 @@
      * with a saved surface, and mark them destroying.
      */
     void stopUsingSavedSurfaceLocked() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             if (w.isAnimatingInvisibleWithSavedSurface()) {
                 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
                         "stopUsingSavedSurfaceLocked: " + w);
@@ -670,8 +667,8 @@
     }
 
     void markSavedSurfaceExiting() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             if (w.isAnimatingInvisibleWithSavedSurface()) {
                 w.mAnimatingExit = true;
                 w.mWinAnimator.mAnimating = true;
@@ -687,8 +684,8 @@
         // Check if we have enough drawn windows to mark allDrawn= true.
         int numInteresting = 0;
         int numDrawn = 0;
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            WindowState w = windows.get(i);
             if (w != startingWindow && !w.mAppDied && w.wasVisibleBeforeClientHidden()
                     && (!mAppAnimator.freezingScreen || !w.mAppFreezing)) {
                 numInteresting++;
@@ -715,8 +712,8 @@
     }
 
     void destroySavedSurfaces() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            WindowState win = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            WindowState win = windows.get(i);
             win.destroySavedSurface();
         }
     }
@@ -728,40 +725,19 @@
     }
 
     @Override
-    void removeAllWindows() {
-        for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
-                // removeWindowLocked at bottom of loop may remove multiple entries from
-                // allAppWindows if the window to be removed has child windows. It also may
-                // not remove any windows from allAppWindows at all if win is exiting and
-                // currently animating away. This ensures that winNdx is monotonically decreasing
-                // and never beyond allAppWindows bounds.
-                winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
-            WindowState win = allAppWindows.get(winNdx);
-            if (DEBUG_WINDOW_MOVEMENT) {
-                Slog.w(TAG, "removeAllWindows: removing win=" + win);
-            }
-
-            mService.removeWindowLocked(win);
-        }
-        allAppWindows.clear();
-        windows.clear();
-    }
-
-    @Override
     void removeWindow(WindowState win) {
         super.removeWindow(win);
 
-        allAppWindows.remove(win);
-
+        // TODO: Something smells about the code below...Is there a better way?
         if (startingWindow == win) {
             if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
             mService.scheduleRemoveStartingWindowLocked(this);
-        } else if (allAppWindows.size() == 0 && startingData != null) {
+        } else if (windows.size() == 0 && startingData != null) {
             // If this is the last window and we had requested a starting transition window,
             // well there is no point now.
             if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingWindow");
             startingData = null;
-        } else if (allAppWindows.size() == 1 && startingView != null) {
+        } else if (windows.size() == 1 && startingView != null) {
             // If this is the last window except for a starting transition window,
             // we need to get rid of the starting transition.
             mService.scheduleRemoveStartingWindowLocked(this);
@@ -769,28 +745,27 @@
     }
 
     void removeAllDeadWindows() {
-        for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
-            // removeWindowLocked at bottom of loop may remove multiple entries from
-            // allAppWindows if the window to be removed has child windows. It also may
-            // not remove any windows from allAppWindows at all if win is exiting and
+        for (int winNdx = windows.size() - 1; winNdx >= 0;
+            // WindowState#removeIfPossible() at bottom of loop may remove multiple entries from
+            // windows if the window to be removed has child windows. It also may
+            // not remove any windows from windows at all if win is exiting and
             // currently animating away. This ensures that winNdx is monotonically decreasing
-            // and never beyond allAppWindows bounds.
-            winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
-            WindowState win = allAppWindows.get(winNdx);
+            // and never beyond windows bounds.
+            winNdx = Math.min(winNdx - 1, windows.size() - 1)) {
+            WindowState win = windows.get(winNdx);
             if (win.mAppDied) {
-                if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) {
-                    Slog.w(TAG, "removeAllDeadWindows: " + win);
-                }
+                if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
+                        "removeAllDeadWindows: " + win);
                 // Set mDestroying, we don't want any animation or delayed removal here.
                 win.mDestroying = true;
-                mService.removeWindowLocked(win);
+                win.removeIfPossible();
             }
         }
     }
 
     boolean hasWindowsAlive() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            if (!allAppWindows.get(i).mAppDied) {
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            if (!windows.get(i).mAppDied) {
                 return true;
             }
         }
@@ -801,8 +776,8 @@
         if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
                 "Marking app token " + this + " with replacing windows.");
 
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             w.setReplacing(animate);
         }
         if (animate) {
@@ -818,8 +793,8 @@
     void setReplacingChildren() {
         if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
                 + " with replacing child windows.");
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             if (w.shouldBeReplacedWithChildren()) {
                 w.setReplacing(false /* animate */);
             }
@@ -830,15 +805,15 @@
         if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
                 "Resetting app token " + this + " of replacing window marks.");
 
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             w.resetReplacing();
         }
     }
 
     void requestUpdateWallpaperIfNeeded() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState w = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState w = windows.get(i);
             w.requestUpdateWallpaperIfNeeded();
         }
     }
@@ -873,28 +848,26 @@
         mPendingRelaunchCount = 0;
     }
 
+    @Override
     void addWindow(WindowState w) {
-        if (allAppWindows.contains(w)) {
-            return;
-        }
+        super.addWindow(w);
 
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            WindowState candidate = allAppWindows.get(i);
-            if (candidate.mWillReplaceWindow && candidate.mReplacingWindow == null &&
-                    candidate.getWindowTag().toString().equals(w.getWindowTag().toString())) {
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState candidate = windows.get(i);
+            if (candidate.mWillReplaceWindow && candidate.mReplacingWindow == null
+                    && candidate.getWindowTag().toString().equals(w.getWindowTag().toString())) {
+
                 candidate.mReplacingWindow = w;
                 w.mSkipEnterAnimationForSeamlessReplacement = !candidate.mAnimateReplacingWindow;
-
                 // if we got a replacement window, reset the timeout to give drawing more time
                 mService.scheduleReplacingWindowTimeouts(this);
             }
         }
-        allAppWindows.add(w);
     }
 
     boolean waitingForReplacement() {
-        for (int i = allAppWindows.size() -1; i >= 0; i--) {
-            WindowState candidate = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            WindowState candidate = windows.get(i);
             if (candidate.mWillReplaceWindow) {
                 return true;
             }
@@ -903,15 +876,14 @@
     }
 
     void clearTimedoutReplacesLocked() {
-        for (int i = allAppWindows.size() - 1; i >= 0;
-             // removeWindowLocked at bottom of loop may remove multiple entries from
-             // allAppWindows if the window to be removed has child windows. It also may
-             // not remove any windows from allAppWindows at all if win is exiting and
-             // currently animating away. This ensures that winNdx is monotonically decreasing
-             // and never beyond allAppWindows bounds.
-             i = Math.min(i - 1, allAppWindows.size() - 1)) {
-            WindowState candidate = allAppWindows.get(i);
-            if (candidate.mWillReplaceWindow == false) {
+        for (int i = windows.size() - 1; i >= 0;
+             // WindowState#remove() at bottom of loop may remove multiple entries from windows if
+             // the window to be removed has child windows. It also may not remove any windows from
+             // windows at all if win is exiting and currently animating away. This ensures that
+             // winNdx is monotonically decreasing and never beyond windows bounds.
+             i = Math.min(i - 1, windows.size() - 1)) {
+            final WindowState candidate = windows.get(i);
+            if (!candidate.mWillReplaceWindow) {
                 continue;
             }
             candidate.mWillReplaceWindow = false;
@@ -919,7 +891,7 @@
                 candidate.mReplacingWindow.mSkipEnterAnimationForSeamlessReplacement = false;
             }
             // Since the window already timed out, remove it immediately now.
-            // Use WindowState#remove() instead of removeWindowLocked(), as the latter
+            // Use WindowState#remove() instead of WindowState#removeIfPossible(), as the latter
             // delays removal on certain conditions, which will leave the stale window in the
             // stack and marked mWillReplaceWindow=false, so the window will never be removed.
             candidate.remove();
@@ -962,8 +934,8 @@
         if (!mFrozenMergedConfig.isEmpty()) {
             mFrozenMergedConfig.remove();
         }
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState win = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState win = windows.get(i);
             if (!win.mHasSurface) {
                 continue;
             }
@@ -1007,49 +979,14 @@
     }
 
     void resetJustMovedInStack() {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            allAppWindows.get(i).resetJustMovedInStack();
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            windows.get(i).resetJustMovedInStack();
         }
     }
 
-    @Override
-    int adjustAnimLayer(int adj) {
-        int highestAnimLayer = super.adjustAnimLayer(adj);
-
-        final int windowCount = allAppWindows.size();
-
-        for (int i = 0; i < windowCount; i++) {
-            final WindowState w = allAppWindows.get(i);
-            w.adjustAnimLayer(adj);
-
-            final int animLayer = w.mWinAnimator.mAnimLayer;
-            if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": " + animLayer);
-            if (animLayer > highestAnimLayer) {
-                highestAnimLayer = animLayer;
-            }
-            if (w == mService.mInputMethodTarget && !mService.mInputMethodTargetWaitingAnim) {
-                mService.mLayersController.setInputMethodAnimLayerAdjustment(adj);
-            }
-        }
-
-        return highestAnimLayer;
-    }
-
-    @Override
-    int getHighestAnimLayer() {
-        int layer = super.getHighestAnimLayer();
-        for (int j = 0; j < allAppWindows.size(); j++) {
-            final WindowState win = allAppWindows.get(j);
-            if (win.mWinAnimator.mAnimLayer > layer) {
-                layer = win.mWinAnimator.mAnimLayer;
-            }
-        }
-        return layer;
-    }
-
     void setWaitingForDrawnIfResizingChanged() {
-        for (int i = allAppWindows.size() - 1; i >= 0; --i) {
-            final WindowState win = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; --i) {
+            final WindowState win = windows.get(i);
             if (win.isDragResizeChanged()) {
                 mService.mWaitingForDrawn.add(win);
             }
@@ -1062,8 +999,8 @@
         // destroy all saved surfaces here.
         destroySavedSurfaces();
 
-        for (int winNdx = allAppWindows.size() - 1; winNdx >= 0; --winNdx) {
-            final WindowState win = allAppWindows.get(winNdx);
+        for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
+            final WindowState win = windows.get(winNdx);
             if (win.mHasSurface && !resizingWindows.contains(win)) {
                 if (DEBUG_RESIZE) Slog.d(TAG, "resizeWindows: Resizing " + win);
                 resizingWindows.add(win);
@@ -1092,33 +1029,33 @@
     }
 
     void moveWindows() {
-        for (int winNdx = allAppWindows.size() - 1; winNdx >= 0; --winNdx) {
-            final WindowState win = allAppWindows.get(winNdx);
+        for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
+            final WindowState win = windows.get(winNdx);
             if (DEBUG_RESIZE) Slog.d(TAG, "moveWindows: Moving " + win);
             win.mMovedByResize = true;
         }
     }
 
     void notifyMovedInStack() {
-        for (int winNdx = allAppWindows.size() - 1; winNdx >= 0; --winNdx) {
-            final WindowState win = allAppWindows.get(winNdx);
+        for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
+            final WindowState win = windows.get(winNdx);
             win.notifyMovedInStack();
         }
     }
 
     void resetDragResizingChangeReported() {
-        for (int winNdx = allAppWindows.size() - 1; winNdx >= 0; --winNdx) {
-            final WindowState win = allAppWindows.get(winNdx);
+        for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
+            final WindowState win = windows.get(winNdx);
             win.resetDragResizingChangeReported();
         }
     }
 
     void detachDisplay() {
         boolean doAnotherLayoutPass = false;
-        for (int winNdx = allAppWindows.size() - 1; winNdx >= 0; --winNdx) {
+        for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
             // We are in the middle of changing the state of displays/stacks/tasks. We need
             // to finish that, before we let layout interfere with it.
-            mService.removeWindowLocked(allAppWindows.get(winNdx));
+            windows.get(winNdx).removeIfPossible();
             doAnotherLayoutPass = true;
         }
         if (doAnotherLayoutPass) {
@@ -1127,8 +1064,8 @@
     }
 
     void forceWindowsScaleableInTransaction(boolean force) {
-        for (int winNdx = allAppWindows.size() - 1; winNdx >= 0; --winNdx) {
-            final WindowStateAnimator winAnimator = allAppWindows.get(winNdx).mWinAnimator;
+        for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
+            final WindowStateAnimator winAnimator = windows.get(winNdx).mWinAnimator;
             if (winAnimator == null || !winAnimator.hasSurface()) {
                 continue;
             }
@@ -1137,8 +1074,8 @@
     }
 
     boolean isAnimating() {
-        for (int winNdx = allAppWindows.size() - 1; winNdx >= 0; --winNdx) {
-            final WindowStateAnimator winAnimator = allAppWindows.get(winNdx).mWinAnimator;
+        for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
+            final WindowStateAnimator winAnimator = windows.get(winNdx).mWinAnimator;
             if (winAnimator.isAnimationSet() || winAnimator.mWin.mAnimatingExit) {
                 return true;
             }
@@ -1148,8 +1085,8 @@
 
     void setAppLayoutChanges(int changes, String reason, int displayId) {
         final WindowAnimator windowAnimator = mAppAnimator.mAnimator;
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            if (displayId == allAppWindows.get(i).getDisplayId()) {
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            if (displayId == windows.get(i).getDisplayId()) {
                 windowAnimator.setPendingLayoutChanges(displayId, changes);
                 if (DEBUG_LAYOUT_REPEATS) {
                     mService.mWindowPlacerLocked.debugLayoutRepeats(
@@ -1161,8 +1098,8 @@
     }
 
     void removeReplacedWindowIfNeeded(WindowState replacement) {
-        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
-            final WindowState win = allAppWindows.get(i);
+        for (int i = windows.size() - 1; i >= 0; i--) {
+            final WindowState win = windows.get(i);
             if (win.mWillReplaceWindow && win.mReplacingWindow == replacement
                     && replacement.hasDrawnLw()) {
                 replacement.mSkipEnterAnimationForSeamlessReplacement = false;
@@ -1185,9 +1122,9 @@
                     mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
                 }
             }
-            final int count = allAppWindows.size();
+            final int count = windows.size();
             for (int i = 0; i < count; i++) {
-                final WindowState w = allAppWindows.get(i);
+                final WindowState w = windows.get(i);
                 w.mAppFreezing = true;
             }
         }
@@ -1198,10 +1135,10 @@
             return;
         }
         if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
-        final int count = allAppWindows.size();
+        final int count = windows.size();
         boolean unfrozeWindows = false;
         for (int i = 0; i < count; i++) {
-            final WindowState w = allAppWindows.get(i);
+            final WindowState w = windows.get(i);
             if (w.mAppFreezing) {
                 w.mAppFreezing = false;
                 if (w.mHasSurface && !w.mOrientationChanging
@@ -1269,8 +1206,7 @@
             if (DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
                     "Removing starting " + tStartingWindow + " from " + fromToken);
             fromToken.removeWindow(tStartingWindow);
-            fromToken.allAppWindows.remove(tStartingWindow);
-            addWindowToList(tStartingWindow);
+            addWindow(tStartingWindow);
 
             // Propagate other interesting state between the tokens. If the old token is displayed,
             // we should immediately force the new one to be displayed. If it is animating, we need
@@ -1330,25 +1266,19 @@
     }
 
     int getWindowsCount() {
-        return allAppWindows.size();
+        return windows.size();
     }
 
     void setAllAppWinAnimators() {
         final ArrayList<WindowStateAnimator> allAppWinAnimators = mAppAnimator.mAllAppWinAnimators;
         allAppWinAnimators.clear();
 
-        final int windowsCount = allAppWindows.size();
+        final int windowsCount = windows.size();
         for (int j = 0; j < windowsCount; j++) {
-            allAppWinAnimators.add(allAppWindows.get(j).mWinAnimator);
+            allAppWinAnimators.add(windows.get(j).mWinAnimator);
         }
     }
 
-    /** Returns true if the app token windows list is empty. */
-    @Override
-    boolean isEmpty() {
-        return allAppWindows.isEmpty();
-    }
-
     @Override
     AppWindowToken asAppWindowToken() {
         // I am an app window token!
@@ -1361,9 +1291,6 @@
         if (appToken != null) {
             pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
         }
-        if (allAppWindows.size() > 0) {
-            pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
-        }
         pw.print(prefix); pw.print("task="); pw.println(mTask);
         pw.print(prefix); pw.print(" appFullscreen="); pw.print(appFullscreen);
                 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
diff --git a/services/core/java/com/android/server/wm/InputMonitor.java b/services/core/java/com/android/server/wm/InputMonitor.java
index 00781c5..b9c55a5 100644
--- a/services/core/java/com/android/server/wm/InputMonitor.java
+++ b/services/core/java/com/android/server/wm/InputMonitor.java
@@ -87,7 +87,7 @@
             WindowState windowState = (WindowState) inputWindowHandle.windowState;
             if (windowState != null) {
                 Slog.i(TAG_WM, "WINDOW DIED " + windowState);
-                mService.removeWindowLocked(windowState);
+                windowState.removeIfPossible();
             }
         }
     }
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index f41693aa9..be68b92 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -1269,12 +1269,12 @@
             moveInputMethodDialogsLocked(pos + 1);
             return;
         }
-        win.mToken.addWindowToList(win);
+        win.mToken.addWindow(win);
         moveInputMethodDialogsLocked(pos);
     }
 
     private void reAddWindowToListInOrderLocked(WindowState win) {
-        win.mToken.addWindowToList(win);
+        win.mToken.addWindow(win);
         // This is a hack to get all of the child windows added as well at the right position. Child
         // windows should be rare and this case should be rare, so it shouldn't be that big a deal.
         WindowList windows = win.getWindowList();
@@ -1283,7 +1283,7 @@
             if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "ReAdd removing from " + wpos + ": " + win);
             windows.remove(wpos);
             mWindowsChanged = true;
-            win.reAddWindowLocked(wpos);
+            win.reAddWindow(wpos);
         }
     }
 
@@ -1325,7 +1325,7 @@
             if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Adding " + N + " dialogs at pos=" + pos);
             for (int i=0; i<N; i++) {
                 WindowState win = dialogs.get(i);
-                pos = win.reAddWindowLocked(pos);
+                pos = win.reAddWindow(pos);
             }
             if (DEBUG_INPUT_METHOD) {
                 Slog.v(TAG_WM, "Final window list:");
@@ -1404,7 +1404,7 @@
                     Slog.v(TAG_WM, "List after removing with new pos " + imPos + ":");
                     logWindowList(windows, "  ");
                 }
-                imWin.reAddWindowLocked(imPos);
+                imWin.reAddWindow(imPos);
                 if (DEBUG_INPUT_METHOD) {
                     Slog.v(TAG_WM, "List after moving IM to " + imPos + ":");
                     logWindowList(windows, "  ");
@@ -1679,11 +1679,11 @@
                 imMayMove = false;
             } else if (type == TYPE_INPUT_METHOD_DIALOG) {
                 mInputMethodDialogs.add(win);
-                win.mToken.addWindowToList(win);
+                win.mToken.addWindow(win);
                 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
                 imMayMove = false;
             } else {
-                win.mToken.addWindowToList(win);
+                win.mToken.addWindow(win);
                 if (type == TYPE_WALLPAPER) {
                     mWallpaperControllerLocked.clearLastWallpaperTimeoutTime();
                     displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
@@ -1878,14 +1878,10 @@
             if (win == null) {
                 return;
             }
-            removeWindowLocked(win);
+            win.removeIfPossible();
         }
     }
 
-    void removeWindowLocked(WindowState win) {
-        win.removeIfPossible(false /*keepVisibleDeadWindow*/);
-    }
-
     /**
      * Performs some centralized bookkeeping clean-up on the window that is being removed.
      * NOTE: Should only be called from {@link WindowState#remove()}
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index 67a7a09..546498a 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -1486,6 +1486,10 @@
         mService.postWindowRemoveCleanupLocked(this);
     }
 
+    void removeIfPossible() {
+        removeIfPossible(false /*keepVisibleDeadWindow*/);
+    }
+
     void removeIfPossible(boolean keepVisibleDeadWindow) {
         mWindowRemovalAllowed = true;
         if (DEBUG_ADD_REMOVE) Slog.v(TAG,
@@ -1949,7 +1953,7 @@
                         }
                     } else if (mHasSurface) {
                         Slog.e(TAG, "!!! LEAK !!! Window removed but surface still valid.");
-                        mService.removeWindowLocked(WindowState.this);
+                        WindowState.this.removeIfPossible();
                     }
                 }
             } catch (IllegalArgumentException ex) {
@@ -3213,7 +3217,7 @@
     // TODO: come-up with a better name for this method that represents what it does.
     // Or, it is probably not going to matter anyways if we are successful in getting rid of
     // the WindowList concept.
-    int reAddWindowLocked(int index) {
+    int reAddWindow(int index) {
         final WindowList windows = getWindowList();
         // Adding child windows relies on child windows being ordered by mSubLayer using
         // {@link #sWindowSubLayerComparator}.
diff --git a/services/core/java/com/android/server/wm/WindowToken.java b/services/core/java/com/android/server/wm/WindowToken.java
index f56a0f1..b54cbe1 100644
--- a/services/core/java/com/android/server/wm/WindowToken.java
+++ b/services/core/java/com/android/server/wm/WindowToken.java
@@ -16,6 +16,7 @@
 
 package com.android.server.wm;
 
+import android.annotation.CallSuper;
 import android.os.Bundle;
 import android.os.Debug;
 import android.os.IBinder;
@@ -95,10 +96,16 @@
     }
 
     void removeAllWindows() {
-        for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
+        for (int winNdx = windows.size() - 1; winNdx >= 0;
+                // WindowState#removeIfPossible() at bottom of loop may remove multiple entries from
+                // allAppWindows if the window to be removed has child windows. It also may not
+                // remove any windows from allAppWindows at all if win is exiting and currently
+                // animating away. This ensures that winNdx is monotonically decreasing and never
+                // beyond allAppWindows bounds.
+                winNdx = Math.min(winNdx - 1, windows.size() - 1)) {
             WindowState win = windows.get(winNdx);
             if (DEBUG_WINDOW_MOVEMENT) Slog.w(TAG_WM, "removeAllWindows: removing win=" + win);
-            win.mService.removeWindowLocked(win);
+            win.removeIfPossible();
         }
         windows.clear();
     }
@@ -157,6 +164,9 @@
             if (animLayer > highestAnimLayer) {
                 highestAnimLayer = animLayer;
             }
+            if (w == mService.mInputMethodTarget && !mService.mInputMethodTargetWaitingAnim) {
+                mService.mLayersController.setInputMethodAnimLayerAdjustment(adj);
+            }
         }
         return highestAnimLayer;
     }
@@ -258,8 +268,7 @@
         }
     }
 
-    // TODO: Rename to addWindow when conflict with AppWindowToken is resolved. The call below.
-    void addWindowToList(final WindowState win) {
+    void addWindow(final WindowState win) {
         if (DEBUG_FOCUS) Slog.d(TAG_WM, "addWindow: win=" + win + " Callers=" + Debug.getCallers(5));
 
         if (!win.isChildWindow()) {
@@ -276,11 +285,6 @@
         } else {
             addChildWindow(win);
         }
-
-        final AppWindowToken appToken = win.mAppToken;
-        if (appToken != null) {
-            appToken.addWindow(win);
-        }
     }
 
     private int addAppWindow(final WindowState win) {
@@ -452,7 +456,7 @@
         for (int i = 0; i < count; i++) {
             final WindowState win = windows.get(i);
             if (win.isChildWindow()) {
-                // The WindowState.reAddWindowLocked below already takes care of re-adding the
+                // The WindowState.reAddWindow below already takes care of re-adding the
                 // child windows for any parent window in this token. This is a side effect of
                 // ensuring child windows are in the same WindowToken as their parent window.
                 //
@@ -464,7 +468,7 @@
             final DisplayContent winDisplayContent = win.getDisplayContent();
             if (winDisplayContent == displayContent || winDisplayContent == null) {
                 win.mDisplayContent = displayContent;
-                index = win.reAddWindowLocked(index);
+                index = win.reAddWindow(index);
             }
         }
         return index;
@@ -496,6 +500,7 @@
         return null;
     }
 
+    @CallSuper
     void removeWindow(WindowState win) {
         windows.remove(win);
     }