2018-09-11 07:56:29 +00:00
|
|
|
package eu.siacs.conversations.ui.adapter;
|
|
|
|
|
2019-09-29 00:00:06 +00:00
|
|
|
import android.content.ActivityNotFoundException;
|
2018-09-12 12:37:41 +00:00
|
|
|
import android.content.Context;
|
2019-09-29 00:00:06 +00:00
|
|
|
import android.content.Intent;
|
2018-09-11 07:56:29 +00:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2019-09-29 00:00:06 +00:00
|
|
|
import android.net.Uri;
|
2018-09-11 07:56:29 +00:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
2019-09-29 00:00:06 +00:00
|
|
|
import android.widget.Toast;
|
2018-09-11 07:56:29 +00:00
|
|
|
|
2021-01-23 08:25:34 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.databinding.DataBindingUtil;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2018-09-11 07:56:29 +00:00
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.databinding.MediaPreviewBinding;
|
2019-09-29 00:00:06 +00:00
|
|
|
import eu.siacs.conversations.persistance.FileBackend;
|
2018-09-11 07:56:29 +00:00
|
|
|
import eu.siacs.conversations.ui.ConversationFragment;
|
|
|
|
import eu.siacs.conversations.ui.XmppActivity;
|
|
|
|
import eu.siacs.conversations.ui.util.Attachment;
|
|
|
|
|
|
|
|
public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapter.MediaPreviewViewHolder> {
|
|
|
|
|
2018-09-12 20:20:19 +00:00
|
|
|
private final ArrayList<Attachment> mediaPreviews = new ArrayList<>();
|
2018-09-11 07:56:29 +00:00
|
|
|
|
|
|
|
private final ConversationFragment conversationFragment;
|
|
|
|
|
|
|
|
public MediaPreviewAdapter(ConversationFragment fragment) {
|
|
|
|
this.conversationFragment = fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public MediaPreviewViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
|
|
|
|
MediaPreviewBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.media_preview, parent, false);
|
|
|
|
return new MediaPreviewViewHolder(binding);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull MediaPreviewViewHolder holder, int position) {
|
2018-09-12 12:37:41 +00:00
|
|
|
final Context context = conversationFragment.getActivity();
|
2018-09-11 07:56:29 +00:00
|
|
|
final Attachment attachment = mediaPreviews.get(position);
|
2018-09-12 12:37:41 +00:00
|
|
|
if (attachment.renderThumbnail()) {
|
|
|
|
holder.binding.mediaPreview.setImageAlpha(255);
|
|
|
|
loadPreview(attachment, holder.binding.mediaPreview);
|
|
|
|
} else {
|
|
|
|
cancelPotentialWork(attachment, holder.binding.mediaPreview);
|
2018-09-15 17:38:45 +00:00
|
|
|
MediaAdapter.renderPreview(context, attachment, holder.binding.mediaPreview);
|
2018-09-12 12:37:41 +00:00
|
|
|
}
|
2018-09-11 07:56:29 +00:00
|
|
|
holder.binding.deleteButton.setOnClickListener(v -> {
|
2019-09-29 00:00:06 +00:00
|
|
|
final int pos = mediaPreviews.indexOf(attachment);
|
2018-09-11 07:56:29 +00:00
|
|
|
mediaPreviews.remove(pos);
|
|
|
|
notifyItemRemoved(pos);
|
|
|
|
conversationFragment.toggleInputMethod();
|
|
|
|
});
|
2019-09-29 00:00:06 +00:00
|
|
|
holder.binding.mediaPreview.setOnClickListener(v -> view(context, attachment));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void view(final Context context, Attachment attachment) {
|
|
|
|
final Intent view = new Intent(Intent.ACTION_VIEW);
|
|
|
|
final Uri uri = FileBackend.getUriForUri(context, attachment.getUri());
|
|
|
|
view.setDataAndType(uri, attachment.getMime());
|
|
|
|
view.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
try {
|
|
|
|
context.startActivity(view);
|
2021-09-10 16:46:37 +00:00
|
|
|
} catch (final ActivityNotFoundException e) {
|
2019-09-29 00:00:06 +00:00
|
|
|
Toast.makeText(context, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
|
2021-09-10 16:46:37 +00:00
|
|
|
} catch (final SecurityException e) {
|
|
|
|
Toast.makeText(context, R.string.sharing_application_not_grant_permission, Toast.LENGTH_SHORT).show();
|
2019-09-29 00:00:06 +00:00
|
|
|
}
|
2018-09-11 07:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addMediaPreviews(List<Attachment> attachments) {
|
|
|
|
this.mediaPreviews.addAll(attachments);
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadPreview(Attachment attachment, ImageView imageView) {
|
|
|
|
if (cancelPotentialWork(attachment, imageView)) {
|
|
|
|
XmppActivity activity = (XmppActivity) conversationFragment.getActivity();
|
|
|
|
final Bitmap bm = activity.xmppConnectionService.getFileBackend().getPreviewForUri(attachment,Math.round(activity.getResources().getDimension(R.dimen.media_preview_size)),true);
|
|
|
|
if (bm != null) {
|
|
|
|
cancelPotentialWork(attachment, imageView);
|
|
|
|
imageView.setImageBitmap(bm);
|
|
|
|
imageView.setBackgroundColor(0x00000000);
|
|
|
|
} else {
|
2018-09-12 12:37:41 +00:00
|
|
|
imageView.setBackgroundColor(0xff333333);
|
2018-09-11 07:56:29 +00:00
|
|
|
imageView.setImageDrawable(null);
|
|
|
|
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
|
|
|
|
final AsyncDrawable asyncDrawable = new AsyncDrawable(conversationFragment.getActivity().getResources(), null, task);
|
|
|
|
imageView.setImageDrawable(asyncDrawable);
|
|
|
|
try {
|
|
|
|
task.execute(attachment);
|
|
|
|
} catch (final RejectedExecutionException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return mediaPreviews.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasAttachments() {
|
|
|
|
return mediaPreviews.size() > 0;
|
|
|
|
}
|
|
|
|
|
2018-09-12 20:20:19 +00:00
|
|
|
public ArrayList<Attachment> getAttachments() {
|
2018-09-11 07:56:29 +00:00
|
|
|
return mediaPreviews;
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:48:21 +00:00
|
|
|
public void clearPreviews() {
|
|
|
|
this.mediaPreviews.clear();
|
|
|
|
}
|
|
|
|
|
2018-09-11 07:56:29 +00:00
|
|
|
class MediaPreviewViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
private final MediaPreviewBinding binding;
|
|
|
|
|
|
|
|
MediaPreviewViewHolder(MediaPreviewBinding binding) {
|
|
|
|
super(binding.getRoot());
|
|
|
|
this.binding = binding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 11:27:57 +00:00
|
|
|
private static class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> {
|
2018-09-11 07:56:29 +00:00
|
|
|
private final WeakReference<ImageView> imageViewReference;
|
|
|
|
private Attachment attachment = null;
|
|
|
|
|
|
|
|
BitmapWorkerTask(ImageView imageView) {
|
|
|
|
imageViewReference = new WeakReference<>(imageView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Bitmap doInBackground(Attachment... params) {
|
2019-01-24 11:27:57 +00:00
|
|
|
this.attachment = params[0];
|
|
|
|
final XmppActivity activity = XmppActivity.find(imageViewReference);
|
|
|
|
if (activity == null) {
|
2018-09-11 07:56:29 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-01-24 11:27:57 +00:00
|
|
|
return activity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, Math.round(activity.getResources().getDimension(R.dimen.media_preview_size)), false);
|
2018-09-11 07:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bitmap bitmap) {
|
|
|
|
if (bitmap != null && !isCancelled()) {
|
|
|
|
final ImageView imageView = imageViewReference.get();
|
|
|
|
if (imageView != null) {
|
|
|
|
imageView.setImageBitmap(bitmap);
|
|
|
|
imageView.setBackgroundColor(0x00000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|