create notification channel for quite hours. fixes #3212

This commit is contained in:
Daniel Gultsch 2018-10-02 20:25:03 +02:00
parent d7f058c5b8
commit 98d3d91a8e
1 changed files with 33 additions and 17 deletions

View File

@ -67,6 +67,8 @@ public class NotificationService {
public static final Object CATCHUP_LOCK = new Object(); public static final Object CATCHUP_LOCK = new Object();
private static final int LED_COLOR = 0xff00ff00;
private static final String CONVERSATIONS_GROUP = "eu.siacs.conversations"; private static final String CONVERSATIONS_GROUP = "eu.siacs.conversations";
private static final int NOTIFICATION_ID_MULTIPLIER = 1024 * 1024; private static final int NOTIFICATION_ID_MULTIPLIER = 1024 * 1024;
private static final int NOTIFICATION_ID = 2 * NOTIFICATION_ID_MULTIPLIER; private static final int NOTIFICATION_ID = 2 * NOTIFICATION_ID_MULTIPLIER;
@ -146,7 +148,7 @@ public class NotificationService {
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build()); .build());
messagesChannel.setLightColor(0xff00ff00); messagesChannel.setLightColor(LED_COLOR);
final int dat = 70; final int dat = 70;
final long[] pattern = {0, 3 * dat, dat, dat}; final long[] pattern = {0, 3 * dat, dat, dat};
messagesChannel.setVibrationPattern(pattern); messagesChannel.setVibrationPattern(pattern);
@ -159,10 +161,22 @@ public class NotificationService {
NotificationManager.IMPORTANCE_LOW); NotificationManager.IMPORTANCE_LOW);
silentMessagesChannel.setDescription(c.getString(R.string.silent_messages_channel_description)); silentMessagesChannel.setDescription(c.getString(R.string.silent_messages_channel_description));
silentMessagesChannel.setShowBadge(true); silentMessagesChannel.setShowBadge(true);
silentMessagesChannel.setLightColor(0xff00ff00); silentMessagesChannel.setLightColor(LED_COLOR);
silentMessagesChannel.enableLights(true); silentMessagesChannel.enableLights(true);
silentMessagesChannel.setGroup("chats"); silentMessagesChannel.setGroup("chats");
notificationManager.createNotificationChannel(silentMessagesChannel); notificationManager.createNotificationChannel(silentMessagesChannel);
final NotificationChannel quietHoursChannel = new NotificationChannel("quiet_hours",
c.getString(R.string.title_pref_quiet_hours),
NotificationManager.IMPORTANCE_LOW);
quietHoursChannel.setShowBadge(true);
quietHoursChannel.setLightColor(LED_COLOR);
quietHoursChannel.enableLights(true);
quietHoursChannel.setGroup("chats");
quietHoursChannel.enableVibration(false);
quietHoursChannel.setSound(null, null);
notificationManager.createNotificationChannel(quietHoursChannel);
} }
public boolean notify(final Message message) { public boolean notify(final Message message) {
@ -170,8 +184,7 @@ public class NotificationService {
return message.getStatus() == Message.STATUS_RECEIVED return message.getStatus() == Message.STATUS_RECEIVED
&& !conversation.isMuted() && !conversation.isMuted()
&& (conversation.alwaysNotify() || wasHighlightedOrPrivate(message)) && (conversation.alwaysNotify() || wasHighlightedOrPrivate(message))
&& (!conversation.isWithStranger() || notificationsFromStrangers()) && (!conversation.isWithStranger() || notificationsFromStrangers());
;
} }
private boolean notificationsFromStrangers() { private boolean notificationsFromStrangers() {
@ -362,8 +375,11 @@ public class NotificationService {
private void updateNotification(final boolean notify, final List<String> conversations, final boolean summaryOnly) { private void updateNotification(final boolean notify, final List<String> conversations, final boolean summaryOnly) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
final boolean quiteHours = isQuietHours();
final boolean notifyOnlyOneChild = notify && conversations != null && conversations.size() == 1; //if this check is changed to > 0 catchup messages will create one notification per conversation final boolean notifyOnlyOneChild = notify && conversations != null && conversations.size() == 1; //if this check is changed to > 0 catchup messages will create one notification per conversation
if (notifications.size() == 0) { if (notifications.size() == 0) {
cancel(NOTIFICATION_ID); cancel(NOTIFICATION_ID);
} else { } else {
@ -372,24 +388,24 @@ public class NotificationService {
} }
final Builder mBuilder; final Builder mBuilder;
if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mBuilder = buildSingleConversations(notifications.values().iterator().next(), notify); mBuilder = buildSingleConversations(notifications.values().iterator().next(), notify, quiteHours);
modifyForSoundVibrationAndLight(mBuilder, notify, preferences); modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences);
notify(NOTIFICATION_ID, mBuilder.build()); notify(NOTIFICATION_ID, mBuilder.build());
} else { } else {
mBuilder = buildMultipleConversation(notify); mBuilder = buildMultipleConversation(notify, quiteHours);
if (notifyOnlyOneChild) { if (notifyOnlyOneChild) {
mBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN); mBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
} }
modifyForSoundVibrationAndLight(mBuilder, notify, preferences); modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences);
if (!summaryOnly) { if (!summaryOnly) {
for (Map.Entry<String, ArrayList<Message>> entry : notifications.entrySet()) { for (Map.Entry<String, ArrayList<Message>> entry : notifications.entrySet()) {
String uuid = entry.getKey(); String uuid = entry.getKey();
final boolean notifyThis = notifyOnlyOneChild ? conversations.contains(uuid) : notify; final boolean notifyThis = notifyOnlyOneChild ? conversations.contains(uuid) : notify;
Builder singleBuilder = buildSingleConversations(entry.getValue(),notifyThis); Builder singleBuilder = buildSingleConversations(entry.getValue(),notifyThis, quiteHours);
if (!notifyOnlyOneChild) { if (!notifyOnlyOneChild) {
singleBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY); singleBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
} }
modifyForSoundVibrationAndLight(singleBuilder, notifyThis, preferences); modifyForSoundVibrationAndLight(singleBuilder, notifyThis, quiteHours, preferences);
singleBuilder.setGroup(CONVERSATIONS_GROUP); singleBuilder.setGroup(CONVERSATIONS_GROUP);
setNotificationColor(singleBuilder); setNotificationColor(singleBuilder);
notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build()); notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build());
@ -400,13 +416,13 @@ public class NotificationService {
} }
} }
private void modifyForSoundVibrationAndLight(Builder mBuilder, boolean notify, SharedPreferences preferences) { private void modifyForSoundVibrationAndLight(Builder mBuilder, boolean notify, boolean quietHours, SharedPreferences preferences) {
final Resources resources = mXmppConnectionService.getResources(); final Resources resources = mXmppConnectionService.getResources();
final String ringtone = preferences.getString("notification_ringtone", resources.getString(R.string.notification_ringtone)); final String ringtone = preferences.getString("notification_ringtone", resources.getString(R.string.notification_ringtone));
final boolean vibrate = preferences.getBoolean("vibrate_on_notification", resources.getBoolean(R.bool.vibrate_on_notification)); final boolean vibrate = preferences.getBoolean("vibrate_on_notification", resources.getBoolean(R.bool.vibrate_on_notification));
final boolean led = preferences.getBoolean("led", resources.getBoolean(R.bool.led)); final boolean led = preferences.getBoolean("led", resources.getBoolean(R.bool.led));
final boolean headsup = preferences.getBoolean("notification_headsup", resources.getBoolean(R.bool.headsup_notifications)); final boolean headsup = preferences.getBoolean("notification_headsup", resources.getBoolean(R.bool.headsup_notifications));
if (notify && !isQuietHours()) { if (notify && !quietHours) {
if (vibrate) { if (vibrate) {
final int dat = 70; final int dat = 70;
final long[] pattern = {0, 3 * dat, dat, dat}; final long[] pattern = {0, 3 * dat, dat, dat};
@ -428,7 +444,7 @@ public class NotificationService {
setNotificationColor(mBuilder); setNotificationColor(mBuilder);
mBuilder.setDefaults(0); mBuilder.setDefaults(0);
if (led) { if (led) {
mBuilder.setLights(0xff00FF00, 2000, 3000); mBuilder.setLights(LED_COLOR, 2000, 3000);
} }
} }
@ -440,8 +456,8 @@ public class NotificationService {
} }
} }
private Builder buildMultipleConversation(final boolean notify) { private Builder buildMultipleConversation(final boolean notify, final boolean quietHours) {
final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, notify ? "messages" : "silent_messages"); final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages"));
final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle(notifications.size() style.setBigContentTitle(notifications.size()
+ " " + " "
@ -487,8 +503,8 @@ public class NotificationService {
return mBuilder; return mBuilder;
} }
private Builder buildSingleConversations(final ArrayList<Message> messages, final boolean notify) { private Builder buildSingleConversations(final ArrayList<Message> messages, final boolean notify, final boolean quietHours) {
final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, notify ? "messages" : "silent_messages"); final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages"));
if (messages.size() >= 1) { if (messages.size() >= 1) {
final Conversation conversation = (Conversation) messages.get(0).getConversation(); final Conversation conversation = (Conversation) messages.get(0).getConversation();
final UnreadConversation.Builder mUnreadBuilder = new UnreadConversation.Builder(conversation.getName().toString()); final UnreadConversation.Builder mUnreadBuilder = new UnreadConversation.Builder(conversation.getName().toString());