count messages in backlog to not renotify on prior notifications

This commit is contained in:
Daniel Gultsch 2017-03-23 15:34:52 +01:00
parent 010b341251
commit 00e8ba00c4

View file

@ -23,9 +23,12 @@ import android.util.Log;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -57,6 +60,8 @@ public class NotificationService {
private boolean mIsInForeground; private boolean mIsInForeground;
private long mLastNotification; private long mLastNotification;
private final HashMap<Conversation,AtomicInteger> mBacklogMessageCounter = new HashMap<>();
public NotificationService(final XmppConnectionService service) { public NotificationService(final XmppConnectionService service) {
this.mXmppConnectionService = service; this.mXmppConnectionService = service;
} }
@ -97,11 +102,21 @@ public class NotificationService {
public void pushFromBacklog(final Message message) { public void pushFromBacklog(final Message message) {
if (notify(message)) { if (notify(message)) {
synchronized (notifications) { synchronized (notifications) {
getBacklogMessageCounter(message.getConversation()).incrementAndGet();
pushToStack(message); 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) { public void pushFromDirectReply(final Message message) {
synchronized (notifications) { synchronized (notifications) {
pushToStack(message); pushToStack(message);
@ -115,17 +130,25 @@ public class NotificationService {
if (account == null || !notify) { if (account == null || !notify) {
updateNotification(notify); updateNotification(notify);
} else { } else {
boolean hasPendingMessages = false; updateNotification(getBacklogMessageCount(account) > 0);
for(ArrayList<Message> messages : notifications.values()) {
if (messages.size() > 0 && messages.get(0).getConversation().getAccount() == account) {
hasPendingMessages = true;
break;
} }
} }
updateNotification(hasPendingMessages); }
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();
} }
} }
} }
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": backlog message count="+count);
return count;
}
public void finishBacklog(boolean notify) { public void finishBacklog(boolean notify) {
finishBacklog(notify,null); finishBacklog(notify,null);
@ -174,6 +197,9 @@ public class NotificationService {
} }
public void clear(final Conversation conversation) { public void clear(final Conversation conversation) {
synchronized (this.mBacklogMessageCounter) {
this.mBacklogMessageCounter.remove(conversation);
}
synchronized (notifications) { synchronized (notifications) {
markAsReadIfHasDirectReply(conversation); markAsReadIfHasDirectReply(conversation);
notifications.remove(conversation.getUuid()); notifications.remove(conversation.getUuid());