From c24f79354eecd0691bed83b30d9f1b8b6d91c018 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 19 Jan 2021 11:57:59 +0100 Subject: [PATCH] Optimization --- app/src/main/java/eu/faircode/email/Core.java | 24 ++++++++---- .../java/eu/faircode/email/EntityRule.java | 10 +++-- .../eu/faircode/email/FragmentCompose.java | 18 ++++++--- .../java/eu/faircode/email/HtmlHelper.java | 37 +++++++------------ app/src/main/java/eu/faircode/email/Log.java | 7 +--- .../java/eu/faircode/email/ServiceSend.java | 5 ++- .../java/eu/faircode/email/ServiceUI.java | 5 ++- 7 files changed, 55 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 9b1fe58b9a..7abaf81759 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -1499,11 +1499,13 @@ class Core { String body = parts.getHtml(context); File file = message.getFile(context); Helper.writeText(file, body); + String text = HtmlHelper.getFullText(body); + String preview = HtmlHelper.getPreviewText(text); db.message().setMessageContent(message.id, true, - HtmlHelper.getLanguage(context, body), + HtmlHelper.getLanguage(context, text), parts.isPlainOnly(), - HtmlHelper.getPreview(body), + preview, parts.getWarnings(message.warning)); MessageClassifier.classify(message, folder, null, context); @@ -2208,11 +2210,13 @@ class Core { String body = parts.getHtml(context); File file = message.getFile(context); Helper.writeText(file, body); + String text = HtmlHelper.getFullText(body); + String preview = HtmlHelper.getPreviewText(text); db.message().setMessageContent(message.id, true, - HtmlHelper.getLanguage(context, body), + HtmlHelper.getLanguage(context, text), parts.isPlainOnly(), - HtmlHelper.getPreview(body), + preview, parts.getWarnings(message.warning)); for (EntityAttachment attachment : parts.getAttachments()) @@ -3011,11 +3015,13 @@ class Core { String body = parts.getHtml(context); File file = message.getFile(context); Helper.writeText(file, body); + String text = HtmlHelper.getFullText(body); + String preview = HtmlHelper.getPreviewText(text); db.message().setMessageContent(message.id, true, - HtmlHelper.getLanguage(context, body), + HtmlHelper.getLanguage(context, text), parts.isPlainOnly(), - HtmlHelper.getPreview(body), + preview, parts.getWarnings(message.warning)); MessageClassifier.classify(message, folder, null, context); @@ -3447,11 +3453,13 @@ class Core { String body = parts.getHtml(context); File file = message.getFile(context); Helper.writeText(file, body); + String text = HtmlHelper.getFullText(body); + String preview = HtmlHelper.getPreviewText(text); db.message().setMessageContent(message.id, true, - HtmlHelper.getLanguage(context, body), + HtmlHelper.getLanguage(context, text), parts.isPlainOnly(), - HtmlHelper.getPreview(body), + preview, parts.getWarnings(message.warning)); MessageClassifier.classify(message, folder, null, context); diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index 8dcf13c993..002454cff5 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -611,11 +611,13 @@ public class EntityRule { File file = reply.getFile(context); Helper.writeText(file, body); + String text = HtmlHelper.getFullText(body); + String preview = HtmlHelper.getPreviewText(text); db.message().setMessageContent(reply.id, true, - HtmlHelper.getLanguage(context, body), + HtmlHelper.getLanguage(context, text), false, - HtmlHelper.getPreview(body), + preview, null); if (attachments) @@ -700,7 +702,9 @@ public class EntityRule { .append(' ').append(message.subject).append(". "); String body = Helper.readText(message.getFile(context)); - String preview = HtmlHelper.getPreview(body); + String text = HtmlHelper.getFullText(body); + String preview = HtmlHelper.getPreviewText(text); + if (!TextUtils.isEmpty(preview)) sb.append(res.getString(R.string.title_rule_tts_content)) .append(' ').append(preview); diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 9ef8e420f7..dc279523bf 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -3924,11 +3924,13 @@ public class FragmentCompose extends FragmentBase { Helper.writeText(data.draft.getFile(context), html); Helper.writeText(data.draft.getFile(context, data.draft.revision), html); + String text = HtmlHelper.getFullText(html); + String preview = HtmlHelper.getPreviewText(text); db.message().setMessageContent(data.draft.id, true, - HtmlHelper.getLanguage(context, html), + HtmlHelper.getLanguage(context, text), data.draft.plain_only, - HtmlHelper.getPreview(html), + preview, null); if ("participation".equals(action)) { @@ -4077,11 +4079,13 @@ public class FragmentCompose extends FragmentBase { Helper.writeText(file, html); Helper.writeText(data.draft.getFile(context, data.draft.revision), html); + String text = HtmlHelper.getFullText(html); + String preview = HtmlHelper.getPreviewText(text); db.message().setMessageContent(data.draft.id, true, - HtmlHelper.getLanguage(context, html), + HtmlHelper.getLanguage(context, text), data.draft.plain_only, - HtmlHelper.getPreview(html), + preview, null); } else EntityOperation.queue(context, data.draft, EntityOperation.BODY); @@ -4593,11 +4597,13 @@ public class FragmentCompose extends FragmentBase { Helper.writeText(draft.getFile(context), body); + String full = HtmlHelper.getFullText(body); + String preview = HtmlHelper.getPreviewText(full); db.message().setMessageContent(draft.id, true, - HtmlHelper.getLanguage(context, body), + HtmlHelper.getLanguage(context, full), draft.plain_only, // unchanged - HtmlHelper.getPreview(body), + preview, null); db.message().setMessageRevision(draft.id, draft.revision); diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 5b5f925542..f7264c3b24 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -1709,14 +1709,13 @@ public class HtmlHelper { Log.d(document.head().html()); } - static String getLanguage(Context context, String body) { + static String getLanguage(Context context, String text) { try { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean language_detection = prefs.getBoolean("language_detection", false); if (!language_detection) return null; - String text = getFullText(body); Locale locale = TextHelper.detectLanguage(context, text); return (locale == null ? null : locale.getLanguage()); } catch (Throwable ex) { @@ -1725,25 +1724,22 @@ public class HtmlHelper { } } - static String getPreview(String body) { - try { - if (body == null) - return null; - Document d = JsoupEx.parse(body); - return _getText(d, false); - } catch (OutOfMemoryError ex) { - Log.e(ex); + static String getPreviewText(String text) { + if (text == null) return null; - } + + String preview = text + .replace("\u200C", "") // Zero-width non-joiner + .replaceAll("\\s+", " "); + return truncate(preview, PREVIEW_SIZE); } - @Deprecated static String getFullText(String body) { try { if (body == null) return null; Document d = JsoupEx.parse(body); - return _getText(d, true); + return _getText(d); } catch (OutOfMemoryError ex) { Log.e(ex); return null; @@ -1753,29 +1749,22 @@ public class HtmlHelper { static String getFullText(File file) throws IOException { try { Document d = JsoupEx.parse(file); - return _getText(d, true); + return _getText(d); } catch (OutOfMemoryError ex) { Log.e(ex); return null; } } - private static String _getText(Document d, boolean full) { - truncate(d, !full); + private static String _getText(Document d) { + truncate(d, false); for (Element bq : d.select("blockquote")) { bq.prependChild(new TextNode("[")); bq.appendChild(new TextNode("]")); } - String text = d.text(); - if (full) - return text; - - String preview = text - .replace("\u200C", "") // Zero-width non-joiner - .replaceAll("\\s+", " "); - return truncate(preview, PREVIEW_SIZE); + return d.text(); } static String truncate(String text, int at) { diff --git a/app/src/main/java/eu/faircode/email/Log.java b/app/src/main/java/eu/faircode/email/Log.java index 44a465e0bf..fc506177bb 100644 --- a/app/src/main/java/eu/faircode/email/Log.java +++ b/app/src/main/java/eu/faircode/email/Log.java @@ -1251,12 +1251,7 @@ public class Log { File file = draft.getFile(context); Helper.writeText(file, body); - db.message().setMessageContent(draft.id, - true, - HtmlHelper.getLanguage(context, body), - false, - HtmlHelper.getPreview(body), - null); + db.message().setMessageContent(draft.id, true, null, false, null, null); attachSettings(context, draft.id, 1); attachAccounts(context, draft.id, 2); diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index 51c5abe7a4..abe43f02b8 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -538,8 +538,9 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar if (plain != null && plain) body = body.replace("
", "
"); - String language = HtmlHelper.getLanguage(this, body); - String preview = HtmlHelper.getPreview(body); + String text = HtmlHelper.getFullText(body); + String language = HtmlHelper.getLanguage(this, text); + String preview = HtmlHelper.getPreviewText(text); try { db.beginTransaction(); diff --git a/app/src/main/java/eu/faircode/email/ServiceUI.java b/app/src/main/java/eu/faircode/email/ServiceUI.java index 7318ca5891..b7b5937c43 100644 --- a/app/src/main/java/eu/faircode/email/ServiceUI.java +++ b/app/src/main/java/eu/faircode/email/ServiceUI.java @@ -288,8 +288,9 @@ public class ServiceUI extends IntentService { if (body != null) body = "

" + body.replaceAll("\\r?\\n", "
") + "

"; - String language = HtmlHelper.getLanguage(this, body); - String preview = HtmlHelper.getPreview(body); + String text = HtmlHelper.getFullText(body); + String language = HtmlHelper.getLanguage(this, text); + String preview = HtmlHelper.getPreviewText(text); DB db = DB.getInstance(this); try {