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

Commit 0cb64694 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Inline InputMethodSettings#isCurrentProfile()

With this commit,

  InputMethodSettings#isCurrentProfile()

is inlined into InputMethodManagerService without caching the result
of

  UserManagerInternal#getProfileIds(int userId, boolean enabledOnly)

for simplicity.

The observable behavior should semantically remain the same.

This is a preparation to decouple InputMethodSettings from
InputMethodManagerService global instance.

Bug: 309837937
Test: presubmit
Change-Id: I1c2659cd3c9c2c2908f1ae5be870052284d559c2
parent df614b67
Loading
Loading
Loading
Loading
+8 −38
Original line number Original line Diff line number Diff line
@@ -1187,23 +1187,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        }
        }
    }
    }


    /**
     * {@link BroadcastReceiver} that is intended to listen to broadcasts sent to the system user
     * only.
     */
    private final class ImmsBroadcastReceiverForSystemUser extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_USER_ADDED.equals(action)
                    || Intent.ACTION_USER_REMOVED.equals(action)) {
                updateCurrentProfileIds();
            } else {
                Slog.w(TAG, "Unexpected intent " + intent);
            }
        }
    }

    /**
    /**
     * {@link BroadcastReceiver} that is intended to listen to broadcasts sent to all the users.
     * {@link BroadcastReceiver} that is intended to listen to broadcasts sent to all the users.
     */
     */
@@ -1700,7 +1683,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        // mSettings should be created before buildInputMethodListLocked
        // mSettings should be created before buildInputMethodListLocked
        mSettings = new InputMethodSettings(mContext, mMethodMap, userId, !mSystemReady);
        mSettings = new InputMethodSettings(mContext, mMethodMap, userId, !mSystemReady);


        updateCurrentProfileIds();
        AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, userId);
        AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, userId);
        mSwitchingController =
        mSwitchingController =
                InputMethodSubtypeSwitchingController.createInstanceLocked(mSettings, context);
                InputMethodSubtypeSwitchingController.createInstanceLocked(mSettings, context);
@@ -1818,7 +1800,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        final boolean useCopyOnWriteSettings =
        final boolean useCopyOnWriteSettings =
                !mSystemReady || !mUserManagerInternal.isUserUnlockingOrUnlocked(newUserId);
                !mSystemReady || !mUserManagerInternal.isUserUnlockingOrUnlocked(newUserId);
        mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
        mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
        updateCurrentProfileIds();
        // Additional subtypes should be reset when the user is changed
        // Additional subtypes should be reset when the user is changed
        AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, newUserId);
        AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, newUserId);
        final String defaultImiId = mSettings.getSelectedInputMethod();
        final String defaultImiId = mSettings.getSelectedInputMethod();
@@ -1869,12 +1850,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        }
        }
    }
    }


    void updateCurrentProfileIds() {
        mSettings.setCurrentProfileIds(
                mUserManagerInternal.getProfileIds(mSettings.getCurrentUserId(),
                        false /* enabledOnly */));
    }

    /**
    /**
     * TODO(b/32343335): The entire systemRunning() method needs to be revisited.
     * TODO(b/32343335): The entire systemRunning() method needs to be revisited.
     */
     */
@@ -1921,12 +1896,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
                mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
                mSettingsObserver.registerContentObserverLocked(currentUserId);
                mSettingsObserver.registerContentObserverLocked(currentUserId);


                final IntentFilter broadcastFilterForSystemUser = new IntentFilter();
                broadcastFilterForSystemUser.addAction(Intent.ACTION_USER_ADDED);
                broadcastFilterForSystemUser.addAction(Intent.ACTION_USER_REMOVED);
                mContext.registerReceiver(new ImmsBroadcastReceiverForSystemUser(),
                        broadcastFilterForSystemUser);

                final IntentFilter broadcastFilterForAllUsers = new IntentFilter();
                final IntentFilter broadcastFilterForAllUsers = new IntentFilter();
                broadcastFilterForAllUsers.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                broadcastFilterForAllUsers.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                mContext.registerReceiverAsUser(new ImmsBroadcastReceiverForAllUsers(),
                mContext.registerReceiverAsUser(new ImmsBroadcastReceiverForAllUsers(),
@@ -3788,8 +3757,14 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
            mVisibilityStateComputer.mShowForced = false;
            mVisibilityStateComputer.mShowForced = false;
        }
        }


        final int currentUserId = mSettings.getCurrentUserId();
        if (userId != currentUserId) {
            if (ArrayUtils.contains(
                    mUserManagerInternal.getProfileIds(currentUserId, false), userId)) {
                // cross-profile access is always allowed here to allow profile-switching.
                // cross-profile access is always allowed here to allow profile-switching.
        if (!mSettings.isCurrentProfile(userId)) {
                scheduleSwitchUserTaskLocked(userId, cs.mClient);
                return InputBindResult.USER_SWITCHING;
            }
            Slog.w(TAG, "A background user is requesting window. Hiding IME.");
            Slog.w(TAG, "A background user is requesting window. Hiding IME.");
            Slog.w(TAG, "If you need to impersonate a foreground user/profile from"
            Slog.w(TAG, "If you need to impersonate a foreground user/profile from"
                    + " a background user, use EditorInfo.targetInputMethodUser with"
                    + " a background user, use EditorInfo.targetInputMethodUser with"
@@ -3799,11 +3774,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
            return InputBindResult.INVALID_USER;
            return InputBindResult.INVALID_USER;
        }
        }


        if (userId != mSettings.getCurrentUserId()) {
            scheduleSwitchUserTaskLocked(userId, cs.mClient);
            return InputBindResult.USER_SWITCHING;
        }

        final boolean sameWindowFocused = mCurFocusedWindow == windowToken;
        final boolean sameWindowFocused = mCurFocusedWindow == windowToken;
        final boolean isTextEditor = (startInputFlags & StartInputFlags.IS_TEXT_EDITOR) != 0;
        final boolean isTextEditor = (startInputFlags & StartInputFlags.IS_TEXT_EDITOR) != 0;
        final boolean startInputByWinGainedFocus =
        final boolean startInputByWinGainedFocus =
+0 −18
Original line number Original line Diff line number Diff line
@@ -45,14 +45,12 @@ import android.view.textservice.SpellCheckerInfo;


import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.inputmethod.StartInputFlags;
import com.android.internal.inputmethod.StartInputFlags;
import com.android.internal.util.ArrayUtils;
import com.android.server.LocalServices;
import com.android.server.LocalServices;
import com.android.server.pm.UserManagerInternal;
import com.android.server.pm.UserManagerInternal;
import com.android.server.textservices.TextServicesManagerInternal;
import com.android.server.textservices.TextServicesManagerInternal;


import java.io.PrintWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.Predicate;


@@ -241,7 +239,6 @@ final class InputMethodUtils {
        private String mEnabledInputMethodsStrCache = "";
        private String mEnabledInputMethodsStrCache = "";
        @UserIdInt
        @UserIdInt
        private int mCurrentUserId;
        private int mCurrentUserId;
        private int[] mCurrentProfileIds = new int[0];


        private static void buildEnabledInputMethodsSettingString(
        private static void buildEnabledInputMethodsSettingString(
                StringBuilder builder, Pair<String, ArrayList<String>> ime) {
                StringBuilder builder, Pair<String, ArrayList<String>> ime) {
@@ -308,14 +305,12 @@ final class InputMethodUtils {
            if (mCurrentUserId != userId || mCopyOnWrite != copyOnWrite) {
            if (mCurrentUserId != userId || mCopyOnWrite != copyOnWrite) {
                mCopyOnWriteDataStore.clear();
                mCopyOnWriteDataStore.clear();
                mEnabledInputMethodsStrCache = "";
                mEnabledInputMethodsStrCache = "";
                // TODO: mCurrentProfileIds should be cleared here.
            }
            }
            if (mUserAwareContext.getUserId() != userId) {
            if (mUserAwareContext.getUserId() != userId) {
                initContentWithUserContext(mUserAwareContext, userId);
                initContentWithUserContext(mUserAwareContext, userId);
            }
            }
            mCurrentUserId = userId;
            mCurrentUserId = userId;
            mCopyOnWrite = copyOnWrite;
            mCopyOnWrite = copyOnWrite;
            // TODO: mCurrentProfileIds should be updated here.
        }
        }


        private void putString(@NonNull String key, @Nullable String str) {
        private void putString(@NonNull String key, @Nullable String str) {
@@ -365,18 +360,6 @@ final class InputMethodUtils {
            return getInt(key, defaultValue ? 1 : 0) == 1;
            return getInt(key, defaultValue ? 1 : 0) == 1;
        }
        }


        public void setCurrentProfileIds(int[] currentProfileIds) {
            synchronized (this) {
                mCurrentProfileIds = currentProfileIds;
            }
        }

        public boolean isCurrentProfile(int userId) {
            synchronized (this) {
                return ArrayUtils.contains(mCurrentProfileIds, userId);
            }
        }

        ArrayList<InputMethodInfo> getEnabledInputMethodListLocked() {
        ArrayList<InputMethodInfo> getEnabledInputMethodListLocked() {
            return getEnabledInputMethodListWithFilterLocked(null /* matchingCondition */);
            return getEnabledInputMethodListWithFilterLocked(null /* matchingCondition */);
        }
        }
@@ -947,7 +930,6 @@ final class InputMethodUtils {


        public void dumpLocked(final Printer pw, final String prefix) {
        public void dumpLocked(final Printer pw, final String prefix) {
            pw.println(prefix + "mCurrentUserId=" + mCurrentUserId);
            pw.println(prefix + "mCurrentUserId=" + mCurrentUserId);
            pw.println(prefix + "mCurrentProfileIds=" + Arrays.toString(mCurrentProfileIds));
            pw.println(prefix + "mCopyOnWrite=" + mCopyOnWrite);
            pw.println(prefix + "mCopyOnWrite=" + mCopyOnWrite);
            pw.println(prefix + "mEnabledInputMethodsStrCache=" + mEnabledInputMethodsStrCache);
            pw.println(prefix + "mEnabledInputMethodsStrCache=" + mEnabledInputMethodsStrCache);
        }
        }