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

Commit 51eb78f8 authored by Julia Reynolds's avatar Julia Reynolds
Browse files

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
parent 0b4626aa
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -78,6 +78,10 @@ public class NotificationMessagingUtil {
            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;
+1 −6
Original line number Diff line number Diff line
@@ -712,13 +712,8 @@ public final class NotificationRecord {
        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;
    }

    /**
+11 −13
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.util.ArrayMap;
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 @@ public class ZenModeFiltering {
    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 @@ public class ZenModeFiltering {
        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 @@ public class ZenModeFiltering {
                && 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) {
+56 −2
Original line number Diff line number Diff line
@@ -25,12 +25,14 @@ import static android.provider.Settings.Global.ZEN_MODE_OFF;
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.AndroidTestingRunner;
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