Cache html, fixes

pull/147/head
M66B 7 years ago
parent 19fd778c83
commit e0f4294e01

@ -697,8 +697,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
boolean show_expanded = properties.getValue("expanded", message.id); boolean show_expanded = properties.getValue("expanded", message.id);
if (show_expanded) if (show_expanded)
bindExpanded(message); bindExpanded(message);
else else {
properties.setBody(message.id, null); properties.setBody(message.id, null);
properties.setHtml(message.id, null);
}
} }
} }
@ -918,15 +920,12 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}.execute(context, owner, sargs, "message:actions"); }.execute(context, owner, sargs, "message:actions");
// Message text // Message text
Spanned body = properties.getBody(message.id);
tvBody.setText(body);
tvBody.setMovementMethod(null);
if (internet || message.content) if (internet || message.content)
pbBody.setVisibility(View.VISIBLE); pbBody.setVisibility(View.VISIBLE);
else else
tvNoInternetBody.setVisibility(View.VISIBLE); tvNoInternetBody.setVisibility(View.VISIBLE);
if (body == null && message.content) if (message.content)
if (show_html) if (show_html)
onShowHtmlConfirmed(message); onShowHtmlConfirmed(message);
else { else {
@ -1269,6 +1268,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
}; };
webView.setId(vwBody.getId()); webView.setId(vwBody.getId());
webView.setVisibility(vwBody.getVisibility());
ConstraintLayout cl = (ConstraintLayout) itemView; ConstraintLayout cl = (ConstraintLayout) itemView;
cl.removeView(vwBody); cl.removeView(vwBody);
@ -1278,7 +1278,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
final WebView webView = (WebView) vwBody; final WebView webView = (WebView) vwBody;
webView.setVisibility(View.INVISIBLE); webView.loadUrl("about:blank");
WebSettings settings = webView.getSettings(); WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true); settings.setJavaScriptEnabled(true);
@ -1289,18 +1289,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
webView.setWebViewClient(new WebViewClient() { webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageCommitVisible(WebView view, String url) {
pbBody.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
pbBody.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) { public boolean shouldOverrideUrlLoading(WebView view, String url) {
Helper.view(context, owner, Uri.parse(url), true); Helper.view(context, owner, Uri.parse(url), true);
return true; return true;
@ -1356,63 +1344,75 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
}); });
Bundle args = new Bundle(); String html = properties.getHtml(message.id);
args.putLong("id", message.id); if (TextUtils.isEmpty(html)) {
Bundle args = new Bundle();
new SimpleTask<String>() { args.putLong("id", message.id);
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
String html = Helper.readText(EntityMessage.getFile(context, id)); new SimpleTask<String>() {
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
Document doc = Jsoup.parse(html); String html = Helper.readText(EntityMessage.getFile(context, id));
for (Element img : doc.select("img"))
try { Document doc = Jsoup.parse(html);
String src = img.attr("src"); for (Element img : doc.select("img"))
if (src.startsWith("cid:")) { try {
String cid = src.substring(4); String src = img.attr("src");
EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid); if (src.startsWith("cid:")) {
if (attachment != null && attachment.available) { String cid = src.substring(4);
InputStream is = null; EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid);
try { if (attachment != null && attachment.available) {
File file = EntityAttachment.getFile(context, attachment.id); InputStream is = null;
try {
File file = EntityAttachment.getFile(context, attachment.id);
is = new BufferedInputStream(new FileInputStream(file)); is = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[(int) file.length()]; byte[] bytes = new byte[(int) file.length()];
if (is.read(bytes) != bytes.length) if (is.read(bytes) != bytes.length)
throw new IOException("length"); throw new IOException("length");
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("data:"); sb.append("data:");
sb.append(attachment.type); sb.append(attachment.type);
sb.append(";base64,"); sb.append(";base64,");
sb.append(Base64.encodeToString(bytes, Base64.DEFAULT)); sb.append(Base64.encodeToString(bytes, Base64.DEFAULT));
img.attr("src", sb.toString()); img.attr("src", sb.toString());
} finally { } finally {
if (is != null) if (is != null)
is.close(); is.close();
}
} }
} }
} catch (Throwable ex) {
Log.e(ex);
} }
} catch (Throwable ex) {
Log.e(ex);
}
return doc.html(); return doc.html();
} }
@Override @Override
protected void onExecuted(Bundle args, String html) { protected void onExecuted(Bundle args, String html) {
webView.loadDataWithBaseURL("email://", html, "text/html", "UTF-8", null); properties.setHtml(message.id, html);
} webView.loadDataWithBaseURL("email://", html, "text/html", "UTF-8", null);
@Override boolean expanded = properties.getValue("expanded", message.id);
protected void onException(Bundle args, Throwable ex) { pbBody.setVisibility(View.GONE);
Helper.unexpectedError(context, owner, ex); webView.setVisibility(expanded ? View.VISIBLE : View.GONE);
} }
}.execute(context, owner, args, "message:webview");
@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) { private void onShowQuotes(final TupleMessageEx message) {
@ -1463,6 +1463,14 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private SimpleTask<SpannableStringBuilder> bodyTask = new SimpleTask<SpannableStringBuilder>() { private SimpleTask<SpannableStringBuilder> bodyTask = new SimpleTask<SpannableStringBuilder>() {
private String body = null; 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 @Override
protected SpannableStringBuilder onExecute(Context context, final Bundle args) { protected SpannableStringBuilder onExecute(Context context, final Bundle args) {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
@ -2657,6 +2665,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
Spanned getBody(long id); Spanned getBody(long id);
void setHtml(long id, String html);
String getHtml(long id);
void scrollTo(int pos, int dy); void scrollTo(int pos, int dy);
void move(long id, String target, boolean type); void move(long id, String target, boolean type);

@ -143,6 +143,7 @@ public class FragmentMessages extends FragmentBase {
private boolean autoExpanded = true; private boolean autoExpanded = true;
private Map<String, List<Long>> values = new HashMap<>(); private Map<String, List<Long>> values = new HashMap<>();
private LongSparseArray<Spanned> bodies = new LongSparseArray<>(); private LongSparseArray<Spanned> bodies = new LongSparseArray<>();
private LongSparseArray<String> html = new LongSparseArray<>();
private LongSparseArray<TupleAccountSwipes> accountSwipes = new LongSparseArray<>(); private LongSparseArray<TupleAccountSwipes> accountSwipes = new LongSparseArray<>();
private BoundaryCallbackMessages boundaryCallback = null; private BoundaryCallbackMessages boundaryCallback = null;
@ -555,11 +556,11 @@ public class FragmentMessages extends FragmentBase {
} }
@Override @Override
public void setBody(long id, Spanned body) { public void setBody(long id, Spanned value) {
if (body == null) if (value == null)
bodies.remove(id); bodies.remove(id);
else else
bodies.put(id, body); bodies.put(id, value);
} }
@Override @Override
@ -567,6 +568,19 @@ public class FragmentMessages extends FragmentBase {
return bodies.get(id); 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 @Override
public void scrollTo(final int pos, final int dy) { public void scrollTo(final int pos, final int dy) {
new Handler().post(new Runnable() { new Handler().post(new Runnable() {

Loading…
Cancel
Save