Fix '-u <user id>' handling in 'adb ime enable' and 'adb ime set'

This is a follow up CL to my previous CLs [1][2], which added
'-u <user id>' option to
   adb shell ime enable <ime id>
and
   adb shell ime set <ime id>
respectively.

Those CL had a bug that was introduced during the last round of the
code review when I was addressing a review comment.  As a result,
those two commands simply fail if '-u' or '--user' option is
specified.  With this CL, those we can finally start specifying the
user ID for those two commands.

This CL also fixes a bug that output for
  adb shell ime set -u all <ime id>
are not correctly formatted.

Note that this CL only fixes a bug in debug shell commands. There
should be no user-visible behavior change in end-user use cases.

 [1]: Ia0f873e4589a9fc3f549469e3d1d966640dc2df5
      e177170f6432631d54788a37f4fb468a02468ed0
 [2]: I397cf0fb418a395dcafc0ab0d8d4e553b0f2eaab
      099f80ce9762e8b20998759fd6082bdcd6291103

Bug: 120784635
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 disable -u 0 com.example.android.softkeyboard/.SoftKeyboard
     -> Input method com.example.android.softkeyboard/.SoftKeyboard: now disabled for user #0
  7. adb shell ime enable -u 0 com.example.android.softkeyboard/.SoftKeyboard
     -> Input method com.example.android.softkeyboard/.SoftKeyboard: now enabled for user #0
  8. 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 test
  3. adb shell pm create-user restricted_test
  4. adb root
  5. adb shell pm set-user-restriction --user 11 no_debugging_features 1
  6. adb shell am switch-user 10
  7. adb shell am switch-user 11
  8. adb shell am switch-user 0
  9. adb shell ime set -u all com.android.inputmethod.latin/.LatinIME
     -> Input method com.android.inputmethod.latin/.LatinIME selected for user #0
        Input method com.android.inputmethod.latin/.LatinIME selected for user #10
        User #11 is restricted with DISALLOW_DEBUGGING_FEATURES.
 10. adb shell ime disable -u all com.android.inputmethod.latin/.LatinIME
     -> Input method com.android.inputmethod.latin/.LatinIME: now disabled for user #0
        Input method com.android.inputmethod.latin/.LatinIME: now disabled for user #10
        User #11 is restricted with DISALLOW_DEBUGGING_FEATURES.
Change-Id: I4e396519400c0ccd17322fe085b45456621714e7
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
index e88d62f..3c97c39 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
@@ -4710,10 +4710,10 @@
     @ShellCommandResult
     private int handleShellCommandEnableDisableInputMethod(
             @NonNull ShellCommand shellCommand, boolean enabled) {
+        final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
         final String imeId = shellCommand.getNextArgRequired();
         final PrintWriter out = shellCommand.getOutPrintWriter();
         final PrintWriter error = shellCommand.getErrPrintWriter();
-        final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
         synchronized (mMethodMap) {
             final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
                     mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
@@ -4733,6 +4733,10 @@
      *
      * <p>You cannot use this helper method if the command has other options.</p>
      *
+     * <p>CAVEAT: This method must be called only once before any other
+     * {@link ShellCommand#getNextArg()} and {@link ShellCommand#getNextArgRequired()} for the
+     * main arguments.</p>
+     *
      * @param shellCommand {@link ShellCommand} from which options should be obtained.
      * @return User ID to be resolved. {@link UserHandle#CURRENT} if not specified.
      */
@@ -4819,10 +4823,10 @@
     @BinderThread
     @ShellCommandResult
     private int handleShellCommandSetInputMethod(@NonNull ShellCommand shellCommand) {
+        final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
         final String imeId = shellCommand.getNextArgRequired();
         final PrintWriter out = shellCommand.getOutPrintWriter();
         final PrintWriter error = shellCommand.getErrPrintWriter();
-        final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
         synchronized (mMethodMap) {
             final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
                     mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
@@ -4864,7 +4868,7 @@
                     out.print("Input method ");
                     out.print(imeId);
                     out.print(" selected for user #");
-                    error.println(userId);
+                    out.println(userId);
                 }
             }
         }