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

Commit 61732f5f authored by Ahaan Ugale's avatar Ahaan Ugale
Browse files

VIMS: Simplify findAvailInteractor() by getting metadata in initial pm query.

Also reorders the conditional blocks for readability.

Currently, available interactors are queried and one of them is selected, then the
metadata for it is retrieved through the VoiceInteractionServiceInfo constructor.

With this change, both things are done in a single step. That might mean we retrieve some
unneeded data (the metadata for the packages that aren't selected), but optimizing it
isn't important since there's usually just one interactor.

Bug: 178410946
Test: atest CtsVoiceInteractionTestCases
Change-Id: I9a0d859f12d043f47f33a307609dbcf9de759e7e
parent ba37dbb3
Loading
Loading
Loading
Loading
+28 −36
Original line number Diff line number Diff line
@@ -585,26 +585,29 @@ public class VoiceInteractionManagerService extends SystemService {
                    mContext.getPackageManager().queryIntentServicesAsUser(
                            new Intent(VoiceInteractionService.SERVICE_INTERFACE)
                                    .setPackage(packageName),
                            PackageManager.MATCH_DIRECT_BOOT_AWARE
                            PackageManager.GET_META_DATA
                                    | PackageManager.MATCH_DIRECT_BOOT_AWARE
                                    | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userHandle);
            int numAvailable = available.size();

            if (numAvailable == 0) {
                Slog.w(TAG, "no available voice interaction services found for user " + userHandle);
                return null;
            } else {
            }
            // Find first system package.  We never want to allow third party services to
            // be automatically selected, because those require approval of the user.
            VoiceInteractionServiceInfo foundInfo = null;
            for (int i = 0; i < numAvailable; i++) {
                ServiceInfo cur = available.get(i).serviceInfo;
                    if ((cur.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
                        ComponentName comp = new ComponentName(cur.packageName, cur.name);
                        try {
                            VoiceInteractionServiceInfo info = new VoiceInteractionServiceInfo(
                                    mContext.getPackageManager(), comp, userHandle);
                            if (info.getParseError() == null) {
                                if (foundInfo == null) {
                if ((cur.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                    continue;
                }
                VoiceInteractionServiceInfo info =
                        new VoiceInteractionServiceInfo(mContext.getPackageManager(), cur);
                if (info.getParseError() != null) {
                    Slog.w(TAG,
                            "Bad interaction service " + cur.packageName + "/"
                                    + cur.name + ": " + info.getParseError());
                } else if (foundInfo == null) {
                    foundInfo = info;
                } else {
                    Slog.w(TAG, "More than one voice interaction service, "
@@ -615,19 +618,9 @@ public class VoiceInteractionManagerService extends SystemService {
                            + " over "
                            + new ComponentName(cur.packageName, cur.name));
                }
                            } else {
                                Slog.w(TAG, "Bad interaction service " + comp + ": "
                                        + info.getParseError());
                            }
                        } catch (PackageManager.NameNotFoundException e) {
                            Slog.w(TAG, "Failure looking up interaction service " + comp);
                        }
            }
                }

            return foundInfo;
        }
        }

        ComponentName getCurInteractor(int userHandle) {
            String curInteractor = Settings.Secure.getStringForUser(
@@ -656,7 +649,6 @@ public class VoiceInteractionManagerService extends SystemService {
                            PackageManager.MATCH_DIRECT_BOOT_AWARE
                                    | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userHandle);
            int numAvailable = available.size();

            if (numAvailable == 0) {
                Slog.w(TAG, "no available voice recognition services found for user " + userHandle);
                return null;