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

Commit bb80da14 authored by Lyn's avatar Lyn
Browse files

Add @hide Notification.Builder.setSilent(boolean)

Bug: 290381858
Test: NotificationTest
Change-Id: I7e894032731b5561ff815b247570e673ed28836c
parent 33adcdf6
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
@@ -1628,6 +1628,14 @@ public class Notification implements Parcelable
     */
    public static final int GROUP_ALERT_CHILDREN = 2;
    /**
     * Constant for the {@link Builder#setGroup(String) group key} that is added to notifications
     * that are not already grouped when {@link Builder#setSilent()} is used.
     *
     * @hide
     */
    public static final String GROUP_KEY_SILENT = "silent";
    private int mGroupAlertBehavior = GROUP_ALERT_ALL;
    /**
@@ -4289,6 +4297,35 @@ public class Notification implements Parcelable
            return this;
        }
        /**
         * If {@code true}, silences this instance of the notification, regardless of the sounds or
         * vibrations set on the notification or notification channel. If {@code false}, then the
         * normal sound and vibration logic applies.
         *
         * @hide
         */
        public @NonNull Builder setSilent(boolean silent) {
            if (!silent) {
                return this;
            }
            if (mN.isGroupSummary()) {
                setGroupAlertBehavior(GROUP_ALERT_CHILDREN);
            } else {
                setGroupAlertBehavior(GROUP_ALERT_SUMMARY);
            }
            setVibrate(null);
            setSound(null);
            mN.defaults &= ~DEFAULT_SOUND;
            mN.defaults &= ~DEFAULT_VIBRATE;
            setDefaults(mN.defaults);
            if (TextUtils.isEmpty(mN.mGroupKey)) {
                setGroup(GROUP_KEY_SILENT);
            }
            return this;
        }
        /**
         * Set the first line of text in the platform notification template.
         */
+74 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package android.app;
import static android.app.Notification.CarExtender.UnreadConversation.KEY_ON_READ;
import static android.app.Notification.CarExtender.UnreadConversation.KEY_ON_REPLY;
import static android.app.Notification.CarExtender.UnreadConversation.KEY_REMOTE_INPUT;
import static android.app.Notification.DEFAULT_SOUND;
import static android.app.Notification.DEFAULT_VIBRATE;
import static android.app.Notification.EXTRA_ANSWER_INTENT;
import static android.app.Notification.EXTRA_BUILDER_APPLICATION_INFO;
import static android.app.Notification.EXTRA_CALL_PERSON;
@@ -35,6 +37,9 @@ import static android.app.Notification.EXTRA_PICTURE;
import static android.app.Notification.EXTRA_PICTURE_ICON;
import static android.app.Notification.EXTRA_SUMMARY_TEXT;
import static android.app.Notification.EXTRA_TITLE;
import static android.app.Notification.GROUP_ALERT_CHILDREN;
import static android.app.Notification.GROUP_ALERT_SUMMARY;
import static android.app.Notification.GROUP_KEY_SILENT;
import static android.app.Notification.MessagingStyle.Message.KEY_DATA_URI;
import static android.app.Notification.MessagingStyle.Message.KEY_SENDER_PERSON;
import static android.app.Notification.MessagingStyle.Message.KEY_TEXT;
@@ -510,6 +515,75 @@ public class NotificationTest {
                .isGreaterThan(ContrastColorUtil.calculateLuminance(background));
    }

    @Test
    public void testBuilder_setSilent_summaryBehavior_groupAlertChildren() {
        Notification summaryNotif = new Notification.Builder(mContext, "channelId")
                .setGroupSummary(true)
                .setGroup("groupKey")
                .setSilent(true)
                .build();
        assertEquals(GROUP_ALERT_CHILDREN, summaryNotif.getGroupAlertBehavior());
    }

    @Test
    public void testBuilder_setSilent_childBehavior_groupAlertSummary() {
        Notification childNotif = new Notification.Builder(mContext, "channelId")
                .setGroupSummary(false)
                .setGroup("groupKey")
                .setSilent(true)
                .build();
        assertEquals(GROUP_ALERT_SUMMARY, childNotif.getGroupAlertBehavior());
    }

    @Test
    public void testBuilder_setSilent_emptyGroupKey_groupKeySilent() {
        Notification emptyGroupKeyNotif = new Notification.Builder(mContext, "channelId")
                .setGroup("")
                .setSilent(true)
                .build();
        assertEquals(GROUP_KEY_SILENT, emptyGroupKeyNotif.getGroup());
    }

    @Test
    public void testBuilder_setSilent_vibrateNull() {
        Notification silentNotif = new Notification.Builder(mContext, "channelId")
                .setGroup("")
                .setSilent(true)
                .build();

        assertNull(silentNotif.vibrate);
    }

    @Test
    public void testBuilder_setSilent_soundNull() {
        Notification silentNotif = new Notification.Builder(mContext, "channelId")
                .setGroup("")
                .setSilent(true)
                .build();

        assertNull(silentNotif.sound);
    }

    @Test
    public void testBuilder_setSilent_noDefaultSound() {
        Notification silentNotif = new Notification.Builder(mContext, "channelId")
                .setGroup("")
                .setSilent(true)
                .build();

        assertEquals(0, (silentNotif.defaults & DEFAULT_SOUND));
    }

    @Test
    public void testBuilder_setSilent_noDefaultVibrate() {
        Notification silentNotif = new Notification.Builder(mContext, "channelId")
                .setGroup("")
                .setSilent(true)
                .build();

        assertEquals(0, (silentNotif.defaults & DEFAULT_VIBRATE));
    }

    @Test
    public void testCallStyle_getSystemActions_forIncomingCall() {
        PendingIntent answerIntent = createPendingIntent("answer");