Search in sources :

Example 1 with BodyPart

use of com.android.voicemail.impl.mail.BodyPart in project android_packages_apps_Dialer by LineageOS.

the class MimeUtility method collectParts.

/**
 * Recursively scan a Part (usually a Message) and sort out which of its children will be
 * "viewable" and which will be attachments.
 *
 * @param part The part to be broken down
 * @param viewables This arraylist will be populated with all parts that appear to be the
 *     "message" (e.g. text/plain & text/html)
 * @param attachments This arraylist will be populated with all parts that appear to be
 *     attachments (including inlines)
 * @throws MessagingException
 */
public static void collectParts(Part part, ArrayList<Part> viewables, ArrayList<Part> attachments) throws MessagingException {
    String disposition = part.getDisposition();
    String dispositionType = MimeUtility.getHeaderParameter(disposition, null);
    // If a disposition is not specified, default to "inline"
    boolean inline = TextUtils.isEmpty(dispositionType) || "inline".equalsIgnoreCase(dispositionType);
    // The lower-case mime type
    String mimeType = part.getMimeType().toLowerCase();
    if (part.getBody() instanceof Multipart) {
        // If the part is Multipart but not alternative it's either mixed or
        // something we don't know about, which means we treat it as mixed
        // per the spec. We just process its pieces recursively.
        MimeMultipart mp = (MimeMultipart) part.getBody();
        boolean foundHtml = false;
        if (mp.getSubTypeForTest().equals("alternative")) {
            for (int i = 0; i < mp.getCount(); i++) {
                if (mp.getBodyPart(i).isMimeType("text/html")) {
                    foundHtml = true;
                    break;
                }
            }
        }
        for (int i = 0; i < mp.getCount(); i++) {
            // See if we have text and html
            BodyPart bp = mp.getBodyPart(i);
            // If there's html, don't bother loading text
            if (foundHtml && bp.isMimeType("text/plain")) {
                continue;
            }
            collectParts(bp, viewables, attachments);
        }
    } else if (part.getBody() instanceof Message) {
        // If the part is an embedded message we just continue to process
        // it, pulling any viewables or attachments into the running list.
        Message message = (Message) part.getBody();
        collectParts(message, viewables, attachments);
    } else if (inline && (mimeType.startsWith("text") || (mimeType.startsWith("image")))) {
        // We'll treat text and images as viewables
        viewables.add(part);
    } else {
        // Everything else is an attachment.
        attachments.add(part);
    }
}
Also used : BodyPart(com.android.voicemail.impl.mail.BodyPart) Multipart(com.android.voicemail.impl.mail.Multipart) Message(com.android.voicemail.impl.mail.Message)

Example 2 with BodyPart

use of com.android.voicemail.impl.mail.BodyPart in project android_packages_apps_Dialer by MoKee.

the class MimeUtility method collectParts.

/**
 * Recursively scan a Part (usually a Message) and sort out which of its children will be
 * "viewable" and which will be attachments.
 *
 * @param part The part to be broken down
 * @param viewables This arraylist will be populated with all parts that appear to be the
 *     "message" (e.g. text/plain & text/html)
 * @param attachments This arraylist will be populated with all parts that appear to be
 *     attachments (including inlines)
 * @throws MessagingException
 */
public static void collectParts(Part part, ArrayList<Part> viewables, ArrayList<Part> attachments) throws MessagingException {
    String disposition = part.getDisposition();
    String dispositionType = MimeUtility.getHeaderParameter(disposition, null);
    // If a disposition is not specified, default to "inline"
    boolean inline = TextUtils.isEmpty(dispositionType) || "inline".equalsIgnoreCase(dispositionType);
    // The lower-case mime type
    String mimeType = part.getMimeType().toLowerCase();
    if (part.getBody() instanceof Multipart) {
        // If the part is Multipart but not alternative it's either mixed or
        // something we don't know about, which means we treat it as mixed
        // per the spec. We just process its pieces recursively.
        MimeMultipart mp = (MimeMultipart) part.getBody();
        boolean foundHtml = false;
        if (mp.getSubTypeForTest().equals("alternative")) {
            for (int i = 0; i < mp.getCount(); i++) {
                if (mp.getBodyPart(i).isMimeType("text/html")) {
                    foundHtml = true;
                    break;
                }
            }
        }
        for (int i = 0; i < mp.getCount(); i++) {
            // See if we have text and html
            BodyPart bp = mp.getBodyPart(i);
            // If there's html, don't bother loading text
            if (foundHtml && bp.isMimeType("text/plain")) {
                continue;
            }
            collectParts(bp, viewables, attachments);
        }
    } else if (part.getBody() instanceof Message) {
        // If the part is an embedded message we just continue to process
        // it, pulling any viewables or attachments into the running list.
        Message message = (Message) part.getBody();
        collectParts(message, viewables, attachments);
    } else if (inline && (mimeType.startsWith("text") || (mimeType.startsWith("image")))) {
        // We'll treat text and images as viewables
        viewables.add(part);
    } else {
        // Everything else is an attachment.
        attachments.add(part);
    }
}
Also used : BodyPart(com.android.voicemail.impl.mail.BodyPart) Multipart(com.android.voicemail.impl.mail.Multipart) Message(com.android.voicemail.impl.mail.Message)

Example 3 with BodyPart

use of com.android.voicemail.impl.mail.BodyPart in project android_packages_apps_Dialer by LineageOS.

the class MimeMultipart method writeTo.

@Override
public void writeTo(OutputStream out) throws IOException, MessagingException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
    if (preamble != null) {
        writer.write(preamble + "\r\n");
    }
    for (int i = 0, count = parts.size(); i < count; i++) {
        BodyPart bodyPart = parts.get(i);
        writer.write("--" + boundary + "\r\n");
        writer.flush();
        bodyPart.writeTo(out);
        writer.write("\r\n");
    }
    writer.write("--" + boundary + "--\r\n");
    writer.flush();
}
Also used : BodyPart(com.android.voicemail.impl.mail.BodyPart) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 4 with BodyPart

use of com.android.voicemail.impl.mail.BodyPart in project android_packages_apps_Dialer by MoKee.

the class MimeMultipart method writeTo.

@Override
public void writeTo(OutputStream out) throws IOException, MessagingException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
    if (preamble != null) {
        writer.write(preamble + "\r\n");
    }
    for (int i = 0, count = parts.size(); i < count; i++) {
        BodyPart bodyPart = parts.get(i);
        writer.write("--" + boundary + "\r\n");
        writer.flush();
        bodyPart.writeTo(out);
        writer.write("\r\n");
    }
    writer.write("--" + boundary + "--\r\n");
    writer.flush();
}
Also used : BodyPart(com.android.voicemail.impl.mail.BodyPart) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

BodyPart (com.android.voicemail.impl.mail.BodyPart)4 Message (com.android.voicemail.impl.mail.Message)2 Multipart (com.android.voicemail.impl.mail.Multipart)2 BufferedWriter (java.io.BufferedWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2