Persist Gravatar negative responses

pull/172/head
M66B 5 years ago
parent 9999c91dc1
commit c6f6f2298f

@ -57,11 +57,13 @@ public class ContactInfo {
private static Map<String, Uri> emailLookup = new ConcurrentHashMap<>(); private static Map<String, Uri> emailLookup = new ConcurrentHashMap<>();
private static final Map<String, ContactInfo> emailContactInfo = new HashMap<>(); private static final Map<String, ContactInfo> emailContactInfo = new HashMap<>();
private static final Map<String, Gravatar> emailGravatar = new HashMap<>();
private static final ExecutorService executor = private static final ExecutorService executor =
Helper.getBackgroundExecutor(1, "contact"); Helper.getBackgroundExecutor(1, "contact");
private static final int GRAVATAR_TIMEOUT = 5 * 1000; // milliseconds 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_CONTACT_DURATION = 2 * 60 * 1000L; // milliseconds
private static final long CACHE_GRAVATAR_DURATION = 2 * 60 * 60 * 1000L; // milliseconds
private ContactInfo() { private ContactInfo() {
} }
@ -169,29 +171,45 @@ public class ContactInfo {
if (info.bitmap == null) { if (info.bitmap == null) {
boolean gravatars = prefs.getBoolean("gravatars", false); boolean gravatars = prefs.getBoolean("gravatars", false);
if (gravatars && BuildConfig.DEBUG) { if (gravatars && BuildConfig.DEBUG) {
HttpURLConnection urlConnection = null; boolean lookup;
try { synchronized (emailGravatar) {
String hash = Helper.md5(address.getAddress().getBytes()); Gravatar g = emailGravatar.get(address.getAddress());
URL url = new URL("https://www.gravatar.com/avatar/" + hash + "?d=404"); lookup = (g == null || g.isExpired() || g.isAvailable());
}
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET"); if (lookup) {
urlConnection.setDoOutput(false); HttpURLConnection urlConnection = null;
urlConnection.setReadTimeout(GRAVATAR_TIMEOUT); try {
urlConnection.setConnectTimeout(GRAVATAR_TIMEOUT); String hash = Helper.md5(address.getAddress().getBytes());
urlConnection.connect(); URL url = new URL("https://www.gravatar.com/avatar/" + hash + "?d=404");
Log.i("Gravatar url=" + url);
int status = urlConnection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) urlConnection = (HttpURLConnection) url.openConnection();
info.bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream()); urlConnection.setRequestMethod("GET");
else if (status != HttpURLConnection.HTTP_NOT_FOUND) urlConnection.setDoOutput(false);
throw new IOException("HTTP status=" + status); urlConnection.setReadTimeout(GRAVATAR_TIMEOUT);
urlConnection.setConnectTimeout(GRAVATAR_TIMEOUT);
} catch (Throwable ex) { urlConnection.connect();
Log.w(ex);
} finally { int status = urlConnection.getResponseCode();
if (urlConnection != null) if (status == HttpURLConnection.HTTP_OK) {
urlConnection.disconnect(); 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()); Log.i("Read email/uri=" + all.size());
return all; 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);
}
}
} }
Loading…
Cancel
Save