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

Commit 9473c364 authored by Guojing Yuan's avatar Guojing Yuan Committed by Android Build Coastguard Worker
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
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:2a6e5b9b41a715431a1c0ee18e647db72c9f7dca)
Merged-In: I193b0dbb700f891d0dbfcf31cf9548fbd7ca0866
Change-Id: I193b0dbb700f891d0dbfcf31cf9548fbd7ca0866
parent aa93bf9d
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -67,10 +67,12 @@ import android.companion.ISystemDataTransferCallback;
import android.companion.utils.FeatureUtils;
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;
@@ -87,6 +89,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;
@@ -436,6 +439,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);
        }