diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-09-13 19:54:13 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-09-13 19:54:13 +0200 |
commit | 5a53f19406f23dd7000eca156ee486e92e94f072 (patch) | |
tree | e2be847f68ceb034e9ccd70d30b0035f0f0b31cc /src/main/java | |
parent | a3d8450d9d551c59277614fd50cad1d7b910d649 (diff) |
avoid duplicate notifcation sounds
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/de/pixart/messenger/services/NotificationService.java | 57 |
1 files changed, 42 insertions, 15 deletions
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<String> conversations; + synchronized (this.mBacklogMessageCounter) { + conversations = getBacklogConversations(account); + count = getBacklogMessageCount(account); + } + updateNotification(count > 0, conversations); } } } + private List<String> getBacklogConversations(Account account) { + final List<String> conversations = new ArrayList<>(); + for (Iterator<Map.Entry<Conversation, AtomicInteger>> it = mBacklogMessageCounter.entrySet().iterator(); it.hasNext(); ) { + Map.Entry<Conversation, AtomicInteger> 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<Map.Entry<Conversation, AtomicInteger>> it = mBacklogMessageCounter.entrySet().iterator(); it.hasNext(); ) { - Map.Entry<Conversation, AtomicInteger> entry = it.next(); - if (entry.getKey().getAccount() == account) { - count += entry.getValue().get(); - it.remove(); - } + for (Iterator<Map.Entry<Conversation, AtomicInteger>> it = mBacklogMessageCounter.entrySet().iterator(); it.hasNext(); ) { + Map.Entry<Conversation, AtomicInteger> 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<String> conversations) { + updateNotification(notify, conversations, false); } - private void updateNotification(final boolean notify, boolean summaryOnly) { + private void updateNotification(final boolean notify, final List<String> 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<String, ArrayList<Message>> 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()); |