From 00b7b748780f83490335896877dd0300f5e1ce48 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 29 Jan 2015 19:27:48 +0100 Subject: [PATCH] pass pendingIntent, and some refactoring --- .../k9/mailstore/LocalMessageExtractor.java | 30 ++++----- .../k9/mailstore/OpenPgpResultBodyPart.java | 11 ++++ .../ui/messageview/MessageViewFragment.java | 66 ++++++++++--------- 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java index ad17bcc2f..9001fad6b 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java @@ -42,7 +42,7 @@ public class LocalMessageExtractor { private static final int FILENAME_PREFIX_LENGTH = FILENAME_PREFIX.length(); private static final String FILENAME_SUFFIX = " "; private static final int FILENAME_SUFFIX_LENGTH = FILENAME_SUFFIX.length(); - private static final OpenPgpSignatureResult NO_SIGNATURE_RESULT = null; + private static final OpenPgpResultBodyPart NO_SIGNATURE_RESULT = null; private LocalMessageExtractor() {} /** @@ -437,24 +437,24 @@ public class LocalMessageExtractor { attachments); List attachmentInfos = extractAttachmentInfos(attachments); - // TODO fill from part - OpenPgpSignatureResult pgpResult = getSignatureResultForPart(part); - OpenPgpError pgpError = null; - boolean wasEncrypted = getPartWasEncrypted(part); - PendingIntent pendingIntent = null; + OpenPgpResultBodyPart resultBodyPart = getSignatureResultForPart(part); + if (resultBodyPart != NO_SIGNATURE_RESULT) { + OpenPgpSignatureResult pgpResult = resultBodyPart.getSignatureResult(); + OpenPgpError pgpError = null; + boolean wasEncrypted = resultBodyPart.wasEncrypted(); + PendingIntent pendingIntent = resultBodyPart.getPendingIntent(); - containers.add(new MessageViewContainer( - viewable.html, attachmentInfos, pgpResult, pgpError, wasEncrypted, pendingIntent)); + containers.add(new MessageViewContainer( + viewable.html, attachmentInfos, pgpResult, pgpError, wasEncrypted, pendingIntent)); + } else { + containers.add(new MessageViewContainer(viewable.html, attachmentInfos)); + } } return new MessageViewInfo(containers, message); } - private static boolean getPartWasEncrypted(Part part) { - return (part instanceof OpenPgpResultBodyPart) && ((OpenPgpResultBodyPart) part).wasEncrypted(); - } - public static List getCryptPieces(Part part) throws MessagingException { ArrayList parts = new ArrayList(); if (!getCryptSubPieces(part, parts)) { @@ -496,16 +496,16 @@ public class LocalMessageExtractor { && ((Multipart) part.getBody()).getCount() == 3; } - private static OpenPgpSignatureResult getSignatureResultForPart(Part part) { + private static OpenPgpResultBodyPart getSignatureResultForPart(Part part) { if (part instanceof OpenPgpResultBodyPart) { OpenPgpResultBodyPart openPgpResultBodyPart = (OpenPgpResultBodyPart) part; - return openPgpResultBodyPart.getSignatureResult(); + return openPgpResultBodyPart; } if (MessageDecryptVerifyer.isPgpMimeSignedPart(part)) { Multipart multi = (Multipart) part.getBody(); if (multi.getCount() == 3 && multi.getBodyPart(2) instanceof OpenPgpResultBodyPart) { OpenPgpResultBodyPart openPgpResultBodyPart = (OpenPgpResultBodyPart) multi.getBodyPart(2); - return openPgpResultBodyPart.getSignatureResult(); + return openPgpResultBodyPart; } } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/OpenPgpResultBodyPart.java b/k9mail/src/main/java/com/fsck/k9/mailstore/OpenPgpResultBodyPart.java index 555966f90..47e2f10fe 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/OpenPgpResultBodyPart.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/OpenPgpResultBodyPart.java @@ -1,6 +1,8 @@ package com.fsck.k9.mailstore; +import android.app.PendingIntent; + import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.internet.MimeBodyPart; import org.openintents.openpgp.OpenPgpError; @@ -11,6 +13,7 @@ public class OpenPgpResultBodyPart extends MimeBodyPart { private boolean wasEncrypted; private OpenPgpSignatureResult signatureResult; private OpenPgpError error; + private PendingIntent pendingIntent; public OpenPgpResultBodyPart(boolean wasEncrypted) throws MessagingException { this.wasEncrypted = wasEncrypted; @@ -20,10 +23,18 @@ public class OpenPgpResultBodyPart extends MimeBodyPart { return signatureResult; } + public PendingIntent getPendingIntent() { + return pendingIntent; + } + public void setSignatureResult(OpenPgpSignatureResult signatureResult) { this.signatureResult = signatureResult; } + public void setPendingIntent(PendingIntent pendingIntent) { + this.pendingIntent = pendingIntent; + } + public OpenPgpError getError() { return error; } diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java index 1145de324..30fee2533 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java @@ -340,38 +340,11 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF intent.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, accountName); try { - - PipedInputStream pipedInputStream; - PipedOutputStream decryptedOutputStream; - final CountDownLatch latch; - if (MessageDecryptVerifyer.isPgpMimeSignedPart(currentlyDecrypringOrVerifyingPart)) { - pipedInputStream = getPipedInputStreamForSignedData(); - - byte[] signatureData = MessageDecryptVerifyer.getSignatureData(currentlyDecrypringOrVerifyingPart); - intent.putExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE, signatureData); - decryptedOutputStream = null; - latch = null; + callAsyncDetachedVerify(intent); } else { - pipedInputStream = getPipedInputStreamForEncryptedData(); - latch = new CountDownLatch(1); - decryptedOutputStream = getPipedOutputStreamForDecryptedData(latch); + callAsyncDecrypt(intent); } - - openPgpApi.executeApiAsync(intent, pipedInputStream, decryptedOutputStream, new IOpenPgpCallback() { - @Override - public void onReturn(Intent result) { - currentCryptoResult = result; - - if (latch != null) { - Log.d(K9.LOG_TAG, "on result!"); - latch.countDown(); - return; - } - - onCryptoConverge(null); - } - }); } catch (IOException e) { Log.e(K9.LOG_TAG, "IOException", e); } catch (MessagingException e) { @@ -379,6 +352,35 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF } } + private void callAsyncDecrypt(Intent intent) throws IOException { + final CountDownLatch latch = new CountDownLatch(1); + PipedInputStream pipedInputStream = getPipedInputStreamForEncryptedData(); + PipedOutputStream decryptedOutputStream = getPipedOutputStreamForDecryptedData(latch); + + openPgpApi.executeApiAsync(intent, pipedInputStream, decryptedOutputStream, new IOpenPgpCallback() { + @Override + public void onReturn(Intent result) { + currentCryptoResult = result; + latch.countDown(); + } + }); + } + + private void callAsyncDetachedVerify(Intent intent) throws IOException, MessagingException { + PipedInputStream pipedInputStream = getPipedInputStreamForSignedData(); + + byte[] signatureData = MessageDecryptVerifyer.getSignatureData(currentlyDecrypringOrVerifyingPart); + intent.putExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE, signatureData); + + openPgpApi.executeApiAsync(intent, pipedInputStream, null, new IOpenPgpCallback() { + @Override + public void onReturn(Intent result) { + currentCryptoResult = result; + onCryptoConverge(null); + } + }); + } + private PipedInputStream getPipedInputStreamForSignedData() throws IOException { PipedInputStream pipedInputStream = new PipedInputStream(); @@ -498,6 +500,10 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE); openPgpResultBodyPart.setSignatureResult(signatureResult); + PendingIntent pendingIntent = + currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_INTENT); + openPgpResultBodyPart.setPendingIntent(pendingIntent); + onCryptoSuccess(openPgpResultBodyPart); break; } @@ -514,7 +520,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF if (resultCode == Activity.RESULT_OK) { decryptOrVerifyNextPartOrStartExtractingTextAndAttachments(); } else { - //FIXME: don't pass null + // FIXME: don't pass null onCryptoFailed(null); } }