diff --git a/app/src/main/java/eu/faircode/email/ContactInfo.java b/app/src/main/java/eu/faircode/email/ContactInfo.java index 62c88df4f2..4397c15bf8 100644 --- a/app/src/main/java/eu/faircode/email/ContactInfo.java +++ b/app/src/main/java/eu/faircode/email/ContactInfo.java @@ -57,11 +57,13 @@ public class ContactInfo { private static Map emailLookup = new ConcurrentHashMap<>(); private static final Map emailContactInfo = new HashMap<>(); + private static final Map emailGravatar = new HashMap<>(); private static final ExecutorService executor = Helper.getBackgroundExecutor(1, "contact"); private static final int GRAVATAR_TIMEOUT = 5 * 1000; // milliseconds private static final long CACHE_CONTACT_DURATION = 2 * 60 * 1000L; // milliseconds + private static final long CACHE_GRAVATAR_DURATION = 2 * 60 * 60 * 1000L; // milliseconds private ContactInfo() { } @@ -169,29 +171,45 @@ public class ContactInfo { if (info.bitmap == null) { boolean gravatars = prefs.getBoolean("gravatars", false); if (gravatars && BuildConfig.DEBUG) { - HttpURLConnection urlConnection = null; - try { - String hash = Helper.md5(address.getAddress().getBytes()); - URL url = new URL("https://www.gravatar.com/avatar/" + hash + "?d=404"); - - urlConnection = (HttpURLConnection) url.openConnection(); - urlConnection.setRequestMethod("GET"); - urlConnection.setDoOutput(false); - urlConnection.setReadTimeout(GRAVATAR_TIMEOUT); - urlConnection.setConnectTimeout(GRAVATAR_TIMEOUT); - urlConnection.connect(); - - int status = urlConnection.getResponseCode(); - if (status == HttpURLConnection.HTTP_OK) - info.bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream()); - else if (status != HttpURLConnection.HTTP_NOT_FOUND) - throw new IOException("HTTP status=" + status); - - } catch (Throwable ex) { - Log.w(ex); - } finally { - if (urlConnection != null) - urlConnection.disconnect(); + boolean lookup; + synchronized (emailGravatar) { + Gravatar g = emailGravatar.get(address.getAddress()); + lookup = (g == null || g.isExpired() || g.isAvailable()); + } + + if (lookup) { + HttpURLConnection urlConnection = null; + try { + String hash = Helper.md5(address.getAddress().getBytes()); + URL url = new URL("https://www.gravatar.com/avatar/" + hash + "?d=404"); + Log.i("Gravatar url=" + url); + + urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setRequestMethod("GET"); + urlConnection.setDoOutput(false); + urlConnection.setReadTimeout(GRAVATAR_TIMEOUT); + urlConnection.setConnectTimeout(GRAVATAR_TIMEOUT); + urlConnection.connect(); + + int status = urlConnection.getResponseCode(); + if (status == HttpURLConnection.HTTP_OK) { + info.bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream()); + synchronized (emailGravatar) { + emailGravatar.put(address.getAddress(), new Gravatar(true)); + } + } else if (status == HttpURLConnection.HTTP_NOT_FOUND) { + synchronized (emailGravatar) { + emailGravatar.put(address.getAddress(), new Gravatar(false)); + } + } else + throw new IOException("HTTP status=" + status); + + } catch (Throwable ex) { + Log.w(ex); + } finally { + if (urlConnection != null) + urlConnection.disconnect(); + } } } } @@ -315,4 +333,22 @@ public class ContactInfo { Log.i("Read email/uri=" + all.size()); return all; } + + private static class Gravatar { + private boolean available; + private long time; + + Gravatar(boolean available) { + this.available = available; + this.time = new Date().getTime(); + } + + boolean isAvailable() { + return available; + } + + boolean isExpired() { + return (new Date().getTime() - time > CACHE_GRAVATAR_DURATION); + } + } } \ No newline at end of file