Pre-emptively take a snapshot when finishing an activity before changing visibility

- Finishing an activity in force landscape will normally trigger a
  visibility change and configuration change before the closing apps
  are handled (when snapshots are normally taken) resulting in snapshots
  showing for the wrong orientation.  Preemptively take the screenshot
  if we know that we are finishing the activity and ignore the subsequent
  closing app snapshot.

Bug: 132687470
Test: Go into forced landscape app, back out of the app, then go to
      Overview and ensure that the full snapshot is shown in the right
      orientation

Change-Id: I8f9283188da00a09e499c3c16eff5b45fcaa73d1
diff --git a/services/core/java/com/android/server/wm/ActivityStack.java b/services/core/java/com/android/server/wm/ActivityStack.java
index 74c3069..5591cb3 100644
--- a/services/core/java/com/android/server/wm/ActivityStack.java
+++ b/services/core/java/com/android/server/wm/ActivityStack.java
@@ -164,6 +164,8 @@
 import com.android.server.am.EventLogTags;
 import com.android.server.am.PendingIntentRecord;
 
+import com.google.android.collect.Sets;
+
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.lang.ref.WeakReference;
@@ -4022,6 +4024,14 @@
                 }
                 getDisplay().mDisplayContent.prepareAppTransition(transit, false);
 
+                // When finishing the activity pre-emptively take the snapshot before the app window
+                // is marked as hidden and any configuration changes take place
+                if (mWindowManager.mTaskSnapshotController != null) {
+                    final ArraySet<Task> tasks = Sets.newArraySet(task.mTask);
+                    mWindowManager.mTaskSnapshotController.snapshotTasks(tasks);
+                    mWindowManager.mTaskSnapshotController.addSkipClosingAppSnapshotTasks(tasks);
+                }
+
                 // Tell window manager to prepare for this one to be removed.
                 r.setVisibility(false);