From 2cc1eee4a7625497d47fd406c2cfaac42136b702 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Thu, 28 Jul 2022 09:04:16 +0900 Subject: [PATCH] Let InputMethodSettings have its own user-aware Context This CL makes it clear that any Context-dependent system services used in InputMethodSettings are associated with the correct user. With this CL, methods defined in InputMethodSettings no longer need to take Context as an input parameter. Bug: 234882948 Test: presubmit Change-Id: I7e6e41775c0a7f119772a2ec957ec45a830f7a70 --- .../InputMethodManagerService.java | 43 ++++++++----------- ...InputMethodSubtypeSwitchingController.java | 2 +- .../server/inputmethod/InputMethodUtils.java | 17 +++++--- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java index bbf9410ae63d6..e20f0df597300 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java @@ -1765,8 +1765,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub mLastSwitchUserId = userId; // mSettings should be created before buildInputMethodListLocked - mSettings = new InputMethodSettings( - mRes, context.getContentResolver(), mMethodMap, userId, !mSystemReady); + mSettings = new InputMethodSettings(mContext, mMethodMap, userId, !mSystemReady); updateCurrentProfileIds(); AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, userId); @@ -2138,8 +2137,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub //TODO(b/197848765): This can be optimized by caching multi-user methodMaps/methodList. //TODO(b/210039666): use cache. final ArrayMap methodMap = queryMethodMapForUser(userId); - final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(), - mContext.getContentResolver(), methodMap, userId, true); + final InputMethodSettings settings = new InputMethodSettings(mContext, methodMap, + userId, true); final InputMethodInfo imi = methodMap.get(settings.getSelectedInputMethod()); return imi != null && imi.supportsStylusHandwriting(); } @@ -2171,8 +2170,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub return mSettings.getEnabledInputMethodListLocked(); } final ArrayMap methodMap = queryMethodMapForUser(userId); - final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(), - mContext.getContentResolver(), methodMap, userId, true); + final InputMethodSettings settings = new InputMethodSettings(mContext, methodMap, userId, + true); return settings.getEnabledInputMethodListLocked(); } @@ -2237,17 +2236,17 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub return Collections.emptyList(); } return mSettings.getEnabledInputMethodSubtypeListLocked( - mContext, imi, allowsImplicitlySelectedSubtypes); + imi, allowsImplicitlySelectedSubtypes); } final ArrayMap methodMap = queryMethodMapForUser(userId); final InputMethodInfo imi = methodMap.get(imiId); if (imi == null) { return Collections.emptyList(); } - final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(), - mContext.getContentResolver(), methodMap, userId, true); + final InputMethodSettings settings = new InputMethodSettings(mContext, methodMap, userId, + true); return settings.getEnabledInputMethodSubtypeListLocked( - mContext, imi, allowsImplicitlySelectedSubtypes); + imi, allowsImplicitlySelectedSubtypes); } /** @@ -3066,7 +3065,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub for (int i = 0; i < numImes; ++i) { final InputMethodInfo imi = imes.get(i); final List subtypes = - mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true); + mSettings.getEnabledInputMethodSubtypeListLocked(imi, true); final int subtypeCount = subtypes.size(); if (subtypeCount == 0) { ++nonAuxCount; @@ -4155,8 +4154,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub } final ArrayMap methodMap = queryMethodMapForUser(userId); - final InputMethodSettings settings = new InputMethodSettings( - mContext.getResources(), mContext.getContentResolver(), methodMap, + final InputMethodSettings settings = new InputMethodSettings(mContext, methodMap, userId, false); return settings.getLastInputMethodSubtypeLocked(); } @@ -5317,7 +5315,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub // the most applicable subtype from explicitly or implicitly enabled // subtypes. List explicitlyOrImplicitlyEnabledSubtypes = - mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true); + mSettings.getEnabledInputMethodSubtypeListLocked(imi, true); // If there is only one explicitly or implicitly enabled subtype, // just returns it. if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) { @@ -5361,9 +5359,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub return true; } final ArrayMap methodMap = queryMethodMapForUser(userId); - final InputMethodSettings settings = new InputMethodSettings( - mContext.getResources(), mContext.getContentResolver(), methodMap, - userId, false); + final InputMethodSettings settings = new InputMethodSettings(mContext, methodMap, userId, + false); if (!methodMap.containsKey(imeId) || !settings.getEnabledInputMethodListLocked().contains(methodMap.get(imeId))) { return false; // IME is not found or not enabled. @@ -5435,8 +5432,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub return true; } final ArrayMap methodMap = queryMethodMapForUser(userId); - final InputMethodSettings settings = new InputMethodSettings( - mContext.getResources(), mContext.getContentResolver(), methodMap, + final InputMethodSettings settings = new InputMethodSettings(mContext, methodMap, userId, false); if (!methodMap.containsKey(imeId)) { return false; // IME is not found. @@ -6138,8 +6134,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub } } else { final ArrayMap methodMap = queryMethodMapForUser(userId); - final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(), - mContext.getContentResolver(), methodMap, userId, false); + final InputMethodSettings settings = new InputMethodSettings(mContext, methodMap, + userId, false); if (enabled) { if (!methodMap.containsKey(imeId)) { failedToEnableUnknownIme = true; @@ -6275,9 +6271,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub AdditionalSubtypeUtils.load(additionalSubtypeMap, userId); queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, methodMap, methodList, DirectBootAwareness.AUTO); - final InputMethodSettings settings = new InputMethodSettings( - mContext.getResources(), mContext.getContentResolver(), methodMap, - userId, false); + final InputMethodSettings settings = new InputMethodSettings(mContext, + methodMap, userId, false); nextEnabledImes = InputMethodInfoUtils.getDefaultEnabledImes(mContext, methodList); diff --git a/services/core/java/com/android/server/inputmethod/InputMethodSubtypeSwitchingController.java b/services/core/java/com/android/server/inputmethod/InputMethodSubtypeSwitchingController.java index a64322625797c..c83a969383254 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodSubtypeSwitchingController.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodSubtypeSwitchingController.java @@ -200,7 +200,7 @@ final class InputMethodSubtypeSwitchingController { continue; } final List explicitlyOrImplicitlyEnabledSubtypeList = - mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true); + mSettings.getEnabledInputMethodSubtypeListLocked(imi, true); final ArraySet enabledSubtypeSet = new ArraySet<>(); for (InputMethodSubtype subtype : explicitlyOrImplicitlyEnabledSubtypeList) { enabledSubtypeSet.add(String.valueOf(subtype.hashCode())); diff --git a/services/core/java/com/android/server/inputmethod/InputMethodUtils.java b/services/core/java/com/android/server/inputmethod/InputMethodUtils.java index 5e20dfbc71444..da6d4b8becf90 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodUtils.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodUtils.java @@ -233,6 +233,7 @@ final class InputMethodUtils { * TODO: Move all putters and getters of settings to this class. * TODO(b/235661780): Make the setting supports multi-users. */ + @UserHandleAware public static class InputMethodSettings { private final TextUtils.SimpleStringSplitter mInputMethodSplitter = new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATOR); @@ -240,6 +241,8 @@ final class InputMethodUtils { private final TextUtils.SimpleStringSplitter mSubtypeSplitter = new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR); + @NonNull + private final Context mUserAwareContext; private final Resources mRes; private final ContentResolver mResolver; private final ArrayMap mMethodMap; @@ -299,11 +302,14 @@ final class InputMethodUtils { return imsList; } - InputMethodSettings(Resources res, ContentResolver resolver, + InputMethodSettings(@NonNull Context context, ArrayMap methodMap, @UserIdInt int userId, boolean copyOnWrite) { - mRes = res; - mResolver = resolver; + mUserAwareContext = context.getUserId() == userId + ? context + : context.createContextAsUser(UserHandle.of(userId), 0 /* flags */); + mRes = mUserAwareContext.getResources(); + mResolver = mUserAwareContext.getContentResolver(); mMethodMap = methodMap; switchCurrentUser(userId, copyOnWrite); } @@ -405,12 +411,11 @@ final class InputMethodUtils { } List getEnabledInputMethodSubtypeListLocked( - Context context, InputMethodInfo imi, boolean allowsImplicitlySelectedSubtypes) { + InputMethodInfo imi, boolean allowsImplicitlySelectedSubtypes) { List enabledSubtypes = getEnabledInputMethodSubtypeListLocked(imi); if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) { - enabledSubtypes = SubtypeUtils.getImplicitlyApplicableSubtypesLocked( - context.getResources(), imi); + enabledSubtypes = SubtypeUtils.getImplicitlyApplicableSubtypesLocked(mRes, imi); } return InputMethodSubtype.sort(imi, enabledSubtypes); }