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

Commit 00e0a7be authored by Hai Zhang's avatar Hai Zhang Committed by android-build-team Robot
Browse files

Fix assistant role availability on low ram devices.

The assistant role should be available on low ram devices, just that
only assistant activities but not assistant services can be qualified.

Fixes: 137388024
Test: presubmit & manual
Change-Id: Ie84fd163fb49c0514f51ba64ba89c225dceb2e5b
(cherry picked from commit 2300b890)
parent e6f518c0
Loading
Loading
Loading
Loading
+34 −30
Original line number Diff line number Diff line
@@ -61,8 +61,7 @@ public class AssistantRoleBehavior implements RoleBehavior {
    @Override
    public boolean isAvailableAsUser(@NonNull Role role, @NonNull UserHandle user,
            @NonNull Context context) {
        return !UserUtils.isWorkProfile(user, context)
                && !context.getSystemService(ActivityManager.class).isLowRamDevice();
        return !UserUtils.isWorkProfile(user, context);
    }

    @Nullable
@@ -97,13 +96,15 @@ public class AssistantRoleBehavior implements RoleBehavior {
    public List<String> getQualifyingPackagesAsUser(@NonNull Role role, @NonNull UserHandle user,
            @NonNull Context context) {
        Context userContext = UserUtils.getUserContext(context, user);
        ActivityManager userActivityManager = userContext.getSystemService(ActivityManager.class);
        PackageManager userPackageManager = userContext.getPackageManager();
        Set<String> availableAssistants = new ArraySet<>();

        List<ResolveInfo> services = userPackageManager.queryIntentServices(ASSIST_SERVICE_PROBE,
                PackageManager.GET_META_DATA | PackageManager.MATCH_DIRECT_BOOT_AWARE
        if (!userActivityManager.isLowRamDevice()) {
            List<ResolveInfo> services = userPackageManager.queryIntentServices(
                    ASSIST_SERVICE_PROBE, PackageManager.GET_META_DATA
                            | PackageManager.MATCH_DIRECT_BOOT_AWARE
                            | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);

            int numServices = services.size();
            for (int i = 0; i < numServices; i++) {
                ResolveInfo service = services.get(i);
@@ -112,12 +113,12 @@ public class AssistantRoleBehavior implements RoleBehavior {
                    availableAssistants.add(service.serviceInfo.packageName);
                }
            }
        }

        List<ResolveInfo> activities = userPackageManager.queryIntentActivities(
                ASSIST_ACTIVITY_PROBE, PackageManager.MATCH_DEFAULT_ONLY
                        | PackageManager.MATCH_DIRECT_BOOT_AWARE
                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);

        int numActivities = activities.size();
        for (int i = 0; i < numActivities; i++) {
            availableAssistants.add(activities.get(i).activityInfo.packageName);
@@ -130,31 +131,34 @@ public class AssistantRoleBehavior implements RoleBehavior {
    @Override
    public Boolean isPackageQualified(@NonNull Role role, @NonNull String packageName,
            @NonNull Context context) {
        PackageManager pm = context.getPackageManager();
        ActivityManager activityManager = context.getSystemService(ActivityManager.class);
        PackageManager packageManager = context.getPackageManager();

        boolean hasAssistantService = false;
        if (!activityManager.isLowRamDevice()) {
            Intent pkgServiceProbe = new Intent(ASSIST_SERVICE_PROBE).setPackage(packageName);
        List<ResolveInfo> services = pm.queryIntentServices(pkgServiceProbe,
            List<ResolveInfo> services = packageManager.queryIntentServices(pkgServiceProbe,
                    PackageManager.GET_META_DATA | PackageManager.MATCH_DIRECT_BOOT_AWARE
                            | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);

            hasAssistantService = !services.isEmpty();
            int numServices = services.size();
            for (int i = 0; i < numServices; i++) {
                ResolveInfo service = services.get(i);

            if (isAssistantVoiceInteractionService(pm, service.serviceInfo)) {
                if (isAssistantVoiceInteractionService(packageManager, service.serviceInfo)) {
                    return true;
                }
            }
        }

        Intent pkgActivityProbe = new Intent(ASSIST_ACTIVITY_PROBE).setPackage(packageName);
        boolean hasAssistantActivity = !pm.queryIntentActivities(pkgActivityProbe,
        boolean hasAssistantActivity = !packageManager.queryIntentActivities(pkgActivityProbe,
                PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_DIRECT_BOOT_AWARE
                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE).isEmpty();

        if (!hasAssistantActivity) {
            Log.w(LOG_TAG, "Package " + packageName + " not qualified for " + role.getName()
                    + " due to " + (services.isEmpty() ? "missing service"
                    : "service without qualifying metadata") + " and missing activity");
                    + " due to " + (hasAssistantService ? "unqualified" : "missing")
                    + " service and missing activity");
        }

        return hasAssistantActivity;