Refactor removeApp and removeTask for last removals.

Move app token removal to the AppWindowToken class so cleanup can
be done locally. Move task removal to the Task class so cleanup can
be done locally. Call task removal when the last app is removed.
Merge actions done prior to method calls into methods.

Fixes bug 18088522 item #12.

Change-Id: I5ce85d2bb309ceb82bd7404e27a56a7c31cd7359
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index 8519f3d..3b27cd2 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -252,6 +252,20 @@
         return false;
     }
 
+    void removeAppFromTaskLocked() {
+        mIsExiting = false;
+        removeAllWindows();
+
+        final Task task = service.mTaskIdToTask.get(groupId);
+        if (task != null) {
+            if (!task.removeAppToken(this)) {
+                Slog.e(WindowManagerService.TAG, "removeAppFromTaskLocked: token=" + this
+                        + " not found.");
+            }
+            task.mStack.mExitingAppTokens.remove(this);
+        }
+    }
+
     @Override
     void removeAllWindows() {
         for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
@@ -266,8 +280,10 @@
                 Slog.w(WindowManagerService.TAG, "removeAllWindows: removing win=" + win);
             }
 
-            win.mService.removeWindowLocked(win.mSession, win);
+            service.removeWindowLocked(win.mSession, win);
         }
+        allAppWindows.clear();
+        windows.clear();
     }
 
     @Override