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

Commit 05139322 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Extract IME list creation logic from IMMS#buildInputMethodListLocked()

This is a preparation to introcude following two methods into
InputMethodManagerService (IMMS).

  * getInputMethodListForUser(int userId)
  * getEnabledInputMethodListForUser(int UserId).

In order to implement those methods (with minimum changes), the logic
to create List<InputMethodInfo> for the given user needs to be
extracted out from IMMS#buildInputMethodListLocked() to avoid code
duplicate.

This CL itself is a mechanical refactoring hence there should be no
behavior change.

Bug: 120709962
Test: atest CtsInputMethodTestCases CtsInputMethodServiceHostTestCases
Change-Id: I7f7e3637ea44dddc1210bc2889b519f17bbcd545
parent 6048d894
Loading
Loading
Loading
Loading
+36 −31
Original line number Original line Diff line number Diff line
@@ -3621,34 +3621,23 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
        return false;
        return false;
    }
    }


    @GuardedBy("mMethodMap")
    static void queryInputMethodServicesInternal(Context context,
    void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
            @UserIdInt int userId, ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap,
        if (DEBUG) {
            ArrayMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
            Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
        methodList.clear();
                    + " \n ------ caller=" + Debug.getCallers(10));
        methodMap.clear();
        }
        if (!mSystemReady) {
            Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
            return;
        }
        mMethodList.clear();
        mMethodMap.clear();
        mMethodMapUpdateCount++;
        mMyPackageMonitor.clearKnownImePackageNamesLocked();

        // Use for queryIntentServicesAsUser
        final PackageManager pm = mContext.getPackageManager();


        // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
        // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
        // behavior of PackageManager is exactly what we want.  It by default picks up appropriate
        // behavior of PackageManager is exactly what we want.  It by default picks up appropriate
        // services depending on the unlock state for the specified user.
        // services depending on the unlock state for the specified user.
        final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
        final List<ResolveInfo> services = context.getPackageManager().queryIntentServicesAsUser(
                new Intent(InputMethod.SERVICE_INTERFACE),
                new Intent(InputMethod.SERVICE_INTERFACE),
                PackageManager.GET_META_DATA | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS,
                PackageManager.GET_META_DATA | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS,
                mSettings.getCurrentUserId());
                userId);

        methodList.ensureCapacity(services.size());
        methodMap.ensureCapacity(services.size());


        final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
                mFileManager.getAllAdditionalInputMethodSubtypes();
        for (int i = 0; i < services.size(); ++i) {
        for (int i = 0; i < services.size(); ++i) {
            ResolveInfo ri = services.get(i);
            ResolveInfo ri = services.get(i);
            ServiceInfo si = ri.serviceInfo;
            ServiceInfo si = ri.serviceInfo;
@@ -3662,20 +3651,35 @@ public class InputMethodManagerService extends IInputMethodManager.Stub


            if (DEBUG) Slog.d(TAG, "Checking " + imeId);
            if (DEBUG) Slog.d(TAG, "Checking " + imeId);


            final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
            try {
            try {
                InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
                final InputMethodInfo imi = new InputMethodInfo(context, ri,
                mMethodList.add(p);
                        additionalSubtypeMap.get(imeId));
                final String id = p.getId();
                methodList.add(imi);
                mMethodMap.put(id, p);
                methodMap.put(imi.getId(), imi);

                if (DEBUG) {
                if (DEBUG) {
                    Slog.d(TAG, "Found an input method " + p);
                    Slog.d(TAG, "Found an input method " + imi);
                }
                }
            } catch (Exception e) {
            } catch (Exception e) {
                Slog.wtf(TAG, "Unable to load input method " + imeId, e);
                Slog.wtf(TAG, "Unable to load input method " + imeId, e);
            }
            }
        }
        }
    }

    @GuardedBy("mMethodMap")
    void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
        if (DEBUG) {
            Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
                    + " \n ------ caller=" + Debug.getCallers(10));
        }
        if (!mSystemReady) {
            Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
            return;
        }
        mMethodMapUpdateCount++;
        mMyPackageMonitor.clearKnownImePackageNamesLocked();

        queryInputMethodServicesInternal(mContext, mSettings.getCurrentUserId(),
                mFileManager.getAllAdditionalInputMethodSubtypes(), mMethodMap, mMethodList);


        // Construct the set of possible IME packages for onPackageChanged() to avoid false
        // Construct the set of possible IME packages for onPackageChanged() to avoid false
        // negatives when the package state remains to be the same but only the component state is
        // negatives when the package state remains to be the same but only the component state is
@@ -3684,7 +3688,8 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
            // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
            // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
            // of this query is to avoid false negatives.  PackageManager.MATCH_ALL could be more
            // of this query is to avoid false negatives.  PackageManager.MATCH_ALL could be more
            // conservative, but it seems we cannot use it for now (Issue 35176630).
            // conservative, but it seems we cannot use it for now (Issue 35176630).
            final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
            final List<ResolveInfo> allInputMethodServices =
                    mContext.getPackageManager().queryIntentServicesAsUser(
                            new Intent(InputMethod.SERVICE_INTERFACE),
                            new Intent(InputMethod.SERVICE_INTERFACE),
                            PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
                            PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
            final int N = allInputMethodServices.size();
            final int N = allInputMethodServices.size();