From 5a53f19406f23dd7000eca156ee486e92e94f072 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Thu, 13 Sep 2018 19:54:13 +0200 Subject: avoid duplicate notifcation sounds --- .../messenger/services/NotificationService.java | 57 ++++++++++++++++------ 1 file changed, 42 insertions(+), 15 deletions(-) (limited to 'src/main/java/de/pixart/messenger/services/NotificationService.java') diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index e37dc14f4..8f82207ac 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -35,6 +35,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; @@ -237,20 +238,34 @@ public class NotificationService { if (account == null || !notify) { updateNotification(notify); } else { - updateNotification(getBacklogMessageCount(account) > 0); + final int count; + final List conversations; + synchronized (this.mBacklogMessageCounter) { + conversations = getBacklogConversations(account); + count = getBacklogMessageCount(account); + } + updateNotification(count > 0, conversations); } } } + private List getBacklogConversations(Account account) { + final List conversations = new ArrayList<>(); + for (Iterator> it = mBacklogMessageCounter.entrySet().iterator(); it.hasNext(); ) { + Map.Entry entry = it.next(); + if (entry.getKey().getAccount() == account) { + conversations.add(entry.getKey().getUuid()); + } + } + return conversations; + } private int getBacklogMessageCount(Account account) { int count = 0; - synchronized (this.mBacklogMessageCounter) { - for (Iterator> it = mBacklogMessageCounter.entrySet().iterator(); it.hasNext(); ) { - Map.Entry entry = it.next(); - if (entry.getKey().getAccount() == account) { - count += entry.getValue().get(); - it.remove(); - } + for (Iterator> it = mBacklogMessageCounter.entrySet().iterator(); it.hasNext(); ) { + Map.Entry entry = it.next(); + if (entry.getKey().getAccount() == account) { + count += entry.getValue().get(); + it.remove(); } } Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": backlog message count=" + count); @@ -300,11 +315,12 @@ public class NotificationService { } synchronized (notifications) { pushToStack(message); - final Account account = message.getConversation().getAccount(); + final Conversational conversation = message.getConversation(); + final Account account = conversation.getAccount(); final boolean doNotify = (!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn) && !account.inGracePeriod() && !this.inMiniGracePeriod(account); - updateNotification(doNotify); + updateNotification(doNotify, Collections.singletonList(conversation.getUuid())); } } @@ -326,7 +342,7 @@ public class NotificationService { markAsReadIfHasDirectReply(conversation); if (notifications.remove(conversation.getUuid()) != null) { cancel(conversation.getUuid(), NOTIFICATION_ID); - updateNotification(false, true); + updateNotification(false, null, true); } } } @@ -351,11 +367,16 @@ public class NotificationService { } public void updateNotification(final boolean notify) { - updateNotification(notify, false); + updateNotification(notify, null, false); + } + + public void updateNotification(final boolean notify, final List conversations) { + updateNotification(notify, conversations, false); } - private void updateNotification(final boolean notify, boolean summaryOnly) { + private void updateNotification(final boolean notify, final List conversations, final boolean summaryOnly) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService); + 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); } else { @@ -369,11 +390,17 @@ public class NotificationService { notify(NOTIFICATION_ID, mBuilder.build()); } else { mBuilder = buildMultipleConversation(notify); - mBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN); + if (notifyOnlyOneChild) { + mBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN); + } modifyForSoundVibrationAndLight(mBuilder, notify, preferences); if (!summaryOnly) { for (Map.Entry> entry : notifications.entrySet()) { - Builder singleBuilder = buildSingleConversations(entry.getValue(), notify); + String uuid = entry.getKey(); + Builder singleBuilder = buildSingleConversations(entry.getValue(), notifyOnlyOneChild ? conversations.contains(uuid) : notify); + if (!notifyOnlyOneChild) { + singleBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY); + } singleBuilder.setGroup(CONVERSATIONS_GROUP); setNotificationColor(singleBuilder); notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build()); -- cgit v1.2.3