Search in sources :

Example 66 with MessagingException

use of com.fsck.k9.mail.MessagingException in project k-9 by k9mail.

the class MessagingControllerTest method refreshRemoteSynchronous_shouldNotDeleteSpecialFoldersNotOnRemote.

@Test
public void refreshRemoteSynchronous_shouldNotDeleteSpecialFoldersNotOnRemote() throws MessagingException {
    configureRemoteStoreWithFolder();
    LocalFolder missingSpecialFolder = mock(LocalFolder.class);
    when(account.isSpecialFolder("Outbox")).thenReturn(true);
    when(missingSpecialFolder.getName()).thenReturn("Outbox");
    when(localStore.getPersonalNamespaces(false)).thenReturn(Collections.singletonList(missingSpecialFolder));
    List<Folder> folders = Collections.emptyList();
    when(remoteStore.getPersonalNamespaces(false)).thenAnswer(createAnswer(folders));
    controller.refreshRemoteSynchronous(account, listener);
    verify(missingSpecialFolder, never()).delete(false);
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) Test(org.junit.Test)

Example 67 with MessagingException

use of com.fsck.k9.mail.MessagingException in project sms-backup-plus by jberkel.

the class MessageConverter method messageToContentValues.

@NonNull
public ContentValues messageToContentValues(final Message message) throws IOException, MessagingException {
    if (message == null)
        throw new MessagingException("message is null");
    final ContentValues values = new ContentValues();
    switch(getDataType(message)) {
        case SMS:
            if (message.getBody() == null)
                throw new MessagingException("body is null");
            InputStream is = MimeUtility.decodeBody(message.getBody());
            if (is == null) {
                throw new MessagingException("body.getInputStream() is null for " + message.getBody());
            }
            final String body = IOUtils.toString(is);
            final String address = Headers.get(message, Headers.ADDRESS);
            values.put(Telephony.TextBasedSmsColumns.BODY, body);
            values.put(Telephony.TextBasedSmsColumns.ADDRESS, address);
            values.put(Telephony.TextBasedSmsColumns.TYPE, Headers.get(message, Headers.TYPE));
            values.put(Telephony.TextBasedSmsColumns.PROTOCOL, Headers.get(message, Headers.PROTOCOL));
            values.put(Telephony.TextBasedSmsColumns.SERVICE_CENTER, Headers.get(message, Headers.SERVICE_CENTER));
            values.put(Telephony.TextBasedSmsColumns.DATE, Headers.get(message, Headers.DATE));
            values.put(Telephony.TextBasedSmsColumns.STATUS, Headers.get(message, Headers.STATUS));
            values.put(Telephony.TextBasedSmsColumns.THREAD_ID, threadHelper.getThreadId(mContext, address));
            values.put(Telephony.TextBasedSmsColumns.READ, mMarkAsReadOnRestore ? "1" : Headers.get(message, Headers.READ));
            break;
        case CALLLOG:
            values.put(CallLog.Calls.NUMBER, Headers.get(message, Headers.ADDRESS));
            values.put(CallLog.Calls.TYPE, Integer.valueOf(Headers.get(message, Headers.TYPE)));
            values.put(CallLog.Calls.DATE, Headers.get(message, Headers.DATE));
            values.put(CallLog.Calls.DURATION, Long.valueOf(Headers.get(message, Headers.DURATION)));
            values.put(CallLog.Calls.NEW, 0);
            PersonRecord record = mPersonLookup.lookupPerson(Headers.get(message, Headers.ADDRESS));
            if (!record.isUnknown()) {
                values.put(CallLog.Calls.CACHED_NAME, record.getName());
                values.put(CallLog.Calls.CACHED_NUMBER_TYPE, -2);
            }
            break;
        default:
            throw new MessagingException("don't know how to restore " + getDataType(message));
    }
    return values;
}
Also used : ContentValues(android.content.ContentValues) MessagingException(com.fsck.k9.mail.MessagingException) InputStream(java.io.InputStream) NonNull(android.support.annotation.NonNull)

Example 68 with MessagingException

use of com.fsck.k9.mail.MessagingException in project sms-backup-plus by jberkel.

the class MessageConverter method convertMessages.

@NonNull
public ConversionResult convertMessages(final Cursor cursor, DataType dataType) throws MessagingException {
    final Map<String, String> msgMap = getMessageMap(cursor);
    final Message m = mMessageGenerator.messageForDataType(msgMap, dataType);
    final ConversionResult result = new ConversionResult(dataType);
    if (m != null) {
        m.setFlag(Flag.SEEN, markAsSeen(dataType, msgMap));
        result.add(m, msgMap);
    }
    return result;
}
Also used : Message(com.fsck.k9.mail.Message) NonNull(android.support.annotation.NonNull)

Example 69 with MessagingException

use of com.fsck.k9.mail.MessagingException in project sms-backup-plus by jberkel.

the class MessageGenerator method messageFromMapSms.

@Nullable
private Message messageFromMapSms(Map<String, String> msgMap) throws MessagingException {
    final String address = msgMap.get(Telephony.TextBasedSmsColumns.ADDRESS);
    if (TextUtils.isEmpty(address))
        return null;
    PersonRecord record = personLookup.lookupPerson(address);
    if (!includePersonInBackup(record, DataType.SMS))
        return null;
    final Message msg = new MimeMessage();
    msg.setSubject(getSubject(DataType.SMS, record));
    setBody(msg, new TextBody(msgMap.get(Telephony.TextBasedSmsColumns.BODY)));
    final int messageType = toInt(msgMap.get(Telephony.TextBasedSmsColumns.TYPE));
    if (Telephony.TextBasedSmsColumns.MESSAGE_TYPE_INBOX == messageType) {
        // Received message
        msg.setFrom(record.getAddress(addressStyle));
        msg.setRecipient(Message.RecipientType.TO, userAddress);
    } else {
        // Sent message
        msg.setRecipient(Message.RecipientType.TO, record.getAddress(addressStyle));
        msg.setFrom(userAddress);
    }
    Date sentDate;
    try {
        // TODO: should probably be TextBasedSmsColumns.DATE_SENT
        sentDate = new Date(Long.valueOf(msgMap.get(Telephony.TextBasedSmsColumns.DATE)));
    } catch (NumberFormatException n) {
        Log.e(TAG, ERROR_PARSING_DATE, n);
        sentDate = new Date();
    }
    headerGenerator.setHeaders(msg, msgMap, DataType.SMS, address, record, sentDate, messageType);
    return msg;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Date(java.util.Date) Nullable(android.support.annotation.Nullable)

Example 70 with MessagingException

use of com.fsck.k9.mail.MessagingException in project sms-backup-plus by jberkel.

the class MessageGenerator method messageFromMapMms.

@Nullable
private Message messageFromMapMms(Map<String, String> msgMap) throws MessagingException {
    if (LOCAL_LOGV)
        Log.v(TAG, "messageFromMapMms(" + msgMap + ")");
    final Uri mmsUri = Uri.withAppendedPath(Consts.MMS_PROVIDER, msgMap.get(Telephony.BaseMmsColumns._ID));
    MmsSupport.MmsDetails details = mmsSupport.getDetails(mmsUri, addressStyle);
    if (details.isEmpty()) {
        Log.w(TAG, "no recipients found");
        return null;
    } else if (!includeInBackup(DataType.MMS, details.records)) {
        Log.w(TAG, "no recipients included");
        return null;
    }
    final Message msg = new MimeMessage();
    msg.setSubject(getSubject(DataType.MMS, details.getRecipient()));
    if (details.inbound) {
        // msg_box == MmsConsts.MESSAGE_BOX_INBOX does not work
        msg.setFrom(details.getRecipientAddress());
        msg.setRecipient(Message.RecipientType.TO, userAddress);
    } else {
        msg.setRecipients(Message.RecipientType.TO, details.getAddresses());
        msg.setFrom(userAddress);
    }
    Date sentDate;
    try {
        sentDate = new Date(1000 * Long.valueOf(msgMap.get(Telephony.BaseMmsColumns.DATE)));
    } catch (NumberFormatException n) {
        Log.e(TAG, ERROR_PARSING_DATE, n);
        sentDate = new Date();
    }
    final int msg_box = toInt(msgMap.get("msg_box"));
    headerGenerator.setHeaders(msg, msgMap, DataType.MMS, details.address, details.getRecipient(), sentDate, msg_box);
    MimeMultipart body = MimeMultipart.newInstance();
    for (BodyPart p : mmsSupport.getMMSBodyParts(Uri.withAppendedPath(mmsUri, MMS_PART))) {
        body.addBodyPart(p);
    }
    setBody(msg, body);
    return msg;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Uri(android.net.Uri) Date(java.util.Date) Nullable(android.support.annotation.Nullable)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)159 Test (org.junit.Test)73 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)52 LocalFolder (com.fsck.k9.mailstore.LocalFolder)49 LocalStore (com.fsck.k9.mailstore.LocalStore)49 ArrayList (java.util.ArrayList)49 Message (com.fsck.k9.mail.Message)44 LocalMessage (com.fsck.k9.mailstore.LocalMessage)42 IOException (java.io.IOException)42 FetchProfile (com.fsck.k9.mail.FetchProfile)30 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)26 BodyPart (com.fsck.k9.mail.BodyPart)23 Part (com.fsck.k9.mail.Part)22 Account (com.fsck.k9.Account)21 Body (com.fsck.k9.mail.Body)21 TextBody (com.fsck.k9.mail.internet.TextBody)21 Date (java.util.Date)20 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)18