From c8fe17b296a241599a9507dedec9ad8a908545a3 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 28 Dec 2022 10:32:41 +0100 Subject: [PATCH] Download executor --- .../java/eu/faircode/email/ContactInfo.java | 31 ++++++------------- .../java/eu/faircode/email/EmailProvider.java | 2 +- .../eu/faircode/email/FragmentMessages.java | 2 +- .../main/java/eu/faircode/email/Helper.java | 17 +++++++--- .../java/eu/faircode/email/ImageHelper.java | 19 +++++++++--- 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ContactInfo.java b/app/src/main/java/eu/faircode/email/ContactInfo.java index 2796082a83..5df00df8b5 100644 --- a/app/src/main/java/eu/faircode/email/ContactInfo.java +++ b/app/src/main/java/eu/faircode/email/ContactInfo.java @@ -100,8 +100,6 @@ public class ContactInfo { private static Map emailLookup = new ConcurrentHashMap<>(); private static final Map emailContactInfo = new HashMap<>(); - private static final ExecutorService executor = Helper.getBackgroundExecutor(0, "contact"); - private static final ExecutorService favicons = Helper.getBackgroundExecutor(0, "favicons"); private static final int GENERATED_ICON_SIZE = 48; // dp private static final int FAVICON_ICON_SIZE = 64; // dp @@ -377,7 +375,7 @@ public class ContactInfo { List> futures = new ArrayList<>(); if (bimi) - futures.add(executor.submit(new Callable() { + futures.add(Helper.getDownloadTaskExecutor().submit(new Callable() { @Override public Favicon call() throws Exception { Pair bimi = @@ -387,9 +385,9 @@ public class ContactInfo { })); if (gravatars) - futures.add(executor.submit(Avatar.getGravatar(email, scaleToPixels, context))); + futures.add(Helper.getDownloadTaskExecutor().submit(Avatar.getGravatar(email, scaleToPixels, context))); if (libravatars) - futures.add(executor.submit(Avatar.getLibravatar(email, scaleToPixels, context))); + futures.add(Helper.getDownloadTaskExecutor().submit(Avatar.getLibravatar(email, scaleToPixels, context))); if (favicons) { String host = domain; @@ -398,7 +396,7 @@ public class ContactInfo { while (host.indexOf('.') > 0) { final URL base = new URL("https://" + host); - futures.add(executor.submit(new Callable() { + futures.add(Helper.getDownloadTaskExecutor().submit(new Callable() { @Override public Favicon call() throws Exception { return parseFavicon(base, scaleToPixels, context); @@ -416,7 +414,7 @@ public class ContactInfo { final URL base = new URL("https://" + host); for (String name : FIXED_FAVICONS) - futures.add(executor.submit(new Callable() { + futures.add(Helper.getDownloadTaskExecutor().submit(new Callable() { @Override public Favicon call() throws Exception { return getFavicon(new URL(base, name), null, scaleToPixels, context); @@ -735,7 +733,6 @@ public class ContactInfo { for (int i = 0; i < imgs.size(); i++) Log.i("Favicon #" + getOrder(host, imgs.get(i)) + " " + i + "=" + imgs.get(i) + " @" + base); - List>> futures = new ArrayList<>(); for (Element img : imgs) { String rel = img.attr("rel").trim().toLowerCase(Locale.ROOT); if (REL_EXCLUDE.contains(rel)) // dns-prefetch: gmx.net @@ -747,20 +744,11 @@ public class ContactInfo { if (TextUtils.isEmpty(favicon)) continue; - final URL url = new URL(base, favicon); - futures.add(favicons.submit(new Callable>() { - @Override - public Pair call() throws Exception { - return new Pair(getFavicon(url, img.attr("type"), scaleToPixels, context), url); - } - })); - } - - for (Future> future : futures) try { - Pair result = future.get(); - Log.i("Using favicon=" + result.second); - return result.first; + URL url = new URL(base, favicon); + Favicon f = getFavicon(url, img.attr("type"), scaleToPixels, context); + Log.i("Using favicon=" + url); + return f; } catch (Throwable ex) { if (ex.getCause() instanceof FileNotFoundException || ex.getCause() instanceof CertPathValidatorException) @@ -768,6 +756,7 @@ public class ContactInfo { else Log.e(ex); } + } return null; } diff --git a/app/src/main/java/eu/faircode/email/EmailProvider.java b/app/src/main/java/eu/faircode/email/EmailProvider.java index 542d71d6b2..03a25a3e2d 100644 --- a/app/src/main/java/eu/faircode/email/EmailProvider.java +++ b/app/src/main/java/eu/faircode/email/EmailProvider.java @@ -1099,7 +1099,7 @@ public class EmailProvider implements Parcelable { private Future getReachable(Context context) { Log.i("Scanning " + this); - return Helper.getParallelExecutor().submit(new Callable() { + return Helper.getDownloadTaskExecutor().submit(new Callable() { // Returns: // false: closed // true: listening diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index fcd6ef6eeb..914e5a3bb8 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -9820,7 +9820,7 @@ public class FragmentMessages extends FragmentBase continue; } - futures.add(Helper.getParallelExecutor().submit(new Callable() { + futures.add(Helper.getDownloadTaskExecutor().submit(new Callable() { @Override public Void call() throws Exception { try (OutputStream os = new FileOutputStream(out)) { diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index dbdf7d9a2b..e3936091e3 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -245,12 +245,13 @@ public class Helper { "wsc", "wsf", "wsh" )); - static ExecutorService sSerialExecutor = null; - static ExecutorService sParallelExecutor = null; - static ExecutorService sSerialTaskExecutor = null; + private static ExecutorService sSerialExecutor = null; + private static ExecutorService sParallelExecutor = null; + private static ExecutorService sSerialTaskExecutor = null; + private static ExecutorService sDownloadExecutor = null; - static int sOperationIndex = 0; - static final ExecutorService[] sOperationExecutor = new ExecutorService[OPERATION_WORKERS]; + private static int sOperationIndex = 0; + private static final ExecutorService[] sOperationExecutor = new ExecutorService[OPERATION_WORKERS]; static ExecutorService getSerialExecutor() { if (sSerialExecutor == null) @@ -270,6 +271,12 @@ public class Helper { return sSerialTaskExecutor; } + static ExecutorService getDownloadTaskExecutor() { + if (sDownloadExecutor == null) + sDownloadExecutor = getBackgroundExecutor(0, "download"); + return sDownloadExecutor; + } + static ExecutorService getOperationExecutor() { synchronized (sOperationExecutor) { if (sOperationExecutor[sOperationIndex] == null) diff --git a/app/src/main/java/eu/faircode/email/ImageHelper.java b/app/src/main/java/eu/faircode/email/ImageHelper.java index 94577d4ec2..b8a087010c 100644 --- a/app/src/main/java/eu/faircode/email/ImageHelper.java +++ b/app/src/main/java/eu/faircode/email/ImageHelper.java @@ -79,6 +79,7 @@ import java.util.List; import java.util.Map; import java.util.WeakHashMap; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Semaphore; class ImageHelper { static final int DOWNLOAD_TIMEOUT = 15; // seconds @@ -470,11 +471,10 @@ class ImageHelper { lld.setLevel(1); Integer kbps = ConnectionHelper.getLinkDownstreamBandwidthKbps(context); - ExecutorService executor = (kbps != null && kbps < SLOW_CONNECTION - ? Helper.getSerialExecutor() - : Helper.getParallelExecutor()); + boolean slow = (kbps != null && kbps < SLOW_CONNECTION); + Semaphore semaphore = new Semaphore(1); - executor.submit(new Runnable() { + Helper.getDownloadTaskExecutor().submit(new Runnable() { @Override public void run() { try { @@ -487,7 +487,16 @@ class ImageHelper { } // Download image - Drawable d = downloadImage(context, id, source, null); + Drawable d; + try { + if (slow) + semaphore.acquire(); + d = downloadImage(context, id, source, null); + } finally { + if (slow) + semaphore.release(); + } + fitDrawable(d, aw, ah, scale, view); post(d, source); } catch (Throwable ex) {