[CDM] Limit NLS revoke to applicable roles.

Bug: 421919075
Flag: EXEMPT bugfix
Test: CTS
(cherry picked from commit 8ccb7103a2d58ec2cee0a1cf5f8c7fefa5e8e8ee)
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:ffa1ea81ac0c11f9d0c5a81b6e85772c23743210
Merged-In: Icd3cdb3234a516ff7f752f39d7b1d8e6f5cde1ef
Change-Id: Icd3cdb3234a516ff7f752f39d7b1d8e6f5cde1ef
diff --git a/services/companion/java/com/android/server/companion/association/DisassociationProcessor.java b/services/companion/java/com/android/server/companion/association/DisassociationProcessor.java
index d250939..3f3bd82 100644
--- a/services/companion/java/com/android/server/companion/association/DisassociationProcessor.java
+++ b/services/companion/java/com/android/server/companion/association/DisassociationProcessor.java
@@ -20,6 +20,7 @@
 import static android.companion.AssociationRequest.DEVICE_PROFILE_AUTOMOTIVE_PROJECTION;
 
 import static com.android.internal.util.CollectionUtils.any;
+import static com.android.server.companion.utils.RolesUtils.NLS_PROFILES;
 import static com.android.server.companion.utils.RolesUtils.removeRoleHolderForAssociation;
 
 import static java.util.concurrent.TimeUnit.DAYS;
@@ -146,15 +147,18 @@
         // 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 (association.getDeviceProfile() != null
+                        && NLS_PROFILES.contains(association.getDeviceProfile())) {
+                    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, false);
+                        }
                     }
                 }
             }
diff --git a/services/companion/java/com/android/server/companion/utils/RolesUtils.java b/services/companion/java/com/android/server/companion/utils/RolesUtils.java
index 2b281f3f..9c65baf 100644
--- a/services/companion/java/com/android/server/companion/utils/RolesUtils.java
+++ b/services/companion/java/com/android/server/companion/utils/RolesUtils.java
@@ -17,6 +17,10 @@
 package com.android.server.companion.utils;
 
 import static android.app.role.RoleManager.MANAGE_HOLDERS_FLAG_DONT_KILL_APP;
+import static android.companion.AssociationRequest.DEVICE_PROFILE_APP_STREAMING;
+import static android.companion.AssociationRequest.DEVICE_PROFILE_COMPUTER;
+import static android.companion.AssociationRequest.DEVICE_PROFILE_GLASSES;
+import static android.companion.AssociationRequest.DEVICE_PROFILE_WATCH;
 
 import android.annotation.NonNull;
 import android.annotation.SuppressLint;
@@ -41,6 +45,12 @@
 
     private static final String TAG = "CDM_RolesUtils";
 
+    public static final Set<String> NLS_PROFILES = Set.of(
+            DEVICE_PROFILE_WATCH,
+            DEVICE_PROFILE_GLASSES,
+            DEVICE_PROFILE_APP_STREAMING,
+            DEVICE_PROFILE_COMPUTER);
+
     private static final Set<String> ROLELESS_DEVICE_PROFILES;
     static {
         final Set<String> profiles = new ArraySet<>();