Dismiss dream with launchTaskBehind

When we are launching underneath an AOT screensaver, force launch-behind,
attach the process, and go through the resume lifecycle events instead
of creating the activity in stopped state.

The check for turning the window on during relayout also needs a fix to
check for isDreaming() as well as isInteractive() when deciding whether
to call PowerManager.wakeUp() during window relayout.

Test: atest DreamManagerServiceTests
Bug: 154290545
Bug: 159821769
Change-Id: Ib3f6e6caad3abd793f5c66eece8beffcffde2017
diff --git a/services/core/java/com/android/server/wm/ActivityStarter.java b/services/core/java/com/android/server/wm/ActivityStarter.java
index daa97b5..d2f4ea6 100644
--- a/services/core/java/com/android/server/wm/ActivityStarter.java
+++ b/services/core/java/com/android/server/wm/ActivityStarter.java
@@ -1662,6 +1662,12 @@
                     mTargetStack.setAlwaysOnTop(true);
                 }
             }
+            if (!mTargetStack.isTopStackInDisplayArea() && mService.mInternal.isDreaming()) {
+                // Launching underneath dream activity (fullscreen, always-on-top). Run the launch-
+                // -behind transition so the Activity gets created and starts in visible state.
+                mLaunchTaskBehind = true;
+                r.mLaunchTaskBehind = true;
+            }
         }
 
         mService.mUgmInternal.grantUriPermissionUncheckedFromIntent(intentGrants,
@@ -1911,6 +1917,12 @@
             return START_SUCCESS;
         }
 
+        // At this point we are certain we want the task moved to the front. If we need to dismiss
+        // any other always-on-top stacks, now is the time to do it.
+        if (targetTaskTop.canTurnScreenOn() && mService.mInternal.isDreaming()) {
+            targetTaskTop.mStackSupervisor.wakeUp("recycleTask#turnScreenOnFlag");
+        }
+
         if (mMovedToFront) {
             // We moved the task to front, use starting window to hide initial drawn delay.
             targetTaskTop.showStartingWindow(null /* prev */, false /* newTask */,
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index bb9b8b6..1f34f0a 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -2685,7 +2685,9 @@
                             Settings.Global.THEATER_MODE_ON, 0) == 0;
             boolean canTurnScreenOn = mActivityRecord == null || mActivityRecord.currentLaunchCanTurnScreenOn();
 
-            if (allowTheaterMode && canTurnScreenOn && !mPowerManagerWrapper.isInteractive()) {
+            if (allowTheaterMode && canTurnScreenOn
+                        && (mWmService.mAtmInternal.isDreaming()
+                        || !mPowerManagerWrapper.isInteractive())) {
                 if (DEBUG_VISIBILITY || DEBUG_POWER) {
                     Slog.v(TAG, "Relayout window turning screen on: " + this);
                 }