From 63e6c61368703f02622040372c0defe8f557a4b7 Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 2 Jun 2021 00:32:35 +0200 Subject: [PATCH] Make sure BodyTextExtractor.getBodyTextFromMessage() never returns null --- .../k9/message/extractors/BodyTextExtractor.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/message/extractors/BodyTextExtractor.java b/app/core/src/main/java/com/fsck/k9/message/extractors/BodyTextExtractor.java index a1af349a4..e1d9c0f7d 100644 --- a/app/core/src/main/java/com/fsck/k9/message/extractors/BodyTextExtractor.java +++ b/app/core/src/main/java/com/fsck/k9/message/extractors/BodyTextExtractor.java @@ -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 : ""; + } }