Copy the source before modifying it before dispatching

Otherwise, we will modify the same instance referenced by the raw
InsetsState at the server side.

Fix: 154292314
Test: atest ChildlessActivityTest InsetsStateControllerTest
Change-Id: I1cc0122dc99ed00ad904c13158cb4b56e485401f
diff --git a/services/core/java/com/android/server/wm/InsetsStateController.java b/services/core/java/com/android/server/wm/InsetsStateController.java
index 4ac319d..125e200 100644
--- a/services/core/java/com/android/server/wm/InsetsStateController.java
+++ b/services/core/java/com/android/server/wm/InsetsStateController.java
@@ -170,8 +170,13 @@
         }
 
         if (aboveIme) {
-            state = new InsetsState(state);
-            state.setSourceVisible(ITYPE_IME, false);
+            InsetsSource imeSource = state.peekSource(ITYPE_IME);
+            if (imeSource != null && imeSource.isVisible()) {
+                imeSource = new InsetsSource(imeSource);
+                imeSource.setVisible(false);
+                state = new InsetsState(state);
+                state.addSource(imeSource);
+            }
         }
 
         return state;
diff --git a/services/tests/wmtests/src/com/android/server/wm/InsetsStateControllerTest.java b/services/tests/wmtests/src/com/android/server/wm/InsetsStateControllerTest.java
index 9f28f45..2af98d8 100644
--- a/services/tests/wmtests/src/com/android/server/wm/InsetsStateControllerTest.java
+++ b/services/tests/wmtests/src/com/android/server/wm/InsetsStateControllerTest.java
@@ -153,6 +153,21 @@
     }
 
     @Test
+    public void testStripForDispatch_independentSources() {
+        getController().getSourceProvider(ITYPE_IME).setWindow(mImeWindow, null, null);
+
+        final WindowState app1 = createWindow(null, TYPE_APPLICATION, "app1");
+        app1.mBehindIme = true;
+
+        final WindowState app2 = createWindow(null, TYPE_APPLICATION, "app2");
+        app2.mBehindIme = false;
+
+        getController().getRawInsetsState().setSourceVisible(ITYPE_IME, true);
+        assertFalse(getController().getInsetsForDispatch(app2).getSource(ITYPE_IME).isVisible());
+        assertTrue(getController().getInsetsForDispatch(app1).getSource(ITYPE_IME).isVisible());
+    }
+
+    @Test
     public void testStripForDispatch_belowIme() {
         getController().getSourceProvider(ITYPE_IME).setWindow(mImeWindow, null, null);