From 0435f9278047dae73f1502e30c9978ce91ef9e96 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 15 Jun 2016 12:44:29 +0200 Subject: delay notification until after pgp decryption --- .../conversations/crypto/PgpDecryptionService.java | 37 +++++++++++++++++----- .../siacs/conversations/entities/Conversation.java | 2 +- .../eu/siacs/conversations/entities/Message.java | 2 +- .../conversations/http/HttpDownloadConnection.java | 5 +-- .../siacs/conversations/parser/MessageParser.java | 14 ++++---- .../conversations/ui/ConversationFragment.java | 2 +- .../xmpp/jingle/JingleConnection.java | 11 +++++-- 7 files changed, 51 insertions(+), 22 deletions(-) (limited to 'src/main/java/eu') diff --git a/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java b/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java index 13985cca7..45dfd7d92 100644 --- a/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java +++ b/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java @@ -14,6 +14,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.ArrayDeque; +import java.util.HashSet; import java.util.List; import java.util.Collections; import java.util.LinkedList; @@ -35,6 +36,7 @@ public class PgpDecryptionService { private OpenPgpApi openPgpApi = null; protected final ArrayDeque messages = new ArrayDeque(); + protected final HashSet pendingNotifications = new HashSet<>(); Message currentMessage; private PendingIntent pendingIntent; @@ -43,9 +45,16 @@ public class PgpDecryptionService { this.mXmppConnectionService = service; } - public synchronized void decrypt(final Message message) { + public synchronized boolean decrypt(final Message message, boolean notify) { messages.add(message); - continueDecryption(); + if (notify && pendingIntent == null) { + pendingNotifications.add(message); + continueDecryption(); + return false; + } else { + continueDecryption(); + return notify; + } } public synchronized void decrypt(final List list) { @@ -59,6 +68,7 @@ public class PgpDecryptionService { public synchronized void discard(List discards) { this.messages.removeAll(discards); + this.pendingNotifications.removeAll(discards); } protected synchronized void decryptNext() { @@ -120,9 +130,11 @@ public class PgpDecryptionService { mXmppConnectionService.updateMessage(message); break; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: - messages.addFirst(message); - currentMessage = null; - storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + synchronized (PgpDecryptionService.this) { + messages.addFirst(message); + currentMessage = null; + storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + } break; case OpenPgpApi.RESULT_CODE_ERROR: message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED); @@ -148,9 +160,11 @@ public class PgpDecryptionService { mXmppConnectionService.updateMessage(message); break; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: - messages.addFirst(message); - currentMessage = null; - storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + synchronized (PgpDecryptionService.this) { + messages.addFirst(message); + currentMessage = null; + storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT)); + } break; case OpenPgpApi.RESULT_CODE_ERROR: message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED); @@ -162,6 +176,13 @@ public class PgpDecryptionService { mXmppConnectionService.updateMessage(message); } } + notifyIfPending(message); + } + + private synchronized void notifyIfPending(Message message) { + if (pendingNotifications.remove(message)) { + mXmppConnectionService.getNotificationService().push(message); + } } private void storePendingIntent(PendingIntent pendingIntent) { diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index 8cff7b959..c3294f774 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -788,7 +788,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl public boolean hasDuplicateMessage(Message message) { synchronized (this.messages) { for (int i = this.messages.size() - 1; i >= 0; --i) { - if (this.messages.get(i).equals(message)) { + if (this.messages.get(i).similar(message)) { return true; } } diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index 3c871dc79..f38875e6e 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -376,7 +376,7 @@ public class Message extends AbstractEntity { this.transferable = transferable; } - public boolean equals(Message message) { + public boolean similar(Message message) { if (this.serverMsgId != null && message.getServerMsgId() != null) { return this.serverMsgId.equals(message.getServerMsgId()); } else if (this.body == null || this.counterpart == null) { diff --git a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java index 92cc2c664..c46f2b977 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java @@ -132,11 +132,12 @@ public class HttpDownloadConnection implements Transferable { mXmppConnectionService.getFileBackend().updateMediaScanner(file); message.setTransferable(null); mHttpConnectionManager.finishConnection(this); + boolean notify = acceptedAutomatically && !message.isRead(); if (message.getEncryption() == Message.ENCRYPTION_PGP) { - message.getConversation().getAccount().getPgpDecryptionService().decrypt(message); + notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify); } mXmppConnectionService.updateConversationUi(); - if (acceptedAutomatically) { + if (notify) { mXmppConnectionService.getNotificationService().push(message); } } diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index 86f96c370..1dd8026ba 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -342,6 +342,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece final Jid to = packet.getTo(); final Jid from = packet.getFrom(); final String remoteMsgId = packet.getId(); + boolean notify = false; if (from == null) { Log.d(Config.LOGTAG,"no from in: "+packet.toString()); @@ -482,7 +483,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece sendMessageReceipts(account, packet); } if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) { - conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage); + conversation.getAccount().getPgpDecryptionService().decrypt(replacedMessage, false); } return; } else { @@ -505,10 +506,6 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece conversation.add(message); } - if (message.getEncryption() == Message.ENCRYPTION_PGP) { - conversation.getAccount().getPgpDecryptionService().decrypt(message); - } - if (query == null || query.getWith() == null) { //either no mam or catchup if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) { mXmppConnectionService.markRead(conversation); @@ -517,9 +514,14 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece } } else { message.markUnread(); + notify = true; } } + if (message.getEncryption() == Message.ENCRYPTION_PGP) { + notify = conversation.getAccount().getPgpDecryptionService().decrypt(message, notify); + } + if (query == null) { mXmppConnectionService.updateConversationUi(); } @@ -541,7 +543,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager(); if (message.trusted() && message.treatAsDownloadable() != Message.Decision.NEVER && manager.getAutoAcceptFileSize() > 0) { manager.createNewDownloadConnection(message); - } else if (!message.isRead()) { + } else if (notify) { if (query == null) { mXmppConnectionService.getNotificationService().push(message); } else if (query.getWith() == null) { // mam catchup diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index c97576db4..f0d13ea96 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -706,7 +706,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa message.setEncryption(Message.ENCRYPTION_PGP); activity.updateConversationList(); updateMessages(); - conversation.getAccount().getPgpDecryptionService().decrypt(message); + conversation.getAccount().getPgpDecryptionService().decrypt(message, false); } protected void privateMessageWith(final Jid counterpart) { diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index 2d5b6433c..a2ddaa4ae 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -102,9 +102,16 @@ public class JingleConnection implements Transferable { mXmppConnectionService.markMessage(message,Message.STATUS_RECEIVED); if (acceptedAutomatically) { message.markUnread(); - JingleConnection.this.mXmppConnectionService.getNotificationService().push(message); + if (message.getEncryption() == Message.ENCRYPTION_PGP) { + account.getPgpDecryptionService().decrypt(message, true); + } else { + JingleConnection.this.mXmppConnectionService.getNotificationService().push(message); + } } } else { + if (message.getEncryption() == Message.ENCRYPTION_PGP) { + account.getPgpDecryptionService().decrypt(message, false); + } if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { file.delete(); } @@ -112,8 +119,6 @@ public class JingleConnection implements Transferable { Log.d(Config.LOGTAG,"successfully transmitted file:" + file.getAbsolutePath()+" ("+file.getSha1Sum()+")"); if (message.getEncryption() != Message.ENCRYPTION_PGP) { mXmppConnectionService.getFileBackend().updateMediaScanner(file); - } else { - account.getPgpDecryptionService().decrypt(message); } } -- cgit v1.2.3