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

Commit a7babbbf authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Add a debug command to poll IMMS.Lifecycle#onSwitchUser()

This is a quick workaround for a known limitation that

  adb shell am switch-user <user id>

doesn't wait until com.android.server.SystemService#onSwitchUser()
gets called.

The problem is that UserController.switchUser() immediately returns
soon after enqueuing a task to call back SystemService#onSwitchUser().
This means that on a slow device / environment, there is a chance that
IMMS continues behaving as if a previous user was still interacting
after switching a user.

Ideally there should be a way to wait until all system servers finish
handling SystemService#onSwitchUser() in am shell commands, but it may
require non-trivial amount of changes in ActivityManagerShellCommand.
In order to unblock on-going per-profile IME effort, this CL exposes
an internal state of InputMethodManagerService (IMMS) so that CTS
tests can wait until IMMS.Lifecycle#onSwitchUser() gets called with an
expected user ID as a quick workaround.

Bug: 120784635
Bug: 122469833
Test: adb shell cmd input_method get-last-switch-user-id
Test: atest CtsInputMethodTestCases CtsInputMethodServiceHostTestCases
Change-Id: I07a0120d44bcb3e3d60d51c510e7e566ce424148
parent 95cb8e40
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -291,6 +291,8 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
                new DebugFlag("debug.optimize_startinput", false);
    }

    @UserIdInt
    private int mLastSwitchUserId;

    final Context mContext;
    final Resources mRes;
@@ -1436,6 +1438,8 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
            Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
        }

        mLastSwitchUserId = userId;

        // mSettings should be created before buildInputMethodListLocked
        mSettings = new InputMethodSettings(
                mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
@@ -1523,6 +1527,8 @@ public class InputMethodManagerService extends IInputMethodManager.Stub

        if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
                + " selectedIme=" + mSettings.getSelectedInputMethod());

        mLastSwitchUserId = newUserId;
    }

    void updateCurrentProfileIds() {
@@ -4424,6 +4430,10 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
                return refreshDebugProperties();
            }

            if ("get-last-switch-user-id".equals(cmd)) {
                return mService.getLastSwitchUserId(this);
            }

            // For existing "adb shell ime <command>".
            if ("ime".equals(cmd)) {
                final String imeCommand = getNextArg();
@@ -4516,6 +4526,15 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
    // ----------------------------------------------------------------------
    // Shell command handlers:

    @BinderThread
    @ShellCommandResult
    private int getLastSwitchUserId(@NonNull ShellCommand shellCommand) {
        synchronized (mMethodMap) {
            shellCommand.getOutPrintWriter().println(mLastSwitchUserId);
            return ShellCommandResult.SUCCESS;
        }
    }

    /**
     * Handles {@code adb shell ime list}.
     * @param shellCommand {@link ShellCommand} object that is handling this command.