Prevent crash

pull/162/head
M66B 6 years ago
parent 7c445696dc
commit ad5b4f3f73

@ -523,222 +523,229 @@ public class HtmlHelper {
boolean inline = prefs.getBoolean("inline_images", false); boolean inline = prefs.getBoolean("inline_images", false);
final int px = Helper.dp2pixels(context, (zoom + 1) * 24); final int px = Helper.dp2pixels(context, (zoom + 1) * 24);
final Resources.Theme theme = context.getTheme(); final Resources.Theme theme = context.getTheme();
final Resources res = context.getResources(); final Resources res = context.getResources();
final AnnotatedSource a = new AnnotatedSource(source); try {
final AnnotatedSource a = new AnnotatedSource(source);
if (TextUtils.isEmpty(a.source)) {
Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme);
d.setBounds(0, 0, px, px);
return d;
}
boolean embedded = a.source.startsWith("cid:");
boolean data = a.source.startsWith("data:");
if (BuildConfig.DEBUG)
Log.i("Image show=" + show + " inline=" + inline +
" embedded=" + embedded + " data=" + data + " source=" + a.source);
// Embedded images if (TextUtils.isEmpty(a.source)) {
if (embedded && (show || inline)) {
DB db = DB.getInstance(context);
String cid = "<" + a.source.substring(4) + ">";
EntityAttachment attachment = db.attachment().getAttachment(id, cid);
if (attachment == null) {
Log.i("Image not found CID=" + cid);
Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme); Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme);
d.setBounds(0, 0, px, px); d.setBounds(0, 0, px, px);
return d; return d;
} else if (!attachment.available) { }
Log.i("Image not available CID=" + cid);
Drawable d = res.getDrawable(R.drawable.baseline_photo_library_24, theme); boolean embedded = a.source.startsWith("cid:");
d.setBounds(0, 0, px, px); boolean data = a.source.startsWith("data:");
return d;
} else { if (BuildConfig.DEBUG)
int scaleToPixels = res.getDisplayMetrics().widthPixels; Log.i("Image show=" + show + " inline=" + inline +
if ("image/gif".equals(attachment.type) && " embedded=" + embedded + " data=" + data + " source=" + a.source);
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.Source isource = ImageDecoder.createSource(attachment.getFile(context)); // Embedded images
Drawable gif; if (embedded && (show || inline)) {
try { DB db = DB.getInstance(context);
gif = ImageDecoder.decodeDrawable(isource, new ImageDecoder.OnHeaderDecodedListener() { String cid = "<" + a.source.substring(4) + ">";
@Override EntityAttachment attachment = db.attachment().getAttachment(id, cid);
public void onHeaderDecoded( if (attachment == null) {
@NonNull ImageDecoder decoder, Log.i("Image not found CID=" + cid);
@NonNull ImageDecoder.ImageInfo info, Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme);
@NonNull ImageDecoder.Source source) { d.setBounds(0, 0, px, px);
int factor = 1; return d;
while (info.getSize().getWidth() / factor > scaleToPixels) } else if (!attachment.available) {
factor *= 2; Log.i("Image not available CID=" + cid);
Drawable d = res.getDrawable(R.drawable.baseline_photo_library_24, theme);
decoder.setTargetSampleSize(factor); d.setBounds(0, 0, px, px);
} return d;
});
} catch (IOException ex) {
Log.w(ex);
gif = null;
}
if (gif == null) {
Log.i("GIF not decodable CID=" + cid);
Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme);
d.setBounds(0, 0, px, px);
return d;
} else {
if (view != null)
fitDrawable(gif, a, view);
return gif;
}
} else { } else {
Bitmap bm = Helper.decodeImage(attachment.getFile(context), scaleToPixels); int scaleToPixels = res.getDisplayMetrics().widthPixels;
if (bm == null) { if ("image/gif".equals(attachment.type) &&
Log.i("Image not decodable CID=" + cid); Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme); ImageDecoder.Source isource = ImageDecoder.createSource(attachment.getFile(context));
d.setBounds(0, 0, px, px); Drawable gif;
return d; try {
gif = ImageDecoder.decodeDrawable(isource, new ImageDecoder.OnHeaderDecodedListener() {
@Override
public void onHeaderDecoded(
@NonNull ImageDecoder decoder,
@NonNull ImageDecoder.ImageInfo info,
@NonNull ImageDecoder.Source source) {
int factor = 1;
while (info.getSize().getWidth() / factor > scaleToPixels)
factor *= 2;
decoder.setTargetSampleSize(factor);
}
});
} catch (IOException ex) {
Log.w(ex);
gif = null;
}
if (gif == null) {
Log.i("GIF not decodable CID=" + cid);
Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme);
d.setBounds(0, 0, px, px);
return d;
} else {
if (view != null)
fitDrawable(gif, a, view);
return gif;
}
} else { } else {
Drawable d = new BitmapDrawable(res, bm); Bitmap bm = Helper.decodeImage(attachment.getFile(context), scaleToPixels);
DisplayMetrics dm = context.getResources().getDisplayMetrics(); if (bm == null) {
d.setBounds(0, 0, Math.round(bm.getWidth() * dm.density), Math.round(bm.getHeight() * dm.density)); Log.i("Image not decodable CID=" + cid);
if (view != null) Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme);
fitDrawable(d, a, view); d.setBounds(0, 0, px, px);
return d; return d;
} else {
Drawable d = new BitmapDrawable(res, bm);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
d.setBounds(0, 0, Math.round(bm.getWidth() * dm.density), Math.round(bm.getHeight() * dm.density));
if (view != null)
fitDrawable(d, a, view);
return d;
}
} }
} }
} }
}
// Data URI // Data URI
if (data && (show || inline)) if (data && (show || inline))
try { try {
Drawable d = getDataDrawable(context, a.source); Drawable d = getDataDrawable(context, a.source);
if (view != null) if (view != null)
fitDrawable(d, a, view); fitDrawable(d, a, view);
return d; return d;
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
Log.w(ex); Log.w(ex);
Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme); Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme);
d.setBounds(0, 0, px, px);
return d;
}
if (!show) {
// Show placeholder icon
int resid = (embedded || data ? R.drawable.baseline_photo_library_24 : R.drawable.baseline_image_24);
Drawable d = res.getDrawable(resid, theme);
d.setBounds(0, 0, px, px); d.setBounds(0, 0, px, px);
return d; return d;
} }
if (!show) { // Get cache file name
// Show placeholder icon File dir = new File(context.getCacheDir(), "images");
int resid = (embedded || data ? R.drawable.baseline_photo_library_24 : R.drawable.baseline_image_24); if (!dir.exists())
Drawable d = res.getDrawable(resid, theme); dir.mkdir();
d.setBounds(0, 0, px, px); final File file = new File(dir, id + "_" + Math.abs(a.source.hashCode()) + ".png");
return d;
} Drawable cached = getCachedImage(context, file);
if (cached != null || view == null) {
if (view == null)
if (cached == null) {
Drawable d = res.getDrawable(R.drawable.baseline_hourglass_empty_24, theme);
d.setBounds(0, 0, px, px);
return d;
} else
return cached;
else
fitDrawable(cached, a, view);
return cached;
}
// Get cache file name final LevelListDrawable lld = new LevelListDrawable();
File dir = new File(context.getCacheDir(), "images"); Drawable wait = res.getDrawable(R.drawable.baseline_hourglass_empty_24, theme);
if (!dir.exists()) lld.addLevel(1, 1, wait);
dir.mkdir(); lld.setBounds(0, 0, px, px);
final File file = new File(dir, id + "_" + Math.abs(a.source.hashCode()) + ".png"); lld.setLevel(1);
Drawable cached = getCachedImage(context, file);
if (cached != null || view == null) {
if (view == null)
if (cached == null) {
Drawable d = res.getDrawable(R.drawable.baseline_hourglass_empty_24, theme);
d.setBounds(0, 0, px, px);
return d;
} else
return cached;
else
fitDrawable(cached, a, view);
return cached;
}
final LevelListDrawable lld = new LevelListDrawable(); executor.submit(new Runnable() {
Drawable wait = res.getDrawable(R.drawable.baseline_hourglass_empty_24, theme); @Override
lld.addLevel(1, 1, wait); public void run() {
lld.setBounds(0, 0, px, px); try {
lld.setLevel(1); Drawable cached = getCachedImage(context, file);
if (cached != null) {
fitDrawable(cached, a, view);
post(cached, a.source);
return;
}
executor.submit(new Runnable() { BitmapFactory.Options options = new BitmapFactory.Options();
@Override Log.i("Probe " + a.source);
public void run() { try (InputStream probe = new URL(a.source).openStream()) {
try { options.inJustDecodeBounds = true;
Drawable cached = getCachedImage(context, file); BitmapFactory.decodeStream(probe, null, options);
if (cached != null) { }
fitDrawable(cached, a, view);
post(cached, a.source);
return;
}
BitmapFactory.Options options = new BitmapFactory.Options(); Log.i("Download " + a.source);
Log.i("Probe " + a.source); Bitmap bm;
try (InputStream probe = new URL(a.source).openStream()) { try (InputStream is = new URL(a.source).openStream()) {
options.inJustDecodeBounds = true; int scaleTo = res.getDisplayMetrics().widthPixels;
BitmapFactory.decodeStream(probe, null, options); int factor = 1;
} while (options.outWidth / factor > scaleTo)
factor *= 2;
if (factor > 1) {
Log.i("Download image factor=" + factor);
options.inJustDecodeBounds = false;
options.inSampleSize = factor;
bm = BitmapFactory.decodeStream(is, null, options);
} else
bm = BitmapFactory.decodeStream(is);
}
Log.i("Download " + a.source); if (bm == null)
Bitmap bm; throw new FileNotFoundException("Download image failed source=" + a.source);
try (InputStream is = new URL(a.source).openStream()) {
int scaleTo = res.getDisplayMetrics().widthPixels;
int factor = 1;
while (options.outWidth / factor > scaleTo)
factor *= 2;
if (factor > 1) {
Log.i("Download image factor=" + factor);
options.inJustDecodeBounds = false;
options.inSampleSize = factor;
bm = BitmapFactory.decodeStream(is, null, options);
} else
bm = BitmapFactory.decodeStream(is);
}
if (bm == null) Log.i("Downloaded image source=" + a.source);
throw new FileNotFoundException("Download image failed source=" + a.source);
Log.i("Downloaded image source=" + a.source); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
bm.compress(Bitmap.CompressFormat.PNG, 90, os);
}
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { // Create drawable from bitmap
bm.compress(Bitmap.CompressFormat.PNG, 90, os); Drawable d = new BitmapDrawable(res, bm);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
d.setBounds(0, 0, Math.round(bm.getWidth() * dm.density), Math.round(bm.getHeight() * dm.density));
fitDrawable(d, a, view);
post(d, a.source);
} catch (Throwable ex) {
// Show broken icon
Log.w(ex);
int resid = (ex instanceof IOException && !(ex instanceof FileNotFoundException)
? R.drawable.baseline_cloud_off_24
: R.drawable.baseline_broken_image_24);
Drawable d = res.getDrawable(resid, theme);
d.setBounds(0, 0, px, px);
post(d, a.source);
} }
// Create drawable from bitmap
Drawable d = new BitmapDrawable(res, bm);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
d.setBounds(0, 0, Math.round(bm.getWidth() * dm.density), Math.round(bm.getHeight() * dm.density));
fitDrawable(d, a, view);
post(d, a.source);
} catch (Throwable ex) {
// Show broken icon
Log.w(ex);
int resid = (ex instanceof IOException && !(ex instanceof FileNotFoundException)
? R.drawable.baseline_cloud_off_24
: R.drawable.baseline_broken_image_24);
Drawable d = res.getDrawable(resid, theme);
d.setBounds(0, 0, px, px);
post(d, a.source);
} }
}
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);
new Handler(context.getMainLooper()).post(new Runnable() { new Handler(context.getMainLooper()).post(new Runnable() {
@Override @Override
public void run() { public void run() {
Rect bounds = d.getBounds(); Rect bounds = d.getBounds();
lld.addLevel(0, 0, d); lld.addLevel(0, 0, d);
lld.setBounds(0, 0, bounds.width(), bounds.height()); lld.setBounds(0, 0, bounds.width(), bounds.height());
lld.setLevel(0); lld.setLevel(0);
view.setText(view.getText()); view.setText(view.getText());
} }
}); });
} }
}); });
return lld; return lld;
} catch (Throwable ex) {
Log.e(ex);
Drawable d = res.getDrawable(R.drawable.baseline_broken_image_24, theme);
d.setBounds(0, 0, px, px);
return d;
}
} }
private static void fitDrawable(Drawable d, AnnotatedSource a, View view) { private static void fitDrawable(Drawable d, AnnotatedSource a, View view) {
@ -787,7 +794,7 @@ public class HtmlHelper {
return d; return d;
} }
static private Drawable getCachedImage(Context context, File file) { private static Drawable getCachedImage(Context context, File file) {
if (file.exists()) { if (file.exists()) {
Log.i("Using cached " + file); Log.i("Using cached " + file);
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath()); Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());

Loading…
Cancel
Save