Use user-aware context in IMMS#queryInputMethodServicesInternal

As queryInputMethodServicesInternal is to query the installed IME apps
of the given userId with returning the list of InputMethodInfo, however,
there is a bug that it always initializes InputMethodInfo with user 0's
XML resources from the system context.

Fix this issue by using user-aware context to ensure creating
InputMethodInfo will load the correct resources settings with the
corresponding context of the userId.

Fix: 249081323
Test: atest CtsInputMethodTestCases
Change-Id: I47578c8bba5d9e04301ed3727ad1efa6496653e6
This commit is contained in:
Ming-Shin Lu
2022-09-30 11:21:13 +08:00
parent f636af7055
commit c131c6e5f7

View File

@@ -5034,6 +5034,10 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
@UserIdInt int userId, ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap,
ArrayMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList,
@DirectBootAwareness int directBootAwareness) {
final Context userAwareContext = context.getUserId() == userId
? context
: context.createContextAsUser(UserHandle.of(userId), 0 /* flags */);
methodList.clear();
methodMap.clear();
@@ -5055,8 +5059,9 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
final int flags = PackageManager.GET_META_DATA
| PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
| directBootAwarenessFlags;
final List<ResolveInfo> services = context.getPackageManager().queryIntentServicesAsUser(
new Intent(InputMethod.SERVICE_INTERFACE), flags, userId);
final List<ResolveInfo> services = userAwareContext.getPackageManager().queryIntentServices(
new Intent(InputMethod.SERVICE_INTERFACE),
PackageManager.ResolveInfoFlags.of(flags));
methodList.ensureCapacity(services.size());
methodMap.ensureCapacity(services.size());
@@ -5075,7 +5080,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
if (DEBUG) Slog.d(TAG, "Checking " + imeId);
try {
final InputMethodInfo imi = new InputMethodInfo(context, ri,
final InputMethodInfo imi = new InputMethodInfo(userAwareContext, ri,
additionalSubtypeMap.get(imeId));
if (imi.isVrOnly()) {
continue; // Skip VR-only IME, which isn't supported for now.