Download executor

This commit is contained in:
M66B 2022-12-28 10:32:41 +01:00
parent fe42eba34d
commit c8fe17b296
5 changed files with 38 additions and 33 deletions

View File

@ -100,8 +100,6 @@ public class ContactInfo {
private static Map<String, Lookup> emailLookup = new ConcurrentHashMap<>();
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 FAVICON_ICON_SIZE = 64; // dp
@ -377,7 +375,7 @@ public class ContactInfo {
List<Future<Favicon>> futures = new ArrayList<>();
if (bimi)
futures.add(executor.submit(new Callable<Favicon>() {
futures.add(Helper.getDownloadTaskExecutor().submit(new Callable<Favicon>() {
@Override
public Favicon call() throws Exception {
Pair<Bitmap, Boolean> 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<Favicon>() {
futures.add(Helper.getDownloadTaskExecutor().submit(new Callable<Favicon>() {
@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<Favicon>() {
futures.add(Helper.getDownloadTaskExecutor().submit(new Callable<Favicon>() {
@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<Future<Pair<Favicon, URL>>> 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<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 {
Pair<Favicon, URL> 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;
}

View File

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

View File

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

View File

@ -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)

View File

@ -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) {