From 903c834f972d18586b8cd7506e1249afa5ad0d98 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Thu, 23 Mar 2017 23:43:23 +0100 Subject: count messages in backlog to not renotify on prior notifications --- .../messenger/services/NotificationService.java | 40 ++++++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index a571c1816..accd2e933 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -22,9 +22,12 @@ import android.util.Log; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -52,6 +55,8 @@ public class NotificationService { private boolean mIsInForeground; private long mLastNotification; + private final HashMap mBacklogMessageCounter = new HashMap<>(); + public NotificationService(final XmppConnectionService service) { this.mXmppConnectionService = service; } @@ -102,11 +107,21 @@ public class NotificationService { public void pushFromBacklog(final Message message) { if (notify(message)) { synchronized (notifications) { + getBacklogMessageCounter(message.getConversation()).incrementAndGet(); pushToStack(message); } } } + private AtomicInteger getBacklogMessageCounter(Conversation conversation) { + synchronized (mBacklogMessageCounter) { + if (!mBacklogMessageCounter.containsKey(conversation)) { + mBacklogMessageCounter.put(conversation, new AtomicInteger(0)); + } + return mBacklogMessageCounter.get(conversation); + } + } + public void pushFromDirectReply(final Message message) { synchronized (notifications) { pushToStack(message); @@ -120,16 +135,24 @@ public class NotificationService { if (account == null || !notify) { updateNotification(notify); } else { - boolean hasPendingMessages = false; - for (ArrayList messages : notifications.values()) { - if (messages.size() > 0 && messages.get(0).getConversation().getAccount() == account) { - hasPendingMessages = true; - break; - } + updateNotification(getBacklogMessageCount(account) > 0); + } + } + } + + 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(); } - updateNotification(hasPendingMessages); } } + Log.d(Config.LOGTAG,account.getJid().toBareJid()+": backlog message count="+count); + return count; } public void finishBacklog(boolean notify) { @@ -184,6 +207,9 @@ public class NotificationService { } public void clear(final Conversation conversation) { + synchronized (this.mBacklogMessageCounter) { + this.mBacklogMessageCounter.remove(conversation); + } synchronized (notifications) { markAsReadIfHasDirectReply(conversation); notifications.remove(conversation.getUuid()); -- cgit v1.2.3