From 511b7a53f41f8254449e6c2c6eff795f728e7a33 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 29 Sep 2014 18:28:13 +0200 Subject: [PATCH] notifiy only when necessary --- res/xml/preferences.xml | 16 ++++---- .../conversations/parser/MessageParser.java | 4 +- .../services/NotificationService.java | 30 ++++++++++++--- .../services/XmppConnectionService.java | 8 ++-- .../ui/ConversationActivity.java | 38 ++++++++++++------- .../ui/ConversationFragment.java | 11 +----- .../xmpp/jingle/JingleConnection.java | 5 ++- 7 files changed, 68 insertions(+), 44 deletions(-) diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 5af789c0d..eccc8baea 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -89,13 +89,13 @@ android:summary="@string/pref_dont_save_encrypted_summary" android:title="@string/pref_dont_save_encrypted" /> - - - + + + - + \ No newline at end of file diff --git a/src/eu/siacs/conversations/parser/MessageParser.java b/src/eu/siacs/conversations/parser/MessageParser.java index d1964b8c9..af0c96e9a 100644 --- a/src/eu/siacs/conversations/parser/MessageParser.java +++ b/src/eu/siacs/conversations/parser/MessageParser.java @@ -417,7 +417,7 @@ public class MessageParser extends AbstractParser implements lastCarbonMessageReceived = SystemClock .elapsedRealtime(); notify = false; - message.getConversation().markRead(); + mXmppConnectionService.markRead(message.getConversation()); } else { message.markUnread(); } @@ -474,7 +474,7 @@ public class MessageParser extends AbstractParser implements } notify = notify && !conversation.isMuted(); if (notify) { - mXmppConnectionService.pushNotification(message); + mXmppConnectionService.getNotificationService().push(message); } mXmppConnectionService.updateConversationUi(); } 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; } } diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index 3accafe84..03cf753d3 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -12,7 +12,6 @@ import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdat import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate; import eu.siacs.conversations.ui.adapter.ConversationAdapter; import eu.siacs.conversations.utils.ExceptionHelper; -import eu.siacs.conversations.utils.UIHelper; import android.net.Uri; import android.os.Bundle; import android.os.SystemClock; @@ -144,6 +143,9 @@ public class ConversationActivity extends XmppActivity implements } invalidateOptionsMenu(); hideKeyboard(); + if (xmppConnectionServiceBound) { + xmppConnectionService.getNotificationService().setOpenConversation(null); + } } @Override @@ -151,19 +153,7 @@ public class ConversationActivity extends XmppActivity implements paneShouldBeOpen = false; if ((conversationList.size() > 0) && (getSelectedConversation() != null)) { - ActionBar ab = getActionBar(); - if (ab != null) { - ab.setDisplayHomeAsUpEnabled(true); - ab.setHomeButtonEnabled(true); - if (getSelectedConversation().getMode() == Conversation.MODE_SINGLE - || activity.useSubjectToIdentifyConference()) { - ab.setTitle(getSelectedConversation().getName()); - } else { - ab.setTitle(getSelectedConversation() - .getContactJid().split("/")[0]); - } - } - invalidateOptionsMenu(); + openConversation(getSelectedConversation()); if (!getSelectedConversation().isRead()) { xmppConnectionService .markRead(getSelectedConversation()); @@ -179,6 +169,25 @@ public class ConversationActivity extends XmppActivity implements } }); } + + public void openConversation(Conversation conversation) { + ActionBar ab = getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + ab.setHomeButtonEnabled(true); + if (getSelectedConversation().getMode() == Conversation.MODE_SINGLE + || activity.useSubjectToIdentifyConference()) { + ab.setTitle(getSelectedConversation().getName()); + } else { + ab.setTitle(getSelectedConversation() + .getContactJid().split("/")[0]); + } + } + invalidateOptionsMenu(); + if (xmppConnectionServiceBound) { + xmppConnectionService.getNotificationService().setOpenConversation(conversation); + } + } @Override public boolean onCreateOptionsMenu(Menu menu) { @@ -603,6 +612,7 @@ public class ConversationActivity extends XmppActivity implements xmppConnectionService.removeOnConversationListChangedListener(); xmppConnectionService.removeOnAccountListChangedListener(); xmppConnectionService.removeOnRosterUpdateListener(); + xmppConnectionService.getNotificationService().setOpenConversation(null); } super.onStop(); } diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index 7916560d5..064b00be4 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -379,16 +379,7 @@ public class ConversationFragment extends Fragment { if (activity.getSlidingPaneLayout().isSlideable()) { if (!activity.shouldPaneBeOpen()) { activity.getSlidingPaneLayout().closePane(); - activity.getActionBar().setDisplayHomeAsUpEnabled(true); - activity.getActionBar().setHomeButtonEnabled(true); - if (conversation.getMode() == Conversation.MODE_SINGLE - || activity.useSubjectToIdentifyConference()) { - activity.getActionBar().setTitle(conversation.getName()); - } else { - activity.getActionBar().setTitle( - conversation.getContactJid().split("/")[0]); - } - activity.invalidateOptionsMenu(); + activity.openConversation(conversation); } } if (this.conversation.getMode() == Conversation.MODE_MULTI) { diff --git a/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index 3a3d3582c..92fdbe0b6 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -89,7 +89,7 @@ public class JingleConnection implements Downloadable { if (acceptedAutomatically) { message.markUnread(); JingleConnection.this.mXmppConnectionService - .pushNotification(message); + .getNotificationService().push(message); } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; @@ -319,7 +319,8 @@ public class JingleConnection implements Downloadable { + " allowed size:" + this.mJingleConnectionManager .getAutoAcceptFileSize()); - this.mXmppConnectionService.pushNotification(message); + this.mXmppConnectionService.getNotificationService() + .push(message); } this.file = this.mXmppConnectionService.getFileBackend() .getJingleFile(message, false);