Skip to content

Commit

Permalink
Fixes issue where private blog images wouldn't load.
Browse files Browse the repository at this point in the history
  • Loading branch information
roundhill committed Nov 6, 2015
1 parent 0939a67 commit 269ecdf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ public static boolean isImageUrl(String url) {
cleanedUrl.endsWith("gif") || cleanedUrl.endsWith("png");
}

/**
* Returns true if the url is on wordpress.com
*/
public static boolean isWPComUrl(String url) {
return !TextUtils.isEmpty(url) && url.contains("wordpress.com");
}


public static String appendUrlParameter(String url, String paramName, String paramValue) {
Map<String, String> parameters = new HashMap<>();
parameters.put(paramName, paramValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.util.ArrayMap;
import android.text.Html;
import android.text.TextUtils;
import android.widget.TextView;
Expand All @@ -13,14 +14,18 @@
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.PhotonUtils;
import org.wordpress.android.util.UrlUtils;

import java.lang.ref.WeakReference;
import java.util.Map;

/**
* ImageGetter for Html.fromHtml()
* adapted from existing ImageGetter code in NoteCommentFragment
*/
public class WPImageGetter implements Html.ImageGetter {
private static final String IMAGE_QUALITY = "65";

private final WeakReference<TextView> mWeakView;
private final int mMaxSize;
private ImageLoader mImageLoader;
Expand All @@ -32,13 +37,13 @@ public WPImageGetter(TextView view) {
}

public WPImageGetter(TextView view, int maxSize) {
mWeakView = new WeakReference<TextView>(view);
mWeakView = new WeakReference<>(view);
mMaxSize = maxSize;
}

public WPImageGetter(TextView view, int maxSize, ImageLoader imageLoader, Drawable loadingDrawable,
Drawable failedDrawable) {
mWeakView = new WeakReference<TextView>(view);
mWeakView = new WeakReference<>(view);
mMaxSize = maxSize;
mImageLoader = imageLoader;
mLoadingDrawable = loadingDrawable;
Expand All @@ -64,10 +69,20 @@ public Drawable getDrawable(String source) {
source = "http:" + source;
}

// use Photon if a max size is requested (otherwise the full-sized image will be downloaded
// Resize to max size if requested (otherwise the full-sized image will be downloaded
// and then resized)
if (mMaxSize > 0) {
source = PhotonUtils.getPhotonImageUrl(source, mMaxSize, 0);
if (UrlUtils.isWPComUrl(source)) {
// Adjust query params for wp.com urls
Map<String, String> params = new ArrayMap<>();
params.put("w", String.valueOf(mMaxSize));
params.put("quality", IMAGE_QUALITY);

source = UrlUtils.appendUrlParameters(UrlUtils.removeQuery(source), params);
} else {
// Otherwise use photon
source = PhotonUtils.getPhotonImageUrl(source, mMaxSize, 0);
}
}

final RemoteDrawable remote = new RemoteDrawable(mLoadingDrawable, mFailedDrawable);
Expand Down

0 comments on commit 269ecdf

Please sign in to comment.