From 1d82ba058b80c17b75b411cc2a7e4e7cfb3f016c Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 21 Feb 2021 22:27:40 +0100 Subject: [PATCH] Check if images accessible on print --- .../eu/faircode/email/FragmentMessages.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index a77fcdc4db..ae83cba4ab 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -166,6 +166,8 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; import java.math.BigInteger; +import java.net.HttpURLConnection; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.KeyStore; import java.security.PrivateKey; @@ -198,6 +200,9 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Properties; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; import javax.mail.Address; import javax.mail.MessageRemovedException; @@ -355,6 +360,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. private static final long REVIEW_ASK_DELAY = 14 * 24 * 3600 * 1000L; // milliseconds private static final long REVIEW_LATER_DELAY = 3 * 24 * 3600 * 1000L; // milliseconds + private static final int PRINT_IMAGE_TIMEOUT = 10 * 1000; // milliseconds + static final List SORT_DATE_HEADER = Collections.unmodifiableList(Arrays.asList( "time", "unread", "starred", "priority" )); @@ -7348,6 +7355,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. args.putBoolean("print_html_images", print_html_images); new SimpleTask() { + private final ExecutorService executor = Helper.getBackgroundExecutor(0, "print"); + @Override protected String[] onExecute(Context context, Bundle args) throws IOException { long id = args.getLong("id"); @@ -7370,6 +7379,47 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. Document document = JsoupEx.parse(file); HtmlHelper.embedInlineImages(context, id, document, true); + // onPageFinished will not be called if not all images can be loaded + List> futures = new ArrayList<>(); + for (final Element img : document.select("img")) { + final String src = img.attr("src"); + if (src.startsWith("http") || src.startsWith("https")) + futures.add(executor.submit(new Callable() { + @Override + public Void call() throws Exception { + try { + URL url = new URL(src); + Log.i("Checking url=" + url); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setReadTimeout(PRINT_IMAGE_TIMEOUT); + connection.setConnectTimeout(PRINT_IMAGE_TIMEOUT); + connection.setInstanceFollowRedirects(true); + connection.connect(); + + try { + connection.getInputStream(); + } finally { + connection.disconnect(); + } + } catch (Throwable ex) { + img.attr("src", ""); + Log.w(ex); + } + + return null; + } + })); + } + + for (Future future : futures) + try { + future.get(); + } catch (Throwable ex) { + Log.w(ex); + } + // @page WordSection1 {size:612.0pt 792.0pt; margin:70.85pt 70.85pt 70.85pt 70.85pt;} // div.WordSection1 {page:WordSection1;} //