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

Commit 8130073e authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Start using InputMethodSettingsRepository (2nd try)

This is the 2nd attempt of submitting my previous CL [1], which got
reverted due to a boottime regression (Bug 329414077). We have already
addressed the major contributor of the boot time regression [3], so
this CL should be ready to be submitted again.

With this CL we start taking advantage of

  InputMethodSettingsRepository,

which enables us to get a cached InputMethodSettings instead of
dynamically querying available IMEs.

To keep the CL as minimum as possible,

  InputMethodManagerService#mSettings

remains unchanged. It will be removed in a subsequent bug 329163064.

There must be no user observable behavior change.

 [1]: Ie2d3b7e9b75790f64789d0c2e96ae46b1dee3a1e
      03e0580e
 [2]: I20317bdd012c51a5ba57cd6e4d2e1a69df66d6a2
      84c61c45
 [3]: I8e51b1ced4dc16cdca7e898885c64793665fafef
      bce89b57

Bug: 329163064
Bug: 333591545
Fix: 309837937
Test: presubmit
Change-Id: Ic0dd655fbd86b8ccce2b3298b4c70359a468f9ec
parent 443cb5a3
Loading
Loading
Loading
Loading
+20 −60
Original line number Diff line number Diff line
@@ -283,6 +283,10 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
    final Context mContext;
    final Resources mRes;
    private final Handler mHandler;

    /**
     * TODO(b/329163064): Remove this field.
     */
    @NonNull
    @MultiUserUnawareField
    private InputMethodSettings mSettings;
@@ -1076,13 +1080,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                final boolean isCurrentUser = (userId == mSettings.getUserId());
                final AdditionalSubtypeMap additionalSubtypeMap =
                        AdditionalSubtypeMapRepository.get(userId);
                final InputMethodSettings settings;
                if (isCurrentUser) {
                    settings = mSettings;
                } else {
                    settings = queryInputMethodServicesInternal(mContext, userId,
                            additionalSubtypeMap, DirectBootAwareness.AUTO);
                }
                final InputMethodSettings settings = InputMethodSettingsRepository.get(userId);

                InputMethodInfo curIm = null;
                String curInputMethodId = settings.getSelectedInputMethod();
@@ -1740,9 +1738,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                        && (!connectionless
                                || mBindingController.supportsConnectionlessStylusHandwriting());
            }
            //TODO(b/197848765): This can be optimized by caching multi-user methodMaps/methodList.
            //TODO(b/210039666): use cache.
            final InputMethodSettings settings = queryMethodMapForUserLocked(userId);
            final InputMethodSettings settings = InputMethodSettingsRepository.get(userId);
            final InputMethodInfo imi = settings.getMethodMap().get(
                    settings.getSelectedInputMethod());
            return imi != null && imi.supportsStylusHandwriting()
@@ -1766,9 +1762,8 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
    private List<InputMethodInfo> getInputMethodListLocked(@UserIdInt int userId,
            @DirectBootAwareness int directBootAwareness, int callingUid) {
        final InputMethodSettings settings;
        if (userId == mSettings.getUserId()
                && directBootAwareness == DirectBootAwareness.AUTO) {
            settings = mSettings;
        if (directBootAwareness == DirectBootAwareness.AUTO) {
            settings = InputMethodSettingsRepository.get(userId);
        } else {
            final AdditionalSubtypeMap additionalSubtypeMap =
                    AdditionalSubtypeMapRepository.get(userId);
@@ -1792,7 +1787,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
            methodList = mSettings.getEnabledInputMethodList();
            settings = mSettings;
        } else {
            settings = queryMethodMapForUserLocked(userId);
            settings = InputMethodSettingsRepository.get(userId);
            methodList = settings.getEnabledInputMethodList();
        }
        // filter caller's access to input methods
@@ -1852,22 +1847,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
    @GuardedBy("ImfLock.class")
    private List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(String imiId,
            boolean allowsImplicitlyEnabledSubtypes, @UserIdInt int userId, int callingUid) {
        if (userId == mSettings.getUserId()) {
            final InputMethodInfo imi;
            String selectedMethodId = getSelectedMethodIdLocked();
            if (imiId == null && selectedMethodId != null) {
                imi = mSettings.getMethodMap().get(selectedMethodId);
            } else {
                imi = mSettings.getMethodMap().get(imiId);
            }
            if (imi == null || !canCallerAccessInputMethod(
                    imi.getPackageName(), callingUid, userId, mSettings)) {
                return Collections.emptyList();
            }
            return mSettings.getEnabledInputMethodSubtypeList(
                    imi, allowsImplicitlyEnabledSubtypes);
        }
        final InputMethodSettings settings = queryMethodMapForUserLocked(userId);
        final InputMethodSettings settings = InputMethodSettingsRepository.get(userId);
        final InputMethodInfo imi = settings.getMethodMap().get(imiId);
        if (imi == null) {
            return Collections.emptyList();
@@ -4060,8 +4040,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                return mSettings.getLastInputMethodSubtype();
            }

            final InputMethodSettings settings = queryMethodMapForUserLocked(userId);
            return settings.getLastInputMethodSubtype();
            return InputMethodSettingsRepository.get(userId).getLastInputMethodSubtype();
        }
    }

@@ -4137,8 +4116,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
        try {
            synchronized (ImfLock.class) {
                final boolean currentUser = (mSettings.getUserId() == userId);
                final InputMethodSettings settings = currentUser
                        ? mSettings : queryMethodMapForUserLocked(userId);
                final InputMethodSettings settings = InputMethodSettingsRepository.get(userId);
                if (!settings.setEnabledInputMethodSubtypes(imeId, subtypeHashCodes)) {
                    return;
                }
@@ -5309,8 +5287,8 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                return getCurrentInputMethodSubtypeLocked();
            }

            final InputMethodSettings settings = queryMethodMapForUserLocked(userId);
            return settings.getCurrentInputMethodSubtypeForNonCurrentUsers();
            return InputMethodSettingsRepository.get(userId)
                    .getCurrentInputMethodSubtypeForNonCurrentUsers();
        }
    }

@@ -5372,26 +5350,10 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
     */
    @GuardedBy("ImfLock.class")
    private InputMethodInfo queryDefaultInputMethodForUserIdLocked(@UserIdInt int userId) {
        final InputMethodSettings settings;
        if (userId == mSettings.getUserId()) {
            settings = mSettings;
        } else {
            final AdditionalSubtypeMap additionalSubtypeMap =
                    AdditionalSubtypeMapRepository.get(userId);
            settings = queryInputMethodServicesInternal(mContext, userId,
                    additionalSubtypeMap, DirectBootAwareness.AUTO);
        }
        final InputMethodSettings settings = InputMethodSettingsRepository.get(userId);
        return settings.getMethodMap().get(settings.getSelectedInputMethod());
    }

    @GuardedBy("ImfLock.class")
    private InputMethodSettings queryMethodMapForUserLocked(@UserIdInt int userId) {
        final AdditionalSubtypeMap additionalSubtypeMap =
                AdditionalSubtypeMapRepository.get(userId);
        return queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap,
                DirectBootAwareness.AUTO);
    }

    @GuardedBy("ImfLock.class")
    private boolean switchToInputMethodLocked(String imeId, @UserIdInt int userId) {
        if (userId == mSettings.getUserId()) {
@@ -5403,7 +5365,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
            setInputMethodLocked(imeId, NOT_A_SUBTYPE_ID);
            return true;
        }
        final InputMethodSettings settings = queryMethodMapForUserLocked(userId);
        final InputMethodSettings settings = InputMethodSettingsRepository.get(userId);
        if (!settings.getMethodMap().containsKey(imeId)
                || !settings.getEnabledInputMethodList().contains(
                        settings.getMethodMap().get(imeId))) {
@@ -5543,7 +5505,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                    setInputMethodEnabledLocked(imeId, enabled);
                    return true;
                }
                final InputMethodSettings settings = queryMethodMapForUserLocked(userId);
                final InputMethodSettings settings = InputMethodSettingsRepository.get(userId);
                if (!settings.getMethodMap().containsKey(imeId)) {
                    return false; // IME is not found.
                }
@@ -6290,7 +6252,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                previouslyEnabled = setInputMethodEnabledLocked(imeId, enabled);
            }
        } else {
            final InputMethodSettings settings = queryMethodMapForUserLocked(userId);
            final InputMethodSettings settings = InputMethodSettingsRepository.get(userId);
            if (enabled) {
                if (!settings.getMethodMap().containsKey(imeId)) {
                    failedToEnableUnknownIme = true;
@@ -6424,10 +6386,8 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                        nextIme = mSettings.getSelectedInputMethod();
                        nextEnabledImes = mSettings.getEnabledInputMethodList();
                    } else {
                        final AdditionalSubtypeMap additionalSubtypeMap =
                                AdditionalSubtypeMapRepository.get(userId);
                        final InputMethodSettings settings = queryInputMethodServicesInternal(
                                mContext, userId, additionalSubtypeMap, DirectBootAwareness.AUTO);
                        final InputMethodSettings settings =
                                InputMethodSettingsRepository.get(userId);

                        nextEnabledImes = InputMethodInfoUtils.getDefaultEnabledImes(mContext,
                                settings.getMethodList());