Added PDF previews

pull/214/head
M66B 11 months ago
parent 415c5d189b
commit f913347455

@ -23,11 +23,15 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.AnimatedImageDrawable; import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.pdf.PdfRenderer;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -60,6 +64,8 @@ public class AdapterImage extends RecyclerView.Adapter<AdapterImage.ViewHolder>
private List<EntityAttachment> items = new ArrayList<>(); private List<EntityAttachment> items = new ArrayList<>();
private static final int PDF_WIDTH = 120;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private View view; private View view;
private ImageView ivImage; private ImageView ivImage;
@ -108,45 +114,62 @@ public class AdapterImage extends RecyclerView.Adapter<AdapterImage.ViewHolder>
String type = args.getString("type"); String type = args.getString("type");
int max = args.getInt("max"); int max = args.getInt("max");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if ("application/pdf".equals(type)) {
boolean webp = prefs.getBoolean("webp", true); // https://medium.com/@aditya09tyagi/android-pdf-reader-using-pdfrenderer-6daa2dacec1a
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)) {
if ("image/webp".equalsIgnoreCase(type) && !webp) { try (PdfRenderer pdf = new PdfRenderer(pfd)) {
args.putBoolean("nowebp", true); try (PdfRenderer.Page page = pdf.openPage(0)) {
return null; int width = Helper.dp2pixels(context, PDF_WIDTH);
} int height = (int) ((float) width / page.getWidth() * page.getHeight());
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
canvas.drawColor(Color.WHITE);
page.render(bm, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
return new BitmapDrawable(context.getResources(), bm);
}
}
}
} else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean webp = prefs.getBoolean("webp", true);
args.putLong("size", file.length()); if ("image/webp".equalsIgnoreCase(type) && !webp) {
args.putBoolean("nowebp", true);
try { return null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
args.putInt("width", options.outWidth);
args.putInt("height", options.outHeight);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (options.outColorSpace != null)
args.putString("color", options.outColorSpace.getModel().name());
if (options.outConfig != null)
args.putString("config", options.outConfig.name());
} }
} catch (Throwable ex) {
Log.w(ex);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && args.putLong("size", file.length());
!"image/svg+xml".equals(type) &&
!"svg".equals(Helper.getExtension(file.getName())))
try { try {
return ImageHelper.getScaledDrawable(context, file, type, max); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
args.putInt("width", options.outWidth);
args.putInt("height", options.outHeight);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (options.outColorSpace != null)
args.putString("color", options.outColorSpace.getModel().name());
if (options.outConfig != null)
args.putString("config", options.outConfig.name());
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
Bitmap bm = ImageHelper.decodeImage(file, type, max); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P &&
if (bm == null) !"image/svg+xml".equals(type) &&
return null; !"svg".equals(Helper.getExtension(file.getName())))
return new BitmapDrawable(context.getResources(), bm); try {
return ImageHelper.getScaledDrawable(context, file, type, max);
} catch (Throwable ex) {
Log.w(ex);
}
Bitmap bm = ImageHelper.decodeImage(file, type, max);
if (bm == null)
return null;
return new BitmapDrawable(context.getResources(), bm);
}
} }
@Override @Override

@ -3650,9 +3650,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
List<EntityAttachment> images = new ArrayList<>(); List<EntityAttachment> images = new ArrayList<>();
if (thumbnails && bind_extras) { if (thumbnails && bind_extras) {
for (EntityAttachment attachment : attachments) for (EntityAttachment attachment : attachments)
if (attachment.isAttachment() && attachment.isImage()) { if (attachment.isPDF() ||
(attachment.isAttachment() && attachment.isImage())) {
images.add(attachment); images.add(attachment);
if (attachment.available) if (attachment.available && !attachment.isPDF())
iavailable++; iavailable++;
} }
} }

@ -117,6 +117,10 @@ public class EntityAttachment {
return ImageHelper.isImage(getMimeType()); return ImageHelper.isImage(getMimeType());
} }
boolean isPDF() {
return "application/pdf".equals(getMimeType());
}
boolean isCompressed() { boolean isCompressed() {
if ("application/zip".equals(type)) if ("application/zip".equals(type))
return true; return true;

Loading…
Cancel
Save