Take screenshot before hiding the WSA.

The WSA can get hidden immediately in setVisibility. When that happens,
the screenshot will not be taken since it occurs right after the WSA was
hidden. Instead take the screenshot first and then hide the WSA.

Fixes: 73419173
Test: Open new app. Enter split screen from long press recents.
Open an app that can't be in split screen, like camera. Go to recents.
First app should show content in recents screenshot.

Change-Id: I928f7845e5f996f2784b44c9e43caa1a9c998f4b
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index 277a04b..a89b661 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -475,6 +475,20 @@
                 setClientHidden(!visible);
             }
 
+            if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
+                // The token is not closing nor opening, so even if there is an animation set, that
+                // doesn't mean that it goes through the normal app transition cycle so we have
+                // to inform the docked controller about visibility change.
+                // TODO(multi-display): notify docked divider on all displays where visibility was
+                // affected.
+                mService.getDefaultDisplayContentLocked().getDockedDividerController()
+                        .notifyAppVisibilityChanged();
+
+                // Take the screenshot before possibly hiding the WSA, otherwise the screenshot
+                // will not be taken.
+                mService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
+            }
+
             // 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
@@ -492,17 +506,6 @@
                 }
                 SurfaceControl.closeTransaction();
             }
-
-            if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
-                // The token is not closing nor opening, so even if there is an animation set, that
-                // doesn't mean that it goes through the normal app transition cycle so we have
-                // to inform the docked controller about visibility change.
-                // TODO(multi-display): notify docked divider on all displays where visibility was
-                // affected.
-                mService.getDefaultDisplayContentLocked().getDockedDividerController()
-                        .notifyAppVisibilityChanged();
-                mService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
-            }
         }
 
         return delayed;