1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
package de.pixart.messenger.ui.adapter;
import android.content.Context;
import android.content.res.Resources;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.annotation.AttrRes;
import android.support.annotation.DimenRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import de.pixart.messenger.R;
import de.pixart.messenger.databinding.MediaBinding;
import de.pixart.messenger.ui.XmppActivity;
import de.pixart.messenger.ui.util.Attachment;
import de.pixart.messenger.ui.util.StyledAttributes;
import de.pixart.messenger.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));
}
public static void setMediaSize(RecyclerView recyclerView, int mediaSize) {
RecyclerView.Adapter adapter = recyclerView.getAdapter();
if (adapter instanceof MediaAdapter) {
((MediaAdapter) adapter).setMediaSize(mediaSize);
}
}
private static @AttrRes
int getImageAttr(Attachment attachment) {
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();
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;
} else if (DOCUMENT_MIMES.contains(mime)) {
attr = R.attr.media_preview_document;
} else {
attr = R.attr.media_preview_unknown;
}
}
return attr;
}
public static void renderPreview(Context context, Attachment attachment, ImageView imageView) {
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);
}
holder.binding.media.setOnClickListener(v -> ViewUtil.view(activity, attachment));
}
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)) {
final Bitmap bm = activity.xmppConnectionService.getFileBackend().getPreviewForUri(attachment, mediaSize, true);
if (bm != null) {
cancelPotentialWork(attachment, imageView);
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} else {
imageView.setBackgroundColor(0xff333333);
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
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;
}
}
class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Attachment attachment = null;
BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(Attachment... params) {
this.attachment = params[0];
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);
}
}
}
}
}
|