Noti importance from certain Roles cannot be modified

by the notification assistant or user, like the default
phone app.

Fixes: 129358763
Test: atest
Change-Id: I40ef7ff403e2b0d81abe09f15c8804c2d3d2fb8a
diff --git a/services/core/java/com/android/server/notification/PreferencesHelper.java b/services/core/java/com/android/server/notification/PreferencesHelper.java
index 660309c..a3e90dc 100644
--- a/services/core/java/com/android/server/notification/PreferencesHelper.java
+++ b/services/core/java/com/android/server/notification/PreferencesHelper.java
@@ -37,6 +37,8 @@
 import android.service.notification.RankingHelperProto;
 import android.text.TextUtils;
 import android.util.ArrayMap;
+import android.util.ArraySet;
+import android.util.Pair;
 import android.util.Slog;
 import android.util.SparseBooleanArray;
 import android.util.proto.ProtoOutputStream;
@@ -100,6 +102,7 @@
     private static final boolean DEFAULT_SHOW_BADGE = true;
     private static final boolean DEFAULT_ALLOW_BUBBLE = true;
     private static final boolean DEFAULT_OEM_LOCKED_IMPORTANCE  = false;
+    private static final boolean DEFAULT_APP_LOCKED_IMPORTANCE  = false;
 
     /**
      * Default value for what fields are user locked. See {@link LockableAppFields} for all lockable
@@ -659,6 +662,7 @@
                 channel.setImportanceLockedByOEM(true);
             }
         }
+        channel.setImportanceLockedByCriticalDeviceFunction(r.defaultAppLockedImportance);
         if (channel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
             channel.setLockscreenVisibility(
                     NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE);
@@ -707,6 +711,10 @@
         if (updatedChannel.isImportanceLockedByOEM()) {
             updatedChannel.setImportance(channel.getImportance());
         }
+        updatedChannel.setImportanceLockedByCriticalDeviceFunction(r.defaultAppLockedImportance);
+        if (updatedChannel.isImportanceLockedByCriticalDeviceFunction()) {
+            updatedChannel.setImportance(channel.getImportance());
+        }
 
         r.channels.put(updatedChannel.getId(), updatedChannel);
 
@@ -844,6 +852,26 @@
         }
     }
 
+    public void updateDefaultApps(int userId, ArraySet<String> toRemove, ArraySet<String> toAdd) {
+        synchronized (mPackagePreferences) {
+            for (PackagePreferences p : mPackagePreferences.values()) {
+                if (userId == UserHandle.getUserId(p.uid)) {
+                    if (toRemove != null && toRemove.contains(p.pkg)) {
+                        p.defaultAppLockedImportance = false;
+                        for (NotificationChannel channel : p.channels.values()) {
+                            channel.setImportanceLockedByCriticalDeviceFunction(false);
+                        }
+                    } else if (toAdd != null && toAdd.contains(p.pkg)) {
+                        p.defaultAppLockedImportance = true;
+                        for (NotificationChannel channel : p.channels.values()) {
+                            channel.setImportanceLockedByCriticalDeviceFunction(true);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
     public NotificationChannelGroup getNotificationChannelGroupWithChannels(String pkg,
             int uid, String groupId, boolean includeDeleted) {
         Preconditions.checkNotNull(pkg);
@@ -1729,8 +1757,11 @@
         boolean showBadge = DEFAULT_SHOW_BADGE;
         boolean allowBubble = DEFAULT_ALLOW_BUBBLE;
         int lockedAppFields = DEFAULT_LOCKED_APP_FIELDS;
+        // these fields are loaded on boot from a different source of truth and so are not
+        // written to notification policy xml
         boolean oemLockedImportance = DEFAULT_OEM_LOCKED_IMPORTANCE;
         List<String> futureOemLockedChannels = new ArrayList<>();
+        boolean defaultAppLockedImportance = DEFAULT_APP_LOCKED_IMPORTANCE;
 
         Delegate delegate = null;
         ArrayMap<String, NotificationChannel> channels = new ArrayMap<>();