Fix capturing mid-transition frames

To stop capturing mid-transition frames, we listen to visibility changes which indicate an incoming transition to/away from the captured task, and toggle the visibility of the recorded surface accordingly. This allows the virtual display to stop sending new frames before a transition starts, so users will simply see a frozen frame from before the transition instead of the exact last frame before the window is no longer visible (which sometimes is a mid-transition frame).

Bug: 314262003
Flag: None
Test: Manual testing
Change-Id: I0a540cfa4c06b6e7cac628781c4b1773f6d6f9aa
diff --git a/services/core/java/com/android/server/wm/ContentRecorder.java b/services/core/java/com/android/server/wm/ContentRecorder.java
index 8717098..a914c07 100644
--- a/services/core/java/com/android/server/wm/ContentRecorder.java
+++ b/services/core/java/com/android/server/wm/ContentRecorder.java
@@ -650,6 +650,14 @@
         if (isCurrentlyRecording() && mLastRecordedBounds != null) {
             mMediaProjectionManager.notifyActiveProjectionCapturedContentVisibilityChanged(
                     isVisibleRequested);
+
+            if (mContentRecordingSession.getContentToRecord() == RECORD_CONTENT_TASK) {
+                // If capturing a task, then the toggle visibility of the recorded surface to match
+                // visibility of the task, so we don't capture any mid-transition frames
+                mRecordedWindowContainer.getSyncTransaction()
+                        .setVisibility(mRecordedSurface, isVisibleRequested);
+                mRecordedWindowContainer.scheduleAnimation();
+            }
         }
     }