package com.fsck.k9.message.extractors;


import timber.log.Timber;

import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.MessageExtractor;
import com.fsck.k9.mail.internet.MimeUtility;
import com.fsck.k9.message.SimpleMessageFormat;
import com.fsck.k9.message.html.HtmlConverter;


//TODO: Get rid of this class and use MessageViewInfoExtractor instead
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.
     */
    public static String getBodyTextFromMessage(Part messagePart, SimpleMessageFormat format) {
        Part part;
        if (format == SimpleMessageFormat.HTML) {
            // HTML takes precedence, then text.
            part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
            if (part != null) {
                Timber.d("getBodyTextFromMessage: HTML requested, HTML found.");
                return MessageExtractor.getTextFromPart(part);
            }

            part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
            if (part != null) {
                Timber.d("getBodyTextFromMessage: HTML requested, text found.");
                String text = MessageExtractor.getTextFromPart(part);
                return HtmlConverter.textToHtml(text);
            }
        } else if (format == SimpleMessageFormat.TEXT) {
            // Text takes precedence, then html.
            part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
            if (part != null) {
                Timber.d("getBodyTextFromMessage: Text requested, text found.");
                return MessageExtractor.getTextFromPart(part);
            }

            part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
            if (part != null) {
                Timber.d("getBodyTextFromMessage: Text requested, HTML found.");
                String text = MessageExtractor.getTextFromPart(part);
                return HtmlConverter.htmlToText(text);
            }
        }

        // If we had nothing interesting, return an empty string.
        return "";
    }
}

@@ -35,16 +35,52 @@
 tors;%0A%0A%0A
+import androidx.annotation.NonNull;%0A
 import t
@@ -97,16 +97,16 @@
 Timber;%0A
-
 %0Aimport 
@@ -615,16 +615,29 @@
      */%0A
+    @NonNull%0A
     publ
@@ -1060,33 +1060,16 @@
  return 
-MessageExtractor.
 getTextF
@@ -1067,32 +1067,39 @@
  getTextFromPart
+OrEmpty
 (part);%0A        
@@ -1331,33 +1331,16 @@
  text = 
-MessageExtractor.
 getTextF
@@ -1338,32 +1338,39 @@
  getTextFromPart
+OrEmpty
 (part);%0A        
@@ -1755,33 +1755,16 @@
  return 
-MessageExtractor.
 getTextF
@@ -1762,32 +1762,39 @@
  getTextFromPart
+OrEmpty
 (part);%0A        
@@ -2029,25 +2029,8 @@
 t = 
-MessageExtractor.
 getT
@@ -2040,16 +2040,23 @@
 FromPart
+OrEmpty
 (part);%0A
@@ -2201,16 +2201,16 @@
 string.%0A
-
         
@@ -2225,11 +2225,196 @@
 %22;%0A    %7D
+%0A%0A    @NonNull%0A    private static String getTextFromPartOrEmpty(Part part) %7B%0A        String text = MessageExtractor.getTextFromPart(part);%0A        return text != null ? text : %22%22;%0A    %7D
 %0A%7D%0A

Make sure BodyTextExtractor.getBodyTextFromMessage() never returns null