aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java
blob: 967042d802aaeb50b6cda85dfeed04574673d6ac (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package eu.siacs.conversations.ui.adapter;

import java.util.HashMap;
import java.util.List;

import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.jingle.JingleConnection;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MessageAdapter extends ArrayAdapter<Message> {

	private static final int SENT = 0;
	private static final int RECEIVED = 1;
	private static final int STATUS = 2;
	private static final int NULL = 3;

	private ConversationActivity activity;

	private Bitmap accountBitmap;

	private BitmapCache mBitmapCache = new BitmapCache();
	private DisplayMetrics metrics;

	private OnContactPictureClicked mOnContactPictureClickedListener;
	private OnContactPictureLongClicked mOnContactPictureLongClickedListener;

	public MessageAdapter(ConversationActivity activity, List<Message> messages) {
		super(activity, 0, messages);
		this.activity = activity;
		metrics = getContext().getResources().getDisplayMetrics();
	}

	private Bitmap getSelfBitmap() {
		if (this.accountBitmap == null) {

			if (getCount() > 0) {
				this.accountBitmap = getItem(0).getConversation().getAccount()
						.getImage(getContext(), 48);
			}
		}
		return this.accountBitmap;
	}

	public void setOnContactPictureClicked(OnContactPictureClicked listener) {
		this.mOnContactPictureClickedListener = listener;
	}

	public void setOnContactPictureLongClicked(
			OnContactPictureLongClicked listener) {
		this.mOnContactPictureLongClickedListener = listener;
	}

	@Override
	public int getViewTypeCount() {
		return 4;
	}

	@Override
	public int getItemViewType(int position) {
		if (getItem(position).wasMergedIntoPrevious()) {
			return NULL;
		} else if (getItem(position).getType() == Message.TYPE_STATUS) {
			return STATUS;
		} else if (getItem(position).getStatus() <= Message.STATUS_RECEIVED) {
			return RECEIVED;
		} else {
			return SENT;
		}
	}

	private void displayStatus(ViewHolder viewHolder, Message message) {
		String filesize = null;
		String info = null;
		boolean error = false;
		boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI
				&& message.getMergedStatus() <= Message.STATUS_RECEIVED;
		if (message.getType() == Message.TYPE_IMAGE) {
			String[] fileParams = message.getBody().split(",");
			try {
				long size = Long.parseLong(fileParams[0]);
				filesize = size / 1024 + " KB";
			} catch (NumberFormatException e) {
				filesize = "0 KB";
			}
		}
		switch (message.getMergedStatus()) {
		case Message.STATUS_WAITING:
			info = getContext().getString(R.string.waiting);
			break;
		case Message.STATUS_UNSEND:
			info = getContext().getString(R.string.sending);
			break;
		case Message.STATUS_OFFERED:
			info = getContext().getString(R.string.offering);
			break;
		case Message.STATUS_SEND_FAILED:
			info = getContext().getString(R.string.send_failed);
			error = true;
			break;
		case Message.STATUS_SEND_REJECTED:
			info = getContext().getString(R.string.send_rejected);
			error = true;
			break;
		case Message.STATUS_RECEPTION_FAILED:
			info = getContext().getString(R.string.reception_failed);
			error = true;
		default:
			if (multiReceived) {
				Contact contact = message.getContact();
				if (contact != null) {
					info = contact.getDisplayName();
				} else {
					if (message.getPresence() != null) {
						info = message.getPresence();
					} else {
						info = message.getCounterpart();
					}
				}
			}
			break;
		}
		if (error) {
			viewHolder.time.setTextColor(0xFFe92727);
		} else {
			viewHolder.time.setTextColor(activity.getSecondaryTextColor());
		}
		if (message.getEncryption() == Message.ENCRYPTION_NONE) {
			viewHolder.indicator.setVisibility(View.GONE);
		} else {
			viewHolder.indicator.setVisibility(View.VISIBLE);
		}

		String formatedTime = UIHelper.readableTimeDifference(getContext(),
				message.getMergedTimeSent());
		if (message.getStatus() <= Message.STATUS_RECEIVED) {
			if ((filesize != null) && (info != null)) {
				viewHolder.time.setText(filesize + " \u00B7 " + info);
			} else if ((filesize == null) && (info != null)) {
				viewHolder.time.setText(formatedTime + " \u00B7 " + info);
			} else if ((filesize != null) && (info == null)) {
				viewHolder.time.setText(formatedTime + " \u00B7 " + filesize);
			} else {
				viewHolder.time.setText(formatedTime);
			}
		} else {
			if ((filesize != null) && (info != null)) {
				viewHolder.time.setText(filesize + " \u00B7 " + info);
			} else if ((filesize == null) && (info != null)) {
				if (error) {
					viewHolder.time.setText(info + " \u00B7 " + formatedTime);
				} else {
					viewHolder.time.setText(info);
				}
			} else if ((filesize != null) && (info == null)) {
				viewHolder.time.setText(filesize + " \u00B7 " + formatedTime);
			} else {
				viewHolder.time.setText(formatedTime);
			}
		}
	}

	private void displayInfoMessage(ViewHolder viewHolder, int r) {
		if (viewHolder.download_button != null) {
			viewHolder.download_button.setVisibility(View.GONE);
		}
		viewHolder.image.setVisibility(View.GONE);
		viewHolder.messageBody.setVisibility(View.VISIBLE);
		viewHolder.messageBody.setText(getContext().getString(r));
		viewHolder.messageBody.setTextColor(0xff33B5E5);
		viewHolder.messageBody.setTypeface(null, Typeface.ITALIC);
		viewHolder.messageBody.setTextIsSelectable(false);
	}

	private void displayDecryptionFailed(ViewHolder viewHolder) {
		if (viewHolder.download_button != null) {
			viewHolder.download_button.setVisibility(View.GONE);
		}
		viewHolder.image.setVisibility(View.GONE);
		viewHolder.messageBody.setVisibility(View.VISIBLE);
		viewHolder.messageBody.setText(getContext().getString(
				R.string.decryption_failed));
		viewHolder.messageBody.setTextColor(0xFFe92727);
		viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
		viewHolder.messageBody.setTextIsSelectable(false);
	}

	private void displayTextMessage(ViewHolder viewHolder, Message message) {
		if (viewHolder.download_button != null) {
			viewHolder.download_button.setVisibility(View.GONE);
		}
		viewHolder.image.setVisibility(View.GONE);
		viewHolder.messageBody.setVisibility(View.VISIBLE);
		if (message.getBody() != null) {
			if (message.getType() != Message.TYPE_PRIVATE) {
				String body = Config.PARSE_EMOTICONS ? UIHelper
						.transformAsciiEmoticons(message.getMergedBody())
						: message.getMergedBody();
				viewHolder.messageBody.setText(body);
			} else {
				String privateMarker;
				if (message.getStatus() <= Message.STATUS_RECEIVED) {
					privateMarker = activity
							.getString(R.string.private_message);
				} else {
					String to;
					if (message.getPresence() != null) {
						to = message.getPresence();
					} else {
						to = message.getCounterpart();
					}
					privateMarker = activity.getString(
							R.string.private_message_to, to);
				}
				SpannableString span = new SpannableString(privateMarker + " "
						+ message.getBody());
				span.setSpan(
						new ForegroundColorSpan(activity
								.getSecondaryTextColor()), 0, privateMarker
								.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				span.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
						privateMarker.length(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				viewHolder.messageBody.setText(span);
			}
		} else {
			viewHolder.messageBody.setText("");
		}
		viewHolder.messageBody.setTextColor(activity.getPrimaryTextColor());
		viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
		viewHolder.messageBody.setTextIsSelectable(true);
	}

	private void displayImageMessage(ViewHolder viewHolder,
			final Message message) {
		if (viewHolder.download_button != null) {
			viewHolder.download_button.setVisibility(View.GONE);
		}
		viewHolder.messageBody.setVisibility(View.GONE);
		viewHolder.image.setVisibility(View.VISIBLE);
		String[] fileParams = message.getBody().split(",");
		if (fileParams.length == 3) {
			double target = metrics.density * 288;
			int w = Integer.parseInt(fileParams[1]);
			int h = Integer.parseInt(fileParams[2]);
			int scalledW;
			int scalledH;
			if (w <= h) {
				scalledW = (int) (w / ((double) h / target));
				scalledH = (int) target;
			} else {
				scalledW = (int) target;
				scalledH = (int) (h / ((double) w / target));
			}
			viewHolder.image.setLayoutParams(new LinearLayout.LayoutParams(
					scalledW, scalledH));
		}
		activity.loadBitmap(message, viewHolder.image);
		viewHolder.image.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setDataAndType(activity.xmppConnectionService
						.getFileBackend().getJingleFileUri(message), "image/*");
				getContext().startActivity(intent);
			}
		});
		viewHolder.image.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				Intent shareIntent = new Intent();
				shareIntent.setAction(Intent.ACTION_SEND);
				shareIntent.putExtra(Intent.EXTRA_STREAM,
						activity.xmppConnectionService.getFileBackend()
								.getJingleFileUri(message));
				shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
				shareIntent.setType("image/webp");
				getContext().startActivity(
						Intent.createChooser(shareIntent,
								getContext().getText(R.string.share_with)));
				return true;
			}
		});
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		final Message item = getItem(position);
		int type = getItemViewType(position);
		ViewHolder viewHolder;
		if (view == null) {
			viewHolder = new ViewHolder();
			switch (type) {
			case NULL:
				view = (View) activity.getLayoutInflater().inflate(
						R.layout.message_null, parent, false);
				break;
			case SENT:
				view = (View) activity.getLayoutInflater().inflate(
						R.layout.message_sent, parent, false);
				viewHolder.message_box = (LinearLayout) view
						.findViewById(R.id.message_box);
				viewHolder.contact_picture = (ImageView) view
						.findViewById(R.id.message_photo);
				viewHolder.contact_picture.setImageBitmap(getSelfBitmap());
				viewHolder.indicator = (ImageView) view
						.findViewById(R.id.security_indicator);
				viewHolder.image = (ImageView) view
						.findViewById(R.id.message_image);
				viewHolder.messageBody = (TextView) view
						.findViewById(R.id.message_body);
				viewHolder.time = (TextView) view
						.findViewById(R.id.message_time);
				view.setTag(viewHolder);
				break;
			case RECEIVED:
				view = (View) activity.getLayoutInflater().inflate(
						R.layout.message_received, parent, false);
				viewHolder.message_box = (LinearLayout) view
						.findViewById(R.id.message_box);
				viewHolder.contact_picture = (ImageView) view
						.findViewById(R.id.message_photo);

				viewHolder.download_button = (Button) view
						.findViewById(R.id.download_button);

				if (item.getConversation().getMode() == Conversation.MODE_SINGLE) {

					viewHolder.contact_picture.setImageBitmap(mBitmapCache.get(
							item.getConversation().getContact(), getContext()));

				}
				viewHolder.indicator = (ImageView) view
						.findViewById(R.id.security_indicator);
				viewHolder.image = (ImageView) view
						.findViewById(R.id.message_image);
				viewHolder.messageBody = (TextView) view
						.findViewById(R.id.message_body);
				viewHolder.time = (TextView) view
						.findViewById(R.id.message_time);
				view.setTag(viewHolder);
				break;
			case STATUS:
				view = (View) activity.getLayoutInflater().inflate(
						R.layout.message_status, parent, false);
				viewHolder.contact_picture = (ImageView) view
						.findViewById(R.id.message_photo);
				if (item.getConversation().getMode() == Conversation.MODE_SINGLE) {

					viewHolder.contact_picture.setImageBitmap(mBitmapCache.get(
							item.getConversation().getContact(), getContext()));
					viewHolder.contact_picture.setAlpha(128);
					viewHolder.contact_picture
							.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									String name = item.getConversation()
											.getName();
									String read = getContext()
											.getString(
													R.string.contact_has_read_up_to_this_point,
													name);
									Toast.makeText(getContext(), read,
											Toast.LENGTH_SHORT).show();
								}
							});

				}
				break;
			default:
				viewHolder = null;
				break;
			}
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		if (type == STATUS || type == NULL) {
			return view;
		}

		if (type == RECEIVED) {
			if (item.getConversation().getMode() == Conversation.MODE_MULTI) {
				Contact contact = item.getContact();
				if (contact != null) {
					viewHolder.contact_picture.setImageBitmap(mBitmapCache.get(
							contact, getContext()));
				} else {
					String name = item.getPresence();
					if (name == null) {
						name = item.getCounterpart();
					}
					viewHolder.contact_picture.setImageBitmap(mBitmapCache.get(
							name, getContext()));
				}
				viewHolder.contact_picture
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								if (MessageAdapter.this.mOnContactPictureClickedListener != null) {
									MessageAdapter.this.mOnContactPictureClickedListener
											.onContactPictureClicked(item);
									;
								}

							}
						});
				viewHolder.contact_picture
						.setOnLongClickListener(new OnLongClickListener() {

							@Override
							public boolean onLongClick(View v) {
								if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) {
									MessageAdapter.this.mOnContactPictureLongClickedListener
											.onContactPictureLongClicked(item);
									return true;
								} else {
									return false;
								}
							}
						});
			}
		}

		if (item.getType() == Message.TYPE_IMAGE) {
			if (item.getStatus() == Message.STATUS_RECEIVING) {
				displayInfoMessage(viewHolder, R.string.receiving_image);
			} else if (item.getStatus() == Message.STATUS_RECEIVED_OFFER) {
				viewHolder.image.setVisibility(View.GONE);
				viewHolder.messageBody.setVisibility(View.GONE);
				viewHolder.download_button.setVisibility(View.VISIBLE);
				viewHolder.download_button
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								Downloadable downloadable = item
										.getDownloadable();
								if (downloadable != null) {
									downloadable.start();
								}
							}
						});
			} else if ((item.getEncryption() == Message.ENCRYPTION_DECRYPTED)
					|| (item.getEncryption() == Message.ENCRYPTION_NONE)
					|| (item.getEncryption() == Message.ENCRYPTION_OTR)) {
				displayImageMessage(viewHolder, item);
			} else if (item.getEncryption() == Message.ENCRYPTION_PGP) {
				displayInfoMessage(viewHolder, R.string.encrypted_message);
			} else {
				displayDecryptionFailed(viewHolder);
			}
		} else {
			if (item.getEncryption() == Message.ENCRYPTION_PGP) {
				if (activity.hasPgp()) {
					displayInfoMessage(viewHolder, R.string.encrypted_message);
				} else {
					displayInfoMessage(viewHolder,
							R.string.install_openkeychain);
					viewHolder.message_box
							.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									activity.showInstallPgpDialog();
								}
							});
				}
			} else if (item.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
				displayDecryptionFailed(viewHolder);
			} else {
				displayTextMessage(viewHolder, item);
			}
		}

		displayStatus(viewHolder, item);

		return view;
	}

	private static class ViewHolder {

		protected LinearLayout message_box;
		protected Button download_button;
		protected ImageView image;
		protected ImageView indicator;
		protected TextView time;
		protected TextView messageBody;
		protected ImageView contact_picture;

	}

	private class BitmapCache {
		private HashMap<String, Bitmap> contactBitmaps = new HashMap<String, Bitmap>();
		private HashMap<String, Bitmap> unknownBitmaps = new HashMap<String, Bitmap>();

		public Bitmap get(Contact contact, Context context) {
			if (!contactBitmaps.containsKey(contact.getJid())) {
				contactBitmaps.put(contact.getJid(),
						contact.getImage(48, context));
			}
			return contactBitmaps.get(contact.getJid());
		}

		public Bitmap get(String name, Context context) {
			if (unknownBitmaps.containsKey(name)) {
				return unknownBitmaps.get(name);
			} else {
				Bitmap bm = UIHelper
						.getContactPicture(name, 48, context, false);
				unknownBitmaps.put(name, bm);
				return bm;
			}
		}
	}

	public interface OnContactPictureClicked {
		public void onContactPictureClicked(Message message);
	}

	public interface OnContactPictureLongClicked {
		public void onContactPictureLongClicked(Message message);
	}
}