2018-09-15 17:38:45 +00:00
|
|
|
package eu.siacs.conversations.ui.adapter;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.AsyncTask;
|
2020-07-19 19:27:43 +00:00
|
|
|
import android.util.Log;
|
2018-09-15 17:38:45 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
2021-01-23 08:25:34 +00:00
|
|
|
import androidx.annotation.AttrRes;
|
|
|
|
import androidx.annotation.DimenRes;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.databinding.DataBindingUtil;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2018-09-15 17:38:45 +00:00
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
|
|
|
|
2020-07-19 19:27:43 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2018-09-15 17:38:45 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.databinding.MediaBinding;
|
2019-07-15 17:11:00 +00:00
|
|
|
import eu.siacs.conversations.services.ExportBackupService;
|
2018-09-15 17:38:45 +00:00
|
|
|
import eu.siacs.conversations.ui.XmppActivity;
|
|
|
|
import eu.siacs.conversations.ui.util.Attachment;
|
|
|
|
import eu.siacs.conversations.ui.util.StyledAttributes;
|
|
|
|
import eu.siacs.conversations.ui.util.ViewUtil;
|
|
|
|
|
|
|
|
public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaViewHolder> {
|
|
|
|
|
|
|
|
private static final List<String> DOCUMENT_MIMES = Arrays.asList(
|
|
|
|
"application/pdf",
|
|
|
|
"application/vnd.oasis.opendocument.text",
|
|
|
|
"application/msword",
|
|
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
"text/x-tex",
|
|
|
|
"text/plain"
|
|
|
|
);
|
|
|
|
|
|
|
|
private final ArrayList<Attachment> attachments = new ArrayList<>();
|
|
|
|
|
|
|
|
private final XmppActivity activity;
|
|
|
|
|
|
|
|
private int mediaSize = 0;
|
|
|
|
|
|
|
|
public MediaAdapter(XmppActivity activity, @DimenRes int mediaSize) {
|
|
|
|
this.activity = activity;
|
|
|
|
this.mediaSize = Math.round(activity.getResources().getDimension(mediaSize));
|
|
|
|
}
|
|
|
|
|
2020-07-19 19:27:43 +00:00
|
|
|
@SuppressWarnings("rawtypes")
|
2018-09-15 17:38:45 +00:00
|
|
|
public static void setMediaSize(RecyclerView recyclerView, int mediaSize) {
|
2020-07-19 19:27:43 +00:00
|
|
|
final RecyclerView.Adapter adapter = recyclerView.getAdapter();
|
2018-09-15 17:38:45 +00:00
|
|
|
if (adapter instanceof MediaAdapter) {
|
|
|
|
((MediaAdapter) adapter).setMediaSize(mediaSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 19:27:43 +00:00
|
|
|
private static @AttrRes
|
|
|
|
int getImageAttr(Attachment attachment) {
|
2018-09-15 17:38:45 +00:00
|
|
|
final @AttrRes int attr;
|
|
|
|
if (attachment.getType() == Attachment.Type.LOCATION) {
|
|
|
|
attr = R.attr.media_preview_location;
|
|
|
|
} else if (attachment.getType() == Attachment.Type.RECORDING) {
|
|
|
|
attr = R.attr.media_preview_recording;
|
|
|
|
} else {
|
|
|
|
final String mime = attachment.getMime();
|
2020-07-19 19:27:43 +00:00
|
|
|
Log.d(Config.LOGTAG, "mime=" + mime);
|
2018-09-15 17:38:45 +00:00
|
|
|
if (mime == null) {
|
|
|
|
attr = R.attr.media_preview_unknown;
|
|
|
|
} else if (mime.startsWith("audio/")) {
|
|
|
|
attr = R.attr.media_preview_audio;
|
|
|
|
} else if (mime.equals("text/calendar") || (mime.equals("text/x-vcalendar"))) {
|
|
|
|
attr = R.attr.media_preview_calendar;
|
|
|
|
} else if (mime.equals("text/x-vcard")) {
|
|
|
|
attr = R.attr.media_preview_contact;
|
|
|
|
} else if (mime.equals("application/vnd.android.package-archive")) {
|
|
|
|
attr = R.attr.media_preview_app;
|
|
|
|
} else if (mime.equals("application/zip") || mime.equals("application/rar")) {
|
|
|
|
attr = R.attr.media_preview_archive;
|
2018-12-22 12:02:45 +00:00
|
|
|
} else if (mime.equals("application/epub+zip") || mime.equals("application/vnd.amazon.mobi8-ebook")) {
|
|
|
|
attr = R.attr.media_preview_ebook;
|
2019-07-15 17:11:00 +00:00
|
|
|
} else if (mime.equals(ExportBackupService.MIME_TYPE)) {
|
|
|
|
attr = R.attr.media_preview_backup;
|
2018-09-15 17:38:45 +00:00
|
|
|
} else if (DOCUMENT_MIMES.contains(mime)) {
|
|
|
|
attr = R.attr.media_preview_document;
|
2020-07-19 19:27:43 +00:00
|
|
|
} else if (mime.equals("application/gpx+xml")) {
|
|
|
|
attr = R.attr.media_preview_tour;
|
2021-05-14 06:37:42 +00:00
|
|
|
} else if (mime.startsWith("image/")) {
|
|
|
|
attr = R.attr.media_preview_image;
|
2018-09-15 17:38:45 +00:00
|
|
|
} else {
|
|
|
|
attr = R.attr.media_preview_unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
2019-01-24 11:27:57 +00:00
|
|
|
static void renderPreview(Context context, Attachment attachment, ImageView imageView) {
|
2018-09-15 17:38:45 +00:00
|
|
|
imageView.setBackgroundColor(StyledAttributes.getColor(context, R.attr.color_background_tertiary));
|
|
|
|
imageView.setImageAlpha(Math.round(StyledAttributes.getFloat(context, R.attr.icon_alpha) * 255));
|
|
|
|
imageView.setImageDrawable(StyledAttributes.getDrawable(context, getImageAttr(attachment)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean cancelPotentialWork(Attachment attachment, ImageView imageView) {
|
|
|
|
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
|
|
|
|
|
|
|
|
if (bitmapWorkerTask != null) {
|
|
|
|
final Attachment oldAttachment = bitmapWorkerTask.attachment;
|
|
|
|
if (oldAttachment == null || !oldAttachment.equals(attachment)) {
|
|
|
|
bitmapWorkerTask.cancel(true);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
|
|
|
|
if (imageView != null) {
|
|
|
|
final Drawable drawable = imageView.getDrawable();
|
|
|
|
if (drawable instanceof AsyncDrawable) {
|
|
|
|
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
|
|
|
|
return asyncDrawable.getBitmapWorkerTask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public MediaViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
|
|
|
|
MediaBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.media, parent, false);
|
|
|
|
return new MediaViewHolder(binding);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull MediaViewHolder holder, int position) {
|
|
|
|
final Attachment attachment = attachments.get(position);
|
|
|
|
if (attachment.renderThumbnail()) {
|
|
|
|
holder.binding.media.setImageAlpha(255);
|
|
|
|
loadPreview(attachment, holder.binding.media);
|
|
|
|
} else {
|
|
|
|
cancelPotentialWork(attachment, holder.binding.media);
|
|
|
|
renderPreview(activity, attachment, holder.binding.media);
|
|
|
|
}
|
2019-01-27 09:32:27 +00:00
|
|
|
holder.binding.getRoot().setOnClickListener(v -> ViewUtil.view(activity, attachment));
|
2018-09-15 17:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setAttachments(List<Attachment> attachments) {
|
|
|
|
this.attachments.clear();
|
|
|
|
this.attachments.addAll(attachments);
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setMediaSize(int mediaSize) {
|
|
|
|
this.mediaSize = mediaSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadPreview(Attachment attachment, ImageView imageView) {
|
|
|
|
if (cancelPotentialWork(attachment, imageView)) {
|
2020-07-19 19:27:43 +00:00
|
|
|
final Bitmap bm = activity.xmppConnectionService.getFileBackend().getPreviewForUri(attachment, mediaSize, true);
|
2018-09-15 17:38:45 +00:00
|
|
|
if (bm != null) {
|
|
|
|
cancelPotentialWork(attachment, imageView);
|
|
|
|
imageView.setImageBitmap(bm);
|
|
|
|
imageView.setBackgroundColor(0x00000000);
|
|
|
|
} else {
|
|
|
|
imageView.setBackgroundColor(0xff333333);
|
|
|
|
imageView.setImageDrawable(null);
|
2019-01-24 11:27:57 +00:00
|
|
|
final BitmapWorkerTask task = new BitmapWorkerTask(mediaSize, imageView);
|
2018-09-15 17:38:45 +00:00
|
|
|
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
|
|
|
|
imageView.setImageDrawable(asyncDrawable);
|
|
|
|
try {
|
|
|
|
task.execute(attachment);
|
|
|
|
} catch (final RejectedExecutionException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return attachments.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
static class AsyncDrawable extends BitmapDrawable {
|
|
|
|
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
|
|
|
|
|
|
|
|
AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
|
|
|
|
super(res, bitmap);
|
|
|
|
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
BitmapWorkerTask getBitmapWorkerTask() {
|
|
|
|
return bitmapWorkerTaskReference.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MediaViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
private final MediaBinding binding;
|
|
|
|
|
|
|
|
MediaViewHolder(MediaBinding binding) {
|
|
|
|
super(binding.getRoot());
|
|
|
|
this.binding = binding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 11:27:57 +00:00
|
|
|
private static class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> {
|
2018-09-15 17:38:45 +00:00
|
|
|
private final WeakReference<ImageView> imageViewReference;
|
|
|
|
private Attachment attachment = null;
|
2019-01-24 11:27:57 +00:00
|
|
|
private final int mediaSize;
|
2018-09-15 17:38:45 +00:00
|
|
|
|
2019-01-24 11:27:57 +00:00
|
|
|
BitmapWorkerTask(int mediaSize, ImageView imageView) {
|
|
|
|
this.mediaSize = mediaSize;
|
2018-09-15 17:38:45 +00:00
|
|
|
imageViewReference = new WeakReference<>(imageView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Bitmap doInBackground(Attachment... params) {
|
|
|
|
this.attachment = params[0];
|
2019-01-24 11:27:57 +00:00
|
|
|
final XmppActivity activity = XmppActivity.find(imageViewReference);
|
|
|
|
if (activity == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-09-15 17:38:45 +00:00
|
|
|
return activity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, mediaSize, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bitmap bitmap) {
|
|
|
|
if (bitmap != null && !isCancelled()) {
|
|
|
|
final ImageView imageView = imageViewReference.get();
|
|
|
|
if (imageView != null) {
|
|
|
|
imageView.setImageBitmap(bitmap);
|
|
|
|
imageView.setBackgroundColor(0x00000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|