From 3d3abd2c7b3e503f9ca65d7d27f4f00f611749b0 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Tue, 9 Apr 2019 23:20:12 -0700 Subject: [PATCH] Avoid exception from 'adb shell ime enable/disable' This is a preparation to introduce CTS for per-profile IME. Currently adb shell ime enable and adb shell ime disable can throw IllegalArgumentException when 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 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 cannot be enabled" 3. adb shell ime disable com.example.android.softkeyboard/.SoftKeyboard -> "Input method : already disabled" Change-Id: Ib9be7700557f2f606c90d62f79ec3afca2f82c40 --- .../InputMethodManagerService.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java index 6f1929fd464ad..d360a63624649 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 @@ public class InputMethodManagerService extends IInputMethodManager.Stub // ---------------------------------------------------------------------- - 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>> enabledInputMethodsList = mSettings .getEnabledInputMethodsAndSubtypeListLocked(); @@ -4697,6 +4699,14 @@ public class InputMethodManagerService extends IInputMethodManager.Stub 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();