Optimization

pull/193/head
M66B 4 years ago
parent 8f3bd8b5ce
commit c24f79354e

@ -1499,11 +1499,13 @@ class Core {
String body = parts.getHtml(context); String body = parts.getHtml(context);
File file = message.getFile(context); File file = message.getFile(context);
Helper.writeText(file, body); Helper.writeText(file, body);
String text = HtmlHelper.getFullText(body);
String preview = HtmlHelper.getPreviewText(text);
db.message().setMessageContent(message.id, db.message().setMessageContent(message.id,
true, true,
HtmlHelper.getLanguage(context, body), HtmlHelper.getLanguage(context, text),
parts.isPlainOnly(), parts.isPlainOnly(),
HtmlHelper.getPreview(body), preview,
parts.getWarnings(message.warning)); parts.getWarnings(message.warning));
MessageClassifier.classify(message, folder, null, context); MessageClassifier.classify(message, folder, null, context);
@ -2208,11 +2210,13 @@ class Core {
String body = parts.getHtml(context); String body = parts.getHtml(context);
File file = message.getFile(context); File file = message.getFile(context);
Helper.writeText(file, body); Helper.writeText(file, body);
String text = HtmlHelper.getFullText(body);
String preview = HtmlHelper.getPreviewText(text);
db.message().setMessageContent(message.id, db.message().setMessageContent(message.id,
true, true,
HtmlHelper.getLanguage(context, body), HtmlHelper.getLanguage(context, text),
parts.isPlainOnly(), parts.isPlainOnly(),
HtmlHelper.getPreview(body), preview,
parts.getWarnings(message.warning)); parts.getWarnings(message.warning));
for (EntityAttachment attachment : parts.getAttachments()) for (EntityAttachment attachment : parts.getAttachments())
@ -3011,11 +3015,13 @@ class Core {
String body = parts.getHtml(context); String body = parts.getHtml(context);
File file = message.getFile(context); File file = message.getFile(context);
Helper.writeText(file, body); Helper.writeText(file, body);
String text = HtmlHelper.getFullText(body);
String preview = HtmlHelper.getPreviewText(text);
db.message().setMessageContent(message.id, db.message().setMessageContent(message.id,
true, true,
HtmlHelper.getLanguage(context, body), HtmlHelper.getLanguage(context, text),
parts.isPlainOnly(), parts.isPlainOnly(),
HtmlHelper.getPreview(body), preview,
parts.getWarnings(message.warning)); parts.getWarnings(message.warning));
MessageClassifier.classify(message, folder, null, context); MessageClassifier.classify(message, folder, null, context);
@ -3447,11 +3453,13 @@ class Core {
String body = parts.getHtml(context); String body = parts.getHtml(context);
File file = message.getFile(context); File file = message.getFile(context);
Helper.writeText(file, body); Helper.writeText(file, body);
String text = HtmlHelper.getFullText(body);
String preview = HtmlHelper.getPreviewText(text);
db.message().setMessageContent(message.id, db.message().setMessageContent(message.id,
true, true,
HtmlHelper.getLanguage(context, body), HtmlHelper.getLanguage(context, text),
parts.isPlainOnly(), parts.isPlainOnly(),
HtmlHelper.getPreview(body), preview,
parts.getWarnings(message.warning)); parts.getWarnings(message.warning));
MessageClassifier.classify(message, folder, null, context); MessageClassifier.classify(message, folder, null, context);

@ -611,11 +611,13 @@ public class EntityRule {
File file = reply.getFile(context); File file = reply.getFile(context);
Helper.writeText(file, body); Helper.writeText(file, body);
String text = HtmlHelper.getFullText(body);
String preview = HtmlHelper.getPreviewText(text);
db.message().setMessageContent(reply.id, db.message().setMessageContent(reply.id,
true, true,
HtmlHelper.getLanguage(context, body), HtmlHelper.getLanguage(context, text),
false, false,
HtmlHelper.getPreview(body), preview,
null); null);
if (attachments) if (attachments)
@ -700,7 +702,9 @@ public class EntityRule {
.append(' ').append(message.subject).append(". "); .append(' ').append(message.subject).append(". ");
String body = Helper.readText(message.getFile(context)); 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)) if (!TextUtils.isEmpty(preview))
sb.append(res.getString(R.string.title_rule_tts_content)) sb.append(res.getString(R.string.title_rule_tts_content))
.append(' ').append(preview); .append(' ').append(preview);

@ -3924,11 +3924,13 @@ public class FragmentCompose extends FragmentBase {
Helper.writeText(data.draft.getFile(context), html); Helper.writeText(data.draft.getFile(context), html);
Helper.writeText(data.draft.getFile(context, data.draft.revision), 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, db.message().setMessageContent(data.draft.id,
true, true,
HtmlHelper.getLanguage(context, html), HtmlHelper.getLanguage(context, text),
data.draft.plain_only, data.draft.plain_only,
HtmlHelper.getPreview(html), preview,
null); null);
if ("participation".equals(action)) { if ("participation".equals(action)) {
@ -4077,11 +4079,13 @@ public class FragmentCompose extends FragmentBase {
Helper.writeText(file, html); Helper.writeText(file, html);
Helper.writeText(data.draft.getFile(context, data.draft.revision), 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, db.message().setMessageContent(data.draft.id,
true, true,
HtmlHelper.getLanguage(context, html), HtmlHelper.getLanguage(context, text),
data.draft.plain_only, data.draft.plain_only,
HtmlHelper.getPreview(html), preview,
null); null);
} else } else
EntityOperation.queue(context, data.draft, EntityOperation.BODY); EntityOperation.queue(context, data.draft, EntityOperation.BODY);
@ -4593,11 +4597,13 @@ public class FragmentCompose extends FragmentBase {
Helper.writeText(draft.getFile(context), body); Helper.writeText(draft.getFile(context), body);
String full = HtmlHelper.getFullText(body);
String preview = HtmlHelper.getPreviewText(full);
db.message().setMessageContent(draft.id, db.message().setMessageContent(draft.id,
true, true,
HtmlHelper.getLanguage(context, body), HtmlHelper.getLanguage(context, full),
draft.plain_only, // unchanged draft.plain_only, // unchanged
HtmlHelper.getPreview(body), preview,
null); null);
db.message().setMessageRevision(draft.id, draft.revision); db.message().setMessageRevision(draft.id, draft.revision);

@ -1709,14 +1709,13 @@ public class HtmlHelper {
Log.d(document.head().html()); Log.d(document.head().html());
} }
static String getLanguage(Context context, String body) { static String getLanguage(Context context, String text) {
try { try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean language_detection = prefs.getBoolean("language_detection", false); boolean language_detection = prefs.getBoolean("language_detection", false);
if (!language_detection) if (!language_detection)
return null; return null;
String text = getFullText(body);
Locale locale = TextHelper.detectLanguage(context, text); Locale locale = TextHelper.detectLanguage(context, text);
return (locale == null ? null : locale.getLanguage()); return (locale == null ? null : locale.getLanguage());
} catch (Throwable ex) { } catch (Throwable ex) {
@ -1725,25 +1724,22 @@ public class HtmlHelper {
} }
} }
static String getPreview(String body) { static String getPreviewText(String text) {
try { if (text == null)
if (body == null)
return null;
Document d = JsoupEx.parse(body);
return _getText(d, false);
} catch (OutOfMemoryError ex) {
Log.e(ex);
return 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) { static String getFullText(String body) {
try { try {
if (body == null) if (body == null)
return null; return null;
Document d = JsoupEx.parse(body); Document d = JsoupEx.parse(body);
return _getText(d, true); return _getText(d);
} catch (OutOfMemoryError ex) { } catch (OutOfMemoryError ex) {
Log.e(ex); Log.e(ex);
return null; return null;
@ -1753,29 +1749,22 @@ public class HtmlHelper {
static String getFullText(File file) throws IOException { static String getFullText(File file) throws IOException {
try { try {
Document d = JsoupEx.parse(file); Document d = JsoupEx.parse(file);
return _getText(d, true); return _getText(d);
} catch (OutOfMemoryError ex) { } catch (OutOfMemoryError ex) {
Log.e(ex); Log.e(ex);
return null; return null;
} }
} }
private static String _getText(Document d, boolean full) { private static String _getText(Document d) {
truncate(d, !full); truncate(d, false);
for (Element bq : d.select("blockquote")) { for (Element bq : d.select("blockquote")) {
bq.prependChild(new TextNode("[")); bq.prependChild(new TextNode("["));
bq.appendChild(new TextNode("]")); bq.appendChild(new TextNode("]"));
} }
String text = d.text(); return d.text();
if (full)
return text;
String preview = text
.replace("\u200C", "") // Zero-width non-joiner
.replaceAll("\\s+", " ");
return truncate(preview, PREVIEW_SIZE);
} }
static String truncate(String text, int at) { static String truncate(String text, int at) {

@ -1251,12 +1251,7 @@ public class Log {
File file = draft.getFile(context); File file = draft.getFile(context);
Helper.writeText(file, body); Helper.writeText(file, body);
db.message().setMessageContent(draft.id, db.message().setMessageContent(draft.id, true, null, false, null, null);
true,
HtmlHelper.getLanguage(context, body),
false,
HtmlHelper.getPreview(body),
null);
attachSettings(context, draft.id, 1); attachSettings(context, draft.id, 1);
attachAccounts(context, draft.id, 2); attachAccounts(context, draft.id, 2);

@ -538,8 +538,9 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
if (plain != null && plain) if (plain != null && plain)
body = body.replace("<div x-plain=\"true\">", "<div>"); body = body.replace("<div x-plain=\"true\">", "<div>");
String language = HtmlHelper.getLanguage(this, body); String text = HtmlHelper.getFullText(body);
String preview = HtmlHelper.getPreview(body); String language = HtmlHelper.getLanguage(this, text);
String preview = HtmlHelper.getPreviewText(text);
try { try {
db.beginTransaction(); db.beginTransaction();

@ -288,8 +288,9 @@ public class ServiceUI extends IntentService {
if (body != null) if (body != null)
body = "<p>" + body.replaceAll("\\r?\\n", "<br>") + "</p>"; body = "<p>" + body.replaceAll("\\r?\\n", "<br>") + "</p>";
String language = HtmlHelper.getLanguage(this, body); String text = HtmlHelper.getFullText(body);
String preview = HtmlHelper.getPreview(body); String language = HtmlHelper.getLanguage(this, text);
String preview = HtmlHelper.getPreviewText(text);
DB db = DB.getInstance(this); DB db = DB.getInstance(this);
try { try {

Loading…
Cancel
Save