Merge "Allow current IME to start background activity"
diff --git a/services/core/java/com/android/server/wm/ActivityStarter.java b/services/core/java/com/android/server/wm/ActivityStarter.java
index 5208fd5..d560839 100644
--- a/services/core/java/com/android/server/wm/ActivityStarter.java
+++ b/services/core/java/com/android/server/wm/ActivityStarter.java
@@ -1281,6 +1281,15 @@
             return false;
         }
 
+        // IME should always be allowed to start activity, like IME settings.
+        final WindowState imeWindow = mRootWindowContainer.getCurrentInputMethodWindow();
+        if (imeWindow != null && callingAppId == imeWindow.mOwnerUid) {
+            if (DEBUG_ACTIVITY_STARTS) {
+                Slog.d(TAG, "Activity start allowed for active ime (" + callingUid + ")");
+            }
+            return false;
+        }
+
         // App switching will be allowed if BAL app switching flag is not enabled, or if
         // its app switching rule allows it.
         // This is used to block background activity launch even if the app is still
diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
index f607448..f745ffb 100644
--- a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
@@ -126,6 +126,7 @@
     private static final String FAKE_CALLING_PACKAGE = "com.whatever.dude";
     private static final int UNIMPORTANT_UID = 12345;
     private static final int UNIMPORTANT_UID2 = 12346;
+    private static final int CURRENT_IME_UID = 12347;
 
     @Before
     public void setUp() throws Exception {
@@ -315,6 +316,12 @@
         return prepareStarter(launchFlags, mockGetLaunchStack, LAUNCH_MULTIPLE);
     }
 
+    private void setupImeWindow() {
+        final WindowState imeWindow = createWindow(null, W_INPUT_METHOD,
+                "mImeWindow", CURRENT_IME_UID);
+        mDisplayContent.mInputMethodWindow = imeWindow;
+    }
+
     /**
      * Creates a {@link ActivityStarter} with default parameters and necessary mocks.
      *
@@ -654,6 +661,14 @@
                 UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
                 false, false, false, false, true);
+
+        setupImeWindow();
+        runAndVerifyBackgroundActivityStartsSubtest(
+                "disallowed_callingPackageNameIsIme_notAborted", false,
+                CURRENT_IME_UID, false, PROCESS_STATE_TOP + 1,
+                UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
+                false, false, false, false, false);
+
     }
 
     private void runAndVerifyBackgroundActivityStartsSubtest(String name, boolean shouldHaveAborted,