1package eu.siacs.conversations.ui.adapter;
2
3import android.content.Context;
4import android.content.res.Resources;
5import android.graphics.Bitmap;
6import android.graphics.Typeface;
7import android.graphics.drawable.BitmapDrawable;
8import android.graphics.drawable.Drawable;
9import android.os.AsyncTask;
10import android.support.annotation.NonNull;
11import android.util.Log;
12import android.util.Pair;
13import android.view.LayoutInflater;
14import android.view.View;
15import android.view.ViewGroup;
16import android.widget.ArrayAdapter;
17import android.widget.ImageView;
18import android.widget.TextView;
19
20import java.lang.ref.WeakReference;
21import java.util.List;
22import java.util.concurrent.RejectedExecutionException;
23
24import eu.siacs.conversations.Config;
25import eu.siacs.conversations.R;
26import eu.siacs.conversations.entities.Conversation;
27import eu.siacs.conversations.entities.Message;
28import eu.siacs.conversations.entities.Transferable;
29import eu.siacs.conversations.ui.ConversationFragment;
30import eu.siacs.conversations.ui.XmppActivity;
31import eu.siacs.conversations.ui.util.Color;
32import eu.siacs.conversations.ui.widget.UnreadCountCustomView;
33import eu.siacs.conversations.utils.EmojiWrapper;
34import eu.siacs.conversations.utils.IrregularUnicodeDetector;
35import eu.siacs.conversations.utils.UIHelper;
36import rocks.xmpp.addr.Jid;
37
38public class ConversationAdapter extends ArrayAdapter<Conversation> {
39
40 private XmppActivity activity;
41 private Conversation selectedConversation = null;
42
43 public ConversationAdapter(XmppActivity activity, List<Conversation> conversations) {
44 super(activity, 0, conversations);
45 this.activity = activity;
46 }
47
48 @Override
49 public View getView(int position, View view, @NonNull ViewGroup parent) {
50 if (view == null) {
51 LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
52 view = inflater.inflate(R.layout.conversation_list_row,parent, false);
53 }
54 ViewHolder viewHolder = ViewHolder.get(view);
55 Conversation conversation = getItem(position);
56 if (this.activity instanceof XmppActivity) {
57 int c = Color.get(activity, conversation == selectedConversation ? R.attr.color_background_secondary: R.attr.color_background_primary);
58 viewHolder.swipeableItem.setBackgroundColor(c);
59 }
60 if (conversation.getMode() == Conversation.MODE_SINGLE || activity.useSubjectToIdentifyConference()) {
61 CharSequence name = conversation.getName();
62 if (name instanceof Jid) {
63 viewHolder.name.setText(IrregularUnicodeDetector.style(activity, (Jid) name));
64 } else {
65 viewHolder.name.setText(EmojiWrapper.transform(name));
66 }
67 } else {
68 viewHolder.name.setText(conversation.getJid().asBareJid().toString());
69 }
70
71 Message message = conversation.getLatestMessage();
72 int unreadCount = conversation.unreadCount();
73 if (unreadCount > 0) {
74 viewHolder.unreadCount.setVisibility(View.VISIBLE);
75 viewHolder.unreadCount.setUnreadCount(unreadCount);
76 } else {
77 viewHolder.unreadCount.setVisibility(View.GONE);
78 }
79
80 if (!conversation.isRead()) {
81 viewHolder.name.setTypeface(null, Typeface.BOLD);
82 } else {
83 viewHolder.name.setTypeface(null, Typeface.NORMAL);
84 }
85
86 final boolean fileAvailable = message.getTransferable() == null || message.getTransferable().getStatus() != Transferable.STATUS_DELETED;
87 final boolean showPreviewText;
88 if (fileAvailable && (message.isFileOrImage() || message.treatAsDownloadable() || message.isGeoUri())) {
89 final int imageResource;
90 if (message.isGeoUri()) {
91 imageResource = activity.getThemeResource(R.attr.ic_attach_location, R.drawable.ic_attach_location);
92 showPreviewText = false;
93 } else {
94 final String mime = message.getMimeType();
95 switch (mime == null ? "" : mime.split("/")[0]) {
96 case "image":
97 imageResource = activity.getThemeResource(R.attr.ic_attach_photo, R.drawable.ic_attach_photo);
98 showPreviewText = false;
99 break;
100 case "video":
101 imageResource = activity.getThemeResource(R.attr.ic_attach_videocam, R.drawable.ic_attach_videocam);
102 showPreviewText = false;
103 break;
104 case "audio":
105 imageResource = activity.getThemeResource(R.attr.ic_attach_record, R.drawable.ic_attach_record);
106 showPreviewText = false;
107 break;
108 default:
109 imageResource = activity.getThemeResource(R.attr.ic_attach_document, R.drawable.ic_attach_document);
110 showPreviewText = true;
111 break;
112 }
113 }
114 viewHolder.lastMessageIcon.setImageResource(imageResource);
115 viewHolder.lastMessageIcon.setVisibility(View.VISIBLE);
116 } else {
117 viewHolder.lastMessageIcon.setVisibility(View.GONE);
118 showPreviewText = true;
119 }
120 final Pair<String,Boolean> preview = UIHelper.getMessagePreview(activity,message);
121 if (showPreviewText) {
122 viewHolder.lastMessage.setText(EmojiWrapper.transform(preview.first));
123 } else {
124 viewHolder.lastMessageIcon.setContentDescription(preview.first);
125 }
126 viewHolder.lastMessage.setVisibility(showPreviewText ? View.VISIBLE : View.GONE);
127 if (preview.second) {
128 if (conversation.isRead()) {
129 viewHolder.lastMessage.setTypeface(null, Typeface.ITALIC);
130 viewHolder.sender.setTypeface(null, Typeface.NORMAL);
131 } else {
132 viewHolder.lastMessage.setTypeface(null,Typeface.BOLD_ITALIC);
133 viewHolder.sender.setTypeface(null,Typeface.BOLD);
134 }
135 } else {
136 if (conversation.isRead()) {
137 viewHolder.lastMessage.setTypeface(null,Typeface.NORMAL);
138 viewHolder.sender.setTypeface(null,Typeface.NORMAL);
139 } else {
140 viewHolder.lastMessage.setTypeface(null,Typeface.BOLD);
141 viewHolder.sender.setTypeface(null,Typeface.BOLD);
142 }
143 }
144 if (message.getStatus() == Message.STATUS_RECEIVED) {
145 if (conversation.getMode() == Conversation.MODE_MULTI) {
146 viewHolder.sender.setVisibility(View.VISIBLE);
147 viewHolder.sender.setText(UIHelper.getMessageDisplayName(message).split("\\s+")[0]+':');
148 } else {
149 viewHolder.sender.setVisibility(View.GONE);
150 }
151 } else if (message.getType() != Message.TYPE_STATUS) {
152 viewHolder.sender.setVisibility(View.VISIBLE);
153 viewHolder.sender.setText(activity.getString(R.string.me)+':');
154 } else {
155 viewHolder.sender.setVisibility(View.GONE);
156 }
157
158 long muted_till = conversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL,0);
159 if (muted_till == Long.MAX_VALUE) {
160 viewHolder.notificationIcon.setVisibility(View.VISIBLE);
161 int ic_notifications_off = activity.getThemeResource(R.attr.icon_notifications_off, R.drawable.ic_notifications_off_black_24dp);
162 viewHolder.notificationIcon.setImageResource(ic_notifications_off);
163 } else if (muted_till >= System.currentTimeMillis()) {
164 viewHolder.notificationIcon.setVisibility(View.VISIBLE);
165 int ic_notifications_paused = activity.getThemeResource(R.attr.icon_notifications_paused, R.drawable.ic_notifications_paused_black_24dp);
166 viewHolder.notificationIcon.setImageResource(ic_notifications_paused);
167 } else if (conversation.alwaysNotify()) {
168 viewHolder.notificationIcon.setVisibility(View.GONE);
169 } else {
170 viewHolder.notificationIcon.setVisibility(View.VISIBLE);
171 int ic_notifications_none = activity.getThemeResource(R.attr.icon_notifications_none, R.drawable.ic_notifications_none_black_24dp);
172 viewHolder.notificationIcon.setImageResource(ic_notifications_none);
173 }
174
175 viewHolder.timestamp.setText(UIHelper.readableTimeDifference(activity,conversation.getLatestMessage().getTimeSent()));
176 loadAvatar(conversation, viewHolder.avatar);
177
178 return view;
179 }
180
181 @Override
182 public void notifyDataSetChanged() {
183 this.selectedConversation = ConversationFragment.getConversation(activity);
184 super.notifyDataSetChanged();
185 }
186
187 public static class ViewHolder {
188 private View swipeableItem;
189 private TextView name;
190 private TextView lastMessage;
191 private ImageView lastMessageIcon;
192 private TextView sender;
193 private TextView timestamp;
194 private ImageView notificationIcon;
195 private UnreadCountCustomView unreadCount;
196 private ImageView avatar;
197
198 private ViewHolder() {
199
200 }
201
202 public static ViewHolder get(View layout) {
203 ViewHolder viewHolder = (ViewHolder) layout.getTag();
204 if (viewHolder == null) {
205 viewHolder = new ViewHolder();
206 viewHolder.swipeableItem = layout.findViewById(R.id.swipeable_item);
207 viewHolder.name = layout.findViewById(R.id.conversation_name);
208 viewHolder.lastMessage = layout.findViewById(R.id.conversation_lastmsg);
209 viewHolder.lastMessageIcon = layout.findViewById(R.id.conversation_lastmsg_img);
210 viewHolder.timestamp = layout.findViewById(R.id.conversation_lastupdate);
211 viewHolder.sender = layout.findViewById(R.id.sender_name);
212 viewHolder.notificationIcon = layout.findViewById(R.id.notification_status);
213 viewHolder.unreadCount = layout.findViewById(R.id.unread_count);
214 viewHolder.avatar = layout.findViewById(R.id.conversation_image);
215 layout.setTag(viewHolder);
216 }
217 return viewHolder;
218 }
219 }
220
221 class BitmapWorkerTask extends AsyncTask<Conversation, Void, Bitmap> {
222 private final WeakReference<ImageView> imageViewReference;
223 private Conversation conversation = null;
224
225 public BitmapWorkerTask(ImageView imageView) {
226 imageViewReference = new WeakReference<>(imageView);
227 }
228
229 @Override
230 protected Bitmap doInBackground(Conversation... params) {
231 this.conversation = params[0];
232 return activity.avatarService().get(this.conversation, activity.getPixel(56), isCancelled());
233 }
234
235 @Override
236 protected void onPostExecute(Bitmap bitmap) {
237 if (bitmap != null && !isCancelled()) {
238 final ImageView imageView = imageViewReference.get();
239 if (imageView != null) {
240 imageView.setImageBitmap(bitmap);
241 imageView.setBackgroundColor(0x00000000);
242 }
243 }
244 }
245 }
246
247 private void loadAvatar(Conversation conversation, ImageView imageView) {
248 if (cancelPotentialWork(conversation, imageView)) {
249 final Bitmap bm = activity.avatarService().get(conversation, activity.getPixel(56), true);
250 if (bm != null) {
251 cancelPotentialWork(conversation, imageView);
252 imageView.setImageBitmap(bm);
253 imageView.setBackgroundColor(0x00000000);
254 } else {
255 imageView.setBackgroundColor(UIHelper.getColorForName(conversation.getName().toString()));
256 imageView.setImageDrawable(null);
257 final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
258 final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
259 imageView.setImageDrawable(asyncDrawable);
260 try {
261 task.execute(conversation);
262 } catch (final RejectedExecutionException ignored) {
263 }
264 }
265 }
266 }
267
268 private static boolean cancelPotentialWork(Conversation conversation, ImageView imageView) {
269 final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
270
271 if (bitmapWorkerTask != null) {
272 final Conversation oldConversation = bitmapWorkerTask.conversation;
273 if (oldConversation == null || conversation != oldConversation) {
274 bitmapWorkerTask.cancel(true);
275 } else {
276 return false;
277 }
278 }
279 return true;
280 }
281
282 private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
283 if (imageView != null) {
284 final Drawable drawable = imageView.getDrawable();
285 if (drawable instanceof AsyncDrawable) {
286 final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
287 return asyncDrawable.getBitmapWorkerTask();
288 }
289 }
290 return null;
291 }
292
293 static class AsyncDrawable extends BitmapDrawable {
294 private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
295
296 public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
297 super(res, bitmap);
298 bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
299 }
300
301 public BitmapWorkerTask getBitmapWorkerTask() {
302 return bitmapWorkerTaskReference.get();
303 }
304 }
305}