Avoid exception from 'adb shell ime enable/disable'

This is a preparation to introduce CTS for per-profile IME.

Currently
  adb shell ime enable <ime id>
and
  adb shell ime disable <ime id>
can throw IllegalArgumentException when <ime id> is not recognized by
the system.  This makes some sense, but if we start supporting
'-u all' option it'd make more sense if it just shows an error message
rather than letting the command crash with throing an exception.

This behavior change also removes a limitation that developers cannot
use
  adb shell ime disable <ime id>
command to remove an IME ID from Settings.Secure.ENABLED_INPUT_METHODS
if that IME is not currently installed.

Bug: 120784635
Test: Manually verified as follows.
  1. Build aosp_blueline-userdebug and flash it
  2. adb shell ime enable com.example.android.softkeyboard/.SoftKeyboard
     -> "Unknown input method <IME ID here> cannot be enabled"
  3. adb shell ime disable com.example.android.softkeyboard/.SoftKeyboard
     -> "Input method <IME ID here>: already disabled"
Change-Id: Ib9be7700557f2f606c90d62f79ec3afca2f82c40
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
index 6f1929f..d360a63 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
@@ -4101,13 +4101,15 @@
 
     // ----------------------------------------------------------------------
 
-    boolean setInputMethodEnabledLocked(String id, boolean enabled) {
-        // Make sure this is a valid input method.
-        InputMethodInfo imm = mMethodMap.get(id);
-        if (imm == null) {
-            throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
-        }
-
+    /**
+     * Enable or disable the given IME by updating {@link Settings.Secure#ENABLED_INPUT_METHODS}.
+     *
+     * @param id ID of the IME is to be manipulated. It is OK to pass IME ID that is currently not
+     *           recognized by the system.
+     * @param enabled {@code true} if {@code id} needs to be enabled.
+     * @return {@code true} if the IME was previously enabled. {@code false} otherwise.
+     */
+    private boolean setInputMethodEnabledLocked(String id, boolean enabled) {
         List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
                 .getEnabledInputMethodsAndSubtypeListLocked();
 
@@ -4697,6 +4699,14 @@
             if (!userHasDebugPriv(mSettings.getCurrentUserId(), shellCommand)) {
                 return ShellCommandResult.SUCCESS;
             }
+            // Make sure this is a valid input method.
+            if (enabled && !mMethodMap.containsKey(id)) {
+                final PrintWriter error = shellCommand.getErrPrintWriter();
+                error.print("Unknown input method ");
+                error.print(id);
+                error.println(" cannot be enabled");
+                return ShellCommandResult.SUCCESS;
+            }
             previouslyEnabled = setInputMethodEnabledLocked(id, enabled);
         }
         final PrintWriter pr = shellCommand.getOutPrintWriter();