Remove stacks from topTaks that no longer exist.

Bug: 119124086
Fix: 119124086
Fixes: 119124086
Test: tested locally.  See bug for repro steps.
Change-Id: If09dfc254cd60e4392c5021ef4dc7b353312af63
(cherry picked from commit cd731d2aa8c65a4cc8230cf6619ad8be7c743697)
diff --git a/service/src/com/android/car/SystemActivityMonitoringService.java b/service/src/com/android/car/SystemActivityMonitoringService.java
index 2df9b00..468dde4 100644
--- a/service/src/com/android/car/SystemActivityMonitoringService.java
+++ b/service/src/com/android/car/SystemActivityMonitoringService.java
@@ -85,6 +85,7 @@
         void onActivityLaunch(TopTaskInfoContainer topTask);
     }
 
+    private static final int INVALID_STACK_ID = -1;
     private final Context mContext;
     private final IActivityManager mAm;
     private final ProcessObserver mProcessObserver;
@@ -97,7 +98,7 @@
     private final SparseArray<TopTaskInfoContainer> mTopTasks = new SparseArray<>();
     /** K: uid, V : list of pid */
     private final Map<Integer, Set<Integer>> mForegroundUidPids = new ArrayMap<>();
-    private int mFocusedStackId = -1;
+    private int mFocusedStackId = INVALID_STACK_ID;
 
     /**
      * Temporary container to dispatch tasks for onActivityLaunch. Only used in handler thread.
@@ -253,7 +254,7 @@
             Log.e(CarLog.TAG_AM, "cannot getTasks", e);
             return;
         }
-        int focusedStackId = -1;
+        int focusedStackId = INVALID_STACK_ID;
         try {
             // TODO(b/66955160): Someone on the Auto-team should probably re-work the code in the
             // synchronized block below based on this new API.
@@ -269,10 +270,13 @@
         ActivityLaunchListener listener;
         synchronized (this) {
             listener = mActivityLaunchListener;
+            Set<Integer> allStackIds = new ArraySet<>(infos.size());
+            Set<Integer> stackIdsToRemove = new ArraySet<>(infos.size());
             for (StackInfo info : infos) {
                 int stackId = info.stackId;
+                allStackIds.add(info.stackId);
                 if (info.taskNames.length == 0 || !info.visible) { // empty stack or not shown
-                    mTopTasks.remove(stackId);
+                    stackIdsToRemove.add(stackId);
                     continue;
                 }
                 TopTaskInfoContainer newTopTaskInfo = new TopTaskInfoContainer(
@@ -290,6 +294,19 @@
                     }
                 }
             }
+            for (int i = 0; i < mTopTasks.size(); i++) {
+                TopTaskInfoContainer topTask = mTopTasks.valueAt(i);
+                if (topTask == null) {
+                    Log.wtf(CarLog.TAG_AM, "unexpected null value in sparse array");
+                    continue;
+                }
+                if (!allStackIds.contains(mTopTasks.keyAt(i))) {
+                    stackIdsToRemove.add(mTopTasks.keyAt(i));
+                }
+            }
+            for (int stackIdToRemove : stackIdsToRemove) {
+                mTopTasks.remove(stackIdToRemove);
+            }
             mFocusedStackId = focusedStackId;
         }
         if (listener != null) {