aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart
diff options
context:
space:
mode:
authorChristian Schneppe <christian.schneppe@pix-art.de>2020-02-09 20:26:15 +0100
committerChristian Schneppe <christian.schneppe@pix-art.de>2020-02-09 21:03:37 +0100
commitb10b73caad1737cce836193414feeb8379b886e0 (patch)
treec8cd7178835181c89014747473052b0449e1cde8 /src/main/java/de/pixart
parent482cb8926799b226d9b74c1e47fe05d1ba070ad2 (diff)
introduce preview for PDF files in chat
Diffstat (limited to 'src/main/java/de/pixart')
-rw-r--r--src/main/java/de/pixart/messenger/persistance/FileBackend.java32
-rw-r--r--src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java62
-rw-r--r--src/main/java/de/pixart/messenger/utils/Compatibility.java4
3 files changed, 79 insertions, 19 deletions
diff --git a/src/main/java/de/pixart/messenger/persistance/FileBackend.java b/src/main/java/de/pixart/messenger/persistance/FileBackend.java
index babc7f888..fb095502a 100644
--- a/src/main/java/de/pixart/messenger/persistance/FileBackend.java
+++ b/src/main/java/de/pixart/messenger/persistance/FileBackend.java
@@ -14,6 +14,7 @@ import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
+import android.graphics.pdf.PdfRenderer;
import android.media.MediaMetadataRetriever;
import android.media.MediaScannerConnection;
import android.net.Uri;
@@ -63,6 +64,7 @@ import de.pixart.messenger.entities.Message;
import de.pixart.messenger.services.AttachFileToConversationRunnable;
import de.pixart.messenger.services.XmppConnectionService;
import de.pixart.messenger.ui.util.Attachment;
+import de.pixart.messenger.utils.Compatibility;
import de.pixart.messenger.utils.CryptoHelper;
import de.pixart.messenger.utils.ExifHelper;
import de.pixart.messenger.utils.FileUtils;
@@ -651,7 +653,9 @@ public class FileBackend {
final String mime = file.getMimeType();
if (mime.startsWith("video/")) {
thumbnail = getVideoPreview(file, size);
- } else {
+ } else if ((mime.contains("pdf")) && Compatibility.runsTwentyOne()) {
+ thumbnail = getPDFPreview(file, size);
+ } else if (mime.startsWith("image/")) {
Bitmap fullsize = getFullsizeImagePreview(file, size);
if (fullsize == null) {
throw new FileNotFoundException();
@@ -671,6 +675,32 @@ public class FileBackend {
return thumbnail;
}
+ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
+ private Bitmap getPDFPreview(DownloadableFile file, int size) {
+ try {
+ ParcelFileDescriptor mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
+ if (mFileDescriptor == null) {
+ return null;
+ }
+ PdfRenderer renderer = new PdfRenderer(mFileDescriptor);
+ PdfRenderer.Page page = renderer.openPage(0);
+ int width = page.getWidth();
+ int height = page.getHeight();
+ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
+ bitmap.eraseColor(Color.WHITE);
+ page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
+ drawOverlay(bitmap, R.drawable.show_pdf, 0.75f);
+ page.close();
+ renderer.close();
+ return bitmap;
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
private Bitmap getFullsizeImagePreview(File file, int size) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = calcSampleSize(file, size);
diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java
index 90582815e..38016229d 100644
--- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java
+++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java
@@ -100,6 +100,7 @@ import static de.pixart.messenger.ui.SettingsActivity.SHOW_LINKS_INSIDE;
import static de.pixart.messenger.ui.SettingsActivity.SHOW_MAPS_INSIDE;
import static de.pixart.messenger.ui.util.MyLinkify.removeTrailingBracket;
import static de.pixart.messenger.ui.util.MyLinkify.replaceYoutube;
+import static de.pixart.messenger.utils.Compatibility.runsTwentyOne;
public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextView.CopyHandler {
@@ -674,19 +675,14 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
private void displayOpenableMessage(ViewHolder viewHolder, final Message message, final boolean darkBackground) {
toggleWhisperInfo(viewHolder, message, false, darkBackground);
- viewHolder.audioPlayer.setVisibility(View.GONE);
- viewHolder.image.setVisibility(View.GONE);
- viewHolder.gifImage.setVisibility(View.GONE);
- viewHolder.richlinkview.setVisibility(View.GONE);
- viewHolder.progressBar.setVisibility(View.GONE);
- viewHolder.download_button.setVisibility(View.VISIBLE);
final String mimeType = message.getMimeType();
if (mimeType != null && message.getMimeType().contains("pdf")) {
- Drawable icon = activity.getResources().getDrawable(R.drawable.ic_file_pdf_grey600_48dp);
- Drawable drawable = DrawableCompat.wrap(icon);
- DrawableCompat.setTint(drawable, StyledAttributes.getColor(getContext(), R.attr.colorAccent));
- viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
- viewHolder.download_button.setText(activity.getString(R.string.open_x_file, UIHelper.getFileDescriptionString(activity, message)));
+ try {
+ showPDF(message, viewHolder, false);
+ } catch (Exception e) {
+ e.printStackTrace();
+ showPDF(message, viewHolder, true);
+ }
} else if (mimeType != null && message.getMimeType().contains("vcard")) {
try {
showVCard(message, viewHolder);
@@ -733,7 +729,33 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.download_button.setOnClickListener(v -> openDownloadable(message));
}
- private void showAPK(final Message message, ViewHolder viewHolder) {
+ private void showPDF(final Message message, final ViewHolder viewHolder, boolean compat) {
+ if (runsTwentyOne() && !compat) {
+ viewHolder.audioPlayer.setVisibility(View.GONE);
+ viewHolder.image.setVisibility(View.VISIBLE);
+ viewHolder.gifImage.setVisibility(View.GONE);
+ viewHolder.richlinkview.setVisibility(View.GONE);
+ viewHolder.progressBar.setVisibility(View.GONE);
+ viewHolder.download_button.setVisibility(View.GONE);
+ activity.loadBitmap(message, viewHolder.image);
+ viewHolder.image.setOnClickListener(v -> openDownloadable(message));
+ } else {
+ viewHolder.audioPlayer.setVisibility(View.GONE);
+ viewHolder.image.setVisibility(View.GONE);
+ viewHolder.gifImage.setVisibility(View.GONE);
+ viewHolder.richlinkview.setVisibility(View.GONE);
+ viewHolder.progressBar.setVisibility(View.GONE);
+ viewHolder.download_button.setVisibility(View.VISIBLE);
+ Drawable icon = activity.getResources().getDrawable(R.drawable.ic_file_pdf_grey600_48dp);
+ Drawable drawable = DrawableCompat.wrap(icon);
+ DrawableCompat.setTint(drawable, StyledAttributes.getColor(getContext(), R.attr.colorAccent));
+ viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
+ viewHolder.download_button.setText(activity.getString(R.string.open_x_file, UIHelper.getFileDescriptionString(activity, message)));
+ viewHolder.download_button.setOnClickListener(v -> openDownloadable(message));
+ }
+ }
+
+ private void showAPK(final Message message, final ViewHolder viewHolder) {
String APKName = "";
if (message.getFileParams().subject.length() != 0) {
try {
@@ -769,7 +791,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.download_button.setText(activity.getString(R.string.open_x_file, UIHelper.getFileDescriptionString(activity, message) + VCardName));
}
- private void displayRichLinkMessage(ViewHolder viewHolder, final Message message, boolean darkBackground) {
+ private void displayRichLinkMessage(final ViewHolder viewHolder, final Message message, boolean darkBackground) {
toggleWhisperInfo(viewHolder, message, true, darkBackground);
viewHolder.audioPlayer.setVisibility(View.GONE);
viewHolder.image.setVisibility(View.GONE);
@@ -817,7 +839,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
}
}
- private void displayLocationMessage(ViewHolder viewHolder, final Message message, final boolean darkBackground) {
+ private void displayLocationMessage(final ViewHolder viewHolder, final Message message, final boolean darkBackground) {
toggleWhisperInfo(viewHolder, message, false, darkBackground);
viewHolder.audioPlayer.setVisibility(View.GONE);
String url = GeoHelper.MapPreviewUri(message);
@@ -864,7 +886,8 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
}
}
- private void displayAudioMessage(ViewHolder viewHolder, Message message, boolean darkBackground) {
+ private void displayAudioMessage(ViewHolder viewHolder, Message message,
+ boolean darkBackground) {
toggleWhisperInfo(viewHolder, message, showTitle(message), darkBackground);
viewHolder.image.setVisibility(View.GONE);
viewHolder.gifImage.setVisibility(View.GONE);
@@ -902,7 +925,8 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
return "";
}
- private void displayImageMessage(ViewHolder viewHolder, final Message message, final boolean darkBackground) {
+ private void displayImageMessage(ViewHolder viewHolder, final Message message,
+ final boolean darkBackground) {
toggleWhisperInfo(viewHolder, message, false, darkBackground);
viewHolder.download_button.setVisibility(View.GONE);
viewHolder.audioPlayer.setVisibility(View.GONE);
@@ -968,7 +992,8 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
}
}
- private void toggleWhisperInfo(ViewHolder viewHolder, final Message message, final boolean includeBody, final boolean darkBackground) {
+ private void toggleWhisperInfo(ViewHolder viewHolder, final Message message,
+ final boolean includeBody, final boolean darkBackground) {
SpannableStringBuilder messageBody = new SpannableStringBuilder(replaceYoutube(activity.getApplicationContext(), message.getBody()));
final String mimeType = message.getMimeType();
@@ -1495,7 +1520,8 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
}
}
- public void setBubbleBackgroundColor(final View viewHolder, final int type, final boolean isPrivateMessage, final boolean isInValidSession) {
+ public void setBubbleBackgroundColor(final View viewHolder, final int type,
+ final boolean isPrivateMessage, final boolean isInValidSession) {
if (type == RECEIVED) {
if (isInValidSession) {
if (isPrivateMessage) {
diff --git a/src/main/java/de/pixart/messenger/utils/Compatibility.java b/src/main/java/de/pixart/messenger/utils/Compatibility.java
index b184fdce3..96a72fa23 100644
--- a/src/main/java/de/pixart/messenger/utils/Compatibility.java
+++ b/src/main/java/de/pixart/messenger/utils/Compatibility.java
@@ -40,6 +40,10 @@ public class Compatibility {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || (ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
}
+ public static boolean runsTwentyOne() {
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
+ }
+
public static boolean runsTwentySix() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
}