diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 1880b5cf1d..d4d129b542 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -697,8 +697,10 @@ public class AdapterMessage extends RecyclerView.Adapter() { - @Override - protected String onExecute(Context context, Bundle args) throws Throwable { - long id = args.getLong("id"); + String html = properties.getHtml(message.id); + if (TextUtils.isEmpty(html)) { + Bundle args = new Bundle(); + args.putLong("id", message.id); - String html = Helper.readText(EntityMessage.getFile(context, id)); + new SimpleTask() { + @Override + protected String onExecute(Context context, Bundle args) throws Throwable { + long id = args.getLong("id"); - Document doc = Jsoup.parse(html); - for (Element img : doc.select("img")) - try { - String src = img.attr("src"); - if (src.startsWith("cid:")) { - String cid = src.substring(4); - EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid); - if (attachment != null && attachment.available) { - InputStream is = null; - try { - File file = EntityAttachment.getFile(context, attachment.id); + String html = Helper.readText(EntityMessage.getFile(context, id)); + + Document doc = Jsoup.parse(html); + for (Element img : doc.select("img")) + try { + String src = img.attr("src"); + if (src.startsWith("cid:")) { + String cid = src.substring(4); + EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid); + if (attachment != null && attachment.available) { + InputStream is = null; + try { + File file = EntityAttachment.getFile(context, attachment.id); - is = new BufferedInputStream(new FileInputStream(file)); - byte[] bytes = new byte[(int) file.length()]; - if (is.read(bytes) != bytes.length) - throw new IOException("length"); + is = new BufferedInputStream(new FileInputStream(file)); + byte[] bytes = new byte[(int) file.length()]; + if (is.read(bytes) != bytes.length) + throw new IOException("length"); - StringBuilder sb = new StringBuilder(); - sb.append("data:"); - sb.append(attachment.type); - sb.append(";base64,"); - sb.append(Base64.encodeToString(bytes, Base64.DEFAULT)); + StringBuilder sb = new StringBuilder(); + sb.append("data:"); + sb.append(attachment.type); + sb.append(";base64,"); + sb.append(Base64.encodeToString(bytes, Base64.DEFAULT)); - img.attr("src", sb.toString()); - } finally { - if (is != null) - is.close(); + img.attr("src", sb.toString()); + } finally { + if (is != null) + is.close(); + } } } + } catch (Throwable ex) { + Log.e(ex); } - } catch (Throwable ex) { - Log.e(ex); - } - return doc.html(); - } + return doc.html(); + } - @Override - protected void onExecuted(Bundle args, String html) { - webView.loadDataWithBaseURL("email://", html, "text/html", "UTF-8", null); - } + @Override + protected void onExecuted(Bundle args, String html) { + properties.setHtml(message.id, html); + webView.loadDataWithBaseURL("email://", html, "text/html", "UTF-8", null); - @Override - protected void onException(Bundle args, Throwable ex) { - Helper.unexpectedError(context, owner, ex); - } - }.execute(context, owner, args, "message:webview"); + boolean expanded = properties.getValue("expanded", message.id); + pbBody.setVisibility(View.GONE); + webView.setVisibility(expanded ? View.VISIBLE : View.GONE); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(context, owner, ex); + } + }.execute(context, owner, args, "message:webview"); + } else { + webView.loadDataWithBaseURL("email://", html, "text/html", "UTF-8", null); + pbBody.setVisibility(View.GONE); + webView.setVisibility(View.VISIBLE); + } } private void onShowQuotes(final TupleMessageEx message) { @@ -1463,6 +1463,14 @@ public class AdapterMessage extends RecyclerView.Adapter bodyTask = new SimpleTask() { private String body = null; + @Override + protected void onPreExecute(Bundle args) { + TupleMessageEx message = (TupleMessageEx) args.getSerializable("message"); + Spanned body = properties.getBody(message.id); + tvBody.setText(body); + tvBody.setMovementMethod(null); + } + @Override protected SpannableStringBuilder onExecute(Context context, final Bundle args) { DB db = DB.getInstance(context); @@ -2657,6 +2665,10 @@ public class AdapterMessage extends RecyclerView.Adapter> values = new HashMap<>(); private LongSparseArray bodies = new LongSparseArray<>(); + private LongSparseArray html = new LongSparseArray<>(); private LongSparseArray accountSwipes = new LongSparseArray<>(); private BoundaryCallbackMessages boundaryCallback = null; @@ -555,11 +556,11 @@ public class FragmentMessages extends FragmentBase { } @Override - public void setBody(long id, Spanned body) { - if (body == null) + public void setBody(long id, Spanned value) { + if (value == null) bodies.remove(id); else - bodies.put(id, body); + bodies.put(id, value); } @Override @@ -567,6 +568,19 @@ public class FragmentMessages extends FragmentBase { return bodies.get(id); } + @Override + public void setHtml(long id, String value) { + if (value == null) + html.remove(id); + else + html.put(id, value); + } + + @Override + public String getHtml(long id) { + return html.get(id); + } + @Override public void scrollTo(final int pos, final int dy) { new Handler().post(new Runnable() {