Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// LICENSE: USE IT IF YOU NEED IT, BUT PLEASE MENTION AT LEAST MY NAME, THANKS!
// (Juraj Suchán, juraj.suchan@gmail.com)
//
package sk.virtualvoid.nyxdroid.utils;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Executor;
import sk.virtualvoid.android.AsyncTask;
import sk.virtualvoid.android.Log;
import sk.virtualvoid.nyxdroid.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.widget.TextView;
/**
*
* @author Juraj
*
*/
public class ImageGetterAsync {
private Context mContext;
private Resources mResources;
private Drawable mDefaultDrawable;
private Executor mExecutor;
private static final Map<String, WeakReference<Drawable>> mDrawableCache = Collections.synchronizedMap(new WeakHashMap<String, WeakReference<Drawable>>());
private static final Html.ImageGetter mImageGetterCached = new Html.ImageGetter() {
public Drawable getDrawable(String source) {
if (!mDrawableCache.containsKey(source)) {
return null;
}
return mDrawableCache.get(source).get();
}
};
public ImageGetterAsync(Context context, Executor executor) {
mContext = context;
mResources = mContext.getResources();
mDefaultDrawable = mResources.getDrawable(R.drawable.empty_photo);
mDefaultDrawable.setBounds(0, 0, mDefaultDrawable.getIntrinsicWidth(), mDefaultDrawable.getIntrinsicHeight());
mExecutor = executor;
}
public Html.ImageGetter spawn(String message, TextView textView) {
final String _message = message;
final TextView _textView = textView;
return new Html.ImageGetter() {
public Drawable getDrawable(String source) {
if (mDrawableCache.containsKey(source) && mDrawableCache.get(source).get() != null) {
return mDrawableCache.get(source).get();
}
ImageDownloadData imageDownloadData = new ImageDownloadData(_message, _textView, source);
ImageDownloadAsyncTask imageDownloadTask = new ImageDownloadAsyncTask(imageDownloadData);
imageDownloadTask.executeOnExecutor(mExecutor, null);
return mDefaultDrawable;
}
};
}
public static void invalidate() {
mDrawableCache.clear();
}
/**
*
* @author Juraj
*
*/
class ImageDownloadData {
private String content;
private TextView textView;
private String source;
public ImageDownloadData(String content, TextView textView, String source) {
this.content = content;
this.textView = textView;
this.source = source;
}
public String getContent() {
return this.content;
}
public TextView getTextView() {
return this.textView;
}
public String getSource() {
return this.source;
}
}
/**
*
* @author Juraj
*
*/
class ImageDownloadAsyncTask extends AsyncTask<Void, Void, Drawable> {
private ImageDownloadData imageDownloadData;
public ImageDownloadAsyncTask(ImageDownloadData imageDownloadData) {
this.imageDownloadData = imageDownloadData;
}
@Override
protected Drawable doInBackground(Void... params) {
try {
File file = U.downloadFileCache(imageDownloadData.getSource());
if (file == null) {
file = U.downloadFileWeb(imageDownloadData.getSource());
}
if (file == null) {
return null;
}
Bitmap bitmap = U.getBitmapFromFile(file);
if (bitmap == null) {
return null;
}
Drawable drawable = new BitmapDrawable(mResources, bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
} catch (Throwable t) {
Log.e("ImageDownloadAsyncTask: " + t.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Drawable drawable) {
if (!mDrawableCache.containsKey(imageDownloadData.getSource())) {
mDrawableCache.put(imageDownloadData.getSource(), new WeakReference<Drawable>(drawable));
}
TextView textView = imageDownloadData.getTextView();
String content = imageDownloadData.getContent();
textView.setText(MyHtml.fromHtml(content, mImageGetterCached, null));
}
}
}
private ImageGetterAsync mImageGetter;
mImageGetter = new ImageGetterAsync(mContext, Executors.newSingleThreadExecutor());
textView.setText(Html.fromHtml(htmlString, mImageGetter.spawn(htmlString, textView), null));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment