Run activity visibility adjustment even if there is no top running.

Ensuring visibility is triggered in unpredictable fashion, e.g. it can
be triggered by attachment of application. It can happen between an
activity stopping due to change of user, but before that operation is
finished and as a result the stopped activity will get marked as
visible even if we mark it as invisible in stop. Instead of trying the
activity as invisbile during stopping, we adjust the visibility ensuring
method to go through stack contents even if there is no top running
activity (because the top running activity is run by a different user)
and force the correct visibility.

Bug: 25958227

Change-Id: I4662667e8151fa25d947bacf33ba915389dd32e8
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index a6af0d10..18b3e62 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -2702,7 +2702,6 @@
             Configuration config) {
         if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
 
-        ArrayList<ActivityRecord> stops = null;
         ArrayList<ActivityRecord> finishes = null;
         ArrayList<UserState> startingUsers = null;
         int NS = 0;
@@ -2756,7 +2755,7 @@
         }
 
         // Atomically retrieve all of the other things to do.
-        stops = processStoppingActivitiesLocked(true);
+        final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
         NS = stops != null ? stops.size() : 0;
         if ((NF = mFinishingActivities.size()) > 0) {
             finishes = new ArrayList<>(mFinishingActivities);
@@ -4304,12 +4303,14 @@
         mWindowManager.getStackBounds(stack.mStackId, info.bounds);
         info.displayId = Display.DEFAULT_DISPLAY;
         info.stackId = stack.mStackId;
+        info.userId = stack.mCurrentUser;
 
         ArrayList<TaskRecord> tasks = stack.getAllTasks();
         final int numTasks = tasks.size();
         int[] taskIds = new int[numTasks];
         String[] taskNames = new String[numTasks];
         Rect[] taskBounds = new Rect[numTasks];
+        int[] taskUserIds = new int[numTasks];
         for (int i = 0; i < numTasks; ++i) {
             final TaskRecord task = tasks.get(i);
             taskIds[i] = task.taskId;
@@ -4319,10 +4320,12 @@
                     : "unknown";
             taskBounds[i] = new Rect();
             mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
+            taskUserIds[i] = task.userId;
         }
         info.taskIds = taskIds;
         info.taskNames = taskNames;
         info.taskBounds = taskBounds;
+        info.taskUserIds = taskUserIds;
         return info;
     }