notifiy only when necessary

Daniel Gultsch created

Change summary

res/xml/preferences.xml                                        | 16 
src/eu/siacs/conversations/parser/MessageParser.java           |  4 
src/eu/siacs/conversations/services/NotificationService.java   | 30 ++
src/eu/siacs/conversations/services/XmppConnectionService.java |  8 
src/eu/siacs/conversations/ui/ConversationActivity.java        | 38 ++-
src/eu/siacs/conversations/ui/ConversationFragment.java        | 11 -
src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java   |  5 
7 files changed, 68 insertions(+), 44 deletions(-)

Detailed changes

res/xml/preferences.xml 🔗

@@ -89,13 +89,13 @@
                     android:summary="@string/pref_dont_save_encrypted_summary"
                     android:title="@string/pref_dont_save_encrypted" />
             </PreferenceCategory>
-                <PreferenceCategory android:title="@string/pref_expert_options_other" >
-                    <CheckBoxPreference
-                        android:defaultValue="false"
-                        android:key="indicate_received"
-                        android:summary="@string/pref_use_indicate_received_summary"
-                        android:title="@string/pref_use_indicate_received" />
-                </PreferenceCategory>
+            <PreferenceCategory android:title="@string/pref_expert_options_other" >
+                <CheckBoxPreference
+                    android:defaultValue="false"
+                    android:key="indicate_received"
+                    android:summary="@string/pref_use_indicate_received_summary"
+                    android:title="@string/pref_use_indicate_received" />
+            </PreferenceCategory>
         </PreferenceScreen>
 
         <CheckBoxPreference
@@ -105,4 +105,4 @@
             android:title="@string/pref_never_send_crash" />
     </PreferenceCategory>
 
-</PreferenceScreen>
+</PreferenceScreen>

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();
 	}

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<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>();
 
 	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;
+	}
+
 }

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;
 	}
 }

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();
 	}

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) {

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);