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

Commit 36419b61 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Do more stuff w/o ImfLock in onFinishPackageChangesInternal()

With this CL

  InputMethodManagerService.MyPackageMonitor
          .onFinishPackageChangesInternal()

will start more potentially heavy weight tasks outside of ImfLock to
reduce lock contention.  Most importantly, we no longer acquire
ImfLock at all when there is no update in InputMethodMap. This should
drastically reduce the likelihood of lock contentions around
MyPackageMonitor.

Overall there should be no user perceivable behavior change.

Bug: 329703038
Bug: 343601565
Test: presubmit
Flag: EXEMPT refactor
Change-Id: If140d635dc4f462554c3324cef4dbf902aafc136
parent 56394c58
Loading
Loading
Loading
Loading
+54 −47
Original line number Diff line number Diff line
@@ -799,11 +799,8 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
            final int userId = getChangingUserId();
            final var userData = getUserData(userId);

            // Instantiating InputMethodInfo requires disk I/O.
            // Do them before acquiring the lock to minimize the chances of ANR (b/340221861).
            userData.mRawInputMethodMap.set(queryRawInputMethodServiceMap(mContext, userId));

            synchronized (ImfLock.class) {
            final InputMethodSettings settings = InputMethodSettingsRepository.get(userId);

            InputMethodInfo curIm = null;
@@ -811,6 +808,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
            final List<InputMethodInfo> methodList = settings.getMethodList();

            final ArrayList<String> imesToClearAdditionalSubtypes = new ArrayList<>();
            final ArrayList<String> imesToBeDisabled = new ArrayList<>();
            final int numImes = methodList.size();
            for (int i = 0; i < numImes; i++) {
                InputMethodInfo imi = methodList.get(i);
@@ -824,7 +822,7 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                int change = isPackageDisappearing(imi.getPackageName());
                if (change == PACKAGE_PERMANENT_CHANGE) {
                    Slog.i(TAG, "Input method uninstalled, disabling: " + imi.getComponent());
                        setInputMethodEnabledLocked(imi.getId(), false, userId);
                    imesToBeDisabled.add(imi.getId());
                } else if (change == PACKAGE_UPDATING) {
                    Slog.i(TAG, "Input method reinstalling, clearing additional subtypes: "
                            + imi.getComponent());
@@ -848,14 +846,23 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl.
                    DirectBootAwareness.AUTO,
                    mUserManagerInternal.isUserUnlockingOrUnlocked(userId));

                if (InputMethodMap.areSame(settings.getMethodMap(), newMethodMap)) {
                    // No update in the actual IME map.
            final boolean noUpdate = InputMethodMap.areSame(settings.getMethodMap(), newMethodMap);
            if (noUpdate && imesToBeDisabled.isEmpty()) {
                return;
            }

                final InputMethodSettings newSettings =
                        InputMethodSettings.create(newMethodMap, userId);
                InputMethodSettingsRepository.put(userId, newSettings);
            // Here we start remaining tasks that need to be done with the lock (b/340221861).
            synchronized (ImfLock.class) {
                final int numImesToBeDisabled = imesToBeDisabled.size();
                for (int i = 0; i < numImesToBeDisabled; ++i) {
                    setInputMethodEnabledLocked(imesToBeDisabled.get(i), false /* enabled */,
                            userId);
                }
                if (noUpdate) {
                    return;
                }
                InputMethodSettingsRepository.put(userId,
                        InputMethodSettings.create(newMethodMap, userId));
                postInputMethodSettingUpdatedLocked(false /* resetDefaultEnabledIme */, userId);

                boolean changed = false;