Save memory on previewing

pull/174/head
M66B 5 years ago
parent 88f5a3ec86
commit 232f2726bb

@ -1124,11 +1124,12 @@ class Core {
MessageHelper helper = new MessageHelper((MimeMessage) imessage); MessageHelper helper = new MessageHelper((MimeMessage) imessage);
MessageHelper.MessageParts parts = helper.getMessageParts(context); MessageHelper.MessageParts parts = helper.getMessageParts(context);
String body = parts.getHtml(context); String body = parts.getHtml(context);
Helper.writeText(message.getFile(context), body); File file = message.getFile(context);
Helper.writeText(file, body);
db.message().setMessageContent(message.id, db.message().setMessageContent(message.id,
true, true,
parts.isPlainOnly(), parts.isPlainOnly(),
HtmlHelper.getPreview(body), HtmlHelper.getPreview(file),
parts.getWarnings(message.warning)); parts.getWarnings(message.warning));
} }
@ -1577,11 +1578,12 @@ class Core {
} }
String body = parts.getHtml(context); String body = parts.getHtml(context);
Helper.writeText(message.getFile(context), body); File file = message.getFile(context);
Helper.writeText(file, body);
db.message().setMessageContent(message.id, db.message().setMessageContent(message.id,
true, true,
parts.isPlainOnly(), parts.isPlainOnly(),
HtmlHelper.getPreview(body), HtmlHelper.getPreview(file),
parts.getWarnings(message.warning)); parts.getWarnings(message.warning));
for (EntityAttachment attachment : parts.getAttachments()) for (EntityAttachment attachment : parts.getAttachments())
@ -2316,11 +2318,12 @@ class Core {
if (message.size < maxSize) { if (message.size < maxSize) {
String body = parts.getHtml(context); String body = parts.getHtml(context);
Helper.writeText(message.getFile(context), body); File file = message.getFile(context);
Helper.writeText(file, body);
db.message().setMessageContent(message.id, db.message().setMessageContent(message.id,
true, true,
parts.isPlainOnly(), parts.isPlainOnly(),
HtmlHelper.getPreview(body), HtmlHelper.getPreview(file),
parts.getWarnings(message.warning)); parts.getWarnings(message.warning));
Log.i(folder.name + " inline downloaded message id=" + message.id + Log.i(folder.name + " inline downloaded message id=" + message.id +
" size=" + message.size + "/" + (body == null ? null : body.length())); " size=" + message.size + "/" + (body == null ? null : body.length()));
@ -2649,11 +2652,12 @@ class Core {
if (state.getNetworkState().isUnmetered() || if (state.getNetworkState().isUnmetered() ||
(message.size != null && message.size < maxSize)) { (message.size != null && message.size < maxSize)) {
String body = parts.getHtml(context); String body = parts.getHtml(context);
Helper.writeText(message.getFile(context), body); File file = message.getFile(context);
Helper.writeText(file, body);
db.message().setMessageContent(message.id, db.message().setMessageContent(message.id,
true, true,
parts.isPlainOnly(), parts.isPlainOnly(),
HtmlHelper.getPreview(body), HtmlHelper.getPreview(file),
parts.getWarnings(message.warning)); parts.getWarnings(message.warning));
Log.i(folder.name + " downloaded message id=" + message.id + Log.i(folder.name + " downloaded message id=" + message.id +
" size=" + message.size + "/" + (body == null ? null : body.length())); " size=" + message.size + "/" + (body == null ? null : body.length()));
@ -3212,8 +3216,8 @@ class Core {
String preview = message.preview; String preview = message.preview;
if (notify_preview_all) if (notify_preview_all)
try { try {
String html = Helper.readText(message.getFile(context)); File file = message.getFile(context);
preview = HtmlHelper.getFullText(html); preview = HtmlHelper.getFullText(file);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }

@ -35,6 +35,7 @@ import androidx.room.PrimaryKey;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -411,11 +412,12 @@ public class EntityRule {
reply.id = db.message().insertMessage(reply); reply.id = db.message().insertMessage(reply);
String body = answer.getText(message.from); String body = answer.getText(message.from);
Helper.writeText(reply.getFile(context), body); File file = reply.getFile(context);
Helper.writeText(file, body);
db.message().setMessageContent(reply.id, db.message().setMessageContent(reply.id,
true, true,
false, false,
HtmlHelper.getPreview(body), HtmlHelper.getPreview(file),
null); null);
EntityOperation.queue(context, reply, EntityOperation.SEND); EntityOperation.queue(context, reply, EntityOperation.SEND);

@ -1056,30 +1056,53 @@ public class HtmlHelper {
Log.i(document.head().html()); Log.i(document.head().html());
} }
static String getPreview(File file) throws IOException {
try {
Document d = JsoupEx.parse(file);
return _getText(d, false);
} catch (OutOfMemoryError ex) {
Log.e(ex);
return null;
}
}
@Deprecated
static String getPreview(String body) { static String getPreview(String body) {
try { try {
return _getText(body, false); if (body == null)
return null;
Document d = JsoupEx.parse(body);
return _getText(d, false);
} catch (OutOfMemoryError ex) { } catch (OutOfMemoryError ex) {
Log.e(ex); Log.e(ex);
return null; return null;
} }
} }
@Deprecated
static String getFullText(String body) { static String getFullText(String body) {
try { try {
return _getText(body, true); if (body == null)
return null;
Document d = JsoupEx.parse(body);
return _getText(d, true);
} catch (OutOfMemoryError ex) { } catch (OutOfMemoryError ex) {
Log.e(ex); Log.e(ex);
return null; return null;
} }
} }
private static String _getText(String body, boolean full) { static String getFullText(File file) throws IOException {
if (body == null) try {
Document d = JsoupEx.parse(file);
return _getText(d, true);
} catch (OutOfMemoryError ex) {
Log.e(ex);
return null; return null;
}
}
Document d = JsoupEx.parse(body); private static String _getText(Document d, boolean full) {
truncate(d, !full); truncate(d, !full);
String text = d.text(); String text = d.text();

@ -701,11 +701,13 @@ public class Log {
draft.seen = true; draft.seen = true;
draft.ui_seen = true; draft.ui_seen = true;
draft.id = db.message().insertMessage(draft); draft.id = db.message().insertMessage(draft);
Helper.writeText(draft.getFile(context), body);
File file = draft.getFile(context);
Helper.writeText(file, body);
db.message().setMessageContent(draft.id, db.message().setMessageContent(draft.id,
true, true,
false, false,
HtmlHelper.getPreview(body), HtmlHelper.getPreview(file),
null); null);
attachSettings(context, draft.id, 1); attachSettings(context, draft.id, 1);

@ -39,6 +39,7 @@ import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -392,11 +393,12 @@ public class ServiceSend extends ServiceBase {
message.error = null; message.error = null;
message.id = db.message().insertMessage(message); message.id = db.message().insertMessage(message);
Helper.writeText(EntityMessage.getFile(this, message.id), body); File file = EntityMessage.getFile(this, message.id);
Helper.writeText(file, body);
db.message().setMessageContent(message.id, db.message().setMessageContent(message.id,
true, true,
parts.isPlainOnly(), parts.isPlainOnly(),
HtmlHelper.getPreview(body), HtmlHelper.getPreview(file),
parts.getWarnings(message.warning)); parts.getWarnings(message.warning));
EntityAttachment.copy(this, id, message.id); EntityAttachment.copy(this, id, message.id);

@ -34,6 +34,7 @@ import androidx.core.app.AlarmManagerCompat;
import androidx.core.app.RemoteInput; import androidx.core.app.RemoteInput;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
@ -278,11 +279,14 @@ public class ServiceUI extends IntentService {
reply.seen = true; reply.seen = true;
reply.ui_seen = true; reply.ui_seen = true;
reply.id = db.message().insertMessage(reply); reply.id = db.message().insertMessage(reply);
Helper.writeText(reply.getFile(this), text);
File file = reply.getFile(this);
Helper.writeText(file, text);
db.message().setMessageContent(reply.id, db.message().setMessageContent(reply.id,
true, true,
plain_only || ref.plain_only, plain_only || ref.plain_only,
HtmlHelper.getPreview(text), HtmlHelper.getPreview(file),
null); null);
EntityOperation.queue(this, reply, EntityOperation.SEND); EntityOperation.queue(this, reply, EntityOperation.SEND);

@ -70,8 +70,7 @@ public class WorkerFts extends Worker {
Log.i("FTS index=" + message.id); Log.i("FTS index=" + message.id);
File file = message.getFile(getApplicationContext()); File file = message.getFile(getApplicationContext());
String html = Helper.readText(file); String text = HtmlHelper.getFullText(file);
String text = HtmlHelper.getFullText(html);
try { try {
sdb.beginTransaction(); sdb.beginTransaction();
FtsDbHelper.insert(sdb, message, text); FtsDbHelper.insert(sdb, message, text);

Loading…
Cancel
Save