2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
2018-08-14 05:53:24 +00:00
|
|
|
This file is part of FairEmail.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
2018-08-02 13:33:06 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-02 13:33:06 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-14 09:11:45 +00:00
|
|
|
import android.content.Context;
|
2019-03-10 18:03:05 +00:00
|
|
|
import android.content.SharedPreferences;
|
2018-12-14 09:11:45 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2019-02-19 07:29:03 +00:00
|
|
|
import android.net.Uri;
|
2019-02-10 12:01:21 +00:00
|
|
|
import android.text.Html;
|
|
|
|
import android.text.Spanned;
|
2018-12-14 09:11:45 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Base64;
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.Jsoup;
|
2019-03-10 19:39:17 +00:00
|
|
|
import org.jsoup.nodes.Attribute;
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.nodes.Document;
|
2018-08-28 09:10:31 +00:00
|
|
|
import org.jsoup.nodes.Element;
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.nodes.Node;
|
|
|
|
import org.jsoup.nodes.TextNode;
|
2019-03-12 09:04:16 +00:00
|
|
|
import org.jsoup.safety.Cleaner;
|
2018-08-28 12:52:33 +00:00
|
|
|
import org.jsoup.safety.Whitelist;
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.select.NodeTraversor;
|
|
|
|
import org.jsoup.select.NodeVisitor;
|
|
|
|
|
2019-01-16 13:42:17 +00:00
|
|
|
import java.io.BufferedOutputStream;
|
2018-12-14 09:11:45 +00:00
|
|
|
import java.io.File;
|
2019-01-27 12:01:41 +00:00
|
|
|
import java.io.FileNotFoundException;
|
2018-12-14 09:11:45 +00:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2019-01-16 13:42:17 +00:00
|
|
|
import java.io.OutputStream;
|
2018-12-14 09:11:45 +00:00
|
|
|
import java.net.URL;
|
2019-01-05 11:17:33 +00:00
|
|
|
import java.util.Arrays;
|
2019-03-14 19:33:01 +00:00
|
|
|
import java.util.Collections;
|
2019-01-05 11:17:33 +00:00
|
|
|
import java.util.List;
|
2018-08-05 08:53:43 +00:00
|
|
|
import java.util.regex.Matcher;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-02-10 12:01:21 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.core.text.HtmlCompat;
|
2019-03-09 07:07:01 +00:00
|
|
|
import androidx.core.util.PatternsCompat;
|
2019-03-15 13:54:25 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2019-02-10 12:01:21 +00:00
|
|
|
|
2019-03-06 10:39:10 +00:00
|
|
|
import static androidx.core.text.HtmlCompat.FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM;
|
2019-02-10 12:01:21 +00:00
|
|
|
import static androidx.core.text.HtmlCompat.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE;
|
|
|
|
|
2018-09-12 11:54:16 +00:00
|
|
|
public class HtmlHelper {
|
2019-02-19 16:21:25 +00:00
|
|
|
static final int PREVIEW_SIZE = 250;
|
|
|
|
|
2019-03-12 11:49:13 +00:00
|
|
|
private static final int TRACKING_PIXEL_SURFACE = 25;
|
2019-03-14 19:33:01 +00:00
|
|
|
private static final List<String> heads = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
"h1", "h2", "h3", "h4", "h5", "h6", "p", "ol", "ul", "table", "br", "hr"));
|
|
|
|
private static final List<String> tails = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
"h1", "h2", "h3", "h4", "h5", "h6", "p", "ol", "ul", "li"));
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-03-10 18:03:05 +00:00
|
|
|
static String removeTracking(Context context, String html) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2019-03-30 09:59:38 +00:00
|
|
|
boolean paranoid = prefs.getBoolean("paranoid", true);
|
|
|
|
if (!paranoid)
|
2019-03-10 18:03:05 +00:00
|
|
|
return html;
|
|
|
|
|
|
|
|
Document document = Jsoup.parse(html);
|
|
|
|
|
2019-03-10 19:39:17 +00:00
|
|
|
// Remove tracking pixels
|
2019-03-12 11:49:13 +00:00
|
|
|
for (Element img : document.select("img"))
|
|
|
|
if (isTrackingPixel(img))
|
2019-03-10 18:03:05 +00:00
|
|
|
img.removeAttr("src");
|
|
|
|
|
2019-03-10 19:39:17 +00:00
|
|
|
// Remove Javascript
|
|
|
|
for (Element e : document.select("*"))
|
2019-03-11 06:49:43 +00:00
|
|
|
for (Attribute a : e.attributes()) {
|
|
|
|
String v = a.getValue();
|
|
|
|
if (v != null && v.trim().toLowerCase().startsWith("javascript:"))
|
2019-03-10 19:39:17 +00:00
|
|
|
e.removeAttr(a.getKey());
|
2019-03-11 06:49:43 +00:00
|
|
|
}
|
2019-03-10 19:39:17 +00:00
|
|
|
|
|
|
|
// Remove scripts
|
|
|
|
document.select("script").remove();
|
|
|
|
|
2019-03-10 18:03:05 +00:00
|
|
|
return document.outerHtml();
|
|
|
|
}
|
|
|
|
|
2019-03-10 17:32:32 +00:00
|
|
|
static String sanitize(Context context, String html, boolean showQuotes) {
|
2019-03-30 09:59:38 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean paranoid = prefs.getBoolean("paranoid", true);
|
|
|
|
|
2019-03-12 09:04:16 +00:00
|
|
|
Document parsed = Jsoup.parse(html);
|
|
|
|
Whitelist whitelist = Whitelist.relaxed()
|
2019-03-12 12:13:53 +00:00
|
|
|
.addTags("hr", "abbr")
|
2019-03-10 15:21:46 +00:00
|
|
|
.removeTags("col", "colgroup", "thead", "tbody")
|
2019-03-12 09:04:16 +00:00
|
|
|
.removeAttributes("table", "width")
|
|
|
|
.removeAttributes("td", "colspan", "rowspan", "width")
|
|
|
|
.removeAttributes("th", "colspan", "rowspan", "width")
|
2018-12-14 09:05:48 +00:00
|
|
|
.addProtocols("img", "src", "cid")
|
2019-03-12 09:04:16 +00:00
|
|
|
.addProtocols("img", "src", "data");
|
|
|
|
final Document document = new Cleaner(whitelist).clean(parsed);
|
2018-11-24 11:27:44 +00:00
|
|
|
|
2019-03-10 15:21:46 +00:00
|
|
|
// Quotes
|
|
|
|
if (!showQuotes)
|
|
|
|
for (Element quote : document.select("blockquote"))
|
|
|
|
quote.html("…");
|
2019-03-10 11:14:39 +00:00
|
|
|
|
2019-03-10 15:21:46 +00:00
|
|
|
// Short quotes
|
|
|
|
for (Element q : document.select("q")) {
|
|
|
|
q.prependText("\"");
|
|
|
|
q.appendText("\"");
|
|
|
|
q.tagName("em");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pre formatted text
|
|
|
|
for (Element code : document.select("pre")) {
|
|
|
|
code.html(code.html().replaceAll("\\r?\\n", "<br />"));
|
|
|
|
code.tagName("div");
|
|
|
|
}
|
2019-03-10 11:14:39 +00:00
|
|
|
|
2019-03-10 15:21:46 +00:00
|
|
|
// Code
|
|
|
|
document.select("code").tagName("div");
|
|
|
|
|
|
|
|
// Lines
|
|
|
|
for (Element hr : document.select("hr")) {
|
|
|
|
hr.tagName("div");
|
2019-03-18 18:23:43 +00:00
|
|
|
hr.text("----------------------------------------");
|
2019-02-10 12:01:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 15:21:46 +00:00
|
|
|
// Descriptions
|
|
|
|
document.select("dl").tagName("div");
|
2019-03-11 08:27:56 +00:00
|
|
|
for (Element dt : document.select("dt")) {
|
|
|
|
dt.tagName("strong");
|
2019-03-10 15:21:46 +00:00
|
|
|
dt.appendElement("br");
|
2019-03-11 08:27:56 +00:00
|
|
|
}
|
|
|
|
for (Element dd : document.select("dd")) {
|
|
|
|
dd.tagName("em");
|
|
|
|
dd.appendElement("br").appendElement("br");
|
|
|
|
}
|
2018-11-24 11:27:44 +00:00
|
|
|
|
2019-03-12 12:13:53 +00:00
|
|
|
// Abbreviations
|
|
|
|
document.select("abbr").tagName("u");
|
|
|
|
|
2019-03-10 15:21:46 +00:00
|
|
|
// Images
|
2018-11-24 11:42:21 +00:00
|
|
|
for (Element img : document.select("img")) {
|
2019-03-13 09:35:33 +00:00
|
|
|
// Get image attributes
|
2019-03-10 17:32:32 +00:00
|
|
|
String src = img.attr("src");
|
|
|
|
String alt = img.attr("alt");
|
2019-03-12 09:04:16 +00:00
|
|
|
String title = img.attr("title");
|
2019-03-30 09:59:38 +00:00
|
|
|
boolean tracking = (paranoid && isTrackingPixel(img));
|
2019-03-10 17:32:32 +00:00
|
|
|
|
2019-03-13 09:35:33 +00:00
|
|
|
// Create image container
|
2019-03-10 15:21:46 +00:00
|
|
|
Element div = document.createElement("div");
|
2019-02-11 08:35:03 +00:00
|
|
|
|
2019-03-13 09:35:33 +00:00
|
|
|
// Remove link tracking pixel
|
|
|
|
if (tracking)
|
|
|
|
img.removeAttr("src");
|
2018-11-24 11:27:44 +00:00
|
|
|
|
2019-03-13 09:35:33 +00:00
|
|
|
// Link image to source
|
|
|
|
Element a = document.createElement("a");
|
|
|
|
a.attr("href", src);
|
|
|
|
a.appendChild(img.clone());
|
|
|
|
div.appendChild(a);
|
|
|
|
|
|
|
|
// Show image title
|
|
|
|
if (!TextUtils.isEmpty(title)) {
|
2019-03-10 15:21:46 +00:00
|
|
|
div.appendElement("br");
|
2019-03-13 09:35:33 +00:00
|
|
|
div.appendElement("em").text(title);
|
2019-03-10 15:21:46 +00:00
|
|
|
}
|
2019-03-13 09:35:33 +00:00
|
|
|
if (!TextUtils.isEmpty(alt)) {
|
2019-03-12 09:04:16 +00:00
|
|
|
div.appendElement("br");
|
2019-03-13 09:35:33 +00:00
|
|
|
div.appendElement("em").text(alt);
|
2019-03-12 09:04:16 +00:00
|
|
|
}
|
2019-03-10 15:21:46 +00:00
|
|
|
|
2019-03-13 09:35:33 +00:00
|
|
|
// Show when tracking pixel
|
2019-03-12 11:49:13 +00:00
|
|
|
if (tracking) {
|
2019-03-10 17:32:32 +00:00
|
|
|
div.appendElement("br");
|
2019-03-12 16:24:29 +00:00
|
|
|
div.appendElement("strong").text(
|
|
|
|
context.getString(R.string.title_hint_tracking_image,
|
|
|
|
img.attr("width"), img.attr("height")));
|
2019-03-10 17:32:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:35:33 +00:00
|
|
|
// Split parent link and linked image
|
|
|
|
boolean linked = false;
|
2019-03-30 09:59:38 +00:00
|
|
|
if (paranoid)
|
|
|
|
for (Element parent : img.parents())
|
|
|
|
if ("a".equals(parent.tagName()) &&
|
|
|
|
!TextUtils.isEmpty(parent.attr("href"))) {
|
|
|
|
String text = parent.attr("title").trim();
|
|
|
|
if (TextUtils.isEmpty(text))
|
|
|
|
text = parent.attr("alt").trim();
|
|
|
|
if (TextUtils.isEmpty(text))
|
|
|
|
text = context.getString(R.string.title_hint_image_link);
|
|
|
|
|
|
|
|
img.remove();
|
|
|
|
parent.appendText(text);
|
|
|
|
String outer = parent.outerHtml();
|
|
|
|
|
|
|
|
parent.tagName("span");
|
2019-03-30 16:21:59 +00:00
|
|
|
for (Attribute attr : parent.attributes().asList())
|
|
|
|
parent.attributes().remove(attr.getKey());
|
2019-03-30 09:59:38 +00:00
|
|
|
parent.html(outer);
|
|
|
|
parent.appendChild(div);
|
|
|
|
|
|
|
|
linked = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-13 09:35:33 +00:00
|
|
|
|
2019-03-15 08:01:52 +00:00
|
|
|
if (!linked) {
|
|
|
|
img.tagName("div");
|
2019-03-30 16:21:59 +00:00
|
|
|
for (Attribute attr : img.attributes().asList())
|
|
|
|
img.attributes().remove(attr.getKey());
|
2019-03-15 08:01:52 +00:00
|
|
|
img.html(div.html());
|
|
|
|
}
|
2019-03-10 15:21:46 +00:00
|
|
|
}
|
2018-12-21 14:19:07 +00:00
|
|
|
|
2019-03-30 16:48:04 +00:00
|
|
|
// Tables
|
|
|
|
for (Element col : document.select("th,td")) {
|
|
|
|
// separate columns by a space
|
|
|
|
if (col.nextElementSibling() == null) {
|
|
|
|
if (col.selectFirst("div") == null)
|
|
|
|
col.appendElement("br");
|
|
|
|
} else
|
|
|
|
col.append(" ");
|
|
|
|
|
|
|
|
if ("th".equals(col.tagName()))
|
|
|
|
col.tagName("strong");
|
|
|
|
else
|
|
|
|
col.tagName("span");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Element row : document.select("tr"))
|
|
|
|
row.tagName("span");
|
|
|
|
|
|
|
|
document.select("caption").tagName("p");
|
|
|
|
document.select("table").tagName("div");
|
|
|
|
|
|
|
|
// Lists
|
|
|
|
for (Element li : document.select("li")) {
|
|
|
|
li.tagName("span");
|
|
|
|
li.prependText("* ");
|
|
|
|
li.appendElement("br"); // line break after list item
|
|
|
|
}
|
|
|
|
document.select("ol").tagName("div");
|
|
|
|
document.select("ul").tagName("div");
|
|
|
|
|
2019-02-10 12:01:21 +00:00
|
|
|
// Autolink
|
2018-09-12 11:54:16 +00:00
|
|
|
NodeTraversor.traverse(new NodeVisitor() {
|
|
|
|
@Override
|
|
|
|
public void head(Node node, int depth) {
|
|
|
|
if (node instanceof TextNode) {
|
2019-02-16 21:55:44 +00:00
|
|
|
TextNode tnode = (TextNode) node;
|
|
|
|
|
2019-03-13 06:25:40 +00:00
|
|
|
Matcher matcher = PatternsCompat.WEB_URL.matcher(tnode.text());
|
2019-03-30 08:21:52 +00:00
|
|
|
if (matcher.find()) {
|
2019-03-12 09:04:16 +00:00
|
|
|
Element span = document.createElement("span");
|
|
|
|
|
|
|
|
int pos = 0;
|
2019-03-13 06:25:40 +00:00
|
|
|
String text = tnode.text();
|
|
|
|
|
2019-03-30 08:21:52 +00:00
|
|
|
do {
|
2019-03-12 09:04:16 +00:00
|
|
|
boolean linked = false;
|
|
|
|
Node parent = tnode.parent();
|
|
|
|
while (parent != null) {
|
|
|
|
if ("a".equals(parent.nodeName())) {
|
|
|
|
linked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
parent = parent.parent();
|
2019-02-19 07:29:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 09:04:16 +00:00
|
|
|
String scheme = Uri.parse(matcher.group()).getScheme();
|
|
|
|
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("Web url=" + matcher.group() + " linked=" + linked + " scheme=" + scheme);
|
2019-02-19 07:29:03 +00:00
|
|
|
|
2019-03-12 09:04:16 +00:00
|
|
|
if (linked || scheme == null)
|
|
|
|
span.appendText(text.substring(pos, matcher.end()));
|
|
|
|
else {
|
|
|
|
span.appendText(text.substring(pos, matcher.start()));
|
2019-02-19 07:29:03 +00:00
|
|
|
|
2019-03-12 09:04:16 +00:00
|
|
|
Element a = document.createElement("a");
|
|
|
|
a.attr("href", matcher.group());
|
|
|
|
a.text(matcher.group());
|
|
|
|
span.appendChild(a);
|
|
|
|
}
|
2019-02-19 07:29:03 +00:00
|
|
|
|
2019-03-12 09:04:16 +00:00
|
|
|
pos = matcher.end();
|
2019-03-30 08:21:52 +00:00
|
|
|
} while (matcher.find());
|
2019-03-12 09:04:16 +00:00
|
|
|
span.appendText(text.substring(pos));
|
2019-02-16 21:55:44 +00:00
|
|
|
|
2019-03-12 09:04:16 +00:00
|
|
|
tnode.before(span);
|
|
|
|
tnode.text("");
|
2018-09-02 11:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 11:54:16 +00:00
|
|
|
}
|
2018-09-02 11:18:32 +00:00
|
|
|
|
2018-09-12 11:54:16 +00:00
|
|
|
@Override
|
|
|
|
public void tail(Node node, int depth) {
|
|
|
|
}
|
2019-03-12 09:04:16 +00:00
|
|
|
}, document);
|
|
|
|
|
|
|
|
// Remove block elements displaying nothing
|
|
|
|
for (Element e : document.select("*"))
|
|
|
|
if (e.isBlock() && !e.hasText() && e.select("img").size() == 0)
|
|
|
|
e.remove();
|
2018-12-14 19:15:07 +00:00
|
|
|
|
2019-03-12 10:13:24 +00:00
|
|
|
Element body = document.body();
|
|
|
|
return (body == null ? "" : body.html());
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-12-14 09:11:45 +00:00
|
|
|
|
|
|
|
static Drawable decodeImage(String source, Context context, long id, boolean show) {
|
2018-12-30 14:35:19 +00:00
|
|
|
int px = Helper.dp2pixels(context, 48);
|
2018-12-14 09:11:45 +00:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(source)) {
|
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
2019-01-27 11:17:58 +00:00
|
|
|
d.setBounds(0, 0, px, px);
|
2018-12-14 09:11:45 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean embedded = source.startsWith("cid:");
|
|
|
|
boolean data = source.startsWith("data:");
|
2019-02-19 07:29:03 +00:00
|
|
|
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("Image show=" + show + " embedded=" + embedded + " data=" + data + " source=" + source);
|
2018-12-14 09:11:45 +00:00
|
|
|
|
2019-01-29 13:29:39 +00:00
|
|
|
if (!show) {
|
|
|
|
// Show placeholder icon
|
|
|
|
int resid = (embedded || data ? R.drawable.baseline_photo_library_24 : R.drawable.baseline_image_24);
|
|
|
|
Drawable d = context.getResources().getDrawable(resid, context.getTheme());
|
|
|
|
d.setBounds(0, 0, px, px);
|
|
|
|
return d;
|
|
|
|
}
|
2018-12-20 15:45:42 +00:00
|
|
|
|
2019-01-29 13:29:39 +00:00
|
|
|
// Embedded images
|
|
|
|
if (embedded) {
|
2019-03-14 07:18:42 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2019-01-29 13:29:39 +00:00
|
|
|
String cid = "<" + source.substring(4) + ">";
|
2019-03-14 07:18:42 +00:00
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(id, cid);
|
2019-01-29 13:29:39 +00:00
|
|
|
if (attachment == null) {
|
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
|
|
|
d.setBounds(0, 0, px, px);
|
|
|
|
return d;
|
|
|
|
} else if (!attachment.available) {
|
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_photo_library_24, context.getTheme());
|
|
|
|
d.setBounds(0, 0, px, px);
|
|
|
|
return d;
|
|
|
|
} else {
|
2019-03-14 07:18:42 +00:00
|
|
|
Bitmap bm = Helper.decodeImage(attachment.getFile(context),
|
2019-01-29 13:29:39 +00:00
|
|
|
context.getResources().getDisplayMetrics().widthPixels);
|
|
|
|
if (bm == null) {
|
2018-12-20 15:45:42 +00:00
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
2019-01-27 11:17:58 +00:00
|
|
|
d.setBounds(0, 0, px, px);
|
2018-12-20 15:45:42 +00:00
|
|
|
return d;
|
2019-02-02 18:35:40 +00:00
|
|
|
} else {
|
|
|
|
Drawable d = new BitmapDrawable(bm);
|
2019-02-04 08:18:18 +00:00
|
|
|
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
|
2019-02-02 18:35:40 +00:00
|
|
|
return d;
|
|
|
|
}
|
2019-01-29 13:29:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-14 09:11:45 +00:00
|
|
|
|
2019-01-29 13:29:39 +00:00
|
|
|
// Data URI
|
|
|
|
if (data)
|
2018-12-14 09:11:45 +00:00
|
|
|
try {
|
2019-01-29 13:29:39 +00:00
|
|
|
// "<img src=\"data:image/png;base64,iVBORw0KGgoAAA" +
|
|
|
|
// "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
|
|
|
|
// "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
|
|
|
|
// "5ErkJggg==\" alt=\"Red dot\" />";
|
2018-12-14 09:11:45 +00:00
|
|
|
|
2019-01-29 13:29:39 +00:00
|
|
|
String base64 = source.substring(source.indexOf(',') + 1);
|
|
|
|
byte[] bytes = Base64.decode(base64.getBytes(), 0);
|
2018-12-14 09:11:45 +00:00
|
|
|
|
2019-01-29 13:29:39 +00:00
|
|
|
Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
|
|
|
if (bm == null)
|
|
|
|
throw new IllegalArgumentException("decode byte array failed");
|
2018-12-14 09:11:45 +00:00
|
|
|
|
|
|
|
Drawable d = new BitmapDrawable(context.getResources(), bm);
|
|
|
|
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
|
|
|
|
return d;
|
2019-01-29 13:29:39 +00:00
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
Log.w(ex);
|
2018-12-14 09:11:45 +00:00
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
2019-01-27 11:17:58 +00:00
|
|
|
d.setBounds(0, 0, px, px);
|
2018-12-14 09:11:45 +00:00
|
|
|
return d;
|
2019-01-29 13:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get cache file name
|
|
|
|
File dir = new File(context.getCacheDir(), "images");
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkdir();
|
2019-01-30 09:10:33 +00:00
|
|
|
File file = new File(dir, id + "_" + Math.abs(source.hashCode()) + ".png");
|
2019-01-29 13:29:39 +00:00
|
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
Log.i("Using cached " + file);
|
2019-01-30 09:10:33 +00:00
|
|
|
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
|
|
|
|
if (bm == null) {
|
|
|
|
Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme());
|
|
|
|
d.setBounds(0, 0, px, px);
|
|
|
|
return d;
|
|
|
|
} else {
|
|
|
|
Drawable d = new BitmapDrawable(bm);
|
|
|
|
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
|
|
|
|
return d;
|
|
|
|
}
|
2019-01-29 13:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
2019-02-22 15:59:23 +00:00
|
|
|
Log.i("Probe " + source);
|
|
|
|
try (InputStream probe = new URL(source).openStream()) {
|
2019-01-29 13:29:39 +00:00
|
|
|
options.inJustDecodeBounds = true;
|
|
|
|
BitmapFactory.decodeStream(probe, null, options);
|
2018-12-14 09:11:45 +00:00
|
|
|
}
|
2019-01-29 13:29:39 +00:00
|
|
|
|
2019-02-22 15:59:23 +00:00
|
|
|
Log.i("Download " + source);
|
2019-01-29 13:29:39 +00:00
|
|
|
Bitmap bm;
|
2019-02-22 15:59:23 +00:00
|
|
|
try (InputStream is = new URL(source).openStream()) {
|
2019-01-29 13:29:39 +00:00
|
|
|
int scaleTo = context.getResources().getDisplayMetrics().widthPixels;
|
2019-02-20 07:47:14 +00:00
|
|
|
int factor = 1;
|
|
|
|
while (options.outWidth / factor > scaleTo)
|
|
|
|
factor *= 2;
|
|
|
|
|
2019-01-29 13:29:39 +00:00
|
|
|
if (factor > 1) {
|
|
|
|
Log.i("Download image factor=" + factor);
|
|
|
|
options.inJustDecodeBounds = false;
|
|
|
|
options.inSampleSize = factor;
|
|
|
|
bm = BitmapFactory.decodeStream(is, null, options);
|
|
|
|
} else
|
|
|
|
bm = BitmapFactory.decodeStream(is);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bm == null)
|
|
|
|
throw new FileNotFoundException("Download image failed");
|
|
|
|
|
|
|
|
Log.i("Downloaded image");
|
|
|
|
|
2019-02-22 15:59:23 +00:00
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
2019-02-20 17:03:05 +00:00
|
|
|
bm.compress(Bitmap.CompressFormat.PNG, 90, os);
|
2019-01-29 13:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create drawable from bitmap
|
|
|
|
Drawable d = new BitmapDrawable(context.getResources(), bm);
|
|
|
|
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
|
|
|
|
return d;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
// Show warning icon
|
|
|
|
Log.w(ex);
|
|
|
|
int res = (ex instanceof IOException && !(ex instanceof FileNotFoundException)
|
|
|
|
? R.drawable.baseline_cloud_off_24
|
|
|
|
: R.drawable.baseline_broken_image_24);
|
|
|
|
Drawable d = context.getResources().getDrawable(res, context.getTheme());
|
2018-12-14 09:11:45 +00:00
|
|
|
d.setBounds(0, 0, px, px);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 11:04:50 +00:00
|
|
|
|
2018-12-24 20:09:47 +00:00
|
|
|
static String getPreview(String body) {
|
|
|
|
String text = (body == null ? null : Jsoup.parse(body).text());
|
2019-01-05 11:17:33 +00:00
|
|
|
return (text == null ? null : text.substring(0, Math.min(text.length(), PREVIEW_SIZE)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static String getText(String html) {
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
NodeTraversor.traverse(new NodeVisitor() {
|
2019-02-10 12:01:21 +00:00
|
|
|
private int qlevel = 0;
|
2019-02-11 15:36:42 +00:00
|
|
|
private int tlevel = 0;
|
2019-02-10 12:01:21 +00:00
|
|
|
|
2019-01-05 11:17:33 +00:00
|
|
|
public void head(Node node, int depth) {
|
2019-02-11 15:36:42 +00:00
|
|
|
if (node instanceof TextNode) {
|
|
|
|
append(((TextNode) node).text());
|
|
|
|
append(" ");
|
|
|
|
} else {
|
2019-01-05 11:17:33 +00:00
|
|
|
String name = node.nodeName();
|
2019-02-10 12:01:21 +00:00
|
|
|
if ("li".equals(name))
|
2019-02-11 15:36:42 +00:00
|
|
|
append("* ");
|
2019-02-10 12:01:21 +00:00
|
|
|
else if ("blockquote".equals(name))
|
|
|
|
qlevel++;
|
|
|
|
|
|
|
|
if (heads.contains(name))
|
|
|
|
newline();
|
2019-01-05 11:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void tail(Node node, int depth) {
|
|
|
|
String name = node.nodeName();
|
2019-02-11 15:36:42 +00:00
|
|
|
if ("a".equals(name)) {
|
|
|
|
append("[");
|
|
|
|
append(node.absUrl("href"));
|
|
|
|
append("] ");
|
|
|
|
} else if ("img".equals(name)) {
|
|
|
|
append("[");
|
|
|
|
append(node.absUrl("src"));
|
|
|
|
append("] ");
|
|
|
|
} else if ("th".equals(name) || "td".equals(name)) {
|
2019-02-10 12:01:21 +00:00
|
|
|
Node next = node.nextSibling();
|
|
|
|
if (next == null || !("th".equals(next.nodeName()) || "td".equals(next.nodeName())))
|
|
|
|
newline();
|
|
|
|
} else if ("blockquote".equals(name))
|
|
|
|
qlevel--;
|
|
|
|
|
2019-01-05 11:17:33 +00:00
|
|
|
if (tails.contains(name))
|
2019-02-10 12:01:21 +00:00
|
|
|
newline();
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:36:42 +00:00
|
|
|
private void append(String text) {
|
|
|
|
if (tlevel != qlevel) {
|
|
|
|
newline();
|
|
|
|
tlevel = qlevel;
|
|
|
|
}
|
|
|
|
sb.append(text);
|
|
|
|
}
|
|
|
|
|
2019-02-10 12:01:21 +00:00
|
|
|
private void newline() {
|
|
|
|
trimEnd(sb);
|
|
|
|
sb.append("\n");
|
|
|
|
for (int i = 0; i < qlevel; i++)
|
|
|
|
sb.append('>');
|
|
|
|
if (qlevel > 0)
|
|
|
|
sb.append(' ');
|
2019-01-05 11:17:33 +00:00
|
|
|
}
|
|
|
|
}, Jsoup.parse(html));
|
|
|
|
|
2019-02-10 12:01:21 +00:00
|
|
|
trimEnd(sb);
|
|
|
|
sb.append("\n");
|
|
|
|
|
2019-01-05 11:17:33 +00:00
|
|
|
return sb.toString();
|
2018-12-24 20:09:47 +00:00
|
|
|
}
|
2019-02-10 12:01:21 +00:00
|
|
|
|
2019-03-12 11:49:13 +00:00
|
|
|
static boolean isTrackingPixel(Element img) {
|
|
|
|
String src = img.attr("src");
|
|
|
|
String width = img.attr("width").trim();
|
|
|
|
String height = img.attr("height").trim();
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(src))
|
|
|
|
return false;
|
|
|
|
if (TextUtils.isEmpty(width) || TextUtils.isEmpty(height))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return (Integer.parseInt(width) * Integer.parseInt(height) <= TRACKING_PIXEL_SURFACE);
|
|
|
|
} catch (NumberFormatException ignored) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 12:01:21 +00:00
|
|
|
static void trimEnd(StringBuilder sb) {
|
|
|
|
int length = sb.length();
|
|
|
|
while (length > 0 && sb.charAt(length - 1) == ' ')
|
|
|
|
length--;
|
|
|
|
sb.setLength(length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Spanned fromHtml(@NonNull String html) {
|
|
|
|
return fromHtml(html, null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Spanned fromHtml(@NonNull String html, @Nullable Html.ImageGetter imageGetter, @Nullable Html.TagHandler tagHandler) {
|
2019-03-26 13:37:04 +00:00
|
|
|
Spanned spanned = HtmlCompat.fromHtml(html, FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM, imageGetter, tagHandler);
|
|
|
|
|
|
|
|
int i = spanned.length();
|
|
|
|
while (i > 1 && spanned.charAt(i - 2) == '\n' && spanned.charAt(i - 1) == '\n')
|
|
|
|
i--;
|
|
|
|
if (i != spanned.length())
|
|
|
|
spanned = (Spanned) spanned.subSequence(0, i);
|
|
|
|
|
|
|
|
return spanned;
|
2019-02-10 12:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static String toHtml(Spanned spanned) {
|
|
|
|
return HtmlCompat.toHtml(spanned, TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|