diff options
5 files changed, 86 insertions, 35 deletions
diff --git a/src/eu/siacs/conversations/parser/MessageParser.java b/src/eu/siacs/conversations/parser/MessageParser.java index c8718edb..e09dd0c5 100644 --- a/src/eu/siacs/conversations/parser/MessageParser.java +++ b/src/eu/siacs/conversations/parser/MessageParser.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.parser; import android.os.SystemClock; +import android.util.Log; import net.java.otr4j.session.Session; import net.java.otr4j.session.SessionStatus; import eu.siacs.conversations.Config; @@ -202,10 +203,24 @@ public class MessageParser extends AbstractParser implements return null; } Element message = forwarded.findChild("message"); - if ((message == null) || (!message.hasChild("body"))) { + if (message == null) { + return null; + } + if (!message.hasChild("body")) { if (status == Message.STATUS_RECEIVED && message.getAttribute("from") != null) { parseNonMessage(message, account); + } else if (status == Message.STATUS_SEND + && message.hasChild("displayed", "urn:xmpp:chat-markers:0")) { + String to = message.getAttribute("to"); + if (to != null) { + Conversation conversation = mXmppConnectionService.find( + mXmppConnectionService.getConversations(), account, + to.split("/")[0]); + if (conversation != null) { + mXmppConnectionService.markRead(conversation, false); + } + } } return null; } @@ -419,7 +434,8 @@ public class MessageParser extends AbstractParser implements lastCarbonMessageReceived = SystemClock .elapsedRealtime(); notify = false; - mXmppConnectionService.markRead(message.getConversation()); + mXmppConnectionService.markRead( + message.getConversation(), false); } else { message.markUnread(); } @@ -436,7 +452,8 @@ public class MessageParser extends AbstractParser implements || NotificationService .wasHighlightedOrPrivate(message); } else { - mXmppConnectionService.markRead(message.getConversation()); + mXmppConnectionService.markRead(message.getConversation(), + false); lastCarbonMessageReceived = SystemClock.elapsedRealtime(); notify = false; } diff --git a/src/eu/siacs/conversations/services/NotificationService.java b/src/eu/siacs/conversations/services/NotificationService.java index 831ce51f..9428aa15 100644 --- a/src/eu/siacs/conversations/services/NotificationService.java +++ b/src/eu/siacs/conversations/services/NotificationService.java @@ -12,12 +12,11 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; +import android.os.PowerManager; 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; @@ -41,12 +40,14 @@ public class NotificationService { } public synchronized void push(Message message) { - if (this.mIsInForeground + + PowerManager pm = (PowerManager) mXmppConnectionService + .getSystemService(Context.POWER_SERVICE); + boolean isScreenOn = pm.isScreenOn(); + + if (this.mIsInForeground && isScreenOn && 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)) { @@ -56,7 +57,8 @@ public class NotificationService { mList.add(message); notifications.put(conversationUuid, mList); } - updateNotification(!(this.mIsInForeground && this.mOpenConversation == null)); + updateNotification(!(this.mIsInForeground && this.mOpenConversation == null) + || !isScreenOn); } public void clear() { @@ -120,10 +122,11 @@ public class NotificationService { + mXmppConnectionService .getString(R.string.unread_conversations)); StringBuilder names = new StringBuilder(); + Conversation conversation = null; for (ArrayList<Message> messages : notifications.values()) { if (messages.size() > 0) { - String name = messages.get(0).getConversation() - .getName(); + conversation = messages.get(0).getConversation(); + String name = conversation.getName(); style.addLine(Html.fromHtml("<b>" + name + "</b> " @@ -142,6 +145,10 @@ public class NotificationService { .getString(R.string.unread_conversations)); mBuilder.setContentText(names.toString()); mBuilder.setStyle(style); + if (conversation != null) { + mBuilder.setContentIntent(createContentIntent(conversation + .getUuid())); + } } if (notify) { if (vibrate) { @@ -153,6 +160,7 @@ public class NotificationService { mBuilder.setSound(Uri.parse(ringtone)); } } + mBuilder.setDeleteIntent(createDeleteIntent()); mBuilder.setLights(0xffffffff, 2000, 4000); Notification notification = mBuilder.build(); mNotificationManager.notify(NOTIFICATION_ID, notification); @@ -178,9 +186,19 @@ public class NotificationService { return resultPendingIntent; } + private PendingIntent createDeleteIntent() { + Intent intent = new Intent(mXmppConnectionService, + XmppConnectionService.class); + intent.setAction("clear_notification"); + return PendingIntent.getService(mXmppConnectionService, 0, intent, 0); + } + public static boolean wasHighlightedOrPrivate(Message message) { String nick = message.getConversation().getMucOptions().getActualNick(); Pattern highlight = generateNickHighlightPattern(nick); + if (message.getBody() == null || nick == null) { + return false; + } Matcher m = highlight.matcher(message.getBody()); return (m.find() || message.getType() == Message.TYPE_PRIVATE); } diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index b83be7fb..557a625b 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -90,6 +90,7 @@ public class XmppConnectionService extends Service { public long startDate; private static String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; + public static String ACTION_CLEAR_NOTIFICATION = "clear_notification"; private MemorizingTrustManager mMemorizingTrustManager; @@ -318,14 +319,16 @@ public class XmppConnectionService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { - if ((intent != null) - && (ACTION_MERGE_PHONE_CONTACTS.equals(intent.getAction()))) { - mergePhoneContactsWithRoster(); - return START_STICKY; - } else if ((intent != null) - && (Intent.ACTION_SHUTDOWN.equals(intent.getAction()))) { - logoutAndSave(); - return START_NOT_STICKY; + if (intent != null && intent.getAction() != null) { + if (intent.getAction().equals(ACTION_MERGE_PHONE_CONTACTS)) { + mergePhoneContactsWithRoster(); + return START_STICKY; + } else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) { + logoutAndSave(); + return START_NOT_STICKY; + } else if (intent.getAction().equals(ACTION_CLEAR_NOTIFICATION)) { + mNotificationService.clear(); + } } this.wakeLock.acquire(); ConnectivityManager cm = (ConnectivityManager) getApplicationContext() @@ -1530,24 +1533,34 @@ public class XmppConnectionService extends Service { public boolean markMessage(Account account, String recipient, String uuid, int status) { - for (Conversation conversation : getConversations()) { - if (conversation.getContactJid().equals(recipient) - && conversation.getAccount().equals(account)) { - return markMessage(conversation, uuid, status); + if (uuid == null) { + return false; + } else { + for (Conversation conversation : getConversations()) { + if (conversation.getContactJid().equals(recipient) + && conversation.getAccount().equals(account)) { + return markMessage(conversation, uuid, status); + } } + return false; } - return false; } public boolean markMessage(Conversation conversation, String uuid, int status) { - for (Message message : conversation.getMessages()) { - if (message.getUuid().equals(uuid)) { - markMessage(message, status); - return true; + if (uuid == null) { + return false; + } else { + for (Message message : conversation.getMessages()) { + if (uuid.equals(message.getUuid()) + || (message.getStatus() >= Message.STATUS_SEND && uuid + .equals(message.getRemoteMsgId()))) { + markMessage(message, status); + return true; + } } + return false; } - return false; } public void markMessage(Message message, int status) { @@ -1618,16 +1631,19 @@ public class XmppConnectionService extends Service { return null; } - public void markRead(Conversation conversation) { + public void markRead(Conversation conversation, boolean calledByUi) { conversation.markRead(); mNotificationService.clear(conversation); String id = conversation.popLatestMarkableMessageId(); - if (confirmMessages() && id != null) { + if (confirmMessages() && id != null && calledByUi) { Account account = conversation.getAccount(); String to = conversation.getContactJid(); this.sendMessagePacket(conversation.getAccount(), mMessageGenerator.confirm(account, to, id)); } + if (!calledByUi) { + updateConversationUi(); + } } public void failWaitingOtrMessages(Conversation conversation) { @@ -1755,7 +1771,7 @@ public class XmppConnectionService extends Service { } return contacts; } - + 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 03cf753d..0c67beff 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -156,7 +156,7 @@ public class ConversationActivity extends XmppActivity implements openConversation(getSelectedConversation()); if (!getSelectedConversation().isRead()) { xmppConnectionService - .markRead(getSelectedConversation()); + .markRead(getSelectedConversation(),true); listView.invalidateViews(); } } diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index 064b00be..34c68e00 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -494,7 +494,7 @@ public class ConversationFragment extends Fragment { getActivity().invalidateOptionsMenu(); updateChatMsgHint(); if (!activity.shouldPaneBeOpen()) { - activity.xmppConnectionService.markRead(conversation); + activity.xmppConnectionService.markRead(conversation,true); activity.updateConversationList(); } this.updateSendButton(); |