From 099f80ce9762e8b20998759fd6082bdcd6291103 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Wed, 10 Apr 2019 23:23:17 -0700 Subject: [PATCH] Add '-u ' option to 'adb shell ime set' This is a preparation to add end-to-end CTS for per-profile IME mode. In order to allow CTS tests to switch IME via shell command in multi-user environment, this CL adds '-u ' option to adb shell ime set Another notable behavior change in this CL is that adb shell ime set now gracefully fails with showing one-line error message when does not exist. Previously it failed with showing a stacktrace because of unhandled IllegalArgumentException. Bug: 122924287 Test: Manually tested as follows: 1. Build aosp_blueline-userdebug and flash it 2. make -j SoftKeyboard 3. adb install -r $OUT/system/app/SoftKeyboard/SoftKeyboard.apk 4. adb shell pm create-user test 5. adb shell am switch-user 10 6. adb shell ime enable -u 0 com.example.android.softkeyboard/.SoftKeyboard -> Input method com.example.android.softkeyboard/.SoftKeyboard: now enabled for user #0 7. adb shell ime set -u 0 com.example.android.softkeyboard/.SoftKeyboard -> Input method com.example.android.softkeyboard/.SoftKeyboard selected for user #0 Test: Manually tested as follows. 1. Build aosp_blueline-userdebug and flash it 2. adb shell pm create-user restricted_test 3. adb root 4. adb shell pm set-user-restriction --user 10 no_debugging_features 1 5. adb shell ime set -u all com.android.inputmethod.latin/.LatinIME -> Input method com.android.inputmethod.latin/.LatinIME: now disabled for user #0 User #10 is restricted with DISALLOW_DEBUGGING_FEATURES. Change-Id: I397cf0fb418a395dcafc0ab0d8d4e553b0f2eaab --- .../InputMethodManagerService.java | 60 ++++++++++++++++--- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java index 6d0796ab28a6a..7f9b3bd8fe03e 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java @@ -4607,9 +4607,13 @@ public class InputMethodManagerService extends IInputMethodManager.Stub pw.decreaseIndent(); pw.decreaseIndent(); - pw.println("set "); + pw.println("set [--user ] "); pw.increaseIndent(); pw.println("switches to the given input method ID."); + pw.increaseIndent(); + pw.print("--user : Specify which user to enable."); + pw.println(" Assumes the current user if not specified."); + pw.decreaseIndent(); pw.decreaseIndent(); pw.println("reset"); @@ -4810,17 +4814,55 @@ public class InputMethodManagerService extends IInputMethodManager.Stub @BinderThread @ShellCommandResult private int handleShellCommandSetInputMethod(@NonNull ShellCommand shellCommand) { - final String id = shellCommand.getNextArgRequired(); + final String imeId = shellCommand.getNextArgRequired(); + final PrintWriter out = shellCommand.getOutPrintWriter(); + final PrintWriter error = shellCommand.getErrPrintWriter(); + final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand); synchronized (mMethodMap) { - if (!userHasDebugPriv(mSettings.getCurrentUserId(), shellCommand)) { - return ShellCommandResult.SUCCESS; + final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved, + mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter()); + for (int userId : userIds) { + if (!userHasDebugPriv(userId, shellCommand)) { + continue; + } + boolean failedToSelectUnknownIme = false; + if (userId == mSettings.getCurrentUserId()) { + if (mMethodMap.containsKey(imeId)) { + setInputMethodLocked(imeId, NOT_A_SUBTYPE_ID); + } else { + failedToSelectUnknownIme = true; + } + } else { + final ArrayMap methodMap = new ArrayMap<>(); + final ArrayList methodList = new ArrayList<>(); + final ArrayMap> additionalSubtypeMap = + new ArrayMap<>(); + AdditionalSubtypeUtils.load(additionalSubtypeMap, userId); + queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, + methodMap, methodList); + final InputMethodSettings settings = new InputMethodSettings( + mContext.getResources(), mContext.getContentResolver(), methodMap, + userId, false); + if (methodMap.containsKey(imeId)) { + settings.putSelectedInputMethod(imeId); + settings.putSelectedSubtype(NOT_A_SUBTYPE_ID); + } else { + failedToSelectUnknownIme = true; + } + } + if (failedToSelectUnknownIme) { + error.print("Unknown input method "); + error.print(imeId); + error.print(" cannot be selected for user #"); + error.println(userId); + } else { + out.print("Input method "); + out.print(imeId); + out.print(" selected for user #"); + error.println(userId); + } } - setInputMethodLocked(id, NOT_A_SUBTYPE_ID); } - final PrintWriter pr = shellCommand.getOutPrintWriter(); - pr.print("Input method "); - pr.print(id); - pr.println(" selected"); return ShellCommandResult.SUCCESS; }