diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-10-02 22:18:50 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-10-02 22:18:50 +0200 |
commit | aa0f16a772c4626b900b9f48c6532a245a80f8d4 (patch) | |
tree | 7681ce76e7ea982d3364db672b6a92d1df27e194 | |
parent | d130ca088c2aa62a2f46ffe36993af6e59a4ab97 (diff) |
create notification channel for quite hours
-rw-r--r-- | src/main/java/de/pixart/messenger/services/NotificationService.java | 59 |
1 files changed, 36 insertions, 23 deletions
diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index 57c667a82..32377a00a 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -64,18 +64,18 @@ import de.pixart.messenger.xmpp.XmppConnection; public class NotificationService { public static final Object CATCHUP_LOCK = new Object(); - - private static final String CONVERSATIONS_GROUP = "de.pixart.messenger"; - private static final int NOTIFICATION_ID_MULTIPLIER = 1024 * 1024; - public static final int NOTIFICATION_ID = 2 * NOTIFICATION_ID_MULTIPLIER; - public static final int FOREGROUND_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 4; - public static final int ERROR_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 6; public static final String MESSAGES_CHANNEL_ID = "messages"; public static final String FOREGROUND_CHANNEL_ID = "foreground"; public static final String BACKUP_CHANNEL_ID = "backup"; public static final String UPDATE_CHANNEL_ID = "appupdate"; public static final String VIDEOCOMPRESSION_CHANNEL_ID = "compression"; public static final String ERROR_CHANNEL_ID = "error"; + private static final String CONVERSATIONS_GROUP = "de.pixart.messenger"; + private static final int NOTIFICATION_ID_MULTIPLIER = 1024 * 1024; + public static final int NOTIFICATION_ID = 2 * NOTIFICATION_ID_MULTIPLIER; + public static final int FOREGROUND_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 4; + public static final int ERROR_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 6; + private static final int LED_COLOR = 0xff0080FF; private final XmppConnectionService mXmppConnectionService; private final LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<>(); @@ -159,7 +159,7 @@ public class NotificationService { .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) .build()); - messagesChannel.setLightColor(0xff0080FF); + messagesChannel.setLightColor(LED_COLOR); final int dat = 70; final long[] pattern = {0, 3 * dat, dat, dat}; messagesChannel.setVibrationPattern(pattern); @@ -173,10 +173,21 @@ public class NotificationService { NotificationManager.IMPORTANCE_LOW); silentMessagesChannel.setDescription(c.getString(R.string.silent_messages_channel_description)); silentMessagesChannel.setShowBadge(true); - silentMessagesChannel.setLightColor(0xff0080FF); + silentMessagesChannel.setLightColor(LED_COLOR); silentMessagesChannel.enableLights(true); silentMessagesChannel.setGroup("chats"); 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) { @@ -184,8 +195,7 @@ public class NotificationService { return message.getStatus() == Message.STATUS_RECEIVED && !conversation.isMuted() && (conversation.alwaysNotify() || wasHighlightedOrPrivate(message)) - && (!conversation.isWithStranger() || notificationsFromStrangers()) - ; + && (!conversation.isWithStranger() || notificationsFromStrangers()); } private boolean notificationsFromStrangers() { @@ -200,6 +210,7 @@ public class NotificationService { final long startTime = preferences.getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; final long endTime = preferences.getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; final long nowTime = Calendar.getInstance().getTimeInMillis() % Config.MILLISECONDS_IN_DAY; + if (endTime < startTime) { return nowTime > startTime || nowTime < endTime; } else { @@ -248,6 +259,7 @@ public class NotificationService { } } } + private List<String> getBacklogConversations(Account account) { final List<String> conversations = new ArrayList<>(); for (Iterator<Map.Entry<Conversation, AtomicInteger>> it = mBacklogMessageCounter.entrySet().iterator(); it.hasNext(); ) { @@ -376,6 +388,7 @@ public class NotificationService { private void updateNotification(final boolean notify, final List<String> conversations, final boolean summaryOnly) { 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 if (notifications.size() == 0) { cancel(NOTIFICATION_ID); @@ -385,24 +398,24 @@ public class NotificationService { } final Builder mBuilder; if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { - mBuilder = buildSingleConversations(notifications.values().iterator().next(), notify); - modifyForSoundVibrationAndLight(mBuilder, notify, preferences); + mBuilder = buildSingleConversations(notifications.values().iterator().next(), notify, quiteHours); + modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences); notify(NOTIFICATION_ID, mBuilder.build()); } else { - mBuilder = buildMultipleConversation(notify); + mBuilder = buildMultipleConversation(notify, quiteHours); if (notifyOnlyOneChild) { mBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN); } - modifyForSoundVibrationAndLight(mBuilder, notify, preferences); + modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences); if (!summaryOnly) { for (Map.Entry<String, ArrayList<Message>> entry : notifications.entrySet()) { String uuid = entry.getKey(); final boolean notifyThis = notifyOnlyOneChild ? conversations.contains(uuid) : notify; - Builder singleBuilder = buildSingleConversations(entry.getValue(), notifyThis); + Builder singleBuilder = buildSingleConversations(entry.getValue(), notifyThis, quiteHours); if (!notifyOnlyOneChild) { singleBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY); } - modifyForSoundVibrationAndLight(singleBuilder, notifyThis, preferences); + modifyForSoundVibrationAndLight(singleBuilder, notifyThis, quiteHours, preferences); singleBuilder.setGroup(CONVERSATIONS_GROUP); setNotificationColor(singleBuilder); notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build()); @@ -413,13 +426,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 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 led = preferences.getBoolean("led", resources.getBoolean(R.bool.led)); final boolean headsup = preferences.getBoolean("notification_headsup", resources.getBoolean(R.bool.headsup_notifications)); - if (notify && !isQuietHours()) { + if (notify && !quietHours) { if (vibrate) { final int dat = 70; final long[] pattern = {0, 3 * dat, dat, dat}; @@ -441,7 +454,7 @@ public class NotificationService { setNotificationColor(mBuilder); mBuilder.setDefaults(0); if (led) { - mBuilder.setLights(0xff0080FF, 2000, 3000); + mBuilder.setLights(LED_COLOR, 2000, 3000); } } @@ -453,8 +466,8 @@ public class NotificationService { } } - private Builder buildMultipleConversation(final boolean notify) { - final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, notify ? "messages" : "silent_messages"); + private Builder buildMultipleConversation(final boolean notify, final boolean quietHours) { + final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages")); final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); style.setBigContentTitle(notifications.size() + " " @@ -500,8 +513,8 @@ public class NotificationService { return mBuilder; } - private Builder buildSingleConversations(final ArrayList<Message> messages, final boolean notify) { - final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, notify ? "messages" : "silent_messages"); + private Builder buildSingleConversations(final ArrayList<Message> messages, final boolean notify, final boolean quietHours) { + final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages")); if (messages.size() >= 1) { final Conversation conversation = (Conversation) messages.get(0).getConversation(); final UnreadConversation.Builder mUnreadBuilder = new UnreadConversation.Builder(conversation.getName().toString()); |