From 511b7a53f41f8254449e6c2c6eff795f728e7a33 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 29 Sep 2014 18:28:13 +0200 Subject: notifiy only when necessary --- .../services/NotificationService.java | 30 ++++++++++++++++++---- .../services/XmppConnectionService.java | 8 +++--- 2 files changed, 30 insertions(+), 8 deletions(-) (limited to 'src/eu/siacs/conversations/services') diff --git a/src/eu/siacs/conversations/services/NotificationService.java b/src/eu/siacs/conversations/services/NotificationService.java index 656e16f36..831ce51ff 100644 --- a/src/eu/siacs/conversations/services/NotificationService.java +++ b/src/eu/siacs/conversations/services/NotificationService.java @@ -2,7 +2,6 @@ package eu.siacs.conversations.services; import java.util.ArrayList; import java.util.LinkedHashMap; -import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -16,7 +15,9 @@ import android.net.Uri; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.text.Html; +import android.util.Log; +import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; @@ -30,6 +31,8 @@ public class NotificationService { private LinkedHashMap> notifications = new LinkedHashMap>(); public int NOTIFICATION_ID = 0x2342; + private Conversation mOpenConversation; + private boolean mIsInForeground; public NotificationService(XmppConnectionService service) { this.mXmppConnectionService = service; @@ -38,6 +41,13 @@ public class NotificationService { } public synchronized void push(Message message) { + if (this.mIsInForeground + && this.mOpenConversation == message.getConversation()) { + Log.d(Config.LOGTAG,"ignoring notification because foreground and conv matches"); + return; // simply ignore + } else { + Log.d(Config.LOGTAG,"pushed new notification"); + } String conversationUuid = message.getConversationUuid(); if (notifications.containsKey(conversationUuid)) { notifications.get(conversationUuid).add(message); @@ -46,7 +56,7 @@ public class NotificationService { mList.add(message); notifications.put(conversationUuid, mList); } - updateNotification(true); + updateNotification(!(this.mIsInForeground && this.mOpenConversation == null)); } public void clear() { @@ -93,8 +103,10 @@ public class NotificationService { .bigText(text.toString())); mBuilder.setContentText(messages.get(0).getReadableBody( mXmppConnectionService)); - mBuilder.setTicker(messages.get(messages.size() - 1) - .getReadableBody(mXmppConnectionService)); + if (notify) { + mBuilder.setTicker(messages.get(messages.size() - 1) + .getReadableBody(mXmppConnectionService)); + } mBuilder.setContentIntent(createContentIntent(conversation .getUuid())); } else { @@ -137,11 +149,11 @@ public class NotificationService { long[] pattern = { 0, 3 * dat, dat, dat }; mBuilder.setVibrate(pattern); } - mBuilder.setLights(0xffffffff, 2000, 4000); if (ringtone != null) { mBuilder.setSound(Uri.parse(ringtone)); } } + mBuilder.setLights(0xffffffff, 2000, 4000); Notification notification = mBuilder.build(); mNotificationManager.notify(NOTIFICATION_ID, notification); } @@ -183,4 +195,12 @@ public class NotificationService { Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); } + public void setOpenConversation(Conversation conversation) { + this.mOpenConversation = conversation; + } + + public void setIsInForeground(boolean foreground) { + this.mIsInForeground = foreground; + } + } diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 2ca11286f..b83be7fbd 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -971,6 +971,7 @@ public class XmppConnectionService extends Service { switchToForeground(); } this.mOnConversationUpdate = listener; + this.mNotificationService.setIsInForeground(true); this.convChangedListenerCount++; } @@ -978,6 +979,7 @@ public class XmppConnectionService extends Service { this.convChangedListenerCount--; if (this.convChangedListenerCount == 0) { this.mOnConversationUpdate = null; + this.mNotificationService.setIsInForeground(false); if (checkListeners()) { switchToBackground(); } @@ -1753,8 +1755,8 @@ public class XmppConnectionService extends Service { } return contacts; } - - public void pushNotification(Message message) { - this.mNotificationService.push(message); + + public NotificationService getNotificationService() { + return this.mNotificationService; } } -- cgit v1.2.3