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

Commit a987e9d9 authored by Guojing Yuan's avatar Guojing Yuan
Browse files

[CDM] Revoke NLS when all associations are removed.

Bug: 391895151
Test: manual test
Flag: EXEMPT bugfix
Change-Id: I09ac1ef07b65e5180d38e8697d4507a113162eb7
parent e3b2dbbf
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -152,6 +152,8 @@ public class CompanionDeviceManagerService extends SystemService {
        final UserManager userManager = context.getSystemService(UserManager.class);
        final PowerManagerInternal powerManagerInternal = LocalServices.getService(
                PowerManagerInternal.class);
        final NotificationManager notificationManager = context.getSystemService(
                NotificationManager.class);

        final AssociationDiskStore associationDiskStore = new AssociationDiskStore();
        mAssociationStore = new AssociationStore(context, userManager, associationDiskStore);
@@ -179,7 +181,8 @@ public class CompanionDeviceManagerService extends SystemService {

        mDisassociationProcessor = new DisassociationProcessor(context, activityManager,
                mAssociationStore, packageManagerInternal, mDevicePresenceProcessor,
                mCompanionAppBinder, mSystemDataTransferRequestStore, mTransportManager);
                mCompanionAppBinder, mSystemDataTransferRequestStore, mTransportManager,
                notificationManager);

        mSystemDataTransferProcessor = new SystemDataTransferProcessor(this,
                packageManagerInternal, mAssociationStore,
+27 −2
Original line number Diff line number Diff line
@@ -28,12 +28,16 @@ import android.annotation.NonNull;
import android.annotation.SuppressLint;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.NotificationManager;
import android.companion.AssociationInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManagerInternal;
import android.content.pm.ResolveInfo;
import android.os.Binder;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService;
import android.util.Slog;

import com.android.server.companion.datatransfer.SystemDataTransferRequestStore;
@@ -41,6 +45,8 @@ import com.android.server.companion.devicepresence.CompanionAppBinder;
import com.android.server.companion.devicepresence.DevicePresenceProcessor;
import com.android.server.companion.transport.CompanionTransportManager;

import java.util.List;

/**
 * This class responsible for disassociation.
 */
@@ -76,6 +82,7 @@ public class DisassociationProcessor {
    private final CompanionTransportManager mTransportManager;
    private final OnPackageVisibilityChangeListener mOnPackageVisibilityChangeListener;
    private final ActivityManager mActivityManager;
    private final NotificationManager mNotificationManager;

    public DisassociationProcessor(@NonNull Context context,
            @NonNull ActivityManager activityManager,
@@ -84,7 +91,8 @@ public class DisassociationProcessor {
            @NonNull DevicePresenceProcessor devicePresenceMonitor,
            @NonNull CompanionAppBinder applicationController,
            @NonNull SystemDataTransferRequestStore systemDataTransferRequestStore,
            @NonNull CompanionTransportManager companionTransportManager) {
            @NonNull CompanionTransportManager companionTransportManager,
            @NonNull NotificationManager notificationManager) {
        mContext = context;
        mActivityManager = activityManager;
        mAssociationStore = associationStore;
@@ -95,12 +103,12 @@ public class DisassociationProcessor {
        mCompanionAppController = applicationController;
        mSystemDataTransferRequestStore = systemDataTransferRequestStore;
        mTransportManager = companionTransportManager;
        mNotificationManager = notificationManager;
    }

    /**
     * Disassociate an association by id.
     */
    // TODO: also revoke notification access
    public void disassociate(int id, String reason) {
        Slog.i(TAG, "Disassociating id=[" + id + "]...");

@@ -135,6 +143,23 @@ public class DisassociationProcessor {
        mSystemDataTransferRequestStore.removeRequestsByAssociationId(userId, id);
        mAssociationStore.removeAssociation(association.getId(), reason);

        // Revoke NLS if the last association has been removed for the package
        Binder.withCleanCallingIdentity(() -> {
            if (mAssociationStore.getAssociationsByPackage(userId, packageName).isEmpty()) {
                Intent nlsIntent = new Intent(
                        NotificationListenerService.SERVICE_INTERFACE);
                List<ResolveInfo> matchedServiceList = mContext.getPackageManager()
                        .queryIntentServicesAsUser(nlsIntent, /* flags */ 0, userId);
                for (ResolveInfo service : matchedServiceList) {
                    if (service.getComponentInfo().getComponentName().getPackageName()
                            .equals(packageName)) {
                        mNotificationManager.setNotificationListenerAccessGranted(
                                service.getComponentInfo().getComponentName(), false);
                    }
                }
            }
        });

        // If role is not in use by other associations, revoke the role.
        // Do not need to remove the system role since it was pre-granted by the system.
        if (!isRoleInUseByOtherAssociations && deviceProfile != null && !deviceProfile.equals(