notifiy only when necessary

This commit is contained in:
Daniel Gultsch 2014-09-29 18:28:13 +02:00
parent 87010e6094
commit 511b7a53f4
7 changed files with 68 additions and 44 deletions

View File

@ -417,7 +417,7 @@ public class MessageParser extends AbstractParser implements
lastCarbonMessageReceived = SystemClock lastCarbonMessageReceived = SystemClock
.elapsedRealtime(); .elapsedRealtime();
notify = false; notify = false;
message.getConversation().markRead(); mXmppConnectionService.markRead(message.getConversation());
} else { } else {
message.markUnread(); message.markUnread();
} }
@ -474,7 +474,7 @@ public class MessageParser extends AbstractParser implements
} }
notify = notify && !conversation.isMuted(); notify = notify && !conversation.isMuted();
if (notify) { if (notify) {
mXmppConnectionService.pushNotification(message); mXmppConnectionService.getNotificationService().push(message);
} }
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
} }

View File

@ -2,7 +2,6 @@ package eu.siacs.conversations.services;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -16,7 +15,9 @@ import android.net.Uri;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder; import android.support.v4.app.TaskStackBuilder;
import android.text.Html; import android.text.Html;
import android.util.Log;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
@ -30,6 +31,8 @@ public class NotificationService {
private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>(); private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>();
public int NOTIFICATION_ID = 0x2342; public int NOTIFICATION_ID = 0x2342;
private Conversation mOpenConversation;
private boolean mIsInForeground;
public NotificationService(XmppConnectionService service) { public NotificationService(XmppConnectionService service) {
this.mXmppConnectionService = service; this.mXmppConnectionService = service;
@ -38,6 +41,13 @@ public class NotificationService {
} }
public synchronized void push(Message message) { 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(); String conversationUuid = message.getConversationUuid();
if (notifications.containsKey(conversationUuid)) { if (notifications.containsKey(conversationUuid)) {
notifications.get(conversationUuid).add(message); notifications.get(conversationUuid).add(message);
@ -46,7 +56,7 @@ public class NotificationService {
mList.add(message); mList.add(message);
notifications.put(conversationUuid, mList); notifications.put(conversationUuid, mList);
} }
updateNotification(true); updateNotification(!(this.mIsInForeground && this.mOpenConversation == null));
} }
public void clear() { public void clear() {
@ -93,8 +103,10 @@ public class NotificationService {
.bigText(text.toString())); .bigText(text.toString()));
mBuilder.setContentText(messages.get(0).getReadableBody( mBuilder.setContentText(messages.get(0).getReadableBody(
mXmppConnectionService)); mXmppConnectionService));
if (notify) {
mBuilder.setTicker(messages.get(messages.size() - 1) mBuilder.setTicker(messages.get(messages.size() - 1)
.getReadableBody(mXmppConnectionService)); .getReadableBody(mXmppConnectionService));
}
mBuilder.setContentIntent(createContentIntent(conversation mBuilder.setContentIntent(createContentIntent(conversation
.getUuid())); .getUuid()));
} else { } else {
@ -137,11 +149,11 @@ public class NotificationService {
long[] pattern = { 0, 3 * dat, dat, dat }; long[] pattern = { 0, 3 * dat, dat, dat };
mBuilder.setVibrate(pattern); mBuilder.setVibrate(pattern);
} }
mBuilder.setLights(0xffffffff, 2000, 4000);
if (ringtone != null) { if (ringtone != null) {
mBuilder.setSound(Uri.parse(ringtone)); mBuilder.setSound(Uri.parse(ringtone));
} }
} }
mBuilder.setLights(0xffffffff, 2000, 4000);
Notification notification = mBuilder.build(); Notification notification = mBuilder.build();
mNotificationManager.notify(NOTIFICATION_ID, notification); mNotificationManager.notify(NOTIFICATION_ID, notification);
} }
@ -183,4 +195,12 @@ public class NotificationService {
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
} }
public void setOpenConversation(Conversation conversation) {
this.mOpenConversation = conversation;
}
public void setIsInForeground(boolean foreground) {
this.mIsInForeground = foreground;
}
} }

View File

@ -971,6 +971,7 @@ public class XmppConnectionService extends Service {
switchToForeground(); switchToForeground();
} }
this.mOnConversationUpdate = listener; this.mOnConversationUpdate = listener;
this.mNotificationService.setIsInForeground(true);
this.convChangedListenerCount++; this.convChangedListenerCount++;
} }
@ -978,6 +979,7 @@ public class XmppConnectionService extends Service {
this.convChangedListenerCount--; this.convChangedListenerCount--;
if (this.convChangedListenerCount == 0) { if (this.convChangedListenerCount == 0) {
this.mOnConversationUpdate = null; this.mOnConversationUpdate = null;
this.mNotificationService.setIsInForeground(false);
if (checkListeners()) { if (checkListeners()) {
switchToBackground(); switchToBackground();
} }
@ -1754,7 +1756,7 @@ public class XmppConnectionService extends Service {
return contacts; return contacts;
} }
public void pushNotification(Message message) { public NotificationService getNotificationService() {
this.mNotificationService.push(message); return this.mNotificationService;
} }
} }

View File

@ -12,7 +12,6 @@ import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdat
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.ui.adapter.ConversationAdapter; import eu.siacs.conversations.ui.adapter.ConversationAdapter;
import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.utils.UIHelper;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.SystemClock; import android.os.SystemClock;
@ -144,6 +143,9 @@ public class ConversationActivity extends XmppActivity implements
} }
invalidateOptionsMenu(); invalidateOptionsMenu();
hideKeyboard(); hideKeyboard();
if (xmppConnectionServiceBound) {
xmppConnectionService.getNotificationService().setOpenConversation(null);
}
} }
@Override @Override
@ -151,19 +153,7 @@ public class ConversationActivity extends XmppActivity implements
paneShouldBeOpen = false; paneShouldBeOpen = false;
if ((conversationList.size() > 0) if ((conversationList.size() > 0)
&& (getSelectedConversation() != null)) { && (getSelectedConversation() != null)) {
ActionBar ab = getActionBar(); openConversation(getSelectedConversation());
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 (!getSelectedConversation().isRead()) { if (!getSelectedConversation().isRead()) {
xmppConnectionService xmppConnectionService
.markRead(getSelectedConversation()); .markRead(getSelectedConversation());
@ -180,6 +170,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 @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.conversations, menu); getMenuInflater().inflate(R.menu.conversations, menu);
@ -603,6 +612,7 @@ public class ConversationActivity extends XmppActivity implements
xmppConnectionService.removeOnConversationListChangedListener(); xmppConnectionService.removeOnConversationListChangedListener();
xmppConnectionService.removeOnAccountListChangedListener(); xmppConnectionService.removeOnAccountListChangedListener();
xmppConnectionService.removeOnRosterUpdateListener(); xmppConnectionService.removeOnRosterUpdateListener();
xmppConnectionService.getNotificationService().setOpenConversation(null);
} }
super.onStop(); super.onStop();
} }

View File

@ -379,16 +379,7 @@ public class ConversationFragment extends Fragment {
if (activity.getSlidingPaneLayout().isSlideable()) { if (activity.getSlidingPaneLayout().isSlideable()) {
if (!activity.shouldPaneBeOpen()) { if (!activity.shouldPaneBeOpen()) {
activity.getSlidingPaneLayout().closePane(); activity.getSlidingPaneLayout().closePane();
activity.getActionBar().setDisplayHomeAsUpEnabled(true); activity.openConversation(conversation);
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();
} }
} }
if (this.conversation.getMode() == Conversation.MODE_MULTI) { if (this.conversation.getMode() == Conversation.MODE_MULTI) {

View File

@ -89,7 +89,7 @@ public class JingleConnection implements Downloadable {
if (acceptedAutomatically) { if (acceptedAutomatically) {
message.markUnread(); message.markUnread();
JingleConnection.this.mXmppConnectionService JingleConnection.this.mXmppConnectionService
.pushNotification(message); .getNotificationService().push(message);
} }
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
@ -319,7 +319,8 @@ public class JingleConnection implements Downloadable {
+ " allowed size:" + " allowed size:"
+ this.mJingleConnectionManager + this.mJingleConnectionManager
.getAutoAcceptFileSize()); .getAutoAcceptFileSize());
this.mXmppConnectionService.pushNotification(message); this.mXmppConnectionService.getNotificationService()
.push(message);
} }
this.file = this.mXmppConnectionService.getFileBackend() this.file = this.mXmppConnectionService.getFileBackend()
.getJingleFile(message, false); .getJingleFile(message, false);