diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index ffdc95878..db2ee4e67 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -101,6 +101,7 @@ import com.fsck.k9.mail.Message.RecipientType; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Part; +import com.fsck.k9.mail.internet.MessageExtractor; import com.fsck.k9.mail.internet.MimeBodyPart; import com.fsck.k9.mail.internet.MimeHeader; import com.fsck.k9.mail.internet.MimeMessage; @@ -2955,10 +2956,10 @@ public class MessageCompose extends K9Activity implements OnClickListener, if (messageFormat == MessageFormat.HTML) { - Part part = message.findFirstPartByMimeType("text/html"); + Part part = MimeUtility.findFirstPartByMimeType(message, "text/html"); if (part != null) { // Shouldn't happen if we were the one who saved it. mQuotedTextFormat = SimpleMessageFormat.HTML; - String text = part.getText(); + String text = MessageExtractor.getTextFromPart(part); if (K9.DEBUG) { Log.d(K9.LOG_TAG, "Loading message with offset " + bodyOffset + ", length " + bodyLength + ". Text length is " + text.length() + "."); } @@ -3021,9 +3022,9 @@ public class MessageCompose extends K9Activity implements OnClickListener, */ private void processSourceMessageText(Message message, Integer bodyOffset, Integer bodyLength, boolean viewMessageContent) throws MessagingException { - Part textPart = message.findFirstPartByMimeType("text/plain"); + Part textPart = MimeUtility.findFirstPartByMimeType(message, "text/plain"); if (textPart != null) { - String text = textPart.getText(); + String text = MessageExtractor.getTextFromPart(textPart); if (K9.DEBUG) { Log.d(K9.LOG_TAG, "Loading message with offset " + bodyOffset + ", length " + bodyLength + ". Text length is " + text.length() + "."); } @@ -3091,7 +3092,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, // Figure out which message format to use for the quoted text by looking if the source // message contains a text/html part. If it does, we use that. mQuotedTextFormat = - (mSourceMessage.findFirstPartByMimeType("text/html") == null) ? + (MimeUtility.findFirstPartByMimeType(mSourceMessage, "text/html") == null) ? SimpleMessageFormat.TEXT : SimpleMessageFormat.HTML; } else { mQuotedTextFormat = SimpleMessageFormat.HTML; @@ -3221,37 +3222,39 @@ public class MessageCompose extends K9Activity implements OnClickListener, Part part; if (format == SimpleMessageFormat.HTML) { // HTML takes precedence, then text. - part = message.findFirstPartByMimeType("text/html"); + part = MimeUtility.findFirstPartByMimeType(message, "text/html"); if (part != null) { if (K9.DEBUG) { Log.d(K9.LOG_TAG, "getBodyTextFromMessage: HTML requested, HTML found."); } - return part.getText(); + return MessageExtractor.getTextFromPart(part); } - part = message.findFirstPartByMimeType("text/plain"); + part = MimeUtility.findFirstPartByMimeType(message, "text/plain"); if (part != null) { if (K9.DEBUG) { Log.d(K9.LOG_TAG, "getBodyTextFromMessage: HTML requested, text found."); } - return HtmlConverter.textToHtml(part.getText()); + String text = MessageExtractor.getTextFromPart(part); + return HtmlConverter.textToHtml(text); } } else if (format == SimpleMessageFormat.TEXT) { // Text takes precedence, then html. - part = message.findFirstPartByMimeType("text/plain"); + part = MimeUtility.findFirstPartByMimeType(message, "text/plain"); if (part != null) { if (K9.DEBUG) { Log.d(K9.LOG_TAG, "getBodyTextFromMessage: Text requested, text found."); } - return part.getText(); + return MessageExtractor.getTextFromPart(part); } - part = message.findFirstPartByMimeType("text/html"); + part = MimeUtility.findFirstPartByMimeType(message, "text/html"); if (part != null) { if (K9.DEBUG) { Log.d(K9.LOG_TAG, "getBodyTextFromMessage: Text requested, HTML found."); } - return HtmlConverter.htmlToText(part.getText()); + String text = MessageExtractor.getTextFromPart(part); + return HtmlConverter.htmlToText(text); } } diff --git a/src/com/fsck/k9/controller/MessagingController.java b/src/com/fsck/k9/controller/MessagingController.java index 76c151c8a..b10d68c07 100644 --- a/src/com/fsck/k9/controller/MessagingController.java +++ b/src/com/fsck/k9/controller/MessagingController.java @@ -78,6 +78,7 @@ import com.fsck.k9.mail.PushReceiver; import com.fsck.k9.mail.Pusher; import com.fsck.k9.mail.Store; import com.fsck.k9.mail.Transport; +import com.fsck.k9.mail.internet.MessageExtractor; import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeUtility; import com.fsck.k9.mail.internet.TextBody; @@ -1744,7 +1745,7 @@ public class MessagingController implements Runnable { * right now, attachments will be left for later. */ - Set viewables = message.collectTextParts(); + Set viewables = MessageExtractor.collectTextParts(message); /* * Now download the parts we're interested in storing. @@ -3197,7 +3198,7 @@ public class MessagingController implements Runnable { try { LocalStore localStore = account.getLocalStore(); - List attachments = message.collectAttachments(); + List attachments = MessageExtractor.collectAttachments(message); for (Part attachment : attachments) { attachment.setBody(null); } @@ -4244,12 +4245,12 @@ public class MessagingController implements Runnable { try { Intent msg = new Intent(Intent.ACTION_SEND); String quotedText = null; - Part part = message.findFirstPartByMimeType("text/plain"); + Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain"); if (part == null) { - part = message.findFirstPartByMimeType("text/html"); + part = MimeUtility.findFirstPartByMimeType(message, "text/html"); } if (part != null) { - quotedText = part.getText(); + quotedText = MessageExtractor.getTextFromPart(part); } if (quotedText != null) { msg.putExtra(Intent.EXTRA_TEXT, quotedText); diff --git a/src/com/fsck/k9/crypto/CryptoHelper.java b/src/com/fsck/k9/crypto/CryptoHelper.java index a03e6fff9..612c4ba3d 100644 --- a/src/com/fsck/k9/crypto/CryptoHelper.java +++ b/src/com/fsck/k9/crypto/CryptoHelper.java @@ -7,6 +7,9 @@ import java.util.regex.Pattern; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Part; +import com.fsck.k9.mail.internet.MessageExtractor; +import com.fsck.k9.mail.internet.MimeUtility; + public class CryptoHelper { @@ -31,12 +34,12 @@ public class CryptoHelper { public boolean isEncrypted(Message message) { String data = null; try { - Part part = message.findFirstPartByMimeType("text/plain"); + Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain"); if (part == null) { - part = message.findFirstPartByMimeType("text/html"); + part = MimeUtility.findFirstPartByMimeType(message, "text/html"); } if (part != null) { - data = part.getText(); + data = MessageExtractor.getTextFromPart(part); } } catch (MessagingException e) { // guess not... @@ -54,12 +57,12 @@ public class CryptoHelper { public boolean isSigned(Message message) { String data = null; try { - Part part = message.findFirstPartByMimeType("text/plain"); + Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain"); if (part == null) { - part = message.findFirstPartByMimeType("text/html"); + part = MimeUtility.findFirstPartByMimeType(message, "text/html"); } if (part != null) { - data = part.getText(); + data = MessageExtractor.getTextFromPart(part); } } catch (MessagingException e) { // guess not... diff --git a/src/com/fsck/k9/mail/BodyPart.java b/src/com/fsck/k9/mail/BodyPart.java index 884b03ae1..551866829 100644 --- a/src/com/fsck/k9/mail/BodyPart.java +++ b/src/com/fsck/k9/mail/BodyPart.java @@ -1,8 +1,5 @@ - package com.fsck.k9.mail; -import com.fsck.k9.mail.internet.MessageExtractor; -import com.fsck.k9.mail.internet.MimeUtility; public abstract class BodyPart implements Part { private Multipart mParent; @@ -16,14 +13,4 @@ public abstract class BodyPart implements Part { } public abstract void setEncoding(String encoding) throws MessagingException; - - @Override - public String getText() { - return MessageExtractor.getTextFromPart(this); - } - - @Override - public Part findFirstPartByMimeType(String mimeType) throws MessagingException { - return MimeUtility.findFirstPartByMimeType(this, mimeType); - } } diff --git a/src/com/fsck/k9/mail/Message.java b/src/com/fsck/k9/mail/Message.java index 368b25dc2..ecc35f193 100644 --- a/src/com/fsck/k9/mail/Message.java +++ b/src/com/fsck/k9/mail/Message.java @@ -2,11 +2,9 @@ package com.fsck.k9.mail; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.EnumSet; -import java.util.List; import java.util.Set; import android.util.Log; @@ -14,8 +12,6 @@ import android.util.Log; import com.fsck.k9.K9; import com.fsck.k9.mail.filter.CountingOutputStream; import com.fsck.k9.mail.filter.EOLConvertingOutputStream; -import com.fsck.k9.mail.internet.MessageExtractor; -import com.fsck.k9.mail.internet.MimeUtility; public abstract class Message implements Part, CompositeBody { @@ -270,41 +266,4 @@ public abstract class Message implements Part, CompositeBody { @Override public abstract Message clone(); - @Override - public String getText() { - return MessageExtractor.getTextFromPart(this); - } - - @Override - public Part findFirstPartByMimeType(String mimeType) throws MessagingException { - return MimeUtility.findFirstPartByMimeType(this, mimeType); - } - - /** - * Collect attachment parts of a message. - * @return A list of parts regarded as attachments. - * @throws MessagingException In case of an error. - */ - public List collectAttachments() throws MessagingException { - try { - List attachments = new ArrayList(); - MessageExtractor.getViewables(this, attachments); - return attachments; - } catch (Exception e) { - throw new MessagingException("Couldn't collect attachment parts", e); - } - } - - /** - * Collect the viewable textual parts of a message. - * @return A set of viewable parts of the message. - * @throws MessagingException In case of an error. - */ - public Set collectTextParts() throws MessagingException { - try { - return MessageExtractor.getTextParts(this); - } catch (Exception e) { - throw new MessagingException("Couldn't extract viewable parts", e); - } - } } diff --git a/src/com/fsck/k9/mail/Part.java b/src/com/fsck/k9/mail/Part.java index bcf5dcc5e..4c692b724 100644 --- a/src/com/fsck/k9/mail/Part.java +++ b/src/com/fsck/k9/mail/Part.java @@ -29,16 +29,6 @@ public interface Part { void writeTo(OutputStream out) throws IOException, MessagingException; - /** - * Reads the Part's body and returns a String based on any charset conversion that needed - * to be done. Note, this does not return a text representation of HTML. - * @return a String containing the converted text in the body, or null if there was no text - * or an error during conversion. - */ - String getText(); - - Part findFirstPartByMimeType(String mimeType) throws MessagingException; - /** * Called just prior to transmission, once the type of transport is known to * be 7bit. diff --git a/src/com/fsck/k9/mail/internet/MessageExtractor.java b/src/com/fsck/k9/mail/internet/MessageExtractor.java index a4b997116..1f35c0491 100644 --- a/src/com/fsck/k9/mail/internet/MessageExtractor.java +++ b/src/com/fsck/k9/mail/internet/MessageExtractor.java @@ -199,6 +199,34 @@ public class MessageExtractor { return getParts(getViewables(part, attachments)); } + /** + * Collect attachment parts of a message. + * @return A list of parts regarded as attachments. + * @throws MessagingException In case of an error. + */ + public static List collectAttachments(Message message) throws MessagingException { + try { + List attachments = new ArrayList(); + getViewables(message, attachments); + return attachments; + } catch (Exception e) { + throw new MessagingException("Couldn't collect attachment parts", e); + } + } + + /** + * Collect the viewable textual parts of a message. + * @return A set of viewable parts of the message. + * @throws MessagingException In case of an error. + */ + public static Set collectTextParts(Message message) throws MessagingException { + try { + return getTextParts(message); + } catch (Exception e) { + throw new MessagingException("Couldn't extract viewable parts", e); + } + } + private static Message getMessageFromPart(Part part) { while (part != null) { if (part instanceof Message) diff --git a/src/com/fsck/k9/mail/internet/MimeUtility.java b/src/com/fsck/k9/mail/internet/MimeUtility.java index 00f037108..a5ac8efa7 100644 --- a/src/com/fsck/k9/mail/internet/MimeUtility.java +++ b/src/com/fsck/k9/mail/internet/MimeUtility.java @@ -16,7 +16,10 @@ import org.apache.james.mime4j.util.MimeUtil; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.regex.Pattern; @@ -952,7 +955,7 @@ public class MimeUtility { if (part.getBody() instanceof Multipart) { Multipart multipart = (Multipart)part.getBody(); for (BodyPart bodyPart : multipart.getBodyParts()) { - Part ret = bodyPart.findFirstPartByMimeType(mimeType); + Part ret = MimeUtility.findFirstPartByMimeType(bodyPart, mimeType); if (ret != null) { return ret; } diff --git a/src/com/fsck/k9/mailstore/LocalMessage.java b/src/com/fsck/k9/mailstore/LocalMessage.java index 555254c8f..75365d922 100644 --- a/src/com/fsck/k9/mailstore/LocalMessage.java +++ b/src/com/fsck/k9/mailstore/LocalMessage.java @@ -20,7 +20,9 @@ import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Folder; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Part; +import com.fsck.k9.mail.internet.MessageExtractor; import com.fsck.k9.mail.internet.MimeMessage; +import com.fsck.k9.mail.internet.MimeUtility; import com.fsck.k9.mailstore.LockableDatabase.DbCallback; import com.fsck.k9.mailstore.LockableDatabase.WrappedException; @@ -119,16 +121,16 @@ public class LocalMessage extends MimeMessage { */ public String getTextForDisplay() throws MessagingException { String text = null; // First try and fetch an HTML part. - Part part = findFirstPartByMimeType("text/html"); + Part part = MimeUtility.findFirstPartByMimeType(this, "text/html"); if (part == null) { // If that fails, try and get a text part. - part = findFirstPartByMimeType("text/plain"); + part = MimeUtility.findFirstPartByMimeType(this, "text/plain"); if (part != null && part.getBody() instanceof LocalTextBody) { text = ((LocalTextBody) part.getBody()).getBodyForDisplay(); } } else { // We successfully found an HTML part; do the necessary character set decoding. - text = part.getText(); + text = MessageExtractor.getTextFromPart(this); } return text; } diff --git a/src/com/fsck/k9/mailstore/LocalMessageExtractor.java b/src/com/fsck/k9/mailstore/LocalMessageExtractor.java index f5d1a9108..864b9132f 100644 --- a/src/com/fsck/k9/mailstore/LocalMessageExtractor.java +++ b/src/com/fsck/k9/mailstore/LocalMessageExtractor.java @@ -170,7 +170,7 @@ class LocalMessageExtractor { Part part = ((Textual)viewable).getPart(); addHtmlDivider(html, part, prependDivider); - String t = part.getText(); + String t = MessageExtractor.getTextFromPart(part); if (t == null) { t = ""; } else if (viewable instanceof Text) { @@ -202,7 +202,7 @@ class LocalMessageExtractor { Part part = ((Textual)viewable).getPart(); addTextDivider(text, part, prependDivider); - String t = part.getText(); + String t = MessageExtractor.getTextFromPart(part); if (t == null) { t = ""; } else if (viewable instanceof Html) { @@ -446,7 +446,7 @@ class LocalMessageExtractor { Body firstBody = part.getBody(); if (part.isMimeType("text/plain")) { - String bodyText = part.getText(); + String bodyText = MessageExtractor.getTextFromPart(part); if (bodyText != null) { text = bodyText; html = HtmlConverter.textToHtml(text); @@ -455,7 +455,7 @@ class LocalMessageExtractor { firstBody instanceof MimeMultipart) { MimeMultipart multipart = (MimeMultipart) firstBody; for (BodyPart bodyPart : multipart.getBodyParts()) { - String bodyText = bodyPart.getText(); + String bodyText = MessageExtractor.getTextFromPart(bodyPart); if (bodyText != null) { if (text.isEmpty() && bodyPart.isMimeType("text/plain")) { text = bodyText; diff --git a/src/com/fsck/k9/view/MessageOpenPgpView.java b/src/com/fsck/k9/view/MessageOpenPgpView.java index cb9be859b..3c0e857da 100644 --- a/src/com/fsck/k9/view/MessageOpenPgpView.java +++ b/src/com/fsck/k9/view/MessageOpenPgpView.java @@ -36,6 +36,8 @@ import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Part; +import com.fsck.k9.mail.internet.MessageExtractor; +import com.fsck.k9.mail.internet.MimeUtility; import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.util.OpenPgpApi; @@ -214,7 +216,7 @@ public class MessageOpenPgpView extends LinearLayout { } else { try { // check for PGP/MIME encryption - Part pgp = message.findFirstPartByMimeType("application/pgp-encrypted"); + Part pgp = MimeUtility.findFirstPartByMimeType(message, "application/pgp-encrypted"); if (pgp != null) { Toast.makeText(mContext, R.string.pgp_mime_unsupported, Toast.LENGTH_LONG) .show(); @@ -239,12 +241,12 @@ public class MessageOpenPgpView extends LinearLayout { public void run() { try { // get data String - Part part = message.findFirstPartByMimeType("text/plain"); + Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain"); if (part == null) { - part = message.findFirstPartByMimeType("text/html"); + part = MimeUtility.findFirstPartByMimeType(message, "text/html"); } if (part != null) { - mData = part.getText(); + mData = MessageExtractor.getTextFromPart(part); } // wait for service to be bound