Make sure BodyTextExtractor.getBodyTextFromMessage() never returns null

This commit is contained in:
cketti 2021-06-02 00:32:35 +02:00
parent c6fc922e5b
commit 63e6c61368

View file

@ -1,6 +1,7 @@
package com.fsck.k9.message.extractors;
import androidx.annotation.NonNull;
import timber.log.Timber;
import com.fsck.k9.mail.Part;
@ -15,6 +16,7 @@ public class BodyTextExtractor {
/** Fetch the body text from a messagePart in the desired messagePart format. This method handles
* conversions between formats (html to text and vice versa) if necessary.
*/
@NonNull
public static String getBodyTextFromMessage(Part messagePart, SimpleMessageFormat format) {
Part part;
if (format == SimpleMessageFormat.HTML) {
@ -22,13 +24,13 @@ public class BodyTextExtractor {
part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
if (part != null) {
Timber.d("getBodyTextFromMessage: HTML requested, HTML found.");
return MessageExtractor.getTextFromPart(part);
return getTextFromPartOrEmpty(part);
}
part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
if (part != null) {
Timber.d("getBodyTextFromMessage: HTML requested, text found.");
String text = MessageExtractor.getTextFromPart(part);
String text = getTextFromPartOrEmpty(part);
return HtmlConverter.textToHtml(text);
}
} else if (format == SimpleMessageFormat.TEXT) {
@ -36,13 +38,13 @@ public class BodyTextExtractor {
part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
if (part != null) {
Timber.d("getBodyTextFromMessage: Text requested, text found.");
return MessageExtractor.getTextFromPart(part);
return getTextFromPartOrEmpty(part);
}
part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
if (part != null) {
Timber.d("getBodyTextFromMessage: Text requested, HTML found.");
String text = MessageExtractor.getTextFromPart(part);
String text = getTextFromPartOrEmpty(part);
return HtmlConverter.htmlToText(text);
}
}
@ -50,4 +52,10 @@ public class BodyTextExtractor {
// If we had nothing interesting, return an empty string.
return "";
}
@NonNull
private static String getTextFromPartOrEmpty(Part part) {
String text = MessageExtractor.getTextFromPart(part);
return text != null ? text : "";
}
}