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,34 +351,31 @@ public class HtmlHelper {
return d; 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(); final LevelListDrawable lld = new LevelListDrawable();
Drawable wait = res.getDrawable(R.drawable.baseline_hourglass_empty_24, theme); Drawable wait = res.getDrawable(R.drawable.baseline_hourglass_empty_24, theme);
lld.addLevel(0, 0, wait); lld.addLevel(0, 0, wait);
lld.setBounds(0, 0, px, px); lld.setBounds(0, 0, px, px);
final Context context = view.getContext().getApplicationContext(); final Context context = view.getContext().getApplicationContext();
final Handler handler = new Handler(view.getContext().getMainLooper());
executor.submit(new Runnable() { executor.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
// Get cache file name Drawable cached = getCachedImage(context, file);
File dir = new File(context.getCacheDir(), "images"); if (cached != null) {
if (!dir.exists()) post(cached, source);
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; return;
} }
}
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
Log.i("Probe " + source); Log.i("Probe " + source);
@ -431,7 +428,7 @@ public class HtmlHelper {
private void post(final Drawable d, String source) { private void post(final Drawable d, String source) {
Log.i("Posting image=" + source); Log.i("Posting image=" + source);
handler.post(new Runnable() { new Handler(context.getMainLooper()).post(new Runnable() {
@Override @Override
public void run() { public void run() {
int w = d.getIntrinsicWidth(); int w = d.getIntrinsicWidth();
@ -458,6 +455,20 @@ public class HtmlHelper {
return lld; 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) { static String getPreview(String body) {
String text = (body == null ? null : Jsoup.parse(body).text()); String text = (body == null ? null : Jsoup.parse(body).text());
return (text == null ? null : text.substring(0, Math.min(text.length(), PREVIEW_SIZE))); return (text == null ? null : text.substring(0, Math.min(text.length(), PREVIEW_SIZE)));