Skip to content

Commit

Permalink
Merge branch 'develop' into feature/modularize-editor
Browse files Browse the repository at this point in the history
Conflicts:
	WordPress/src/main/java/org/wordpress/android/models/Comment.java
	WordPress/src/main/java/org/wordpress/android/models/ReaderPost.java
	WordPress/src/main/java/org/wordpress/android/ui/notifications/blocks/HeaderUserNoteBlock.java
	WordPress/src/main/java/org/wordpress/android/ui/notifications/blocks/UserNoteBlock.java
  • Loading branch information
maxme committed Feb 12, 2015
2 parents d7d1b96 + 9046278 commit 8015176
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,84 @@
package org.wordpress.android.util;

import android.text.TextUtils;

/**
* see https://en.gravatar.com/site/implement/images/
*/
public class GravatarUtils {

// 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,
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";
}
}
}

/*
* see https://en.gravatar.com/site/implement/images/
*/
* 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) {
return fixGravatarUrl(imageUrl, avatarSz, DEFAULT_GRAVATAR);
}
public static String fixGravatarUrl(final String imageUrl, int avatarSz, DefaultImage defaultImage) {
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 default image
return UrlUtils.removeQuery(imageUrl) + "?s=" + avatarSz + "&d=" + defaultImage.toString();
}

public static String gravatarFromEmail(final String email, int size) {
return gravatarFromEmail(email, size, DEFAULT_GRAVATAR);
}
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) {
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))
+ "?d=404&size=" + Integer.toString(size);
+ "?d=" + defaultImage.toString()
+ "&size=" + Integer.toString(size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 8015176

Please sign in to comment.