From 873a696a52655e6b0dd8e5dd22a5cb8618f0a277 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 20 Feb 2020 10:07:01 +0100 Subject: [PATCH] Jsoup parse from file to reduce memory usage --- .../eu/faircode/email/AdapterMessage.java | 15 ++++----- .../eu/faircode/email/FragmentCompose.java | 3 +- .../eu/faircode/email/FragmentMessages.java | 3 +- .../java/eu/faircode/email/HtmlHelper.java | 31 +++++++++++++++++++ .../main/java/eu/faircode/email/JsoupEx.java | 8 +++++ .../java/eu/faircode/email/MessageHelper.java | 3 +- 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 28c51333ea..094545142b 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -1690,18 +1690,19 @@ public class AdapterMessage extends RecyclerView.Adapter 0) signed_data = false; args.putBoolean("signed_data", signed_data); - Document document = JsoupEx.parse(body); + Document document = JsoupEx.parse(file); HtmlHelper.cleanup(document); // Check for inline encryption - int begin = body.indexOf(Helper.PGP_BEGIN_MESSAGE); - int end = body.indexOf(Helper.PGP_END_MESSAGE); - args.putBoolean("inline_encrypted", begin >= 0 && begin < end); + boolean iencrypted = HtmlHelper.contains(document, new String[]{ + Helper.PGP_BEGIN_MESSAGE, + Helper.PGP_END_MESSAGE + }); + args.putBoolean("inline_encrypted", iencrypted); // Check for images boolean has_images = false; @@ -1752,7 +1753,7 @@ public class AdapterMessage extends RecyclerView.Adapter= max); } + static boolean contains(Document d, String[] texts) { + Map condition = new HashMap<>(); + for (String t : texts) + condition.put(t, false); + + for (Element elm : d.select("*")) + for (Node child : elm.childNodes()) { + if (child instanceof TextNode) { + TextNode tnode = ((TextNode) child); + String text = tnode.getWholeText(); + for (String t : texts) + if (!condition.get(t) && text.contains(t)) { + condition.put(t, true); + + boolean found = true; + for (String c : texts) + if (!condition.get(c)) { + found = false; + break; + } + + if (found) + return true; + } + } + } + + return false; + } + static Spanned fromHtml(@NonNull String html) { return fromHtml(html, null, null); } diff --git a/app/src/main/java/eu/faircode/email/JsoupEx.java b/app/src/main/java/eu/faircode/email/JsoupEx.java index 17bde2a0b4..c36b9e3e76 100644 --- a/app/src/main/java/eu/faircode/email/JsoupEx.java +++ b/app/src/main/java/eu/faircode/email/JsoupEx.java @@ -23,6 +23,10 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + public class JsoupEx { static Document parse(String html) { @@ -47,4 +51,8 @@ org.jsoup.UncheckedIOException: java.io.IOException: Input is binary and unsuppo return document; } } + + static Document parse(File in) throws IOException { + return Jsoup.parse(in, StandardCharsets.UTF_8.name()); + } } diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 242122be4b..6aa87ec8ae 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -484,8 +484,7 @@ public class MessageHelper { } // Build html body - String html = Helper.readText(message.getFile(context)); - Document document = JsoupEx.parse(html); + Document document = JsoupEx.parse(message.getFile(context)); // When sending message if (identity != null)