Jsoup parse from file to reduce memory usage

This commit is contained in:
M66B 2020-02-20 10:07:01 +01:00
parent 48a5278ba0
commit 873a696a52
6 changed files with 50 additions and 13 deletions

View File

@ -1690,18 +1690,19 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
if (!file.exists()) if (!file.exists())
return null; return null;
String body = Helper.readText(file); if (file.length() > 0)
if (!TextUtils.isEmpty(body))
signed_data = false; signed_data = false;
args.putBoolean("signed_data", signed_data); args.putBoolean("signed_data", signed_data);
Document document = JsoupEx.parse(body); Document document = JsoupEx.parse(file);
HtmlHelper.cleanup(document); HtmlHelper.cleanup(document);
// Check for inline encryption // Check for inline encryption
int begin = body.indexOf(Helper.PGP_BEGIN_MESSAGE); boolean iencrypted = HtmlHelper.contains(document, new String[]{
int end = body.indexOf(Helper.PGP_END_MESSAGE); Helper.PGP_BEGIN_MESSAGE,
args.putBoolean("inline_encrypted", begin >= 0 && begin < end); Helper.PGP_END_MESSAGE
});
args.putBoolean("inline_encrypted", iencrypted);
// Check for images // Check for images
boolean has_images = false; boolean has_images = false;
@ -1752,7 +1753,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
HtmlHelper.removeTrackingPixels(context, document); HtmlHelper.removeTrackingPixels(context, document);
if (debug) { if (debug) {
Document format = JsoupEx.parse(document.html()); Document format = JsoupEx.parse(file);
format.outputSettings().prettyPrint(true).outline(true).indentAmount(1); format.outputSettings().prettyPrint(true).outline(true).indentAmount(1);
Element pre = document.createElement("pre"); Element pre = document.createElement("pre");
pre.text(format.html()); pre.text(format.html());

View File

@ -3078,8 +3078,7 @@ public class FragmentCompose extends FragmentBase {
div.appendChild(p); div.appendChild(p);
// Get referenced message body // Get referenced message body
String rhtml = Helper.readText(ref.getFile(context)); Document d = JsoupEx.parse(ref.getFile(context));
Document d = JsoupEx.parse(rhtml);
// Remove signature separators // Remove signature separators
boolean remove_signatures = prefs.getBoolean("remove_signatures", false); boolean remove_signatures = prefs.getBoolean("remove_signatures", false);

View File

@ -5801,8 +5801,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (!file.exists()) if (!file.exists())
return null; return null;
String html = Helper.readText(file); Document document = JsoupEx.parse(file);
Document document = JsoupEx.parse(html);
HtmlHelper.truncate(document, false); HtmlHelper.truncate(document, false);
HtmlHelper.embedInlineImages(context, id, document); HtmlHelper.embedInlineImages(context, id, document);

View File

@ -64,6 +64,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -1249,6 +1250,36 @@ public class HtmlHelper {
return (length >= max); return (length >= max);
} }
static boolean contains(Document d, String[] texts) {
Map<String, Boolean> 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) { static Spanned fromHtml(@NonNull String html) {
return fromHtml(html, null, null); return fromHtml(html, null, null);
} }

View File

@ -23,6 +23,10 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class JsoupEx { public class JsoupEx {
static Document parse(String html) { static Document parse(String html) {
@ -47,4 +51,8 @@ org.jsoup.UncheckedIOException: java.io.IOException: Input is binary and unsuppo
return document; return document;
} }
} }
static Document parse(File in) throws IOException {
return Jsoup.parse(in, StandardCharsets.UTF_8.name());
}
} }

View File

@ -484,8 +484,7 @@ public class MessageHelper {
} }
// Build html body // Build html body
String html = Helper.readText(message.getFile(context)); Document document = JsoupEx.parse(message.getFile(context));
Document document = JsoupEx.parse(html);
// When sending message // When sending message
if (identity != null) if (identity != null)