[go: nahoru, domu]

Skip to content

Commit

Permalink
add WPImageGetter and fix HtmlUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Jul 9, 2014
1 parent b4baff8 commit 7c6d4f4
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 12 deletions.
1 change: 1 addition & 0 deletions WordPressUtils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repositories {

dependencies {
compile 'commons-lang:commons-lang:2.6'
compile 'com.mcxiaoke.volley:library:1.0.+'
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public class HtmlUtils {
* but it's very slow, so avoid using this where performance is important
*/
public static String stripHtml(final String text) {
if (TextUtils.isEmpty(text))
if (TextUtils.isEmpty(text)) {
return "";
}
return Html.fromHtml(text).toString().trim();
}

Expand Down Expand Up @@ -55,8 +56,7 @@ private static String trimStart(final String str) {
return "";
}
int start = 0;
while (start != strLen
&& (Character.isWhitespace(str.charAt(start)) || str.charAt(start) == 160)) {
while (start != strLen && (Character.isWhitespace(str.charAt(start)) || str.charAt(start) == 160)) {
start++;
}
return str.substring(start);
Expand All @@ -66,8 +66,9 @@ private static String trimStart(final String str) {
* convert html entities to actual Unicode characters - relies on commons apache lang
*/
public static String fastUnescapeHtml(final String text) {
if (text == null || !text.contains("&"))
if (text == null || !text.contains("&")) {
return text;
}
return StringEscapeUtils.unescapeHtml(text);
}

Expand All @@ -90,16 +91,18 @@ public static String colorResToHtmlColor(Context context, int resId) {
* first seen at http://houseofgeekery.com/2013/11/03/13-terrible-x-men-we-wont-see-in-the-movies/
*/
public static String stripScript(final String text) {
if (text == null)
if (text == null) {
return null;
}

StringBuilder sb = new StringBuilder(text);
int start = sb.indexOf("<script");

while (start > -1) {
int end = sb.indexOf("</script>", start);
if (end == -1)
if (end == -1) {
return sb.toString();
}
sb.delete(start, end + 9);
start = sb.indexOf("<script", start);
}
Expand All @@ -121,10 +124,9 @@ public static SpannableStringBuilder fromHtml(String source, WPImageGetter wpIma
Emoticons.replaceEmoticonsWithEmoji(html);
QuoteSpan spans[] = html.getSpans(0, html.length(), QuoteSpan.class);
for (QuoteSpan span : spans) {
html.setSpan(new WPHtml.WPQuoteSpan(), html.getSpanStart(span), html.getSpanEnd(span), html.getSpanFlags(
span));
html.setSpan(new ForegroundColorSpan(0xFF666666), html.getSpanStart(span), html.getSpanEnd(span), html.getSpanFlags(
span));
html.setSpan(new WPQuoteSpan(), html.getSpanStart(span), html.getSpanEnd(span), html.getSpanFlags(span));
html.setSpan(new ForegroundColorSpan(0xFF666666), html.getSpanStart(span), html.getSpanEnd(span),
html.getSpanFlags(span));
html.removeSpan(span);
}
return html;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package org.wordpress.android.util;

import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import org.wordpress.android.util.AppLog.T;

import java.lang.ref.WeakReference;

/**
* ImageGetter for Html.fromHtml()
* adapted from existing ImageGetter code in NoteCommentFragment
*/
public class WPImageGetter implements Html.ImageGetter {
private WeakReference<TextView> mWeakView;
private int mMaxSize;
private ImageLoader mImageLoader;
private Drawable mLoadingDrawable;
private Drawable mFailedDrawable;

public WPImageGetter(TextView view) {
this(view, 0);
}

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

public WPImageGetter(TextView view, int maxSize, ImageLoader imageLoader, Drawable loadingDrawable,
Drawable failedDrawable) {
mWeakView = new WeakReference<TextView>(view);
mMaxSize = maxSize;
mImageLoader = imageLoader;
mLoadingDrawable = loadingDrawable;
mFailedDrawable = failedDrawable;
}

public void setImageLoader(ImageLoader imageLoader) {
mImageLoader = imageLoader;
}

public void setLoadingDrawable(Drawable loadingDrawable) {
mLoadingDrawable = loadingDrawable;
}

public void setFailedDrawable(Drawable failedDrawable) {
mFailedDrawable = failedDrawable;
}

private TextView getView() {
return mWeakView.get();
}

@Override
public Drawable getDrawable(String source) {
if (mImageLoader == null || mLoadingDrawable == null || mFailedDrawable == null) {
throw new RuntimeException("Developer, you need to call setImageLoader, setLoadingDrawable and setFailedDrawable");
}

if (TextUtils.isEmpty(source)) {
return null;
}

// images in reader comments may skip "http:" (no idea why) so make sure to add protocol here
if (source.startsWith("//")) {
source = "http:" + source;
}

// use Photon if a max size is requested (otherwise the full-sized image will be downloaded
// and then resized)
if (mMaxSize > 0) {
source = PhotonUtils.getPhotonImageUrl(source, mMaxSize, 0);
}

TextView view = getView();
// Drawable loading = view.getContext().getResources().getDrawable(R.drawable.remote_image); FIXME: here
// Drawable failed = view.getContext().getResources().getDrawable(R.drawable.remote_failed);
final RemoteDrawable remote = new RemoteDrawable(mLoadingDrawable, mFailedDrawable);

mImageLoader.get(source, new ImageLoader.ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
remote.displayFailed();
TextView view = getView();
if (view != null) {
view.invalidate();
}
}

@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
if (response.getBitmap() != null) {
// make sure view is still valid
TextView view = getView();
if (view == null) {
AppLog.w(T.UTILS, "WPImageGetter view is invalid");
return;
}

Drawable drawable = new BitmapDrawable(view.getContext().getResources(), response.getBitmap());
final int oldHeight = remote.getBounds().height();
int maxWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
if (mMaxSize > 0 && (maxWidth > mMaxSize || maxWidth == 0)) {
maxWidth = mMaxSize;
}
remote.setRemoteDrawable(drawable, maxWidth);

// image is from cache? don't need to modify view height
if (isImmediate) {
return;
}

int newHeight = remote.getBounds().height();
view.invalidate();
// For ICS
view.setHeight(view.getHeight() + newHeight - oldHeight);
// Pre ICS
view.setEllipsize(null);
}
}
});
return remote;
}

private static class RemoteDrawable extends BitmapDrawable {
protected Drawable mRemoteDrawable;
protected Drawable mLoadingDrawable;
protected Drawable mFailedDrawable;
private boolean mDidFail = false;

public RemoteDrawable(Drawable loadingDrawable, Drawable failedDrawable) {
mLoadingDrawable = loadingDrawable;
mFailedDrawable = failedDrawable;
setBounds(0, 0, mLoadingDrawable.getIntrinsicWidth(), mLoadingDrawable.getIntrinsicHeight());
}

public void displayFailed() {
mDidFail = true;
}

public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
if (mRemoteDrawable != null) {
mRemoteDrawable.setBounds(x, y, width, height);
return;
}
if (mLoadingDrawable != null) {
mLoadingDrawable.setBounds(x, y, width, height);
mFailedDrawable.setBounds(x, y, width, height);
}
}

public void setRemoteDrawable(Drawable remote) {
mRemoteDrawable = remote;
setBounds(0, 0, mRemoteDrawable.getIntrinsicWidth(), mRemoteDrawable.getIntrinsicHeight());
}

public void setRemoteDrawable(Drawable remote, int maxWidth) {
// null sentinel for now
if (remote == null) {
// throw error
return;
}
mRemoteDrawable = remote;
// determine if we need to scale the image to fit in view
int imgWidth = remote.getIntrinsicWidth();
int imgHeight = remote.getIntrinsicHeight();
float xScale = (float) imgWidth / (float) maxWidth;
if (xScale > 1.0f) {
setBounds(0, 0, Math.round(imgWidth / xScale), Math.round(imgHeight / xScale));
} else {
setBounds(0, 0, imgWidth, imgHeight);
}
}

public boolean didFail() {
return mDidFail;
}

public void draw(Canvas canvas) {
if (mRemoteDrawable != null) {
mRemoteDrawable.draw(canvas);
} else if (didFail()) {
mFailedDrawable.draw(canvas);
} else {
mLoadingDrawable.draw(canvas);
}
}
}
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Jun 30 12:35:54 EDT 2014
#Wed Jul 09 11:48:51 CEST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-1.11-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-1.11-all.zip

0 comments on commit 7c6d4f4

Please sign in to comment.