Search in sources :

Example 61 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class NotificationPreferenceControllerTest method isAvailable.

@Test
public void isAvailable() {
    NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    NotificationChannel channel = mock(NotificationChannel.class);
    when(channel.getImportance()).thenReturn(IMPORTANCE_DEFAULT);
    NotificationChannelGroup group = mock(NotificationChannelGroup.class);
    when(group.isBlocked()).thenReturn(false);
    mController.onResume(appRow, channel, group, null);
    assertTrue(mController.isAvailable());
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) Test(org.junit.Test)

Example 62 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project Signal-Android by signalapp.

the class NotificationChannels method isMessagesChannelGroupEnabled.

/**
 * Whether or not the notification category for messages is enabled. Note that even if it is,
 * a user could have blocked the specific channel, or notifications overall, and it'd still be
 * true. See {@link #isMessageChannelEnabled(Context)} and {@link #areNotificationsEnabled(Context)}.
 */
public static synchronized boolean isMessagesChannelGroupEnabled(@NonNull Context context) {
    if (Build.VERSION.SDK_INT < 28) {
        return true;
    }
    NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
    NotificationChannelGroup group = notificationManager.getNotificationChannelGroup(CATEGORY_MESSAGES);
    return group != null && !group.isBlocked();
}
Also used : NotificationChannelGroup(android.app.NotificationChannelGroup) NotificationManager(android.app.NotificationManager)

Example 63 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project xDrip by NightscoutFoundation.

the class NotificationChannels method getChan.

@TargetApi(26)
public static NotificationChannel getChan(Notification.Builder wip) {
    final Notification temp = wip.build();
    if (temp.getChannelId() == null)
        return null;
    // create generic audio attributes
    final AudioAttributes generic_audio = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN).build();
    // create notification channel for hashing purposes from the existing notification builder
    NotificationChannel template = new NotificationChannel(temp.getChannelId(), getString(temp.getChannelId()), NotificationManager.IMPORTANCE_DEFAULT);
    // mirror the notification parameters in the channel
    template.setGroup(temp.getChannelId());
    template.setVibrationPattern(temp.vibrate);
    template.setSound(temp.sound, generic_audio);
    template.setLightColor(temp.ledARGB);
    if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
        // weird how this doesn't work like vibration pattern
        template.enableLights(true);
    template.setDescription(temp.getChannelId() + " " + wip.hashCode());
    // get a nice string to identify the hash
    final String mhash = my_text_hash(template);
    // create another notification channel using the hash because id is immutable
    final NotificationChannel channel = new NotificationChannel(template.getId() + mhash, getString(temp.getChannelId()) + mhash, NotificationManager.IMPORTANCE_DEFAULT);
    // mirror the settings from the previous channel
    channel.setSound(template.getSound(), generic_audio);
    if (addChannelGroup()) {
        channel.setGroup(template.getGroup());
    } else {
        channel.setGroup(channel.getId());
    }
    channel.setDescription(template.getDescription());
    channel.setVibrationPattern(template.getVibrationPattern());
    template.setLightColor(temp.ledARGB);
    if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
        // weird how this doesn't work like vibration pattern
        template.enableLights(true);
    template.setDescription(temp.getChannelId() + " " + wip.hashCode());
    // create a group to hold this channel if one doesn't exist or update text
    getNotifManager().createNotificationChannelGroup(new NotificationChannelGroup(channel.getGroup(), getString(channel.getGroup())));
    // create this channel if it doesn't exist or update text
    getNotifManager().createNotificationChannel(channel);
    return channel;
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) AudioAttributes(android.media.AudioAttributes) Notification(android.app.Notification) TargetApi(android.annotation.TargetApi)

Example 64 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project android_packages_apps_Settings by omnirom.

the class ChannelListPreferenceController method updateGroupList.

private void updateGroupList(@NonNull PreferenceCategory groupPrefsList, @NonNull List<NotificationChannelGroup> channelGroups) {
    // Update the list, but optimize for the most common case where the list hasn't changed.
    int numFinalGroups = channelGroups.size();
    int initialPrefCount = groupPrefsList.getPreferenceCount();
    List<PreferenceCategory> finalOrderedGroups = new ArrayList<>(numFinalGroups);
    for (int i = 0; i < numFinalGroups; i++) {
        NotificationChannelGroup group = channelGroups.get(i);
        PreferenceCategory groupCategory = findOrCreateGroupCategoryForKey(groupPrefsList, group.getId(), i);
        finalOrderedGroups.add(groupCategory);
        updateGroupPreferences(group, groupCategory);
    }
    int postAddPrefCount = groupPrefsList.getPreferenceCount();
    // If any groups were inserted (into a non-empty list) or need to be removed, we need to
    // remove all groups and re-add them all.
    // This is required to ensure proper ordering of inserted groups, and it simplifies logic
    // at the cost of computation in the rare case that the list is changing.
    boolean hasInsertions = initialPrefCount != 0 && initialPrefCount != numFinalGroups;
    boolean requiresRemoval = postAddPrefCount != numFinalGroups;
    if (hasInsertions || requiresRemoval) {
        groupPrefsList.removeAll();
        for (PreferenceCategory group : finalOrderedGroups) {
            groupPrefsList.addPreference(group);
        }
    }
}
Also used : NotificationChannelGroup(android.app.NotificationChannelGroup) PreferenceCategory(androidx.preference.PreferenceCategory) ArrayList(java.util.ArrayList)

Example 65 with NotificationChannelGroup

use of android.app.NotificationChannelGroup in project android_packages_apps_Settings by omnirom.

the class ChannelListPreferenceControllerTest method testUpdateFullList_incrementalUpdates.

@Test
@UiThreadTest
public void testUpdateFullList_incrementalUpdates() {
    // Start by testing the case with no groups or channels
    List<NotificationChannelGroup> inGroups = new ArrayList<>();
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        assertEquals("zeroCategories", mGroupList.getPreference(0).getKey());
    }
    // Test that adding a group clears the zero category and adds everything
    NotificationChannelGroup inGroup1 = new NotificationChannelGroup("group1", "Group 1");
    inGroup1.addChannel(new NotificationChannel("ch1a", "Channel 1A", IMPORTANCE_DEFAULT));
    inGroups.add(inGroup1);
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        PreferenceGroup group1 = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group1", group1.getKey());
        assertEquals(2, group1.getPreferenceCount());
        assertNull(group1.getPreference(0).getKey());
        assertEquals("All \"Group 1\" notifications", group1.getPreference(0).getTitle());
        assertEquals("ch1a", group1.getPreference(1).getKey());
        assertEquals("Channel 1A", group1.getPreference(1).getTitle());
    }
    // Test that adding a channel works -- no dupes or omissions
    inGroup1.addChannel(new NotificationChannel("ch1b", "Channel 1B", IMPORTANCE_DEFAULT));
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        PreferenceGroup group1 = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group1", group1.getKey());
        assertEquals(3, group1.getPreferenceCount());
        assertNull(group1.getPreference(0).getKey());
        assertEquals("All \"Group 1\" notifications", group1.getPreference(0).getTitle());
        assertEquals("ch1a", group1.getPreference(1).getKey());
        assertEquals("Channel 1A", group1.getPreference(1).getTitle());
        assertEquals("ch1b", group1.getPreference(2).getKey());
        assertEquals("Channel 1B", group1.getPreference(2).getTitle());
    }
    // Test that renaming a channel does in fact rename the preferences
    inGroup1.getChannels().get(1).setName("Channel 1B - Renamed");
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        PreferenceGroup group1 = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group1", group1.getKey());
        assertEquals(3, group1.getPreferenceCount());
        assertNull(group1.getPreference(0).getKey());
        assertEquals("All \"Group 1\" notifications", group1.getPreference(0).getTitle());
        assertEquals("ch1a", group1.getPreference(1).getKey());
        assertEquals("Channel 1A", group1.getPreference(1).getTitle());
        assertEquals("ch1b", group1.getPreference(2).getKey());
        assertEquals("Channel 1B - Renamed", group1.getPreference(2).getTitle());
    }
    // Test that adding a group works and results in the correct sorting.
    NotificationChannelGroup inGroup0 = new NotificationChannelGroup("group0", "Group 0");
    inGroup0.addChannel(new NotificationChannel("ch0b", "Channel 0B", IMPORTANCE_DEFAULT));
    // NOTE: updateFullList takes a List which has been sorted, so we insert at 0 for this check
    inGroups.add(0, inGroup0);
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(2, mGroupList.getPreferenceCount());
        PreferenceGroup group0 = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group0", group0.getKey());
        assertEquals(2, group0.getPreferenceCount());
        assertNull(group0.getPreference(0).getKey());
        assertEquals("All \"Group 0\" notifications", group0.getPreference(0).getTitle());
        assertEquals("ch0b", group0.getPreference(1).getKey());
        assertEquals("Channel 0B", group0.getPreference(1).getTitle());
        PreferenceGroup group1 = (PreferenceGroup) mGroupList.getPreference(1);
        assertEquals("group1", group1.getKey());
        assertEquals(3, group1.getPreferenceCount());
        assertNull(group1.getPreference(0).getKey());
        assertEquals("All \"Group 1\" notifications", group1.getPreference(0).getTitle());
        assertEquals("ch1a", group1.getPreference(1).getKey());
        assertEquals("Channel 1A", group1.getPreference(1).getTitle());
        assertEquals("ch1b", group1.getPreference(2).getKey());
        assertEquals("Channel 1B - Renamed", group1.getPreference(2).getTitle());
    }
    // Test that adding a channel that comes before another works and has correct ordering.
    // NOTE: the channels within a group are sorted inside updateFullList.
    inGroup0.addChannel(new NotificationChannel("ch0a", "Channel 0A", IMPORTANCE_DEFAULT));
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(2, mGroupList.getPreferenceCount());
        PreferenceGroup group0 = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group0", group0.getKey());
        assertEquals(3, group0.getPreferenceCount());
        assertNull(group0.getPreference(0).getKey());
        assertEquals("All \"Group 0\" notifications", group0.getPreference(0).getTitle());
        assertEquals("ch0a", group0.getPreference(1).getKey());
        assertEquals("Channel 0A", group0.getPreference(1).getTitle());
        assertEquals("ch0b", group0.getPreference(2).getKey());
        assertEquals("Channel 0B", group0.getPreference(2).getTitle());
        PreferenceGroup group1 = (PreferenceGroup) mGroupList.getPreference(1);
        assertEquals("group1", group1.getKey());
        assertEquals(3, group1.getPreferenceCount());
        assertNull(group1.getPreference(0).getKey());
        assertEquals("All \"Group 1\" notifications", group1.getPreference(0).getTitle());
        assertEquals("ch1a", group1.getPreference(1).getKey());
        assertEquals("Channel 1A", group1.getPreference(1).getTitle());
        assertEquals("ch1b", group1.getPreference(2).getKey());
        assertEquals("Channel 1B - Renamed", group1.getPreference(2).getTitle());
    }
    // Test that the "Other" group works.
    // Also test a simultaneous addition and deletion.
    inGroups.remove(inGroup0);
    NotificationChannelGroup inGroupOther = new NotificationChannelGroup(null, null);
    inGroupOther.addChannel(new NotificationChannel("chXa", "Other A", IMPORTANCE_DEFAULT));
    inGroupOther.addChannel(new NotificationChannel("chXb", "Other B", IMPORTANCE_DEFAULT));
    inGroups.add(inGroupOther);
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(2, mGroupList.getPreferenceCount());
        PreferenceGroup group1 = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group1", group1.getKey());
        assertEquals(3, group1.getPreferenceCount());
        assertNull(group1.getPreference(0).getKey());
        assertEquals("All \"Group 1\" notifications", group1.getPreference(0).getTitle());
        assertEquals("ch1a", group1.getPreference(1).getKey());
        assertEquals("Channel 1A", group1.getPreference(1).getTitle());
        assertEquals("ch1b", group1.getPreference(2).getKey());
        assertEquals("Channel 1B - Renamed", group1.getPreference(2).getTitle());
        PreferenceGroup groupOther = (PreferenceGroup) mGroupList.getPreference(1);
        assertEquals("categories", groupOther.getKey());
        assertEquals(2, groupOther.getPreferenceCount());
        assertEquals("chXa", groupOther.getPreference(0).getKey());
        assertEquals("Other A", groupOther.getPreference(0).getTitle());
        assertEquals("chXb", groupOther.getPreference(1).getKey());
        assertEquals("Other B", groupOther.getPreference(1).getTitle());
    }
    // Test that the removal of a channel works.
    inGroupOther.getChannels().remove(0);
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(2, mGroupList.getPreferenceCount());
        PreferenceGroup group1 = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group1", group1.getKey());
        assertEquals(3, group1.getPreferenceCount());
        assertNull(group1.getPreference(0).getKey());
        assertEquals("All \"Group 1\" notifications", group1.getPreference(0).getTitle());
        assertEquals("ch1a", group1.getPreference(1).getKey());
        assertEquals("Channel 1A", group1.getPreference(1).getTitle());
        assertEquals("ch1b", group1.getPreference(2).getKey());
        assertEquals("Channel 1B - Renamed", group1.getPreference(2).getTitle());
        PreferenceGroup groupOther = (PreferenceGroup) mGroupList.getPreference(1);
        assertEquals("categories", groupOther.getKey());
        assertEquals(1, groupOther.getPreferenceCount());
        assertEquals("chXb", groupOther.getPreference(0).getKey());
        assertEquals("Other B", groupOther.getPreference(0).getTitle());
    }
    // Test that we go back to the empty state when clearing all groups and channels.
    inGroups.clear();
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        assertEquals("zeroCategories", mGroupList.getPreference(0).getKey());
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) ArrayList(java.util.ArrayList) PreferenceGroup(androidx.preference.PreferenceGroup) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

NotificationChannelGroup (android.app.NotificationChannelGroup)80 NotificationChannel (android.app.NotificationChannel)54 Test (org.junit.Test)53 NotificationBackend (com.android.settings.notification.NotificationBackend)21 ArrayList (java.util.ArrayList)12 ConversationChannel (android.app.people.ConversationChannel)10 ShortcutInfo (android.content.pm.ShortcutInfo)10 PreferenceCategory (androidx.preference.PreferenceCategory)9 TargetApi (android.annotation.TargetApi)8 Preference (android.support.v7.preference.Preference)6 PreferenceCategory (android.support.v7.preference.PreferenceCategory)6 Preference (androidx.preference.Preference)6 LayoutPreference (com.android.settings.applications.LayoutPreference)6 MasterSwitchPreference (com.android.settings.widget.MasterSwitchPreference)6 RestrictedSwitchPreference (com.android.settingslib.RestrictedSwitchPreference)6 FooterPreference (com.android.settingslib.widget.FooterPreference)6 NotificationManager (android.app.NotificationManager)5 Notification (android.app.Notification)4 AudioAttributes (android.media.AudioAttributes)4 PreferenceGroup (androidx.preference.PreferenceGroup)3