Remove Task#numFullscreen & ActivityRecord#sendingToBottom/remove (70/n)

- Task#numberFullscreen was only used in one location where we
needed to figure-out if the task has occluding activities.
Just use the hierarchy to figure that out vs. keeping track of
a variable that adds confusion.
- ActivityRecord#sendingToBottom was used to prevent focus and
orientation changes from activity moved to the bottom, however
since we are traversing the hierarchy from top-to-bottom we
should process activities on-top first vs the onces that have
already moved to the bottom.
- ActivityRecord#remove is no longer needed as the object won't
be connected to the hierarchy when it is removed.

Test: They pass!
Bug: 80414790
Bug: 35699615
Change-Id: I23f6501a65bf745dc0fb819c30543cd21241fa91
diff --git a/core/proto/android/server/windowmanagerservice.proto b/core/proto/android/server/windowmanagerservice.proto
index 653d381..56e8790 100644
--- a/core/proto/android/server/windowmanagerservice.proto
+++ b/core/proto/android/server/windowmanagerservice.proto
@@ -244,7 +244,8 @@
     optional int32 num_drawn_windows = 15;
     optional bool all_drawn = 16;
     optional bool last_all_drawn = 17;
-    optional bool removed = 18;
+    // Will be removed soon
+    optional bool removed = 18 [deprecated=true];
     optional IdentifierProto starting_window = 19;
     optional bool starting_displayed = 20;
     optional bool starting_moved = 21;
diff --git a/data/etc/services.core.protolog.json b/data/etc/services.core.protolog.json
index 753f8a0..ed19a37 100644
--- a/data/etc/services.core.protolog.json
+++ b/data/etc/services.core.protolog.json
@@ -169,12 +169,6 @@
       "group": "WM_DEBUG_RESIZE",
       "at": "com\/android\/server\/wm\/WindowState.java"
     },
-    "-1797409732": {
-      "message": "Skipping %s because %s",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_FOCUS",
-      "at": "com\/android\/server\/wm\/DisplayContent.java"
-    },
     "-1782453012": {
       "message": "Checking theme of starting window: 0x%x",
       "level": "VERBOSE",
@@ -1453,12 +1447,6 @@
       "group": "WM_DEBUG_RECENTS_ANIMATIONS",
       "at": "com\/android\/server\/wm\/RecentsAnimation.java"
     },
-    "789829331": {
-      "message": "Aborted starting %s: removed=%b startingData=%s",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_STARTING_WINDOW",
-      "at": "com\/android\/server\/wm\/ActivityRecord.java"
-    },
     "791468751": {
       "message": "Pausing rotation during re-position",
       "level": "DEBUG",
@@ -1927,6 +1915,12 @@
       "group": "WM_ERROR",
       "at": "com\/android\/server\/wm\/WindowManagerService.java"
     },
+    "1822843721": {
+      "message": "Aborted starting %s: startingData=%s",
+      "level": "VERBOSE",
+      "group": "WM_DEBUG_STARTING_WINDOW",
+      "at": "com\/android\/server\/wm\/ActivityRecord.java"
+    },
     "1831008694": {
       "message": "Loading animation for app transition. transit=%s enter=%b frame=%s insets=%s surfaceInsets=%s",
       "level": "DEBUG",
diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java
index 8039724..c96943f 100644
--- a/services/core/java/com/android/server/wm/ActivityRecord.java
+++ b/services/core/java/com/android/server/wm/ActivityRecord.java
@@ -185,7 +185,6 @@
 import static com.android.server.wm.AppWindowTokenProto.NAME;
 import static com.android.server.wm.AppWindowTokenProto.NUM_DRAWN_WINDOWS;
 import static com.android.server.wm.AppWindowTokenProto.NUM_INTERESTING_WINDOWS;
-import static com.android.server.wm.AppWindowTokenProto.REMOVED;
 import static com.android.server.wm.AppWindowTokenProto.REPORTED_DRAWN;
 import static com.android.server.wm.AppWindowTokenProto.REPORTED_VISIBLE;
 import static com.android.server.wm.AppWindowTokenProto.STARTING_DISPLAYED;
@@ -631,9 +630,6 @@
     // Last visibility state we reported to the app token.
     boolean reportedVisible;
 
-    // Set to true when the token has been removed from the window mgr.
-    boolean removed;
-
     boolean mDisablePreviewScreenshots;
 
     // Information about an application starting window if displayed.
@@ -878,9 +874,8 @@
             pw.print(" lastAllDrawn="); pw.print(mLastAllDrawn);
             pw.println(")");
         }
-        if (mStartingData != null || removed || firstWindowDrawn || mIsExiting) {
+        if (mStartingData != null || firstWindowDrawn || mIsExiting) {
             pw.print(prefix); pw.print("startingData="); pw.print(mStartingData);
-            pw.print(" removed="); pw.print(removed);
             pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
             pw.print(" mIsExiting="); pw.println(mIsExiting);
         }
@@ -1750,12 +1745,10 @@
             if (surface != null) {
                 boolean abort = false;
                 synchronized (mWmService.mGlobalLock) {
-                    // If the window was successfully added, then
-                    // we need to remove it.
-                    if (removed || mStartingData == null) {
-                        ProtoLog.v(WM_DEBUG_STARTING_WINDOW,
-                                "Aborted starting %s: removed=%b startingData=%s",
-                                ActivityRecord.this, removed, mStartingData);
+                    // If the window was successfully added, then we need to remove it.
+                    if (mStartingData == null) {
+                        ProtoLog.v(WM_DEBUG_STARTING_WINDOW, "Aborted starting %s: startingData=%s",
+                                ActivityRecord.this, mStartingData);
 
                         startingWindow = null;
                         mStartingData = null;
@@ -2010,12 +2003,8 @@
         setMainWindowOpaque(occludesParent);
         mWmService.mWindowPlacerLocked.requestTraversal();
 
-        if (changed && task != null) {
-            if (!occludesParent) {
-                getActivityStack().convertActivityToTranslucent(this);
-            }
-            // Keep track of the number of fullscreen activities in this task.
-            task.numFullscreen += occludesParent ? +1 : -1;
+        if (changed && task != null && !occludesParent) {
+            getActivityStack().convertActivityToTranslucent(this);
         }
         // Always ensure visibility if this activity doesn't occlude parent, so the
         // {@link #returningOptions} of the activity under this one can be applied in
@@ -3040,7 +3029,6 @@
             removeIfPossible();
         }
 
-        removed = true;
         stopFreezingScreen(true, true);
 
         final DisplayContent dc = getDisplayContent();
@@ -4134,11 +4122,6 @@
         scheduleAnimation();
     }
 
-    @Override
-    void onAppTransitionDone() {
-        sendingToBottom = false;
-    }
-
     /**
      * See {@link Activity#setDisablePreviewScreenshots}.
      */
@@ -6090,10 +6073,10 @@
             return mOrientation;
         }
 
-        // The {@link ActivityRecord} should only specify an orientation when it is not closing or
-        // going to the bottom. Allowing closing {@link ActivityRecord} to participate can lead to
-        // an Activity in another task being started in the wrong orientation during the transition.
-        if (!(sendingToBottom || getDisplayContent().mClosingApps.contains(this))
+        // The {@link ActivityRecord} should only specify an orientation when it is not closing.
+        // Allowing closing {@link ActivityRecord} to participate can lead to an Activity in another
+        // task being started in the wrong orientation during the transition.
+        if (!getDisplayContent().mClosingApps.contains(this)
                 && (isVisible() || getDisplayContent().mOpeningApps.contains(this))) {
             return mOrientation;
         }
@@ -7348,7 +7331,6 @@
         proto.write(NUM_DRAWN_WINDOWS, mNumDrawnWindows);
         proto.write(ALL_DRAWN, allDrawn);
         proto.write(LAST_ALL_DRAWN, mLastAllDrawn);
-        proto.write(REMOVED, removed);
         if (startingWindow != null) {
             startingWindow.writeIdentifierToProto(proto, STARTING_WINDOW);
         }
diff --git a/services/core/java/com/android/server/wm/ActivityStack.java b/services/core/java/com/android/server/wm/ActivityStack.java
index 8385232..d7e6852 100644
--- a/services/core/java/com/android/server/wm/ActivityStack.java
+++ b/services/core/java/com/android/server/wm/ActivityStack.java
@@ -2896,7 +2896,7 @@
         Task task = null;
         if (!newTask) {
             // If starting in an existing task, find where that is...
-            boolean startIt = true;
+            boolean isOccluded = false;
             for (int taskNdx = getChildCount() - 1; taskNdx >= 0; --taskNdx) {
                 task = getChildAt(taskNdx);
                 if (task.getTopNonFinishingActivity() == null) {
@@ -2904,10 +2904,10 @@
                     continue;
                 }
                 if (task == rTask) {
-                    // Here it is!  Now, if this is not yet visible to the
-                    // user, then just add it without starting; it will
-                    // get started when the user navigates back to it.
-                    if (!startIt) {
+                    // Here it is!  Now, if this is not yet visible (occluded by another task) to
+                    // the user, then just add it without starting; it will get started when the
+                    // user navigates back to it.
+                    if (isOccluded) {
                         if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
                                 + task, new RuntimeException("here").fillInStackTrace());
                         rTask.positionChildAtTop(r);
@@ -2915,8 +2915,8 @@
                         return;
                     }
                     break;
-                } else if (task.numFullscreen > 0) {
-                    startIt = false;
+                } else if (!isOccluded) {
+                    isOccluded = task.forAllActivities(ActivityRecord::occludesParent);
                 }
             }
         }
@@ -4670,9 +4670,6 @@
         }
         positionChildAt(position, task, includingParents);
         task.updateTaskMovement(toTop);
-        if (getDisplayContent().mAppTransition.isTransitionSet()) {
-            task.setSendingToBottom(!toTop);
-        }
         getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
 
 
@@ -5214,9 +5211,6 @@
         child.updateTaskMovement(true);
 
         final DisplayContent displayContent = getDisplayContent();
-        if (displayContent.mAppTransition.isTransitionSet()) {
-            child.setSendingToBottom(false);
-        }
         displayContent.layoutAndAssignWindowLayersIfNeeded();
     }
 
@@ -5238,10 +5232,6 @@
         }
 
         positionChildAt(POSITION_BOTTOM, child, includingParents);
-
-        if (getDisplayContent().mAppTransition.isTransitionSet()) {
-            child.setSendingToBottom(true);
-        }
         getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
     }
 
@@ -5284,9 +5274,6 @@
         task.updateTaskMovement(isTop);
         if (isTop) {
             final DisplayContent displayContent = getDisplayContent();
-            if (displayContent.mAppTransition.isTransitionSet()) {
-                task.setSendingToBottom(false);
-            }
             displayContent.layoutAndAssignWindowLayersIfNeeded();
         }
     }
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index b4dd55d..098c978 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -601,13 +601,6 @@
 
         final ActivityRecord activity = w.mActivityRecord;
 
-        // If this window's application has been removed, just skip it.
-        if (activity!= null && (activity.removed || activity.sendingToBottom)) {
-            ProtoLog.v(WM_DEBUG_FOCUS, "Skipping %s because %s", activity,
-                    (activity.removed ? "removed" : "sendingToBottom"));
-            return false;
-        }
-
         if (focusedApp == null) {
             ProtoLog.v(WM_DEBUG_FOCUS_LIGHT,
                     "findFocusedWindow: focusedApp=null using new focus @ %s", w);
diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java
index 96bac88..ba3b8b7 100644
--- a/services/core/java/com/android/server/wm/Task.java
+++ b/services/core/java/com/android/server/wm/Task.java
@@ -252,8 +252,6 @@
     boolean mUserSetupComplete; // The user set-up is complete as of the last time the task activity
                                 // was changed.
 
-    int numFullscreen;      // Number of fullscreen activities.
-
     /** Can't be put in lockTask mode. */
     final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
     /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
@@ -1210,9 +1208,6 @@
         ProtoLog.v(WM_DEBUG_ADD_REMOVE, "addChild: %s at top.", this);
         r.inHistory = true;
 
-        if (r.occludesParent()) {
-            numFullscreen++;
-        }
         // Only set this based on the first activity
         if (!hadChild) {
             if (r.getActivityType() == ACTIVITY_TYPE_UNDEFINED) {
@@ -1256,9 +1251,6 @@
         }
 
         super.removeChild(r);
-        if (r.occludesParent()) {
-            numFullscreen--;
-        }
         if (r.isPersistable()) {
             mAtmService.notifyTaskPersisterLocked(this, false);
         }
@@ -2255,7 +2247,7 @@
             // We want to place all non-overlay activities below overlays.
             while (maxPosition > 0) {
                 final ActivityRecord current = mChildren.get(maxPosition - 1);
-                if (current.mTaskOverlay && !current.removed) {
+                if (current.mTaskOverlay) {
                     --maxPosition;
                     continue;
                 }
@@ -2266,17 +2258,6 @@
             }
         }
 
-        if (suggestedPosition >= maxPosition) {
-            return Math.min(maxPosition, suggestedPosition);
-        }
-
-        for (int pos = 0; pos < maxPosition && pos < suggestedPosition; ++pos) {
-            // TODO: Confirm that this is the behavior we want long term.
-            if (mChildren.get(pos).removed) {
-                // suggestedPosition assumes removed tokens are actually gone.
-                ++suggestedPosition;
-            }
-        }
         return Math.min(maxPosition, suggestedPosition);
     }
 
@@ -2339,12 +2320,6 @@
         mPreserveNonFloatingState = stack.inPinnedWindowingMode();
     }
 
-    void setSendingToBottom(boolean toBottom) {
-        for (int appTokenNdx = 0; appTokenNdx < mChildren.size(); appTokenNdx++) {
-            mChildren.get(appTokenNdx).sendingToBottom = toBottom;
-        }
-    }
-
     public int setBounds(Rect bounds, boolean forceResize) {
         final int boundsChanged = setBounds(bounds);
 
@@ -2981,10 +2956,9 @@
             pw.print(prefix); pw.print("mActivityComponent=");
             pw.println(realActivity.flattenToShortString());
         }
-        if (autoRemoveRecents || isPersistable || !isActivityTypeStandard() || numFullscreen != 0) {
+        if (autoRemoveRecents || isPersistable || !isActivityTypeStandard()) {
             pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
             pw.print(" isPersistable="); pw.print(isPersistable);
-            pw.print(" numFullscreen="); pw.print(numFullscreen);
             pw.print(" activityType="); pw.println(getActivityType());
         }
         if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index 519cc21..57e02a8 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -1473,7 +1473,7 @@
                     ProtoLog.w(WM_ERROR, "Attempted to add window with non-application token "
                             + ".%s Aborting.", token);
                     return WindowManagerGlobal.ADD_NOT_APP_TOKEN;
-                } else if (activity.removed) {
+                } else if (activity.getParent() == null) {
                     ProtoLog.w(WM_ERROR, "Attempted to add window with exiting application token "
                             + ".%s Aborting.", token);
                     return WindowManagerGlobal.ADD_APP_EXITING;
diff --git a/services/core/java/com/android/server/wm/WindowToken.java b/services/core/java/com/android/server/wm/WindowToken.java
index 88a1458..057f493 100644
--- a/services/core/java/com/android/server/wm/WindowToken.java
+++ b/services/core/java/com/android/server/wm/WindowToken.java
@@ -82,10 +82,6 @@
     // will be shown.
     boolean waitingToShow;
 
-    // Set to true when this token is in a pending transaction where its
-    // windows will be put to the bottom of the list.
-    boolean sendingToBottom;
-
     /** The owner has {@link android.Manifest.permission#MANAGE_APP_TOKENS} */
     final boolean mOwnerCanManageAppTokens;
 
@@ -298,9 +294,8 @@
         pw.print(prefix); pw.print("windowType="); pw.print(windowType);
                 pw.print(" hidden="); pw.print(mHidden);
                 pw.print(" hasVisible="); pw.println(hasVisible);
-        if (waitingToShow || sendingToBottom) {
+        if (waitingToShow) {
             pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
-                    pw.print(" sendingToBottom="); pw.print(sendingToBottom);
         }
     }
 
diff --git a/services/tests/wmtests/src/com/android/server/wm/AppWindowTokenTests.java b/services/tests/wmtests/src/com/android/server/wm/AppWindowTokenTests.java
index bd336ad..1c15096 100644
--- a/services/tests/wmtests/src/com/android/server/wm/AppWindowTokenTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/AppWindowTokenTests.java
@@ -262,7 +262,6 @@
 
         mActivity.setOccludesParent(true);
         mActivity.setHidden(true);
-        mActivity.sendingToBottom = true;
         // Can not specify orientation if app isn't visible even though it occludes parent.
         assertEquals(SCREEN_ORIENTATION_UNSET, mActivity.getOrientation());
         // Can specify orientation if the current orientation candidate is orientation behind.
diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskStackTests.java b/services/tests/wmtests/src/com/android/server/wm/TaskStackTests.java
index 5877041..f5e65b1 100644
--- a/services/tests/wmtests/src/com/android/server/wm/TaskStackTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/TaskStackTests.java
@@ -102,10 +102,7 @@
                 WindowTestUtils.createTestActivityRecord(mDisplayContent);
         task2.addChild(activity2, 0);
         activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
-
         assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
-        task2.setSendingToBottom(true);
-        assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
     }
 
     @Test