From 9955a71d79b708b474bc88dab7805500faeeaa8f Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Tue, 10 Feb 2015 12:51:59 -0500 Subject: [PATCH 1/5] Added DefaultImage enum to GravatarUtils --- .../wordpress/android/util/GravatarUtils.java | 47 ++++++++++++++++--- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java index 3d8890802f5a..78c46088e9ef 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java @@ -1,22 +1,55 @@ package org.wordpress.android.util; public class GravatarUtils { + /* * see https://en.gravatar.com/site/implement/images/ */ + + public static enum DefaultImage { + MYSTERY_MAN, + STATUS_404, + IDENTICON, + MONSTER, + WAVATAR, + RETRO, + BLANK; + + @Override + public String toString() { + switch (this) { + case MYSTERY_MAN: + return "mm"; + case STATUS_404: + return "404"; + case IDENTICON: + return "identicon"; + case MONSTER: + return "monsterid"; + case WAVATAR: + return "wavatar"; + case RETRO: + return "retro"; + default: + return "blank"; + } + } + } + public static String gravatarFromEmail(final String email, int size) { + return gravatarFromEmail(email, size, DefaultImage.MYSTERY_MAN); + } + public static String gravatarFromEmail(final String email, int size, DefaultImage defaultImage) { return "http://gravatar.com/avatar/" + StringUtils.getMd5Hash(StringUtils.notNullStr(email)) - + "?d=mm&size=" + Integer.toString(size); + + "?d=" + defaultImage.toString() + + "&size=" + Integer.toString(size); } - /* - * important: the 404 default means the request will 404 if there is no blavatar - * for the passed site - so the caller needs to trap the 404 to provide a default - */ - public static String blavatarFromUrl(final String url, int size) { + public static String blavatarFromUrl(final String url, int size, DefaultImage defaultImage) { return "http://gravatar.com/blavatar/" + StringUtils.getMd5Hash(UrlUtils.getDomainFromUrl(url)) - + "?d=404&size=" + Integer.toString(size); + + "?d=" + defaultImage.toString() + + "&size=" + Integer.toString(size); } } From 4f276965a75a16d2bea55b263d4e68ec6224c239 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Tue, 10 Feb 2015 13:08:01 -0500 Subject: [PATCH 2/5] Moved fixAvatar routine to GravatarUtils --- .../wordpress/android/util/GravatarUtils.java | 21 ++++++++++++++++++- .../wordpress/android/util/PhotonUtils.java | 17 --------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java index 78c46088e9ef..765084b1748a 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java @@ -1,7 +1,8 @@ package org.wordpress.android.util; -public class GravatarUtils { +import android.text.TextUtils; +public class GravatarUtils { /* * see https://en.gravatar.com/site/implement/images/ */ @@ -36,6 +37,24 @@ public String toString() { } } + /* + * gravatars often contain the ?s= parameter which determines their size - detect this and + * replace it with a new ?s= parameter which requests the avatar at the exact size needed + */ + public static String fixGravatarUrl(final String imageUrl, int avatarSz) { + if (TextUtils.isEmpty(imageUrl)) { + return ""; + } + + // if this isn't a gravatar image, return as resized photon image url + if (!imageUrl.contains("gravatar.com")) { + return PhotonUtils.getPhotonImageUrl(imageUrl, avatarSz, avatarSz); + } + + // remove all other params, then add query string for size and "mystery man" default + return UrlUtils.removeQuery(imageUrl) + "?s=" + avatarSz + "&d=mm"; + } + public static String gravatarFromEmail(final String email, int size) { return gravatarFromEmail(email, size, DefaultImage.MYSTERY_MAN); } diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java index fec275791741..aa02d4c9deea 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java @@ -12,23 +12,6 @@ private PhotonUtils() { throw new AssertionError(); } - /* - * gravatars often contain the ?s= parameter which determines their size - detect this and - * replace it with a new ?s= parameter which requests the avatar at the exact size needed - */ - public static String fixAvatar(final String imageUrl, int avatarSz) { - if (TextUtils.isEmpty(imageUrl)) - return ""; - - // if this isn't a gravatar image, return as resized photon image url - if (!imageUrl.contains("gravatar.com")) { - return getPhotonImageUrl(imageUrl, avatarSz, avatarSz); - } - - // remove all other params, then add query string for size and "mystery man" default - return UrlUtils.removeQuery(imageUrl) + "?s=" + avatarSz + "&d=mm"; - } - /* * returns true if the passed url is an obvious "mshots" url */ From 956d44cc598c5ae56f7446a52cb4ea85c277cc18 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Tue, 10 Feb 2015 13:38:51 -0500 Subject: [PATCH 3/5] WPNetworkImageView now accepts a default error image resource, also forcing size when showing blavatars in ReaderBlogAdapter --- .../org/wordpress/android/util/GravatarUtils.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java index 765084b1748a..d4e226d3758a 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java @@ -2,10 +2,10 @@ import android.text.TextUtils; +/** + * see https://en.gravatar.com/site/implement/images/ + */ public class GravatarUtils { - /* - * see https://en.gravatar.com/site/implement/images/ - */ public static enum DefaultImage { MYSTERY_MAN, @@ -42,6 +42,9 @@ public String toString() { * replace it with a new ?s= parameter which requests the avatar at the exact size needed */ public static String fixGravatarUrl(final String imageUrl, int avatarSz) { + return fixGravatarUrl(imageUrl, avatarSz, DefaultImage.MYSTERY_MAN); + } + public static String fixGravatarUrl(final String imageUrl, int avatarSz, DefaultImage defaultImage) { if (TextUtils.isEmpty(imageUrl)) { return ""; } @@ -51,8 +54,8 @@ public static String fixGravatarUrl(final String imageUrl, int avatarSz) { return PhotonUtils.getPhotonImageUrl(imageUrl, avatarSz, avatarSz); } - // remove all other params, then add query string for size and "mystery man" default - return UrlUtils.removeQuery(imageUrl) + "?s=" + avatarSz + "&d=mm"; + // remove all other params, then add query string for size and default image + return UrlUtils.removeQuery(imageUrl) + "?s=" + avatarSz + "&d=" + defaultImage.toString(); } public static String gravatarFromEmail(final String email, int size) { From ef8a8934697f35b9de46577551cbefa3dd38564f Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Tue, 10 Feb 2015 13:46:50 -0500 Subject: [PATCH 4/5] Added DEFAULT_BLAVATAR constant --- .../java/org/wordpress/android/util/GravatarUtils.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java index d4e226d3758a..cb4555777e3d 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java @@ -7,6 +7,8 @@ */ public class GravatarUtils { + private static final DefaultImage DEFAULT_GRAVATAR = DefaultImage.MYSTERY_MAN; + public static enum DefaultImage { MYSTERY_MAN, STATUS_404, @@ -42,7 +44,7 @@ public String toString() { * replace it with a new ?s= parameter which requests the avatar at the exact size needed */ public static String fixGravatarUrl(final String imageUrl, int avatarSz) { - return fixGravatarUrl(imageUrl, avatarSz, DefaultImage.MYSTERY_MAN); + return fixGravatarUrl(imageUrl, avatarSz, DEFAULT_GRAVATAR); } public static String fixGravatarUrl(final String imageUrl, int avatarSz, DefaultImage defaultImage) { if (TextUtils.isEmpty(imageUrl)) { @@ -59,7 +61,7 @@ public static String fixGravatarUrl(final String imageUrl, int avatarSz, Default } public static String gravatarFromEmail(final String email, int size) { - return gravatarFromEmail(email, size, DefaultImage.MYSTERY_MAN); + return gravatarFromEmail(email, size, DEFAULT_GRAVATAR); } public static String gravatarFromEmail(final String email, int size, DefaultImage defaultImage) { return "http://gravatar.com/avatar/" @@ -68,6 +70,9 @@ public static String gravatarFromEmail(final String email, int size, DefaultImag + "&size=" + Integer.toString(size); } + public static String blavatarFromUrl(final String url, int size) { + return blavatarFromUrl(url, size, DEFAULT_GRAVATAR); + } public static String blavatarFromUrl(final String url, int size, DefaultImage defaultImage) { return "http://gravatar.com/blavatar/" + StringUtils.getMd5Hash(UrlUtils.getDomainFromUrl(url)) From 4f21616fa38632688253670303162bd68239e1e7 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Wed, 11 Feb 2015 12:28:46 -0500 Subject: [PATCH 5/5] All gravatar requests now default to 404. --- .../main/java/org/wordpress/android/util/GravatarUtils.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java index cb4555777e3d..7ce327072659 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java @@ -7,7 +7,9 @@ */ public class GravatarUtils { - private static final DefaultImage DEFAULT_GRAVATAR = DefaultImage.MYSTERY_MAN; + // by default tell gravatar to respond to non-existent images with a 404 - this means + // it's up to the caller to catch the 404 and provide a suitable default image + private static final DefaultImage DEFAULT_GRAVATAR = DefaultImage.STATUS_404; public static enum DefaultImage { MYSTERY_MAN,