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

View File

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

View File

@ -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();
}
@ -1754,7 +1756,7 @@ public class XmppConnectionService extends Service {
return contacts;
}
public void pushNotification(Message message) {
this.mNotificationService.push(message);
public NotificationService getNotificationService() {
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.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());
@ -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
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.conversations, menu);
@ -603,6 +612,7 @@ public class ConversationActivity extends XmppActivity implements
xmppConnectionService.removeOnConversationListChangedListener();
xmppConnectionService.removeOnAccountListChangedListener();
xmppConnectionService.removeOnRosterUpdateListener();
xmppConnectionService.getNotificationService().setOpenConversation(null);
}
super.onStop();
}

View File

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

View File

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