// Process each part of the multi-part, taking the appropriate action
// depending on the part's type. This loop should: display text and
// html body parts, recursively display multi-parts and store
// attachments and contacts to display later.
for (int index = 0; index < multipart.getCount(); index++) {
final BodyPart bodyPart = multipart.getBodyPart(index);
// If this body part is text then display all of it
if (bodyPart instanceof TextBodyPart) {
final TextBodyPart textBodyPart = (TextBodyPart) bodyPart;
// If there are missing parts of the text, try to retrieve the
// rest of it.
if (textBodyPart.hasMore()) {
try {
Transport.more(textBodyPart, true);
} catch (final Exception e) {
BlackBerryMailDemo
.errorDialog("Transport.more(BodyPart, boolean) threw "
+ e.toString());
}
}
final String plainText = (String) textBodyPart.getContent();
// Display the plain text, using an EditField if the message is
// editable or a RichTextField if it is not editable. Note: this
// does not add any empty fields.
if (plainText.length() != 0) {
if (_editable) {
addTextFieldToTableAndScreen(new EditField("",
plainText), BODY);
} else {
addTextFieldToTableAndScreen(new RichTextField(
plainText), BODY);
}
}
} else if (bodyPart instanceof MimeBodyPart) {
final MimeBodyPart mimeBodyPart = (MimeBodyPart) bodyPart;
// If the content is text then display it
final String contentType = mimeBodyPart.getContentType();
if (contentType
.startsWith(BodyPart.ContentType.TYPE_TEXT_HTML_STRING)) {
final Object obj = mimeBodyPart.getContent();
if (obj != null) {
final String htmlText = new String((byte[]) obj);
addTextFieldToTableAndScreen(
new RichTextField(htmlText), BODY);
}
} else if (contentType
.equals(BodyPart.ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING)) {
// If the body part is a multi-part and it has the the
// content type of TYPE_MULTIPART_ALTERNATIVE_STRING, then
// recursively display the multi-part.
final Object obj = mimeBodyPart.getContent();
if (obj instanceof Multipart) {
final Multipart childMultipart = (Multipart) obj;
final String childMultipartType =
childMultipart.getContentType();
if (childMultipartType
.equals(BodyPart.ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING)) {
displayMultipart(childMultipart);
}
}
}
} else if (bodyPart instanceof SupportedAttachmentPart
|| bodyPart instanceof UnsupportedAttachmentPart) {
// Extract the content type and name from the attachments
final String contentType = bodyPart.getContentType();
String name;
if (bodyPart instanceof UnsupportedAttachmentPart) {
final UnsupportedAttachmentPart uap =
(UnsupportedAttachmentPart) bodyPart;
name = uap.getName();
} else // The bodyPart is a SupportedAttachmentPart
{
final SupportedAttachmentPart sap =
(SupportedAttachmentPart) bodyPart;
name = sap.getName();
}
// Format the content type and name to display and store
// the field.
final StringBuffer sb =
new StringBuffer(contentType.length() + name.length()
+ 2);
sb.append(contentType);
sb.append('[');
sb.append(name);
sb.append(']');
delayedFields.addElement(new RichTextField(sb.toString()));
} else if (bodyPart instanceof PDAPContactAttachmentPart) {
final Contact contact = (Contact) bodyPart.getContent();
// Build the contact name
final StringBuffer sb = new StringBuffer("Contact: ");
if (contact.countValues(Contact.NAME) > 0) {
final String[] name =