Fixed an issue that activity might be invisible after unlocking.

A just resumed activity can be stopped due to the API
setLockScreenShown been called after keyguardGoingAway.
When receive keyguardGoingAway, AM will goes to resume top activity,
and WM goes to prepare transition, at this moment isKeyguardOrAodShowing
return false.
However if user has enable AOD, systemUI will also receive AOD change and
call setLockScreenShown because AOD turns from on to off, but
setLockScreenShown is post to another thread, so it cannot be ensured to
be called before keyguardGoingAway. If this happen after
keyguardGoingAway, isKeyguardOrAodShowing would become true and push
the resumed activity to stopped list.

Solution:
- Post keyguardGoingAway to the same thread so those calls to ATM will
be in order.
- setKeyguardGoingAway is irrelevant to AOD state.
- Switch the sequence of updateKeyguardSleepToken and
ensureActivitiesVisible, if there is an display awake from sleep, the
top activity can be resumed then visible.

Bug: 130311385
Test: atest KeyguardTests KeyguardLockedTests MultiDisplayLockedKeyguardTests
	    MultiDisplayKeyguardTests ActivityLifecycleKeyguardTests

Change-Id: I7ecad050e6db9ab3486afedf9b4101965e800eba
diff --git a/services/core/java/com/android/server/wm/KeyguardController.java b/services/core/java/com/android/server/wm/KeyguardController.java
index df36b09..169f03b 100644
--- a/services/core/java/com/android/server/wm/KeyguardController.java
+++ b/services/core/java/com/android/server/wm/KeyguardController.java
@@ -133,27 +133,33 @@
      * Update the Keyguard showing state.
      */
     void setKeyguardShown(boolean keyguardShowing, boolean aodShowing) {
-        boolean showingChanged = keyguardShowing != mKeyguardShowing || aodShowing != mAodShowing;
         // If keyguard is going away, but SystemUI aborted the transition, need to reset state.
-        showingChanged |= mKeyguardGoingAway && keyguardShowing;
-        if (!showingChanged) {
+        final boolean keyguardChanged = keyguardShowing != mKeyguardShowing
+                || mKeyguardGoingAway && keyguardShowing;
+        final boolean aodChanged = aodShowing != mAodShowing;
+        if (!keyguardChanged && !aodChanged) {
             return;
         }
         mKeyguardShowing = keyguardShowing;
         mAodShowing = aodShowing;
         mWindowManager.setAodShowing(aodShowing);
-        if (showingChanged) {
+
+        if (keyguardChanged) {
+            // Irrelevant to AOD.
             dismissDockedStackIfNeeded();
             setKeyguardGoingAway(false);
-            // TODO(b/113840485): Check usage for non-default display
-            mWindowManager.setKeyguardOrAodShowingOnDefaultDisplay(
-                    isKeyguardOrAodShowing(DEFAULT_DISPLAY));
             if (keyguardShowing) {
                 mDismissalRequested = false;
             }
         }
-        mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
+        // TODO(b/113840485): Check usage for non-default display
+        mWindowManager.setKeyguardOrAodShowingOnDefaultDisplay(
+                isKeyguardOrAodShowing(DEFAULT_DISPLAY));
+
+        // Update the sleep token first such that ensureActivitiesVisible has correct sleep token
+        // state when evaluating visibilities.
         updateKeyguardSleepToken();
+        mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
     }
 
     /**