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

Commit 4924b16c authored by Guojing Yuan's avatar Guojing Yuan Committed by Mohammed Althaf T
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 87a2162e
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -76,10 +76,12 @@ import android.companion.ObservingDevicePresenceRequest;
import android.companion.datatransfer.PermissionSyncRequest;
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.hardware.power.Mode;
import android.net.MacAddress;
@@ -99,6 +101,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;
@@ -546,6 +549,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);
        }