Download executor

pull/210/head
M66B 3 years ago
parent fe42eba34d
commit c8fe17b296

@ -100,8 +100,6 @@ public class ContactInfo {
private static Map<String, Lookup> emailLookup = new ConcurrentHashMap<>(); private static Map<String, Lookup> emailLookup = new ConcurrentHashMap<>();
private static final Map<String, ContactInfo> emailContactInfo = new HashMap<>(); private static final Map<String, ContactInfo> 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 GENERATED_ICON_SIZE = 48; // dp
private static final int FAVICON_ICON_SIZE = 64; // dp private static final int FAVICON_ICON_SIZE = 64; // dp
@ -377,7 +375,7 @@ public class ContactInfo {
List<Future<Favicon>> futures = new ArrayList<>(); List<Future<Favicon>> futures = new ArrayList<>();
if (bimi) if (bimi)
futures.add(executor.submit(new Callable<Favicon>() { futures.add(Helper.getDownloadTaskExecutor().submit(new Callable<Favicon>() {
@Override @Override
public Favicon call() throws Exception { public Favicon call() throws Exception {
Pair<Bitmap, Boolean> bimi = Pair<Bitmap, Boolean> bimi =
@ -387,9 +385,9 @@ public class ContactInfo {
})); }));
if (gravatars) if (gravatars)
futures.add(executor.submit(Avatar.getGravatar(email, scaleToPixels, context))); futures.add(Helper.getDownloadTaskExecutor().submit(Avatar.getGravatar(email, scaleToPixels, context)));
if (libravatars) if (libravatars)
futures.add(executor.submit(Avatar.getLibravatar(email, scaleToPixels, context))); futures.add(Helper.getDownloadTaskExecutor().submit(Avatar.getLibravatar(email, scaleToPixels, context)));
if (favicons) { if (favicons) {
String host = domain; String host = domain;
@ -398,7 +396,7 @@ public class ContactInfo {
while (host.indexOf('.') > 0) { while (host.indexOf('.') > 0) {
final URL base = new URL("https://" + host); final URL base = new URL("https://" + host);
futures.add(executor.submit(new Callable<Favicon>() { futures.add(Helper.getDownloadTaskExecutor().submit(new Callable<Favicon>() {
@Override @Override
public Favicon call() throws Exception { public Favicon call() throws Exception {
return parseFavicon(base, scaleToPixels, context); return parseFavicon(base, scaleToPixels, context);
@ -416,7 +414,7 @@ public class ContactInfo {
final URL base = new URL("https://" + host); final URL base = new URL("https://" + host);
for (String name : FIXED_FAVICONS) for (String name : FIXED_FAVICONS)
futures.add(executor.submit(new Callable<Favicon>() { futures.add(Helper.getDownloadTaskExecutor().submit(new Callable<Favicon>() {
@Override @Override
public Favicon call() throws Exception { public Favicon call() throws Exception {
return getFavicon(new URL(base, name), null, scaleToPixels, context); return getFavicon(new URL(base, name), null, scaleToPixels, context);
@ -735,7 +733,6 @@ public class ContactInfo {
for (int i = 0; i < imgs.size(); i++) for (int i = 0; i < imgs.size(); i++)
Log.i("Favicon #" + getOrder(host, imgs.get(i)) + " " + i + "=" + imgs.get(i) + " @" + base); Log.i("Favicon #" + getOrder(host, imgs.get(i)) + " " + i + "=" + imgs.get(i) + " @" + base);
List<Future<Pair<Favicon, URL>>> futures = new ArrayList<>();
for (Element img : imgs) { for (Element img : imgs) {
String rel = img.attr("rel").trim().toLowerCase(Locale.ROOT); String rel = img.attr("rel").trim().toLowerCase(Locale.ROOT);
if (REL_EXCLUDE.contains(rel)) // dns-prefetch: gmx.net if (REL_EXCLUDE.contains(rel)) // dns-prefetch: gmx.net
@ -747,20 +744,11 @@ public class ContactInfo {
if (TextUtils.isEmpty(favicon)) if (TextUtils.isEmpty(favicon))
continue; continue;
final URL url = new URL(base, favicon);
futures.add(favicons.submit(new Callable<Pair<Favicon, URL>>() {
@Override
public Pair<Favicon, URL> call() throws Exception {
return new Pair(getFavicon(url, img.attr("type"), scaleToPixels, context), url);
}
}));
}
for (Future<Pair<Favicon, URL>> future : futures)
try { try {
Pair<Favicon, URL> result = future.get(); URL url = new URL(base, favicon);
Log.i("Using favicon=" + result.second); Favicon f = getFavicon(url, img.attr("type"), scaleToPixels, context);
return result.first; Log.i("Using favicon=" + url);
return f;
} catch (Throwable ex) { } catch (Throwable ex) {
if (ex.getCause() instanceof FileNotFoundException || if (ex.getCause() instanceof FileNotFoundException ||
ex.getCause() instanceof CertPathValidatorException) ex.getCause() instanceof CertPathValidatorException)
@ -768,6 +756,7 @@ public class ContactInfo {
else else
Log.e(ex); Log.e(ex);
} }
}
return null; return null;
} }

@ -1099,7 +1099,7 @@ public class EmailProvider implements Parcelable {
private Future<Boolean> getReachable(Context context) { private Future<Boolean> getReachable(Context context) {
Log.i("Scanning " + this); Log.i("Scanning " + this);
return Helper.getParallelExecutor().submit(new Callable<Boolean>() { return Helper.getDownloadTaskExecutor().submit(new Callable<Boolean>() {
// Returns: // Returns:
// false: closed // false: closed
// true: listening // true: listening

@ -9820,7 +9820,7 @@ public class FragmentMessages extends FragmentBase
continue; continue;
} }
futures.add(Helper.getParallelExecutor().submit(new Callable<Void>() { futures.add(Helper.getDownloadTaskExecutor().submit(new Callable<Void>() {
@Override @Override
public Void call() throws Exception { public Void call() throws Exception {
try (OutputStream os = new FileOutputStream(out)) { try (OutputStream os = new FileOutputStream(out)) {

@ -245,12 +245,13 @@ public class Helper {
"wsc", "wsf", "wsh" "wsc", "wsf", "wsh"
)); ));
static ExecutorService sSerialExecutor = null; private static ExecutorService sSerialExecutor = null;
static ExecutorService sParallelExecutor = null; private static ExecutorService sParallelExecutor = null;
static ExecutorService sSerialTaskExecutor = null; private static ExecutorService sSerialTaskExecutor = null;
private static ExecutorService sDownloadExecutor = null;
static int sOperationIndex = 0; private static int sOperationIndex = 0;
static final ExecutorService[] sOperationExecutor = new ExecutorService[OPERATION_WORKERS]; private static final ExecutorService[] sOperationExecutor = new ExecutorService[OPERATION_WORKERS];
static ExecutorService getSerialExecutor() { static ExecutorService getSerialExecutor() {
if (sSerialExecutor == null) if (sSerialExecutor == null)
@ -270,6 +271,12 @@ public class Helper {
return sSerialTaskExecutor; return sSerialTaskExecutor;
} }
static ExecutorService getDownloadTaskExecutor() {
if (sDownloadExecutor == null)
sDownloadExecutor = getBackgroundExecutor(0, "download");
return sDownloadExecutor;
}
static ExecutorService getOperationExecutor() { static ExecutorService getOperationExecutor() {
synchronized (sOperationExecutor) { synchronized (sOperationExecutor) {
if (sOperationExecutor[sOperationIndex] == null) if (sOperationExecutor[sOperationIndex] == null)

@ -79,6 +79,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;
class ImageHelper { class ImageHelper {
static final int DOWNLOAD_TIMEOUT = 15; // seconds static final int DOWNLOAD_TIMEOUT = 15; // seconds
@ -470,11 +471,10 @@ class ImageHelper {
lld.setLevel(1); lld.setLevel(1);
Integer kbps = ConnectionHelper.getLinkDownstreamBandwidthKbps(context); Integer kbps = ConnectionHelper.getLinkDownstreamBandwidthKbps(context);
ExecutorService executor = (kbps != null && kbps < SLOW_CONNECTION boolean slow = (kbps != null && kbps < SLOW_CONNECTION);
? Helper.getSerialExecutor() Semaphore semaphore = new Semaphore(1);
: Helper.getParallelExecutor());
executor.submit(new Runnable() { Helper.getDownloadTaskExecutor().submit(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -487,7 +487,16 @@ class ImageHelper {
} }
// Download image // 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); fitDrawable(d, aw, ah, scale, view);
post(d, source); post(d, source);
} catch (Throwable ex) { } catch (Throwable ex) {

Loading…
Cancel
Save