Search in sources :

Example 6 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class PgpMessageBuilderTest method build__withCryptoProviderNotOk__shouldThrow.

@Test
public void build__withCryptoProviderNotOk__shouldThrow() throws MessagingException {
    cryptoStatusBuilder.setCryptoMode(CryptoMode.SIGN_ONLY);
    CryptoProviderState[] cryptoProviderStates = { CryptoProviderState.LOST_CONNECTION, CryptoProviderState.UNCONFIGURED, CryptoProviderState.UNINITIALIZED, CryptoProviderState.ERROR };
    for (CryptoProviderState state : cryptoProviderStates) {
        cryptoStatusBuilder.setCryptoProviderState(state);
        pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build());
        Callback mockCallback = mock(Callback.class);
        pgpMessageBuilder.buildAsync(mockCallback);
        verify(mockCallback).onMessageBuildException(any(MessagingException.class));
        verifyNoMoreInteractions(mockCallback);
    }
}
Also used : CryptoProviderState(com.fsck.k9.activity.compose.RecipientPresenter.CryptoProviderState) Callback(com.fsck.k9.message.MessageBuilder.Callback) MessagingException(com.fsck.k9.mail.MessagingException) Test(org.junit.Test)

Example 7 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageBuilderTest method build_withMessageAttachment_shouldAttachAsApplicationOctetStream.

@Test
public void build_withMessageAttachment_shouldAttachAsApplicationOctetStream() throws Exception {
    MessageBuilder messageBuilder = createSimpleMessageBuilder();
    Attachment attachment = createAttachmentWithContent("message/rfc822", "attach.txt", TEST_ATTACHMENT_TEXT);
    messageBuilder.setAttachments(Collections.singletonList(attachment));
    messageBuilder.buildAsync(callback);
    MimeMessage message = getMessageFromCallback();
    assertEquals(MESSAGE_HEADERS + MESSAGE_CONTENT_WITH_MESSAGE_ATTACH, getMessageContents(message));
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Attachment(com.fsck.k9.activity.misc.Attachment) Test(org.junit.Test)

Example 8 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class ImapFolder method fetchPart.

@Override
public void fetchPart(Message message, Part part, MessageRetrievalListener<Message> listener) throws MessagingException {
    checkOpen();
    String partId = part.getServerExtra();
    String fetch;
    if ("TEXT".equalsIgnoreCase(partId)) {
        int maximumAutoDownloadMessageSize = store.getStoreConfig().getMaximumAutoDownloadMessageSize();
        fetch = String.format(Locale.US, "BODY.PEEK[TEXT]<0.%d>", maximumAutoDownloadMessageSize);
    } else {
        fetch = String.format("BODY.PEEK[%s]", partId);
    }
    try {
        String command = String.format("UID FETCH %s (UID %s)", message.getUid(), fetch);
        connection.sendCommand(command, false);
        ImapResponse response;
        int messageNumber = 0;
        ImapResponseCallback callback = new FetchPartCallback(part);
        do {
            response = connection.readResponse(callback);
            if (response.getTag() == null && ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH")) {
                ImapList fetchList = (ImapList) response.getKeyedValue("FETCH");
                String uid = fetchList.getKeyedString("UID");
                if (!message.getUid().equals(uid)) {
                    if (K9MailLib.isDebug()) {
                        Log.d(LOG_TAG, "Did not ask for UID " + uid + " for " + getLogId());
                    }
                    handleUntaggedResponse(response);
                    continue;
                }
                if (listener != null) {
                    listener.messageStarted(uid, messageNumber++, 1);
                }
                ImapMessage imapMessage = (ImapMessage) message;
                Object literal = handleFetchResponse(imapMessage, fetchList);
                if (literal != null) {
                    if (literal instanceof Body) {
                        // Most of the work was done in FetchAttchmentCallback.foundLiteral()
                        MimeMessageHelper.setBody(part, (Body) literal);
                    } else if (literal instanceof String) {
                        String bodyString = (String) literal;
                        InputStream bodyStream = new ByteArrayInputStream(bodyString.getBytes());
                        String contentTransferEncoding = part.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0];
                        String contentType = part.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0];
                        MimeMessageHelper.setBody(part, MimeUtility.createBody(bodyStream, contentTransferEncoding, contentType));
                    } else {
                        // This shouldn't happen
                        throw new MessagingException("Got FETCH response with bogus parameters");
                    }
                }
                if (listener != null) {
                    listener.messageFinished(message, messageNumber, 1);
                }
            } else {
                handleUntaggedResponse(response);
            }
        } while (response.getTag() == null);
    } catch (IOException ioe) {
        throw ioExceptionHandler(connection, ioe);
    }
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) Body(com.fsck.k9.mail.Body)

Example 9 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageLoaderHelper method onLoadMessageFromDatabaseFinished.

@UiThread
private void onLoadMessageFromDatabaseFinished() {
    if (callback == null) {
        throw new IllegalStateException("unexpected call when callback is already detached");
    }
    callback.onMessageDataLoadFinished(localMessage);
    boolean downloadedCompletely = localMessage.isSet(Flag.X_DOWNLOADED_FULL);
    boolean downloadedPartially = localMessage.isSet(Flag.X_DOWNLOADED_PARTIAL);
    boolean messageIncomplete = !downloadedCompletely && !downloadedPartially;
    if (messageIncomplete) {
        startDownloadingMessageBody(false);
        return;
    }
    if (onlyLoadMetadata) {
        MessageViewInfo messageViewInfo = MessageViewInfo.createForMetadataOnly(localMessage, !downloadedCompletely);
        onDecodeMessageFinished(messageViewInfo);
        return;
    }
    String openPgpProvider = account.getOpenPgpProvider();
    if (openPgpProvider != null) {
        startOrResumeCryptoOperation(openPgpProvider);
        return;
    }
    startOrResumeDecodeMessage();
}
Also used : MessageViewInfo(com.fsck.k9.mailstore.MessageViewInfo) UiThread(androidx.annotation.UiThread)

Example 10 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class RealImapConnectionTest method createOAuth2TokenProvider.

private OAuth2TokenProvider createOAuth2TokenProvider() {
    return new OAuth2TokenProvider() {

        private int invalidationCount = 0;

        @Override
        public String getToken(String username, long timeoutMillis) throws AuthenticationFailedException {
            assertEquals(USERNAME, username);
            assertEquals(OAUTH2_TIMEOUT, timeoutMillis);
            switch(invalidationCount) {
                case 0:
                    {
                        return XOAUTH_TOKEN;
                    }
                case 1:
                    {
                        return XOAUTH_ANOTHER_TOKEN;
                    }
                default:
                    {
                        throw new AuthenticationFailedException("Ran out of auth tokens. invalidateToken() called too often?");
                    }
            }
        }

        @Override
        public void invalidateToken(String username) {
            assertEquals(USERNAME, username);
            invalidationCount++;
        }

        @Override
        public List<String> getAccounts() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void authorizeApi(String username, Activity activity, OAuth2TokenProviderAuthCallback callback) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) OAuth2TokenProvider(com.fsck.k9.mail.oauth.OAuth2TokenProvider) Activity(android.app.Activity) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ByteString(okio.ByteString)

Aggregations

Test (org.junit.Test)23 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)14 Callback (com.fsck.k9.message.MessageBuilder.Callback)14 MessagingException (com.fsck.k9.mail.MessagingException)11 RobolectricTest (com.fsck.k9.RobolectricTest)10 PendingIntent (android.app.PendingIntent)9 Intent (android.content.Intent)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 OutputStream (java.io.OutputStream)9 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)9 ComposeCryptoStatus (com.fsck.k9.activity.compose.ComposeCryptoStatus)7 Recipient (com.fsck.k9.view.RecipientSelectView.Recipient)5 BodyPart (com.fsck.k9.mail.BodyPart)4 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)4 IOException (java.io.IOException)3 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Activity (android.app.Activity)1