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

Commit 6cd5ab63 authored by Hai Zhang's avatar Hai Zhang
Browse files

Keep "None" set for assistant upon device upgrade.

If the assistant role is added for the first time, the device is
upgrading, and legacy role resolution didn't set any holder, it
implies the user selected "None" for assistant, and we need to keep
that in our new implementation.

Fixes: 141255935
Test: manual
Test: Upgrade P => Q without this CL and "None" assistant is lost
Test: Upgrade P => Q with this CL and "None" assistant is kept
Test: Fresh Q with this CL gets the default assistant set
Change-Id: Ie2668d29c43751686dbe9523bcc298b73618fc91
(cherry picked from commit 20551844)
parent 1189512b
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.packageinstaller.role.model;

import android.app.ActivityManager;
import android.app.Application;
import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
@@ -25,6 +26,7 @@ import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Process;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.voice.VoiceInteractionService;
@@ -58,6 +60,20 @@ public class AssistantRoleBehavior implements RoleBehavior {
            new Intent(VoiceInteractionService.SERVICE_INTERFACE);
    private static final Intent ASSIST_ACTIVITY_PROBE = new Intent(Intent.ACTION_ASSIST);

    @Override
    public void onRoleAdded(@NonNull Role role, @NonNull Context context) {
        PackageManager packageManager = context.getPackageManager();
        if (packageManager.isDeviceUpgrading()) {
            RoleManager roleManager = context.getSystemService(RoleManager.class);
            List<String> packageNames = roleManager.getRoleHolders(role.getName());
            if (packageNames.isEmpty()) {
                // If the device was upgraded, and there isn't any legacy role holders, it means
                // user selected "None" in Settings and we need to keep that.
                role.onNoneHolderSelectedAsUser(Process.myUserHandle(), context);
            }
        }
    }

    @Override
    public boolean isAvailableAsUser(@NonNull Role role, @NonNull UserHandle user,
            @NonNull Context context) {
+11 −0
Original line number Diff line number Diff line
@@ -253,6 +253,17 @@ public class Role {
        return mPreferredActivities;
    }

    /**
     * Callback when this role is added to the system for the first time.
     *
     * @param context the {@code Context} to retrieve system services
     */
    public void onRoleAdded(@NonNull Context context) {
        if (mBehavior != null) {
            mBehavior.onRoleAdded(this, context);
        }
    }

    /**
     * Check whether this role is available.
     *
+5 −0
Original line number Diff line number Diff line
@@ -35,6 +35,11 @@ import java.util.List;
 */
public interface RoleBehavior {

    /**
     * @see Role#onRoleAdded(Context)
     */
    default void onRoleAdded(@NonNull Role role, @NonNull Context context) {}

    /**
     * @see Role#isAvailableAsUser(UserHandle, Context)
     */
+8 −0
Original line number Diff line number Diff line
@@ -89,6 +89,14 @@ public class RoleControllerServiceImpl extends RoleControllerService {
        // Set the available role names in RoleManager.
        mRoleManager.setRoleNamesFromController(roleNames);

        int addedRoleNamesSize = addedRoleNames.size();
        for (int i = 0; i < addedRoleNamesSize; i++) {
            String roleName = addedRoleNames.valueAt(i);

            Role role = roleMap.get(roleName);
            role.onRoleAdded(this);
        }

        // Go through the holders of all roles.
        int rolesSize = roles.size();
        for (int rolesIndex = 0; rolesIndex < rolesSize; rolesIndex++) {