Search in sources :

Example 11 with Body

use of com.fsck.k9.mail.Body 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 12 with Body

use of com.fsck.k9.mail.Body 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)

Example 13 with Body

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

the class MessageCreationHelper method createMessage.

private static Message createMessage(String mimeType, Body body) {
    MimeMessage message = new MimeMessage();
    message.setBody(body);
    message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType);
    return message;
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage)

Example 14 with Body

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

the class MessageExtractorTest method getTextFromPart_withExceptionThrownGettingInputStream_shouldReturnNull.

@Test
public void getTextFromPart_withExceptionThrownGettingInputStream_shouldReturnNull() throws Exception {
    part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html");
    Body body = mock(Body.class);
    when(body.getInputStream()).thenThrow(new MessagingException("Test"));
    part.setBody(body);
    String result = MessageExtractor.getTextFromPart(part);
    assertNull(result);
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) Body(com.fsck.k9.mail.Body) BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody) Test(org.junit.Test)

Example 15 with Body

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

the class MessageExtractorTest method getTextFromPart_withRawDataBodyWithNonText_shouldReturnNull.

@Test
public void getTextFromPart_withRawDataBodyWithNonText_shouldReturnNull() throws Exception {
    part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "image/jpeg");
    BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), MimeUtil.ENC_8BIT);
    part.setBody(body);
    String result = MessageExtractor.getTextFromPart(part);
    assertNull(result);
}
Also used : BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody) Test(org.junit.Test)

Aggregations

Body (com.fsck.k9.mail.Body)44 BodyPart (com.fsck.k9.mail.BodyPart)35 Multipart (com.fsck.k9.mail.Multipart)32 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)32 Part (com.fsck.k9.mail.Part)29 Test (org.junit.Test)29 TextBody (com.fsck.k9.mail.internet.TextBody)23 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)21 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)19 ArrayList (java.util.ArrayList)16 MessagingException (com.fsck.k9.mail.MessagingException)14 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Message (com.fsck.k9.mail.Message)9 OutputStream (java.io.OutputStream)9 Stack (java.util.Stack)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)7 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)7 InputStream (java.io.InputStream)7