step two towards nicer notifications. mark read if carbon arrives

This commit is contained in:
Daniel Gultsch 2014-03-08 05:59:31 +01:00
parent 02883a7ac4
commit 9a5a429888
2 changed files with 18 additions and 17 deletions

View file

@ -101,33 +101,37 @@ public class XmppConnectionService extends Service {
public void onMessagePacketReceived(Account account, public void onMessagePacketReceived(Account account,
MessagePacket packet) { MessagePacket packet) {
Message message = null; Message message = null;
boolean notify = false; boolean notify = true;
if ((packet.getType() == MessagePacket.TYPE_CHAT)) { if ((packet.getType() == MessagePacket.TYPE_CHAT)) {
String pgpBody = MessageParser.getPgpBody(packet); String pgpBody = MessageParser.getPgpBody(packet);
if (pgpBody != null) { if (pgpBody != null) {
message = MessageParser.parsePgpChat(pgpBody, packet, message = MessageParser.parsePgpChat(pgpBody, packet,
account, service); account, service);
notify = false; message.markUnread();
} else if (packet.hasChild("body") } else if (packet.hasChild("body")
&& (packet.getBody().startsWith("?OTR"))) { && (packet.getBody().startsWith("?OTR"))) {
message = MessageParser.parseOtrChat(packet, account, message = MessageParser.parseOtrChat(packet, account,
service); service);
notify = true; message.markUnread();
} else if (packet.hasChild("body")) { } else if (packet.hasChild("body")) {
message = MessageParser.parsePlainTextChat(packet, account, message = MessageParser.parsePlainTextChat(packet, account,
service); service);
notify = true; message.markUnread();
} else if (packet.hasChild("received") } else if (packet.hasChild("received")
|| (packet.hasChild("sent"))) { || (packet.hasChild("sent"))) {
message = MessageParser.parseCarbonMessage(packet, account, message = MessageParser.parseCarbonMessage(packet, account,
service); service);
message.getConversation().markRead();
notify = false;
} }
} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) { } else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
message = MessageParser message = MessageParser
.parseGroupchat(packet, account, service); .parseGroupchat(packet, account, service);
if (message != null) { if (message != null) {
notify = (message.getStatus() == Message.STATUS_RECIEVED); if (message.getStatus() == Message.STATUS_RECIEVED) {
message.markUnread();
}
} }
} else if (packet.getType() == MessagePacket.TYPE_ERROR) { } else if (packet.getType() == MessagePacket.TYPE_ERROR) {
message = MessageParser.parseError(packet, account, service); message = MessageParser.parseError(packet, account, service);
@ -149,9 +153,6 @@ public class XmppConnectionService extends Service {
Log.d(LOGTAG, "error trying to parse date" + e.getMessage()); Log.d(LOGTAG, "error trying to parse date" + e.getMessage());
} }
} }
if (notify) {
message.markUnread();
}
Conversation conversation = message.getConversation(); Conversation conversation = message.getConversation();
conversation.getMessages().add(message); conversation.getMessages().add(message);
if (packet.getType() != MessagePacket.TYPE_ERROR) { if (packet.getType() != MessagePacket.TYPE_ERROR) {
@ -160,12 +161,10 @@ public class XmppConnectionService extends Service {
if (convChangedListener != null) { if (convChangedListener != null) {
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();
} else { } else {
if (notify) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2342, UIHelper mNotificationManager.notify(2342, UIHelper
.getUnreadMessageNotification( .getNotification(
getApplicationContext(),getConversations())); getApplicationContext(),getConversations(),notify));
}
} }
} }
}; };

View file

@ -107,8 +107,8 @@ public class UIHelper {
return bitmap; return bitmap;
} }
public static Notification getUnreadMessageNotification(Context context, public static Notification getNotification(Context context,
List<Conversation> conversations) { List<Conversation> conversations, boolean notify) {
String targetUuid = ""; String targetUuid = "";
List<Conversation> unread = new ArrayList<Conversation>(); List<Conversation> unread = new ArrayList<Conversation>();
@ -169,9 +169,11 @@ public class UIHelper {
mBuilder.setStyle(style); mBuilder.setStyle(style);
} }
mBuilder.setSmallIcon(R.drawable.notification); mBuilder.setSmallIcon(R.drawable.notification);
mBuilder.setLights(0xffffffff, 2000, 4000); if (notify) {
if (ringtone != null) { mBuilder.setLights(0xffffffff, 2000, 4000);
mBuilder.setSound(Uri.parse(ringtone)); if (ringtone != null) {
mBuilder.setSound(Uri.parse(ringtone));
}
} }
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);