Changed WindowContainer to take child type as a generic

Removes the need for sub-classes to cast the children list.
Also means the children list can only contain a single type,
but that is okay.

Bug: 30060889
Test: Existing unit tests still pass.
Change-Id: Ie880f389b8ab790ee65adcdba23b32bdb568854f
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index d78aa32..35494da 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -191,7 +191,7 @@
         mReportedVisibilityResults.reset();
 
         for (int i = 0; i < count; i++) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             win.updateReportedVisibility(mReportedVisibilityResults);
         }
 
@@ -271,7 +271,7 @@
 
             final int windowsCount = mChildren.size();
             for (int i = 0; i < windowsCount; i++) {
-                final WindowState win = (WindowState) mChildren.get(i);
+                final WindowState win = mChildren.get(i);
                 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
             }
 
@@ -307,7 +307,7 @@
         }
 
         for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
-            if (((WindowState) mChildren.get(i)).isWindowAnimationSet()) {
+            if ((mChildren.get(i)).isWindowAnimationSet()) {
                 delayed = true;
             }
         }
@@ -337,7 +337,7 @@
         int j = mChildren.size();
         while (j > 0) {
             j--;
-            final WindowState win = (WindowState) mChildren.get(j);
+            final WindowState win = mChildren.get(j);
             final int type = win.mAttrs.type;
             // No need to loop through child window as base application and starting types can't be
             // child windows.
@@ -388,7 +388,7 @@
     void clearAnimatingFlags() {
         boolean wallpaperMightChange = false;
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             wallpaperMightChange |= win.clearAnimatingFlags();
         }
         if (wallpaperMightChange) {
@@ -412,7 +412,7 @@
     private void destroySurfaces(boolean cleanupOnResume) {
         final DisplayContentList displayList = new DisplayContentList();
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             final boolean destroyed = win.destroySurface(cleanupOnResume, mAppStopped);
 
             if (destroyed) {
@@ -472,7 +472,7 @@
 
     private boolean canRestoreSurfaces() {
         for (int i = mChildren.size() -1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             if (w.canRestoreSurface()) {
                 return true;
             }
@@ -482,7 +482,7 @@
 
     private void clearWasVisibleBeforeClientHidden() {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             w.clearWasVisibleBeforeClientHidden();
         }
     }
@@ -493,7 +493,7 @@
      */
     boolean isAnimatingInvisibleWithSavedSurface() {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             if (w.isAnimatingInvisibleWithSavedSurface()) {
                 return true;
             }
@@ -507,7 +507,7 @@
      */
     void stopUsingSavedSurfaceLocked() {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             w.stopUsingSavedSurface();
         }
         destroySurfaces();
@@ -515,7 +515,7 @@
 
     void markSavedSurfaceExiting() {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             w.markSavedSurfaceExiting();
         }
     }
@@ -530,7 +530,7 @@
         int interestingNotDrawn = -1;
 
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             interestingNotDrawn = w.restoreSavedSurfaceForInterestingWindow();
         }
 
@@ -549,7 +549,7 @@
 
     void destroySavedSurfaces() {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             win.destroySavedSurface();
         }
     }
@@ -582,7 +582,7 @@
 
     void removeDeadWindows() {
         for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
-            WindowState win = (WindowState) mChildren.get(winNdx);
+            WindowState win = mChildren.get(winNdx);
             if (win.mAppDied) {
                 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
                         "removeDeadWindows: " + win);
@@ -598,7 +598,7 @@
         for (int i = mChildren.size() - 1; i >= 0; i--) {
             // No need to loop through child windows as the answer should be the same as that of the
             // parent window.
-            if (!((WindowState) mChildren.get(i)).mAppDied) {
+            if (!(mChildren.get(i)).mAppDied) {
                 return true;
             }
         }
@@ -610,7 +610,7 @@
                 "Marking app token " + this + " with replacing windows.");
 
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             w.setWillReplaceWindow(animate);
         }
         if (animate) {
@@ -627,7 +627,7 @@
         if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
                 + " with replacing child windows.");
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             w.setWillReplaceChildWindows();
         }
     }
@@ -637,14 +637,14 @@
                 "Resetting app token " + this + " of replacing window marks.");
 
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             w.clearWillReplaceWindow();
         }
     }
 
     void requestUpdateWallpaperIfNeeded() {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             w.requestUpdateWallpaperIfNeeded();
         }
     }
@@ -685,7 +685,7 @@
 
         boolean gotReplacementWindow = false;
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState candidate = (WindowState) mChildren.get(i);
+            final WindowState candidate = mChildren.get(i);
             gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
         }
 
@@ -697,7 +697,7 @@
 
     boolean waitingForReplacement() {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState candidate = (WindowState) mChildren.get(i);
+            final WindowState candidate = mChildren.get(i);
             if (candidate.waitingForReplacement()) {
                 return true;
             }
@@ -707,7 +707,7 @@
 
     void onWindowReplacementTimeout() {
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            ((WindowState) mChildren.get(i)).onWindowReplacementTimeout();
+            (mChildren.get(i)).onWindowReplacementTimeout();
         }
     }
 
@@ -748,7 +748,7 @@
             mFrozenMergedConfig.remove();
         }
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             win.onUnfreezeBounds();
         }
         mService.mWindowPlacerLocked.performSurfacePlacement();
@@ -798,13 +798,13 @@
 
     void resetJustMovedInStack() {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            ((WindowState) mChildren.get(i)).resetJustMovedInStack();
+            (mChildren.get(i)).resetJustMovedInStack();
         }
     }
 
     void notifyMovedInStack() {
         for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
-            final WindowState win = (WindowState) mChildren.get(winNdx);
+            final WindowState win = mChildren.get(winNdx);
             win.notifyMovedInStack();
         }
     }
@@ -813,7 +813,7 @@
         final WindowAnimator windowAnimator = mAppAnimator.mAnimator;
         for (int i = mChildren.size() - 1; i >= 0; i--) {
             // Child windows will be on the same display as their parents.
-            if (displayId == ((WindowState) mChildren.get(i)).getDisplayId()) {
+            if (displayId == (mChildren.get(i)).getDisplayId()) {
                 windowAnimator.setPendingLayoutChanges(displayId, changes);
                 if (DEBUG_LAYOUT_REPEATS) {
                     mService.mWindowPlacerLocked.debugLayoutRepeats(
@@ -826,7 +826,7 @@
 
     void removeReplacedWindowIfNeeded(WindowState replacement) {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             if (win.removeReplacedWindowIfNeeded(replacement)) {
                 return;
             }
@@ -851,7 +851,7 @@
             }
             final int count = mChildren.size();
             for (int i = 0; i < count; i++) {
-                final WindowState w = (WindowState) mChildren.get(i);
+                final WindowState w = mChildren.get(i);
                 w.onStartFreezingScreen();
             }
         }
@@ -865,7 +865,7 @@
         final int count = mChildren.size();
         boolean unfrozeWindows = false;
         for (int i = 0; i < count; i++) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             unfrozeWindows |= w.onStopFreezingScreen();
         }
         if (force || unfrozeWindows) {
@@ -998,7 +998,7 @@
 
         final int windowsCount = mChildren.size();
         for (int j = 0; j < windowsCount; j++) {
-            ((WindowState) mChildren.get(j)).addWinAnimatorToList(allAppWinAnimators);
+            (mChildren.get(j)).addWinAnimatorToList(allAppWinAnimators);
         }
     }
 
diff --git a/services/core/java/com/android/server/wm/WindowContainer.java b/services/core/java/com/android/server/wm/WindowContainer.java
index 7273a06..ef05ad2 100644
--- a/services/core/java/com/android/server/wm/WindowContainer.java
+++ b/services/core/java/com/android/server/wm/WindowContainer.java
@@ -32,14 +32,14 @@
  * The test class is {@link WindowContainerTests} which must be kept up-to-date and ran anytime
  * changes are made to this class.
  */
-class WindowContainer implements Comparable<WindowContainer> {
+class WindowContainer<E extends WindowContainer> implements Comparable<WindowContainer> {
 
     // The parent of this window container.
-    private WindowContainer mParent = null;
+    protected WindowContainer mParent = null;
 
     // List of children for this window container. List is in z-order as the children appear on
     // screen with the top-most window container at the tail of the list.
-    protected final LinkedList<WindowContainer> mChildren = new LinkedList();
+    protected final LinkedList<E> mChildren = new LinkedList();
 
     // The specified orientation for this window container.
     protected int mOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
@@ -60,7 +60,7 @@
      *                   If null, the child will be added to the top.
      */
     @CallSuper
-    protected void addChild(WindowContainer child, Comparator<WindowContainer> comparator) {
+    protected void addChild(E child, Comparator<E> comparator) {
         child.mParent = this;
 
         if (mChildren.isEmpty() || comparator == null) {
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index a917011..9213f45 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -141,10 +141,8 @@
     }
 }
 
-/**
- * A window in the window manager.
- */
-class WindowState extends WindowContainer implements WindowManagerPolicy.WindowState {
+/** A window in the window manager. */
+class WindowState extends WindowContainer<WindowState> implements WindowManagerPolicy.WindowState {
     static final String TAG = TAG_WITH_CLASS_NAME ? "WindowState" : TAG_WM;
 
     // The minimal size of a window within the usable area of the freeform stack.
@@ -551,9 +549,9 @@
      * Compares to window sub-layers and returns -1 if the first is lesser than the second in terms
      * of z-order and 1 otherwise.
      */
-    private static final Comparator<WindowContainer> sWindowSubLayerComparator = (w1, w2) -> {
-        final int layer1 = ((WindowState)w1).mSubLayer;
-        final int layer2 = ((WindowState)w2).mSubLayer;
+    private static final Comparator<WindowState> sWindowSubLayerComparator = (w1, w2) -> {
+        final int layer1 = w1.mSubLayer;
+        final int layer2 = w2.mSubLayer;
         if (layer1 < layer2 || (layer1 == layer2 && layer2 < 0 )) {
             // We insert the child window into the list ordered by the sub-layer.
             // For same sub-layers, the negative one should go below others; the positive one should
@@ -1449,7 +1447,7 @@
         boolean changed = false;
 
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             changed |= c.onAppVisibilityChanged(visible, runningAppAnimation);
         }
 
@@ -1500,7 +1498,7 @@
         }
 
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             changed |= c.onSetAppExiting();
         }
 
@@ -1543,7 +1541,7 @@
 
     void onUnfreezeBounds() {
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.onUnfreezeBounds();
         }
 
@@ -1618,7 +1616,7 @@
             removeImmediately();
         } else {
             for (int i = mChildren.size() - 1; i >= 0; --i) {
-                final WindowState c = (WindowState) mChildren.get(i);
+                final WindowState c = mChildren.get(i);
                 c.onWindowReplacementTimeout();
             }
         }
@@ -1891,7 +1889,7 @@
         mJustMovedInStack = true;
 
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.notifyMovedInStack();
         }
     }
@@ -1912,7 +1910,7 @@
         mJustMovedInStack = false;
 
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.resetJustMovedInStack();
         }
     }
@@ -2007,7 +2005,7 @@
         }
 
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             if (c.removeReplacedWindowIfNeeded(replacement)) {
                 return true;
             }
@@ -2041,7 +2039,7 @@
         }
 
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             replacementSet |= c.setReplacementWindowIfNeeded(replacementCandidate);
         }
 
@@ -2414,7 +2412,7 @@
             return true;
         }
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             if (c.isAnimatingInvisibleWithSavedSurface()) {
                 return true;
             }
@@ -2424,7 +2422,7 @@
 
     void stopUsingSavedSurface() {
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.stopUsingSavedSurface();
         }
 
@@ -2445,7 +2443,7 @@
             mWinAnimator.mAnimating = true;
         }
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.markSavedSurfaceExiting();
         }
     }
@@ -2454,7 +2452,7 @@
         animators.add(mWinAnimator);
 
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.addWinAnimatorToList(animators);
         }
     }
@@ -2486,7 +2484,7 @@
     public void clearWasVisibleBeforeClientHidden() {
         mWasVisibleBeforeClientHidden = false;
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.clearWasVisibleBeforeClientHidden();
         }
     }
@@ -2498,7 +2496,7 @@
     void onStartFreezingScreen() {
         mAppFreezing = true;
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.onStartFreezingScreen();
         }
     }
@@ -2506,7 +2504,7 @@
     boolean onStopFreezingScreen() {
         boolean unfrozeWindows = false;
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             unfrozeWindows |= c.onStopFreezingScreen();
         }
 
@@ -2576,7 +2574,7 @@
     boolean destroySurface(boolean cleanupOnResume, boolean appStopped) {
         boolean destroyedSomething = false;
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             destroyedSomething |= c.destroySurface(cleanupOnResume, appStopped);
         }
 
@@ -2636,7 +2634,7 @@
 
     void destroySavedSurface() {
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.destroySavedSurface();
         }
 
@@ -2652,7 +2650,7 @@
     int restoreSavedSurfaceForInterestingWindow() {
         int interestingNotDrawn = -1;
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             final int childInterestingNotDrawn = c.restoreSavedSurfaceForInterestingWindow();
             if (childInterestingNotDrawn != -1) {
                 if (interestingNotDrawn == -1) {
@@ -2723,7 +2721,7 @@
         }
 
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             if (c.canRestoreSurface()) {
                 return true;
             }
@@ -3464,8 +3462,8 @@
     WindowState getBottomChild() {
         // Child windows are z-ordered based on sub-layer using {@link #sWindowSubLayerComparator}
         // and the child with the lowest z-order will be at the head of the list.
-        WindowContainer c = mChildren.peekFirst();
-        return c == null ? null : (WindowState)c;
+        WindowState c = mChildren.peekFirst();
+        return c == null ? null : c;
     }
 
     boolean layoutInParentFrame() {
@@ -3496,7 +3494,7 @@
 
     void setWillReplaceWindow(boolean animate) {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.setWillReplaceWindow(animate);
         }
 
@@ -3518,7 +3516,7 @@
         mAnimateReplacingWindow = false;
 
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.clearWillReplaceWindow();
         }
     }
@@ -3529,7 +3527,7 @@
         }
 
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             if (c.waitingForReplacement()) {
                 return true;
             }
@@ -3545,7 +3543,7 @@
         }
 
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.requestUpdateWallpaperIfNeeded();
         }
     }
@@ -3593,7 +3591,7 @@
             setWillReplaceWindow(false /* animate */);
         }
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.setWillReplaceChildWindows();
         }
     }
@@ -3603,7 +3601,7 @@
             return this;
         }
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             final WindowState replacing = c.getReplacingWindow();
             if (replacing != null) {
                 return replacing;
@@ -3651,7 +3649,7 @@
             final DisplayContent displayContent = getDisplayContent();
 
             for (int i = mChildren.size() - 1; i >= 0; --i) {
-                final WindowState c = (WindowState) mChildren.get(i);
+                final WindowState c = mChildren.get(i);
                 if (c.mWinAnimator.mSurfaceController != null) {
                     c.performShowLocked();
                     // It hadn't been shown, which means layout not performed on it, so now we
@@ -3713,7 +3711,7 @@
                 windowInfo.childTokens = new ArrayList(childCount);
             }
             for (int j = 0; j < childCount; j++) {
-                final WindowState child = (WindowState) mChildren.get(j);
+                final WindowState child = mChildren.get(j);
                 windowInfo.childTokens.add(child.mClient.asBinder());
             }
         }
@@ -3723,7 +3721,7 @@
     int getHighestAnimLayer() {
         int highest = mWinAnimator.mAnimLayer;
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             final int childLayer = c.getHighestAnimLayer();
             if (childLayer > highest) {
                 highest = childLayer;
@@ -3737,7 +3735,7 @@
         if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG_WM,
                 "adjustAnimLayer win=" + this + " anim layer: " + mWinAnimator.mAnimLayer);
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState childWindow = (WindowState) mChildren.get(i);
+            final WindowState childWindow = mChildren.get(i);
             childWindow.adjustAnimLayer(adj);
             if (childWindow.mWinAnimator.mAnimLayer > highestAnimLayer) {
                 highestAnimLayer = childWindow.mWinAnimator.mAnimLayer;
@@ -3766,7 +3764,7 @@
         final int childCount = mChildren.size();
         boolean winAdded = false;
         for (int j = 0; j < childCount; j++) {
-            final WindowState child = (WindowState) mChildren.get(j);
+            final WindowState child = mChildren.get(j);
             if (!winAdded && child.mSubLayer >= 0) {
                 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM,
                         "Re-adding child window at " + index + ": " + child);
@@ -3804,7 +3802,7 @@
         int childCount = mChildren.size();
         while (childCount > 0) {
             childCount--;
-            final WindowState cw = (WindowState) mChildren.get(childCount);
+            final WindowState cw = mChildren.get(childCount);
             int cpos = windows.indexOf(cw);
             if (cpos >= 0) {
                 if (cpos < interestingPos) interestingPos--;
@@ -3821,7 +3819,7 @@
             return true;
         }
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             if (c.isWindowAnimationSet()) {
                 return true;
             }
@@ -3837,9 +3835,9 @@
         if (!mChildren.isEmpty()) {
             // Copying to a different list as multiple children can be removed.
             // TODO: Not sure if we really need to copy this into a different list.
-            final LinkedList childWindows = new LinkedList(mChildren);
+            final LinkedList<WindowState> childWindows = new LinkedList(mChildren);
             for (int i = childWindows.size() - 1; i >= 0; i--) {
-                ((WindowState)childWindows.get(i)).onExitAnimationDone();
+                childWindows.get(i).onExitAnimationDone();
             }
         }
 
@@ -3933,7 +3931,7 @@
         }
 
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            didSomething |= ((WindowState) mChildren.get(i)).clearAnimatingFlags();
+            didSomething |= (mChildren.get(i)).clearAnimatingFlags();
         }
 
         return didSomething;
@@ -3945,7 +3943,7 @@
 
     void hideWallpaperWindow(boolean wasDeferred, String reason) {
         for (int j = mChildren.size() - 1; j >= 0; --j) {
-            final WindowState c = (WindowState) mChildren.get(j);
+            final WindowState c = mChildren.get(j);
             c.hideWallpaperWindow(wasDeferred, reason);
         }
         if (!mWinAnimator.mLastHidden || wasDeferred) {
@@ -3985,7 +3983,7 @@
             return true;
         }
         for (int j = mChildren.size() - 1; j >= 0; --j) {
-            final WindowState c = (WindowState) mChildren.get(j);
+            final WindowState c = mChildren.get(j);
             if (c.hasVisibleNotDrawnWallpaper()) {
                 return true;
             }
@@ -3995,7 +3993,7 @@
 
     void updateReportedVisibility(UpdateReportedVisibilityResults results) {
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState c = (WindowState) mChildren.get(i);
+            final WindowState c = mChildren.get(i);
             c.updateReportedVisibility(results);
         }
 
diff --git a/services/core/java/com/android/server/wm/WindowToken.java b/services/core/java/com/android/server/wm/WindowToken.java
index 2f9ed50..9b92ecda 100644
--- a/services/core/java/com/android/server/wm/WindowToken.java
+++ b/services/core/java/com/android/server/wm/WindowToken.java
@@ -22,14 +22,11 @@
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.util.Slog;
-import android.view.IWindow;
 
 import java.io.PrintWriter;
-import java.util.ArrayList;
 
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
-import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
 import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
@@ -45,7 +42,7 @@
  * which is the handle for an Activity that it uses to display windows. For nested windows, there is
  * a WindowToken created for the parent window to manage its children.
  */
-class WindowToken extends WindowContainer {
+class WindowToken extends WindowContainer<WindowState> {
     private static final String TAG = TAG_WITH_CLASS_NAME ? "WindowToken" : TAG_WM;
 
     // The window manager!
@@ -91,7 +88,7 @@
 
     void removeAllWindows() {
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             if (DEBUG_WINDOW_MOVEMENT) Slog.w(TAG_WM, "removeAllWindows: removing win=" + win);
             win.removeIfPossible();
         }
@@ -109,7 +106,7 @@
         DisplayContent displayContent = null;
 
         for (int i = 0; i < count; i++) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             if (win.mWinAnimator.isAnimationSet()) {
                 delayed = true;
                 // TODO: This is technically wrong as a token can have windows on multi-displays
@@ -134,7 +131,7 @@
     int adjustAnimLayer(int adj) {
         int highestAnimLayer = -1;
         for (int j = mChildren.size() - 1; j >= 0; j--) {
-            final WindowState w = (WindowState) mChildren.get(j);
+            final WindowState w = mChildren.get(j);
             final int winHighestAnimLayer = w.adjustAnimLayer(adj);
             if (winHighestAnimLayer > highestAnimLayer) {
                 highestAnimLayer = winHighestAnimLayer;
@@ -160,7 +157,7 @@
      */
     int getWindowIndex(WindowState target) {
         for (int i = mChildren.size() - 1; i >= 0; --i) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             if (w == target || w.hasChild(target)) {
                 return i;
             }
@@ -193,7 +190,7 @@
     int reAddAppWindows(DisplayContent displayContent, int index) {
         final int count = mChildren.size();
         for (int i = 0; i < count; i++) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             final DisplayContent winDisplayContent = win.getDisplayContent();
             if (winDisplayContent == displayContent || winDisplayContent == null) {
                 win.mDisplayContent = displayContent;
@@ -208,7 +205,7 @@
         final int count = mChildren.size();
         // We only care about parent windows so no need to loop through child windows.
         for (int i = 0; i < count; i++) {
-            final WindowState w = (WindowState) mChildren.get(i);
+            final WindowState w = mChildren.get(i);
             if (w.mAttrs.type != TYPE_APPLICATION_STARTING) {
                 return w;
             }
@@ -228,7 +225,7 @@
 
     WindowState getReplacingWindow() {
         for (int i = mChildren.size() - 1; i >= 0; i--) {
-            final WindowState win = (WindowState) mChildren.get(i);
+            final WindowState win = mChildren.get(i);
             final WindowState replacing = win.getReplacingWindow();
             if (replacing != null) {
                 return replacing;
@@ -239,7 +236,7 @@
 
     void hideWallpaperToken(boolean wasDeferred, String reason) {
         for (int j = mChildren.size() - 1; j >= 0; j--) {
-            final WindowState wallpaper = (WindowState) mChildren.get(j);
+            final WindowState wallpaper = mChildren.get(j);
             wallpaper.hideWallpaperWindow(wasDeferred, reason);
         }
         hidden = true;
@@ -248,7 +245,7 @@
     void sendWindowWallpaperCommand(
             String action, int x, int y, int z, Bundle extras, boolean sync) {
         for (int wallpaperNdx = mChildren.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) {
-            final WindowState wallpaper = (WindowState) mChildren.get(wallpaperNdx);
+            final WindowState wallpaper = mChildren.get(wallpaperNdx);
             try {
                 wallpaper.mClient.dispatchWallpaperCommand(action, x, y, z, extras, sync);
                 // We only want to be synchronous with one wallpaper.
@@ -261,7 +258,7 @@
     void updateWallpaperOffset(int dw, int dh, boolean sync) {
         final WallpaperController wallpaperController = mService.mWallpaperControllerLocked;
         for (int wallpaperNdx = mChildren.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) {
-            final WindowState wallpaper = (WindowState) mChildren.get(wallpaperNdx);
+            final WindowState wallpaper = mChildren.get(wallpaperNdx);
             if (wallpaperController.updateWallpaperOffset(wallpaper, dw, dh, sync)) {
                 final WindowStateAnimator winAnimator = wallpaper.mWinAnimator;
                 winAnimator.computeShownFrameLocked();
@@ -282,7 +279,7 @@
 
         final WallpaperController wallpaperController = mService.mWallpaperControllerLocked;
         for (int wallpaperNdx = mChildren.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) {
-            final WindowState wallpaper = (WindowState) mChildren.get(wallpaperNdx);
+            final WindowState wallpaper = mChildren.get(wallpaperNdx);
             if (visible) {
                 wallpaperController.updateWallpaperOffset(wallpaper, dw, dh, false);
             }
@@ -305,7 +302,7 @@
 
         final WallpaperController wallpaperController = mService.mWallpaperControllerLocked;
         for (int wallpaperNdx = mChildren.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) {
-            final WindowState wallpaper = (WindowState) mChildren.get(wallpaperNdx);
+            final WindowState wallpaper = mChildren.get(wallpaperNdx);
 
             if (visible) {
                 wallpaperController.updateWallpaperOffset(wallpaper, dw, dh, false);
@@ -381,7 +378,7 @@
 
     boolean hasVisibleNotDrawnWallpaper() {
         for (int j = mChildren.size() - 1; j >= 0; --j) {
-            final WindowState wallpaper = (WindowState) mChildren.get(j);
+            final WindowState wallpaper = mChildren.get(j);
             if (wallpaper.hasVisibleNotDrawnWallpaper()) {
                 return true;
             }
@@ -392,7 +389,7 @@
     int getHighestAnimLayer() {
         int highest = -1;
         for (int j = 0; j < mChildren.size(); j++) {
-            final WindowState w = (WindowState) mChildren.get(j);
+            final WindowState w = mChildren.get(j);
             final int wLayer = w.getHighestAnimLayer();
             if (wLayer > highest) {
                 highest = wLayer;