Tighten rules for blocking activity starts.

This means we now block various cases where we failed to before,
including where an app launches an activity shortly after the home
button has been pressed, e.g. from within onPause() - either
immediately or after some blocking call.

Details:
- We no longer use process state at all, since a process may remain
  top for a time after the user has switched away and its
  windows have ceased to be visible.
- We no longer allow a start if callerApp.hasForegroundActivities(),
  because we are checking for windows visible to the user and an
  activity may be considered foreground even when it doesn't have such
  windows.
- Windows are only considered visible if isNowVisible() is true,
  rather than isVisible().

Bug: 128688247
Test: atest BackgroundActivityLaunchTest RootWindowContainerTests \
 WmTests:ActivityStarterTests \
 CtsActivityManagerDeviceTestCases:ActivityStarterTests
Test: Manual verification with self-restarting test app
Change-Id: I9863d8637661f15828ced435ff23812a4beef78d
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
index b424904..22e6a35 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@@ -5872,8 +5872,8 @@
     }
 
     boolean isUidForeground(int uid) {
-        return (getUidState(uid) == ActivityManager.PROCESS_STATE_TOP)
-                || mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(uid);
+        // A uid is considered to be foreground if it has a visible non-toast window.
+        return mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(uid);
     }
 
     boolean isDeviceOwner(int uid) {