1package eu.siacs.conversations.ui.adapter;
2
3import android.content.Context;
4import android.content.res.Resources;
5import android.databinding.DataBindingUtil;
6import android.graphics.Bitmap;
7import android.graphics.drawable.BitmapDrawable;
8import android.graphics.drawable.Drawable;
9import android.os.AsyncTask;
10import android.support.annotation.AttrRes;
11import android.support.annotation.DimenRes;
12import android.support.annotation.NonNull;
13import android.support.v7.widget.RecyclerView;
14import android.view.LayoutInflater;
15import android.view.ViewGroup;
16import android.widget.ImageView;
17
18import java.lang.ref.WeakReference;
19import java.util.ArrayList;
20import java.util.Arrays;
21import java.util.List;
22import java.util.concurrent.RejectedExecutionException;
23
24import eu.siacs.conversations.R;
25import eu.siacs.conversations.databinding.MediaBinding;
26import eu.siacs.conversations.services.ExportBackupService;
27import eu.siacs.conversations.ui.XmppActivity;
28import eu.siacs.conversations.ui.util.Attachment;
29import eu.siacs.conversations.ui.util.StyledAttributes;
30import eu.siacs.conversations.ui.util.ViewUtil;
31
32public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaViewHolder> {
33
34 private static final List<String> DOCUMENT_MIMES = Arrays.asList(
35 "application/pdf",
36 "application/vnd.oasis.opendocument.text",
37 "application/msword",
38 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
39 "text/x-tex",
40 "text/plain"
41 );
42
43 private final ArrayList<Attachment> attachments = new ArrayList<>();
44
45 private final XmppActivity activity;
46
47 private int mediaSize = 0;
48
49 public MediaAdapter(XmppActivity activity, @DimenRes int mediaSize) {
50 this.activity = activity;
51 this.mediaSize = Math.round(activity.getResources().getDimension(mediaSize));
52 }
53
54 public static void setMediaSize(RecyclerView recyclerView, int mediaSize) {
55 RecyclerView.Adapter adapter = recyclerView.getAdapter();
56 if (adapter instanceof MediaAdapter) {
57 ((MediaAdapter) adapter).setMediaSize(mediaSize);
58 }
59 }
60
61 private static @AttrRes int getImageAttr(Attachment attachment) {
62 final @AttrRes int attr;
63 if (attachment.getType() == Attachment.Type.LOCATION) {
64 attr = R.attr.media_preview_location;
65 } else if (attachment.getType() == Attachment.Type.RECORDING) {
66 attr = R.attr.media_preview_recording;
67 } else {
68 final String mime = attachment.getMime();
69 if (mime == null) {
70 attr = R.attr.media_preview_unknown;
71 } else if (mime.startsWith("audio/")) {
72 attr = R.attr.media_preview_audio;
73 } else if (mime.equals("text/calendar") || (mime.equals("text/x-vcalendar"))) {
74 attr = R.attr.media_preview_calendar;
75 } else if (mime.equals("text/x-vcard")) {
76 attr = R.attr.media_preview_contact;
77 } else if (mime.equals("application/vnd.android.package-archive")) {
78 attr = R.attr.media_preview_app;
79 } else if (mime.equals("application/zip") || mime.equals("application/rar")) {
80 attr = R.attr.media_preview_archive;
81 } else if (mime.equals("application/epub+zip") || mime.equals("application/vnd.amazon.mobi8-ebook")) {
82 attr = R.attr.media_preview_ebook;
83 } else if (mime.equals(ExportBackupService.MIME_TYPE)) {
84 attr = R.attr.media_preview_backup;
85 } else if (DOCUMENT_MIMES.contains(mime)) {
86 attr = R.attr.media_preview_document;
87 } else {
88 attr = R.attr.media_preview_unknown;
89 }
90 }
91 return attr;
92 }
93
94 static void renderPreview(Context context, Attachment attachment, ImageView imageView) {
95 imageView.setBackgroundColor(StyledAttributes.getColor(context, R.attr.color_background_tertiary));
96 imageView.setImageAlpha(Math.round(StyledAttributes.getFloat(context, R.attr.icon_alpha) * 255));
97 imageView.setImageDrawable(StyledAttributes.getDrawable(context, getImageAttr(attachment)));
98 }
99
100 private static boolean cancelPotentialWork(Attachment attachment, ImageView imageView) {
101 final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
102
103 if (bitmapWorkerTask != null) {
104 final Attachment oldAttachment = bitmapWorkerTask.attachment;
105 if (oldAttachment == null || !oldAttachment.equals(attachment)) {
106 bitmapWorkerTask.cancel(true);
107 } else {
108 return false;
109 }
110 }
111 return true;
112 }
113
114 private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
115 if (imageView != null) {
116 final Drawable drawable = imageView.getDrawable();
117 if (drawable instanceof AsyncDrawable) {
118 final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
119 return asyncDrawable.getBitmapWorkerTask();
120 }
121 }
122 return null;
123 }
124
125 @NonNull
126 @Override
127 public MediaViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
128 LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
129 MediaBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.media, parent, false);
130 return new MediaViewHolder(binding);
131 }
132
133 @Override
134 public void onBindViewHolder(@NonNull MediaViewHolder holder, int position) {
135 final Attachment attachment = attachments.get(position);
136 if (attachment.renderThumbnail()) {
137 holder.binding.media.setImageAlpha(255);
138 loadPreview(attachment, holder.binding.media);
139 } else {
140 cancelPotentialWork(attachment, holder.binding.media);
141 renderPreview(activity, attachment, holder.binding.media);
142 }
143 holder.binding.getRoot().setOnClickListener(v -> ViewUtil.view(activity, attachment));
144 }
145
146 public void setAttachments(List<Attachment> attachments) {
147 this.attachments.clear();
148 this.attachments.addAll(attachments);
149 notifyDataSetChanged();
150 }
151
152 private void setMediaSize(int mediaSize) {
153 this.mediaSize = mediaSize;
154 }
155
156 private void loadPreview(Attachment attachment, ImageView imageView) {
157 if (cancelPotentialWork(attachment, imageView)) {
158 final Bitmap bm = activity.xmppConnectionService.getFileBackend().getPreviewForUri(attachment,mediaSize,true);
159 if (bm != null) {
160 cancelPotentialWork(attachment, imageView);
161 imageView.setImageBitmap(bm);
162 imageView.setBackgroundColor(0x00000000);
163 } else {
164 imageView.setBackgroundColor(0xff333333);
165 imageView.setImageDrawable(null);
166 final BitmapWorkerTask task = new BitmapWorkerTask(mediaSize, imageView);
167 final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
168 imageView.setImageDrawable(asyncDrawable);
169 try {
170 task.execute(attachment);
171 } catch (final RejectedExecutionException ignored) {
172 }
173 }
174 }
175 }
176
177 @Override
178 public int getItemCount() {
179 return attachments.size();
180 }
181
182 static class AsyncDrawable extends BitmapDrawable {
183 private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
184
185 AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
186 super(res, bitmap);
187 bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
188 }
189
190 BitmapWorkerTask getBitmapWorkerTask() {
191 return bitmapWorkerTaskReference.get();
192 }
193 }
194
195 class MediaViewHolder extends RecyclerView.ViewHolder {
196
197 private final MediaBinding binding;
198
199 MediaViewHolder(MediaBinding binding) {
200 super(binding.getRoot());
201 this.binding = binding;
202 }
203 }
204
205 private static class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> {
206 private final WeakReference<ImageView> imageViewReference;
207 private Attachment attachment = null;
208 private final int mediaSize;
209
210 BitmapWorkerTask(int mediaSize, ImageView imageView) {
211 this.mediaSize = mediaSize;
212 imageViewReference = new WeakReference<>(imageView);
213 }
214
215 @Override
216 protected Bitmap doInBackground(Attachment... params) {
217 this.attachment = params[0];
218 final XmppActivity activity = XmppActivity.find(imageViewReference);
219 if (activity == null) {
220 return null;
221 }
222 return activity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, mediaSize, false);
223 }
224
225 @Override
226 protected void onPostExecute(Bitmap bitmap) {
227 if (bitmap != null && !isCancelled()) {
228 final ImageView imageView = imageViewReference.get();
229 if (imageView != null) {
230 imageView.setImageBitmap(bitmap);
231 imageView.setBackgroundColor(0x00000000);
232 }
233 }
234 }
235 }
236}