Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2d4b343b authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Add '-u <user id>' option to 'adb shell ime reset'

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 <user id>' option to
  adb shell ime reset

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 ime enable com.example.android.softkeyboard/.SoftKeyboard
  5. adb shell ime set com.example.android.softkeyboard/.SoftKeyboard
  6. adb shell pm create-user test
  7. adb shell am switch-user 10
  8. adb shell ime list -s -u 0
     -> com.android.inputmethod.latin/.LatinIME
        com.example.android.softkeyboard/.SoftKeyboard
  9. adb shell ime reset -u 0
     -> Reset current and enabled IMEs for user #0
          Selected: com.android.inputmethod.latin/.LatinIME
           Enabled: com.android.inputmethod.latin/.LatinIME
 10. adb shell ime list -s -u 0
     -> com.android.inputmethod.latin/.LatinIME
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 reset -u all
     -> Reset current and enabled IMEs for user #0
          Selected: com.android.inputmethod.latin/.LatinIME
           Enabled: com.android.inputmethod.latin/.LatinIME
        User #10 is restricted with DISALLOW_DEBUGGING_FEATURES
Change-Id: Ie330c6faa76f366b78c15c0161873a051a3d40e3
parent 099f80ce
Loading
Loading
Loading
Loading
+63 −36
Original line number Original line Diff line number Diff line
@@ -4616,10 +4616,15 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
                pw.decreaseIndent();
                pw.decreaseIndent();
                pw.decreaseIndent();
                pw.decreaseIndent();


                pw.println("reset");
                pw.println("reset [--user <USER_ID>]");
                pw.increaseIndent();
                pw.increaseIndent();
                pw.println("reset currently selected/enabled IMEs to the default ones as if "
                pw.println("reset currently selected/enabled IMEs to the default ones as if "
                        + "the device is initially booted with the current locale.");
                        + "the device is initially booted with the current locale.");
                pw.increaseIndent();
                pw.print("--user <USER_ID>: Specify which user to reset.");
                pw.println(" Assumes the current user if not specified.");
                pw.decreaseIndent();

                pw.decreaseIndent();
                pw.decreaseIndent();


                pw.decreaseIndent();
                pw.decreaseIndent();
@@ -4874,12 +4879,18 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
    @BinderThread
    @BinderThread
    @ShellCommandResult
    @ShellCommandResult
    private int handleShellCommandResetInputMethod(@NonNull ShellCommand shellCommand) {
    private int handleShellCommandResetInputMethod(@NonNull ShellCommand shellCommand) {
        final PrintWriter out = shellCommand.getOutPrintWriter();
        final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
        synchronized (mMethodMap) {
        synchronized (mMethodMap) {
            if (!userHasDebugPriv(mSettings.getCurrentUserId(), shellCommand)) {
            final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
                return ShellCommandResult.SUCCESS;
                    mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
            for (int userId : userIds) {
                if (!userHasDebugPriv(userId, shellCommand)) {
                    continue;
                }
                }
                final String nextIme;
                final String nextIme;
                final List<InputMethodInfo> nextEnabledImes;
                final List<InputMethodInfo> nextEnabledImes;
                if (userId == mSettings.getCurrentUserId()) {
                    hideCurrentInputLocked(0, null);
                    hideCurrentInputLocked(0, null);
                    unbindCurrentMethodLocked();
                    unbindCurrentMethodLocked();
                    // Reset the current IME
                    // Reset the current IME
@@ -4899,20 +4910,36 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
                            mContext.getBasePackageName());
                            mContext.getBasePackageName());
                    nextIme = mSettings.getSelectedInputMethod();
                    nextIme = mSettings.getSelectedInputMethod();
                    nextEnabledImes = mSettings.getEnabledInputMethodListLocked();
                    nextEnabledImes = mSettings.getEnabledInputMethodListLocked();
            final PrintWriter pr = shellCommand.getOutPrintWriter();
                } else {
            pr.println("Reset current and enabled IMEs");
                    final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>();
            pr.println("Newly selected IME:");
                    final ArrayList<InputMethodInfo> methodList = new ArrayList<>();
            pr.print("  "); pr.println(nextIme);
                    final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
            pr.println("Newly enabled IMEs:");
                            new ArrayMap<>();
            {
                    AdditionalSubtypeUtils.load(additionalSubtypeMap, userId);
                final int N = nextEnabledImes.size();
                    queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap,
                for (int i = 0; i < N; ++i) {
                            methodMap, methodList);
                    pr.print("  ");
                    final InputMethodSettings settings = new InputMethodSettings(
                    pr.println(nextEnabledImes.get(i).getId());
                            mContext.getResources(), mContext.getContentResolver(), methodMap,
                            userId, false);

                    nextEnabledImes = InputMethodUtils.getDefaultEnabledImes(mContext, methodList);
                    nextIme = InputMethodUtils.getMostApplicableDefaultIME(nextEnabledImes).getId();

                    // Reset enabled IMEs.
                    settings.putEnabledInputMethodsStr("");
                    nextEnabledImes.forEach(imi -> settings.appendAndPutEnabledInputMethodLocked(
                            imi.getId(), false));

                    // Reset selected IME.
                    settings.putSelectedInputMethod(nextIme);
                    settings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
                }
                }
                out.println("Reset current and enabled IMEs for user #" + userId);
                out.println("  Selected: " + nextIme);
                nextEnabledImes.forEach(ime -> out.println("   Enabled: " + ime.getId()));
            }
            }
            return ShellCommandResult.SUCCESS;
        }
        }
        return ShellCommandResult.SUCCESS;
    }
    }


    /**
    /**
+1 −1
Original line number Original line Diff line number Diff line
@@ -1003,7 +1003,7 @@ final class InputMethodUtils {
            return res;
            return res;
        }
        }


        private void putEnabledInputMethodsStr(@Nullable String str) {
        void putEnabledInputMethodsStr(@Nullable String str) {
            if (DEBUG) {
            if (DEBUG) {
                Slog.d(TAG, "putEnabledInputMethodStr: " + str);
                Slog.d(TAG, "putEnabledInputMethodStr: " + str);
            }
            }