Hide activities when AOD comes in

Display timeout does not lock the keyguard, which means that
activities won't immediatelly disappear.

We need to factor AOD into to equation to make sure that
the user won't see phantom activities under the status bar
window when the display times out.

Fixes: 73120928
Test: open Settings, wait for display timeout
Test: Wait for display timeout, press power, see launcher
Test: Launch maps, start navigation (FLAG_SHOW_WHEN_LOCKED)
Test: Go to AOD, press power button: activity is occluding keyguard
Test: Press home: bouncer shows up
Test: Unlock: maps goes into pip mode
Test: Go to AOD and back to lock screen: keyguard visible
Test: Expand maps, go to AOD, back to lock screen: maps is occluding keyguard
Change-Id: I9b7512313af2f851d8788ec53de7880bce3bed2c
diff --git a/services/core/java/com/android/server/am/KeyguardController.java b/services/core/java/com/android/server/am/KeyguardController.java
index b67dd0d..1b7f75b 100644
--- a/services/core/java/com/android/server/am/KeyguardController.java
+++ b/services/core/java/com/android/server/am/KeyguardController.java
@@ -62,6 +62,7 @@
     private final ActivityStackSupervisor mStackSupervisor;
     private WindowManagerService mWindowManager;
     private boolean mKeyguardShowing;
+    private boolean mAodShowing;
     private boolean mKeyguardGoingAway;
     private boolean mOccluded;
     private boolean mDismissalRequested;
@@ -82,6 +83,15 @@
     }
 
     /**
+     * @return true if either Keyguard or AOD are showing, not going away, and not being occluded
+     *         on the given display, false otherwise
+     */
+    boolean isKeyguardOrAodShowing(int displayId) {
+        return (mKeyguardShowing || mAodShowing) && !mKeyguardGoingAway &&
+                (displayId == DEFAULT_DISPLAY ? !mOccluded : displayId == mSecondaryDisplayShowing);
+    }
+
+    /**
      * @return true if Keyguard is showing, not going away, and not being occluded on the given
      *         display, false otherwise
      */
@@ -108,17 +118,19 @@
     /**
      * Update the Keyguard showing state.
      */
-    void setKeyguardShown(boolean showing, int secondaryDisplayShowing) {
-        boolean showingChanged = showing != mKeyguardShowing;
+    void setKeyguardShown(boolean keyguardShowing, boolean aodShowing,
+            int secondaryDisplayShowing) {
+        boolean showingChanged = keyguardShowing != mKeyguardShowing || aodShowing != mAodShowing;
         if (!showingChanged && secondaryDisplayShowing == mSecondaryDisplayShowing) {
             return;
         }
-        mKeyguardShowing = showing;
+        mKeyguardShowing = keyguardShowing;
+        mAodShowing = aodShowing;
         mSecondaryDisplayShowing = secondaryDisplayShowing;
         if (showingChanged) {
             dismissDockedStackIfNeeded();
             setKeyguardGoingAway(false);
-            if (showing) {
+            if (keyguardShowing) {
                 mDismissalRequested = false;
             }
         }
@@ -230,8 +242,8 @@
         // Allow to show it when we are about to dismiss Keyguard. This isn't allowed if r is
         // already the dismissing activity, in which case we don't allow it to repeatedly dismiss
         // Keyguard.
-        return dismissKeyguard && canDismissKeyguard() &&
-                (mDismissalRequested || r != mDismissingKeyguardActivity);
+        return dismissKeyguard && canDismissKeyguard() && !mAodShowing
+                && (mDismissalRequested || r != mDismissingKeyguardActivity);
     }
 
     /**
@@ -369,9 +381,9 @@
     }
 
     private void updateKeyguardSleepToken() {
-        if (mSleepToken == null && isKeyguardShowing(DEFAULT_DISPLAY)) {
+        if (mSleepToken == null && isKeyguardOrAodShowing(DEFAULT_DISPLAY)) {
             mSleepToken = mService.acquireSleepToken("Keyguard", DEFAULT_DISPLAY);
-        } else if (mSleepToken != null && !isKeyguardShowing(DEFAULT_DISPLAY)) {
+        } else if (mSleepToken != null && !isKeyguardOrAodShowing(DEFAULT_DISPLAY)) {
             mSleepToken.release();
             mSleepToken = null;
         }
@@ -380,6 +392,7 @@
     void dump(PrintWriter pw, String prefix) {
         pw.println(prefix + "KeyguardController:");
         pw.println(prefix + "  mKeyguardShowing=" + mKeyguardShowing);
+        pw.println(prefix + "  mAodShowing=" + mAodShowing);
         pw.println(prefix + "  mKeyguardGoingAway=" + mKeyguardGoingAway);
         pw.println(prefix + "  mOccluded=" + mOccluded);
         pw.println(prefix + "  mDismissingKeyguardActivity=" + mDismissingKeyguardActivity);