Don't inherit task bounds from same instance in DMLPM

During phases of the DesktopModeLaunchParamsModifier calculations the
current activity can be made visible before the bounds phase is
complete. This causes it to be detected as a top visible freeform
activity and since it matches the launching activity, is seen as a
second instance where its bounds are inherited. However, since the
bounds phase is not complete, the bounds it inherits are wrong. To
prevent this, do not inherit the task bounds of an existing instance if
it is that same instance (has the same task).

Flag: com.android.window.flags.inherit_task_bounds_for_trampoline_task_launches
Bug: 422364585
Test: atest WmTests:DesktopModeLaunchParamsModifierTests

Change-Id: Id17a86110633cc00acfb14ce24736b7f95f4f591
diff --git a/services/core/java/com/android/server/wm/DesktopModeLaunchParamsModifier.java b/services/core/java/com/android/server/wm/DesktopModeLaunchParamsModifier.java
index 6ebb0ec..68a779b 100644
--- a/services/core/java/com/android/server/wm/DesktopModeLaunchParamsModifier.java
+++ b/services/core/java/com/android/server/wm/DesktopModeLaunchParamsModifier.java
@@ -354,6 +354,7 @@
         if (existingTaskActivity == null || launchingActivity == null) return false;
         return (Objects.equals(existingTaskActivity.packageName, launchingActivity.packageName))
                 && (existingTaskActivity.mUserId == launchingTask.mUserId)
+                && existingTaskActivity.getTask().mTaskId != launchingTask.mTaskId
                 && isLaunchingNewSingleTask(launchingActivity.launchMode)
                 && isClosingExitingInstance(launchingTask.getBaseIntent().getFlags());
     }
diff --git a/services/tests/wmtests/src/com/android/server/wm/DesktopModeLaunchParamsModifierTests.java b/services/tests/wmtests/src/com/android/server/wm/DesktopModeLaunchParamsModifierTests.java
index 50117ee..fb86ff4 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DesktopModeLaunchParamsModifierTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DesktopModeLaunchParamsModifierTests.java
@@ -576,6 +576,34 @@
     }
 
     @Test
+    @EnableFlags({Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MODE,
+            Flags.FLAG_INHERIT_TASK_BOUNDS_FOR_TRAMPOLINE_TASK_LAUNCHES})
+    public void testDontInheritTaskBoundsFromSameTask() {
+        setupDesktopModeLaunchParamsModifier();
+
+        final String packageName = "com.same.package";
+        // Setup existing task.
+        final DisplayContent dc = spy(createNewDisplay());
+        final Task existingFreeformTask = spy(new TaskBuilder(mSupervisor).setCreateActivity(true)
+                .setWindowingMode(WINDOWING_MODE_FREEFORM).setPackage(packageName).build());
+        existingFreeformTask.topRunningActivity().launchMode = LAUNCH_SINGLE_INSTANCE;
+        existingFreeformTask.setBounds(
+                /* left */ 0,
+                /* top */ 0,
+                /* right */ 500,
+                /* bottom */ 500);
+        doReturn(existingFreeformTask.getRootActivity()).when(dc)
+                .getTopMostVisibleFreeformActivity();
+        existingFreeformTask.onDisplayChanged(dc);
+        // Mock task to not trigger override bounds logic.
+        doReturn(false).when(existingFreeformTask).hasOverrideBounds();
+
+        // New instance should not inherit task bounds of old instance.
+        new CalculateRequestBuilder().setTask(existingFreeformTask).calculate();
+        assertNotEquals(existingFreeformTask.getBounds(), mResult.mBounds);
+    }
+
+    @Test
     @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MODE)
     @DisableFlags(Flags.FLAG_ENABLE_WINDOWING_DYNAMIC_INITIAL_BOUNDS)
     public void testUsesDesiredBoundsIfEmptyLayoutAndActivityOptionsBounds() {