Defer hiding clients until animation is done

This is a preparation for remote animations: We used to set app
visibility state immediately after we started the animation.
However, with remote animations, we'd like to allow them drawing
until the transition is done. For that, we defer hiding the client
until the animation is done.

Instead of special-casing remote animations, we do it for all
apps, as there is no harm in doing so.

Test: Open YouTube, make sure it enters Auto-PIP when pressing
home.
Test: go/wm-smoke
Test: Open trace with open/closing a couple of apps. Make sure
app visibility gets dispatched at the correct time.
Test: WindowStateTests

Bug: 64674361
Change-Id: I8deb6a97ca1c3d8f4a70a6e045f45a6bc16604bb
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index f2098dc..0dfef3a 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -376,7 +376,6 @@
         // Reset the state of mHiddenSetFromTransferredStartingWindow since visibility is actually
         // been set by the app now.
         mHiddenSetFromTransferredStartingWindow = false;
-        setClientHidden(!visible);
 
         // Allow for state changes and animation to be applied if:
         // * token is transitioning visibility state
@@ -461,6 +460,12 @@
                 mService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(token);
             }
 
+            // Update the client visibility if we are not running an animation. Otherwise, we'll
+            // update client visibility state in onAnimationFinished.
+            if (!visible && !delayed) {
+                setClientHidden(true);
+            }
+
             // If we are hidden but there is no delay needed we immediately
             // apply the Surface transaction so that the ActivityManager
             // can have some guarantee on the Surface state following
@@ -1721,6 +1726,7 @@
                 "AppWindowToken");
 
         clearThumbnail();
+        setClientHidden(isHidden());
 
         if (mService.mInputMethodTarget != null && mService.mInputMethodTarget.mAppToken == this) {
             getDisplayContent().computeImeTarget(true /* updateImeTarget */);