Merge pull request #2564 from yushijinhun/fix-decryption-blocked

Daniel Gultsch created

Give up PGP decryption when intent is cancelled

Change summary

src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java | 21 
src/main/java/eu/siacs/conversations/ui/ConversationFragment.java     |  5 
2 files changed, 26 insertions(+)

Detailed changes

src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java 🔗

@@ -69,6 +69,27 @@ public class PgpDecryptionService {
         this.pendingNotifications.remove(message);
     }
 
+    public void giveUpCurrentDecryption(){
+        Message message;
+        synchronized (this) {
+            if(currentMessage != null) {
+                return;
+            }
+            message = messages.peekFirst();
+            if (message == null) {
+                return;
+            }
+            discard(message);
+        }
+        synchronized (message){
+            if (message.getEncryption() == Message.ENCRYPTION_PGP) {
+                message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
+            }
+        }
+        mXmppConnectionService.updateMessage(message);
+        continueDecryption(true);
+    }
+
 	protected synchronized void decryptNext() {
 		if (pendingIntent == null
                 && getOpenPgpApi() != null

src/main/java/eu/siacs/conversations/ui/ConversationFragment.java 🔗

@@ -1658,6 +1658,11 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
 				int choice = data.getIntExtra("choice", ConversationActivity.ATTACHMENT_CHOICE_INVALID);
 				activity.selectPresenceToAttachFile(choice, conversation.getNextEncryption());
 			}
+		} else if (resultCode == Activity.RESULT_CANCELED) {
+			if (requestCode == ConversationActivity.REQUEST_DECRYPT_PGP) {
+				// discard the message to prevent decryption being blocked
+				conversation.getAccount().getPgpDecryptionService().giveUpCurrentDecryption();
+			}
 		}
 	}