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

Commit 215452e2 authored by Lyn Han's avatar Lyn Han Committed by Android (Google) Code Review
Browse files

Merge "Exempt alarms and car emergency notifications from suppression" into main

parents acc88bb2 f8f59245
Loading
Loading
Loading
Loading
+20 −4
Original line number Diff line number Diff line
@@ -230,9 +230,17 @@ public final class NotificationAttentionHelper {
                    mFlagResolver.getIntValue(NotificationFlags.NOTIF_VOLUME1),
                    mFlagResolver.getIntValue(NotificationFlags.NOTIF_VOLUME2),
                    mFlagResolver.getIntValue(NotificationFlags.NOTIF_COOLDOWN_COUNTER_RESET),
                    record -> mPackageManager.checkPermission(
                    record -> {
                        final String category = record.getNotification().category;
                        if (Notification.CATEGORY_ALARM.equals(category)
                                || Notification.CATEGORY_CAR_EMERGENCY.equals(category)
                                || Notification.CATEGORY_CAR_WARNING.equals(category)) {
                            return true;
                        }
                        return mPackageManager.checkPermission(
                            permission.RECEIVE_EMERGENCY_BROADCAST,
                            record.getSbn().getPackageName()) == PERMISSION_GRANTED);
                            record.getSbn().getPackageName()) == PERMISSION_GRANTED;
                    });

            return new StrategyAvalanche(
                    mFlagResolver.getIntValue(NotificationFlags.NOTIF_COOLDOWN_T1),
@@ -248,9 +256,17 @@ public final class NotificationAttentionHelper {
                    mFlagResolver.getIntValue(NotificationFlags.NOTIF_VOLUME1),
                    mFlagResolver.getIntValue(NotificationFlags.NOTIF_VOLUME2),
                    mFlagResolver.getIntValue(NotificationFlags.NOTIF_COOLDOWN_COUNTER_RESET),
                    record -> mPackageManager.checkPermission(
                    record -> {
                        final String category = record.getNotification().category;
                        if (Notification.CATEGORY_ALARM.equals(category)
                                || Notification.CATEGORY_CAR_EMERGENCY.equals(category)
                                || Notification.CATEGORY_CAR_WARNING.equals(category)) {
                            return true;
                        }
                        return mPackageManager.checkPermission(
                            permission.RECEIVE_EMERGENCY_BROADCAST,
                            record.getSbn().getPackageName()) == PERMISSION_GRANTED);
                            record.getSbn().getPackageName()) == PERMISSION_GRANTED;
                    });
        }
    }

+111 −0
Original line number Diff line number Diff line
@@ -2453,6 +2453,50 @@ public class NotificationAttentionHelperTest extends UiServiceTestCase {
        verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt());
    }

    @Test
    public void testBeepVolume_politeNotif_Avalanche_exemptCategories() throws Exception {
        mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
        mSetFlagsRule.enableFlags(Flags.FLAG_CROSS_APP_POLITE_NOTIFICATIONS);
        mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS_ATTN_UPDATE);
        TestableFlagResolver flagResolver = new TestableFlagResolver();
        flagResolver.setFlagOverride(NotificationFlags.NOTIF_VOLUME1, 50);
        flagResolver.setFlagOverride(NotificationFlags.NOTIF_VOLUME2, 0);
        initAttentionHelper(flagResolver);

        // Trigger avalanche trigger intent
        final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", false);
        mAvalancheBroadcastReceiver.onReceive(getContext(), intent);

        // CATEGORY_ALARM is exempted
        NotificationRecord r = getBeepyNotification();
        r.getNotification().category = Notification.CATEGORY_ALARM;
        // Should beep at 100% volume
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        verifyBeepVolume(1.0f);
        assertNotEquals(-1, r.getLastAudiblyAlertedMs());

        // CATEGORY_CAR_EMERGENCY is exempted
        Mockito.reset(mRingtonePlayer);
        NotificationRecord r2 = getBeepyNotification();
        r2.getNotification().category = Notification.CATEGORY_CAR_EMERGENCY;
        // Should beep at 100% volume
        mAttentionHelper.buzzBeepBlinkLocked(r2, DEFAULT_SIGNALS);
        verifyBeepVolume(1.0f);
        assertNotEquals(-1, r2.getLastAudiblyAlertedMs());

        // CATEGORY_CAR_WARNING is exempted
        Mockito.reset(mRingtonePlayer);
        NotificationRecord r3 = getBeepyNotification();
        r3.getNotification().category = Notification.CATEGORY_CAR_WARNING;
        // Should beep at 100% volume
        mAttentionHelper.buzzBeepBlinkLocked(r3, DEFAULT_SIGNALS);
        verifyBeepVolume(1.0f);
        assertNotEquals(-1, r3.getLastAudiblyAlertedMs());

        verify(mAccessibilityService, times(3)).sendAccessibilityEvent(any(), anyInt());
    }

    @Test
    public void testBeepVolume_politeNotif_exemptEmergency() throws Exception {
        mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
@@ -2491,6 +2535,73 @@ public class NotificationAttentionHelperTest extends UiServiceTestCase {
        verify(mAccessibilityService, times(3)).sendAccessibilityEvent(any(), anyInt());
    }

    @Test
    public void testBeepVolume_politeNotif_exemptCategories() throws Exception {
        mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
        mSetFlagsRule.disableFlags(Flags.FLAG_CROSS_APP_POLITE_NOTIFICATIONS);
        mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS_ATTN_UPDATE);
        TestableFlagResolver flagResolver = new TestableFlagResolver();
        flagResolver.setFlagOverride(NotificationFlags.NOTIF_VOLUME1, 50);
        flagResolver.setFlagOverride(NotificationFlags.NOTIF_VOLUME2, 0);
        // NOTIFICATION_COOLDOWN_ALL setting is enabled
        Settings.System.putInt(getContext().getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ALL, 1);
        initAttentionHelper(flagResolver);

        // CATEGORY_ALARM is exempted
        NotificationRecord r = getBeepyNotification();
        r.getNotification().category = Notification.CATEGORY_ALARM;
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        Mockito.reset(mRingtonePlayer);

        // update should beep at 100% volume
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        assertNotEquals(-1, r.getLastAudiblyAlertedMs());
        verifyBeepVolume(1.0f);

        // 2nd update should beep at 100% volume
        Mockito.reset(mRingtonePlayer);
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        assertNotEquals(-1, r.getLastAudiblyAlertedMs());
        verifyBeepVolume(1.0f);

        // CATEGORY_CAR_WARNING is exempted
        r = getBeepyNotification();
        r.getNotification().category = Notification.CATEGORY_CAR_WARNING;
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        Mockito.reset(mRingtonePlayer);

        // update should beep at 100% volume
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        assertNotEquals(-1, r.getLastAudiblyAlertedMs());
        verifyBeepVolume(1.0f);

        // 2nd update should beep at 100% volume
        Mockito.reset(mRingtonePlayer);
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        assertNotEquals(-1, r.getLastAudiblyAlertedMs());
        verifyBeepVolume(1.0f);

        // CATEGORY_CAR_EMERGENCY is exempted
        r = getBeepyNotification();
        r.getNotification().category = Notification.CATEGORY_CAR_EMERGENCY;
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        Mockito.reset(mRingtonePlayer);

        // update should beep at 100% volume
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        assertNotEquals(-1, r.getLastAudiblyAlertedMs());
        verifyBeepVolume(1.0f);

        // 2nd update should beep at 100% volume
        Mockito.reset(mRingtonePlayer);
        mAttentionHelper.buzzBeepBlinkLocked(r, DEFAULT_SIGNALS);
        assertNotEquals(-1, r.getLastAudiblyAlertedMs());
        verifyBeepVolume(1.0f);

        verify(mAccessibilityService, times(9)).sendAccessibilityEvent(any(), anyInt());
    }

    @Test
    public void testBeepVolume_politeNotif_applyPerApp() throws Exception {
        mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);