Fix IME Keyguard exit animation

Keyguard exit animation was always applied no matter whether the
current window is shown or not. Because Keyguard exit starts with
alpha=0, this lead to a flicker with the IME window as a frame
later IME was applied its dismissal animation, which reverts it to
alpha=1 at the beginning.

Fix this by only applying exit animation if window is currently
hidden only by policy. Requires readding the old visibility method
*cough cough cough*

Ideally we would apply non-app window policy visibility/animation
at the same place...but today is not the day! (to minimize risk)

Test: Set password, unlock phone
Test: Don't set password, unlock to activity with IME up
Change-Id: I46110c80da4438223282903fd4defeba2c3f0550
Fixes: 36200726
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index c59065f..257f285 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -2494,7 +2494,8 @@
     void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
         final WindowManagerPolicy policy = mService.mPolicy;
         forAllWindows(w -> {
-            if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
+            if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
+                    && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
                 w.mWinAnimator.setAnimation(
                         policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
             }
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index 67516c1..25b6561 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -1292,36 +1292,14 @@
 
     @Override
     boolean isVisible() {
-        // TODO: The check for hiddenRequested is commented out below, because the window can still
-        // be visible on screen when the flag is true. We would like the isVisible() method to
-        // return an answer closer to if the window is truly visible (can't be an exact answer
-        // without checking the surface state), so comment out the check for now so we can test to
-        // see what problem it causes.
-        // If it doesn't cause any issues, then we can remove just before we lock down the current
-        // release (O) and also consolidate this method with #isVisibleUnchecked() and possibly
-        // other methods like isVisibleNow().
-        // If it does cause problems, then we can look if there are other ways to solve the problem.
-        // If there isn't then uncomment and document here why it is needed.
-        if (/*(mAppToken == null || !mAppToken.hiddenRequested) && */isVisibleUnchecked()
-            // TODO: The window isn't considered visible when the token is hidden, however
-            // uncommenting the check below breaks the visual transition from an app to the launcher
-            // if the home buttons is pressed. Need to investigate an fix that issue before
-            // uncommenting.
-            /* && !mToken.hidden*/) {
-            // Is this window visible?  It is not visible if there is no surface, or we are in the
-            // process of running an exit animation that will remove the surface, or its app token
-            // has been hidden.
-            return true;
-        }
-        return false;
+        return wouldBeVisibleIfPolicyIgnored() && mPolicyVisibility;
     }
 
     /**
-     * Does the minimal check for visibility. Callers generally want to use one of the public
-     * methods as they perform additional checks on the app token.
-     * TODO: See if there are other places we can use this check below instead of duplicating...
+     * @return True if the window would be visible if we'd ignore policy visibility, false
+     *         otherwise.
      */
-    private boolean isVisibleUnchecked() {
+    boolean wouldBeVisibleIfPolicyIgnored() {
         return mHasSurface && mPolicyVisibility && !isParentWindowHidden()
                 && !mAnimatingExit && !mDestroying && (!mIsWallpaper || mWallpaperVisible);
     }
@@ -1338,7 +1316,7 @@
     // TODO: Can we consolidate this with #isVisible() or have a more appropriate name for this?
     boolean isWinVisibleLw() {
         return (mAppToken == null || !mAppToken.hiddenRequested || mAppToken.mAppAnimator.animating)
-                && isVisibleUnchecked();
+                && isVisible();
     }
 
     /**
@@ -1347,7 +1325,7 @@
      */
     boolean isVisibleNow() {
         return (!mToken.hidden || mAttrs.type == TYPE_APPLICATION_STARTING)
-                && isVisibleUnchecked();
+                && isVisible();
     }
 
     /**
@@ -2063,7 +2041,7 @@
             // If app died visible, apply a dim over the window to indicate that it's inactive
             dc.mDimLayerController.applyDimAbove(getDimLayerUser(), mWinAnimator);
         } else if ((mAttrs.flags & FLAG_DIM_BEHIND) != 0
-                && dc != null && !mAnimatingExit && isVisibleUnchecked()) {
+                && dc != null && !mAnimatingExit && isVisible()) {
             dc.mDimLayerController.applyDimBehind(getDimLayerUser(), mWinAnimator);
         }
     }