Added PDF previews

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

@ -23,11 +23,15 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.pdf.PdfRenderer;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
@ -60,6 +64,8 @@ public class AdapterImage extends RecyclerView.Adapter<AdapterImage.ViewHolder>
private List<EntityAttachment> items = new ArrayList<>();
private static final int PDF_WIDTH = 120;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private View view;
private ImageView ivImage;
@ -108,45 +114,62 @@ public class AdapterImage extends RecyclerView.Adapter<AdapterImage.ViewHolder>
String type = args.getString("type");
int max = args.getInt("max");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean webp = prefs.getBoolean("webp", true);
if ("image/webp".equalsIgnoreCase(type) && !webp) {
args.putBoolean("nowebp", true);
return null;
}
if ("application/pdf".equals(type)) {
// https://medium.com/@aditya09tyagi/android-pdf-reader-using-pdfrenderer-6daa2dacec1a
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)) {
try (PdfRenderer pdf = new PdfRenderer(pfd)) {
try (PdfRenderer.Page page = pdf.openPage(0)) {
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());
try {
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());
if ("image/webp".equalsIgnoreCase(type) && !webp) {
args.putBoolean("nowebp", true);
return null;
}
} catch (Throwable ex) {
Log.w(ex);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P &&
!"image/svg+xml".equals(type) &&
!"svg".equals(Helper.getExtension(file.getName())))
args.putLong("size", file.length());
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) {
Log.w(ex);
}
Bitmap bm = ImageHelper.decodeImage(file, type, max);
if (bm == null)
return null;
return new BitmapDrawable(context.getResources(), bm);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P &&
!"image/svg+xml".equals(type) &&
!"svg".equals(Helper.getExtension(file.getName())))
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

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

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

Loading…
Cancel
Save