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

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

Revoke NLS permission if the last association has been removed

When last association of a package is removed, the corresponding NLS permission should be removed too.

Bug: 391895151
Test: manual test + unit test
Change-Id: I193b0dbb700f891d0dbfcf31cf9548fbd7ca0866
Merged-In: I09ac1ef07b65e5180d38e8697d4507a113162eb7
parent 2f7bc0f0
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -60,10 +60,12 @@ import android.companion.ICompanionDeviceManager;
import android.companion.IOnAssociationsChangedListener;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
import android.content.pm.ResolveInfo;
import android.content.pm.UserInfo;
import android.net.MacAddress;
import android.net.NetworkPolicyManager;
@@ -80,6 +82,7 @@ import android.os.ServiceManager;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserManager;
import android.service.notification.NotificationListenerService;
import android.util.ArraySet;
import android.util.ExceptionUtils;
import android.util.Log;
@@ -336,6 +339,24 @@ public class CompanionDeviceManagerService extends SystemService {
        final String packageName = association.getPackageName();

        if (changeType == AssociationStore.CHANGE_TYPE_REMOVED) {
            // Revoke NLS if the last association has been removed for the package
            Binder.withCleanCallingIdentity(() -> {
                if (mAssociationStore.getAssociationsForPackage(userId, packageName).isEmpty()) {
                    NotificationManager nm = getContext().getSystemService(
                        NotificationManager.class);
                    Intent nlsIntent = new Intent(
                            NotificationListenerService.SERVICE_INTERFACE);
                    List<ResolveInfo> matchedServiceList = getContext().getPackageManager()
                            .queryIntentServicesAsUser(nlsIntent, /* flags */ 0, userId);
                    for (ResolveInfo service : matchedServiceList) {
                        if (service.getComponentInfo().getComponentName().getPackageName()
                                .equals(packageName)) {
                            nm.setNotificationListenerAccessGranted(
                                    service.getComponentInfo().getComponentName(), false);
                        }
                    }
                }
            });
            markIdAsPreviouslyUsedForPackage(id, userId, packageName);
        }