Update alarm and messaging dnd filtering

- Check audio attributes on the notificationrecord, not the
notification (to take into account the notification channel audio)
- Allow notifications that use messaging style to be considered a
message for dnd

Test: runtest systemui-notification
Change-Id: I8e6a86ed62089a53fe71000e6d9da00f1c54b59e
Fixes: 74243745
Fixes: 74311423
diff --git a/core/java/com/android/internal/util/NotificationMessagingUtil.java b/core/java/com/android/internal/util/NotificationMessagingUtil.java
index 518cf41..b962d4f 100644
--- a/core/java/com/android/internal/util/NotificationMessagingUtil.java
+++ b/core/java/com/android/internal/util/NotificationMessagingUtil.java
@@ -78,6 +78,10 @@
             return false;
         }
 
+        return isMessaging(sbn);
+    }
+
+    public boolean isMessaging(StatusBarNotification sbn) {
         Class<? extends Notification.Style> style = sbn.getNotification().getNotificationStyle();
         if (Notification.MessagingStyle.class.equals(style)) {
             return true;
diff --git a/services/core/java/com/android/server/notification/NotificationRecord.java b/services/core/java/com/android/server/notification/NotificationRecord.java
index 8d2f0dd..4404c48 100644
--- a/services/core/java/com/android/server/notification/NotificationRecord.java
+++ b/services/core/java/com/android/server/notification/NotificationRecord.java
@@ -712,13 +712,8 @@
         return Objects.equals(getNotification().category, category);
     }
 
-    public boolean isAudioStream(int stream) {
-        return getNotification().audioStreamType == stream;
-    }
-
     public boolean isAudioAttributesUsage(int usage) {
-        final AudioAttributes attributes = getNotification().audioAttributes;
-        return attributes != null && attributes.getUsage() == usage;
+        return mAttributes != null && mAttributes.getUsage() == usage;
     }
 
     /**
diff --git a/services/core/java/com/android/server/notification/ZenModeFiltering.java b/services/core/java/com/android/server/notification/ZenModeFiltering.java
index a7e0c51..71cee05 100644
--- a/services/core/java/com/android/server/notification/ZenModeFiltering.java
+++ b/services/core/java/com/android/server/notification/ZenModeFiltering.java
@@ -34,6 +34,7 @@
 import android.util.Slog;
 
 import com.android.internal.messages.nano.SystemMessageProto;
+import com.android.internal.util.NotificationMessagingUtil;
 
 import java.io.PrintWriter;
 import java.util.Date;
@@ -48,9 +49,16 @@
     private final Context mContext;
 
     private ComponentName mDefaultPhoneApp;
+    private final NotificationMessagingUtil mMessagingUtil;
 
     public ZenModeFiltering(Context context) {
         mContext = context;
+        mMessagingUtil = new NotificationMessagingUtil(mContext);
+    }
+
+    public ZenModeFiltering(Context context, NotificationMessagingUtil messagingUtil) {
+        mContext = context;
+        mMessagingUtil = messagingUtil;
     }
 
     public void dump(PrintWriter pw, String prefix) {
@@ -207,9 +215,8 @@
         return false;
     }
 
-    private static boolean isAlarm(NotificationRecord record) {
+    protected static boolean isAlarm(NotificationRecord record) {
         return record.isCategory(Notification.CATEGORY_ALARM)
-                || record.isAudioStream(AudioManager.STREAM_ALARM)
                 || record.isAudioAttributesUsage(AudioAttributes.USAGE_ALARM);
     }
 
@@ -249,17 +256,8 @@
                 && pkg.equals(mDefaultPhoneApp.getPackageName());
     }
 
-    @SuppressWarnings("deprecation")
-    private boolean isDefaultMessagingApp(NotificationRecord record) {
-        final int userId = record.getUserId();
-        if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false;
-        final String defaultApp = Secure.getStringForUser(mContext.getContentResolver(),
-                Secure.SMS_DEFAULT_APPLICATION, userId);
-        return Objects.equals(defaultApp, record.sbn.getPackageName());
-    }
-
-    private boolean isMessage(NotificationRecord record) {
-        return record.isCategory(Notification.CATEGORY_MESSAGE) || isDefaultMessagingApp(record);
+    protected boolean isMessage(NotificationRecord record) {
+        return mMessagingUtil.isMessaging(record.sbn);
     }
 
     private static boolean audienceMatches(int source, float contactAffinity) {
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeFilteringTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeFilteringTest.java
index 1936439..c0bd7cc 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeFilteringTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeFilteringTest.java
@@ -25,12 +25,14 @@
 import static junit.framework.Assert.assertFalse;
 import static junit.framework.Assert.assertTrue;
 
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import android.app.Notification;
 import android.app.NotificationChannel;
 import android.app.NotificationManager;
+import android.media.AudioAttributes;
 import android.service.notification.StatusBarNotification;
 import android.service.notification.ZenModeConfig;
 import android.test.suitebuilder.annotation.SmallTest;
@@ -38,28 +40,80 @@
 import android.testing.TestableLooper;
 
 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
+import com.android.internal.util.NotificationMessagingUtil;
 import com.android.server.UiServiceTestCase;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper
 public class ZenModeFilteringTest extends UiServiceTestCase {
 
+    @Mock
+    private NotificationMessagingUtil mMessagingUtil;
     private ZenModeFiltering mZenModeFiltering;
 
     @Before
     public void setUp() {
-        mZenModeFiltering = new ZenModeFiltering(mContext);
+        MockitoAnnotations.initMocks(this);
+        mZenModeFiltering = new ZenModeFiltering(mContext, mMessagingUtil);
     }
 
     private NotificationRecord getNotificationRecord() {
+        return getNotificationRecord(mock(NotificationChannel.class));
+    }
+
+    private NotificationRecord getNotificationRecord(NotificationChannel c) {
         StatusBarNotification sbn = mock(StatusBarNotification.class);
         when(sbn.getNotification()).thenReturn(mock(Notification.class));
-        return new NotificationRecord(mContext, sbn, mock(NotificationChannel.class));
+        return new NotificationRecord(mContext, sbn, c);
+    }
+
+    @Test
+    public void testIsMessage() {
+        NotificationRecord r = getNotificationRecord();
+
+        when(mMessagingUtil.isMessaging(any())).thenReturn(true);
+        assertTrue(mZenModeFiltering.isMessage(r));
+
+        when(mMessagingUtil.isMessaging(any())).thenReturn(false);
+        assertFalse(mZenModeFiltering.isMessage(r));
+    }
+
+    @Test
+    public void testIsAlarm() {
+        NotificationChannel c = mock(NotificationChannel.class);
+        when(c.getAudioAttributes()).thenReturn(new AudioAttributes.Builder()
+                .setUsage(AudioAttributes.USAGE_ALARM)
+                .build());
+        NotificationRecord r = getNotificationRecord(c);
+        assertTrue(mZenModeFiltering.isAlarm(r));
+
+        r = getNotificationRecord();
+        r.sbn.getNotification().category = Notification.CATEGORY_ALARM;
+        assertTrue(mZenModeFiltering.isAlarm(r));
+    }
+
+    @Test
+    public void testIsAlarm_wrongCategory() {
+        NotificationRecord r = getNotificationRecord();
+        r.sbn.getNotification().category = Notification.CATEGORY_CALL;
+        assertFalse(mZenModeFiltering.isAlarm(r));
+    }
+
+    @Test
+    public void testIsAlarm_wrongUsage() {
+        NotificationChannel c = mock(NotificationChannel.class);
+        when(c.getAudioAttributes()).thenReturn(new AudioAttributes.Builder()
+                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
+                .build());
+        NotificationRecord r = getNotificationRecord(c);
+        assertFalse(mZenModeFiltering.isAlarm(r));
     }
 
     @Test