Merge "Allow SplitScreenController to explicitly request a new instance." into main
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt
index e078c7e1..4440778 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt
@@ -1225,7 +1225,8 @@
                     .determineNewInstancePosition(callingTaskInfo)
                 splitScreenController.startIntent(
                     launchIntent, context.userId, fillIn, splitPosition,
-                    options.toBundle(), null /* hideTaskToken */
+                    options.toBundle(), null /* hideTaskToken */,
+                    true /* forceLaunchNewTask */
                 )
             }
             WINDOWING_MODE_FREEFORM -> {
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/SplitScreenController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/SplitScreenController.java
index 9e39f44..a23b576 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/SplitScreenController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/SplitScreenController.java
@@ -772,15 +772,25 @@
                 instanceId);
     }
 
-    /**
-     * Starts the given intent into split.
-     * @param hideTaskToken If non-null, a task matching this token will be moved to back in the
-     *                      same window container transaction as the starting of the intent.
-     */
     @Override
     public void startIntent(PendingIntent intent, int userId1, @Nullable Intent fillInIntent,
             @SplitPosition int position, @Nullable Bundle options,
             @Nullable WindowContainerToken hideTaskToken) {
+        startIntent(intent, userId1, fillInIntent, position, options, hideTaskToken,
+                false /* forceLaunchNewTask */);
+    }
+
+    /**
+     * Starts the given intent into split.
+     *
+     * @param hideTaskToken If non-null, a task matching this token will be moved to back in the
+     *                      same window container transaction as the starting of the intent.
+     * @param forceLaunchNewTask If true, this method will skip the check for a background task
+     *                           matching the intent and launch a new task.
+     */
+    public void startIntent(PendingIntent intent, int userId1, @Nullable Intent fillInIntent,
+            @SplitPosition int position, @Nullable Bundle options,
+            @Nullable WindowContainerToken hideTaskToken, boolean forceLaunchNewTask) {
         ProtoLog.v(ShellProtoLogGroup.WM_SHELL_SPLIT_SCREEN,
                 "startIntent(): intent=%s user=%d fillInIntent=%s position=%d", intent, userId1,
                 fillInIntent, position);
@@ -798,8 +808,9 @@
         // To prevent accumulating large number of instances in the background, reuse task
         // in the background. If we don't explicitly reuse, new may be created even if the app
         // isn't multi-instance because WM won't automatically remove/reuse the previous instance
-        final ActivityManager.RecentTaskInfo taskInfo = mRecentTasksOptional
-                .map(recentTasks -> recentTasks.findTaskInBackground(component, userId1,
+        final ActivityManager.RecentTaskInfo taskInfo = forceLaunchNewTask ? null :
+                mRecentTasksOptional
+                        .map(recentTasks -> recentTasks.findTaskInBackground(component, userId1,
                         hideTaskToken))
                 .orElse(null);
         if (taskInfo != null) {
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt
index 6531e2a..f937657 100644
--- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt
@@ -2902,7 +2902,8 @@
     runOpenNewWindow(task)
     verify(splitScreenController)
       .startIntent(any(), anyInt(), any(), any(),
-        optionsCaptor.capture(), anyOrNull())
+        optionsCaptor.capture(), anyOrNull(), eq(true)
+      )
     assertThat(ActivityOptions.fromBundle(optionsCaptor.value).launchWindowingMode)
       .isEqualTo(WINDOWING_MODE_MULTI_WINDOW)
   }
@@ -2917,7 +2918,7 @@
     verify(splitScreenController)
       .startIntent(
         any(), anyInt(), any(), any(),
-        optionsCaptor.capture(), anyOrNull()
+        optionsCaptor.capture(), anyOrNull(), eq(true)
       )
     assertThat(ActivityOptions.fromBundle(optionsCaptor.value).launchWindowingMode)
       .isEqualTo(WINDOWING_MODE_MULTI_WINDOW)
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/splitscreen/SplitScreenControllerTests.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/splitscreen/SplitScreenControllerTests.java
index 9260a07..ef3af8e 100644
--- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/splitscreen/SplitScreenControllerTests.java
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/splitscreen/SplitScreenControllerTests.java
@@ -297,6 +297,28 @@
     }
 
     @Test
+    public void startIntent_forceLaunchNewTaskTrue_skipsBackgroundTasks() {
+        Intent startIntent = createStartIntent("startActivity");
+        PendingIntent pendingIntent =
+                PendingIntent.getActivity(mContext, 0, startIntent, FLAG_IMMUTABLE);
+        mSplitScreenController.startIntent(pendingIntent, mContext.getUserId(), null,
+                SPLIT_POSITION_TOP_OR_LEFT, null /* options */, null /* hideTaskToken */,
+                true /* forceLaunchNewTask */);
+        verify(mRecentTasks, never()).findTaskInBackground(any(), anyInt(), any());
+    }
+
+    @Test
+    public void startIntent_forceLaunchNewTaskFalse_checksBackgroundTasks() {
+        Intent startIntent = createStartIntent("startActivity");
+        PendingIntent pendingIntent =
+                PendingIntent.getActivity(mContext, 0, startIntent, FLAG_IMMUTABLE);
+        mSplitScreenController.startIntent(pendingIntent, mContext.getUserId(), null,
+                SPLIT_POSITION_TOP_OR_LEFT, null /* options */, null /* hideTaskToken */,
+                false /* forceLaunchNewTask */);
+        verify(mRecentTasks).findTaskInBackground(any(), anyInt(), any());
+    }
+
+    @Test
     public void testSwitchSplitPosition_checksIsSplitScreenVisible() {
         final String reason = "test";
         when(mSplitScreenController.isSplitScreenVisible()).thenReturn(true, false);