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

Commit 72892c4f authored by Hai Zhang's avatar Hai Zhang
Browse files

Make onGrantDefaultRoles() run on worker thread.

This re-initialization can happen relatively frequently when packages
or package component state changes, so if we don't do synchronization
we may run into concurrency issues when modifying state in
RoleManagerService.

Bug: 110557011
Test: build
Change-Id: I26dfe1cba9d66d7ee5b9d61480e93b792f2b1f8a
parent b7f954e3
Loading
Loading
Loading
Loading
+14 −8
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import com.android.packageinstaller.role.utils.PackageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Implementation of {@link RoleControllerService}.
@@ -67,6 +68,15 @@ public class RoleControllerServiceImpl extends RoleControllerService {
        mWorkerThread.quitSafely();
    }

    @Override
    public void onGrantDefaultRoles(@NonNull RoleManagerCallback callback) {
        if (callback == null) {
            Log.e(LOG_TAG, "callback cannot be null");
            return;
        }
        mWorkerHandler.post(() -> grantDefaultRoles(callback));
    }

    @Override
    public void onAddRoleHolder(@NonNull String roleName, @NonNull String packageName,
            @NonNull RoleManagerCallback callback) {
@@ -122,18 +132,14 @@ public class RoleControllerServiceImpl extends RoleControllerService {
        mWorkerHandler.post(() -> clearRoleHolders(roleName, callback));
    }

    @Override
    public void onGrantDefaultRoles(@NonNull RoleManagerCallback callback) {
        if (callback == null) {
            Log.e(LOG_TAG, "callback cannot be null");
            return;
        }

    @WorkerThread
    private void grantDefaultRoles(@NonNull RoleManagerCallback callback) {
        ArrayMap<String, Role> roles = Roles.getRoles(this);
        List<String> roleNames = new ArrayList<>();
        int rolesSize = roles.size();
        for (int i = 0; i < rolesSize; i++) {
            Role role = roles.valueAt(i);

            if (!role.isAvailable(this)) {
                continue;
            }
@@ -189,7 +195,7 @@ public class RoleControllerServiceImpl extends RoleControllerService {
            for (int i = 0; i < currentPackageNamesSize; i++) {
                String currentPackageName = currentPackageNames.get(i);

                if (currentPackageName.equals(packageName)) {
                if (Objects.equals(currentPackageName, packageName)) {
                    Log.i(LOG_TAG, packageName + " already holds " + roleName);
                    added = true;
                    continue;