Skip running image task if image cached

This commit is contained in:
M66B 2019-05-04 13:06:22 +02:00
parent d8dc372c37
commit a1ab37367a
1 changed files with 29 additions and 18 deletions

View File

@ -351,33 +351,30 @@ public class HtmlHelper {
return d;
}
// Get cache file name
File dir = new File(view.getContext().getCacheDir(), "images");
if (!dir.exists())
dir.mkdir();
final File file = new File(dir, id + "_" + Math.abs(source.hashCode()) + ".png");
Drawable cached = getCachedImage(view.getContext(), file);
if (cached != null)
return cached;
final LevelListDrawable lld = new LevelListDrawable();
Drawable wait = res.getDrawable(R.drawable.baseline_hourglass_empty_24, theme);
lld.addLevel(0, 0, wait);
lld.setBounds(0, 0, px, px);
final Context context = view.getContext().getApplicationContext();
final Handler handler = new Handler(view.getContext().getMainLooper());
executor.submit(new Runnable() {
@Override
public void run() {
try {
// Get cache file name
File dir = new File(context.getCacheDir(), "images");
if (!dir.exists())
dir.mkdir();
File file = new File(dir, id + "_" + Math.abs(source.hashCode()) + ".png");
// Get cached image
if (file.exists() && !BuildConfig.DEBUG) {
Log.i("Using cached " + file);
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
if (bm != null) {
Drawable d = new BitmapDrawable(res, bm);
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
post(d, source);
return;
}
Drawable cached = getCachedImage(context, file);
if (cached != null) {
post(cached, source);
return;
}
BitmapFactory.Options options = new BitmapFactory.Options();
@ -431,7 +428,7 @@ public class HtmlHelper {
private void post(final Drawable d, String source) {
Log.i("Posting image=" + source);
handler.post(new Runnable() {
new Handler(context.getMainLooper()).post(new Runnable() {
@Override
public void run() {
int w = d.getIntrinsicWidth();
@ -458,6 +455,20 @@ public class HtmlHelper {
return lld;
}
static private Drawable getCachedImage(Context context, File file) {
if (file.exists()) {
Log.i("Using cached " + file);
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
if (bm != null) {
Drawable d = new BitmapDrawable(context.getResources(), bm);
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
return d;
}
}
return null;
}
static String getPreview(String body) {
String text = (body == null ? null : Jsoup.parse(body).text());
return (text == null ? null : text.substring(0, Math.min(text.length(), PREVIEW_SIZE)));