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

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

Deprecate InputMethodSettings#switchCurrentUser()

Previously we have reused the same instance of

  InputMethodManagerService#mSettings

across users, which needs to be updated before supporting concurrent
multiple users in InputMethodManagerService.

With this CL a new instance of InputMethodSettings will be recreated
every time the current IME user is switching.  This is an important
milestone to keep maintaining multiple instances of
InputMethodSettings for each user.

There must be no observable behavior change in this CL.

Bug: 309837937
Fix: 309868254
Test: presubmit
Change-Id: I52297912d0b66e5e40b6a624c35427a377dc7b1b
parent 22b9aa5b
Loading
Loading
Loading
Loading
+4 −8
Original line number Diff line number Diff line
@@ -276,7 +276,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
    final Context mContext;
    final Resources mRes;
    private final Handler mHandler;
    private final InputMethodSettings mSettings;
    @NonNull
    private InputMethodSettings mSettings;
    final SettingsObserver mSettingsObserver;
    private final SparseBooleanArray mLoggedDeniedGetInputMethodWindowVisibleHeightForUid =
            new SparseBooleanArray(0);
@@ -1622,7 +1623,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
            if (userId != currentUserId) {
                return;
            }
            mSettings.switchCurrentUser(currentUserId);
            mSettings = new InputMethodSettings(mMethodMap, userId);
            if (mSystemReady) {
                // We need to rebuild IMEs.
                buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
@@ -1812,11 +1813,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        // ContentObserver should be registered again when the user is changed
        mSettingsObserver.registerContentObserverLocked(newUserId);

        // If the system is not ready or the device is not yed unlocked by the user, then we use
        // copy-on-write settings.
        final boolean useCopyOnWriteSettings =
                !mSystemReady || !mUserManagerInternal.isUserUnlockingOrUnlocked(newUserId);
        mSettings.switchCurrentUser(newUserId);
        mSettings = new InputMethodSettings(mMethodMap, newUserId);
        // Additional subtypes should be reset when the user is changed
        AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, newUserId);
        final String defaultImiId = mSettings.getSelectedInputMethod();
@@ -1878,7 +1875,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
            if (!mSystemReady) {
                mSystemReady = true;
                final int currentUserId = mSettings.getCurrentUserId();
                mSettings.switchCurrentUser(currentUserId);
                mStatusBarManagerInternal =
                        LocalServices.getService(StatusBarManagerInternal.class);
                hideStatusBarIconLocked();
+1 −13
Original line number Diff line number Diff line
@@ -214,7 +214,7 @@ final class InputMethodUtils {
        private final ArrayMap<String, InputMethodInfo> mMethodMap;

        @UserIdInt
        private int mCurrentUserId;
        private final int mCurrentUserId;

        private static void buildEnabledInputMethodsSettingString(
                StringBuilder builder, Pair<String, ArrayList<String>> ime) {
@@ -228,18 +228,6 @@ final class InputMethodUtils {

        InputMethodSettings(ArrayMap<String, InputMethodInfo> methodMap, @UserIdInt int userId) {
            mMethodMap = methodMap;
            switchCurrentUser(userId);
        }

        /**
         * Must be called when the current user is changed.
         *
         * @param userId The user ID.
         */
        void switchCurrentUser(@UserIdInt int userId) {
            if (DEBUG) {
                Slog.d(TAG, "--- Switch the current user from " + mCurrentUserId + " to " + userId);
            }
            mCurrentUserId = userId;
        }

+0 −31
Original line number Diff line number Diff line
@@ -25,10 +25,8 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.ContentResolver;
@@ -1223,35 +1221,6 @@ public class InputMethodUtilsTest {
                StartInputFlags.VIEW_HAS_FOCUS | StartInputFlags.IS_TEXT_EDITOR));
    }

    @Test
    public void testInputMethodSettings_SwitchCurrentUser() {
        TestContext ownerUserContext = createMockContext(0 /* userId */);
        final InputMethodInfo systemIme = createFakeInputMethodInfo(
                "SystemIme", "fake.latin", true /* isSystem */);
        final InputMethodInfo nonSystemIme = createFakeInputMethodInfo("NonSystemIme",
                "fake.voice0", false /* isSystem */);
        final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>();
        methodMap.put(systemIme.getId(), systemIme);

        // Init InputMethodSettings for the owner user (userId=0), verify calls can get the
        // corresponding user's context, contentResolver and the resources configuration.
        InputMethodUtils.InputMethodSettings settings = new InputMethodUtils.InputMethodSettings(
                methodMap, 0 /* userId */);
        assertEquals(0, settings.getCurrentUserId());

        settings.getEnabledInputMethodSubtypeListLocked(nonSystemIme, true);
        verify(ownerUserContext.getResources(), atLeastOnce()).getConfiguration();

        // Calling switchCurrentUser to the secondary user (userId=10), verify calls can get the
        // corresponding user's context, contentResolver and the resources configuration.
        settings.switchCurrentUser(10 /* userId */);
        assertEquals(10, settings.getCurrentUserId());

        settings.getEnabledInputMethodSubtypeListLocked(nonSystemIme, true);
        verify(TestContext.getSecondaryUserContext().getResources(),
                atLeastOnce()).getConfiguration();
    }

    private static IntArray createSubtypeHashCodeArrayFromStr(String subtypeHashCodesStr) {
        final IntArray subtypes = new IntArray();
        final TextUtils.SimpleStringSplitter imeSubtypeSplitter =