1package eu.siacs.conversations.ui.adapter;
2
3import android.content.Intent;
4import android.content.pm.PackageManager;
5import android.content.pm.ResolveInfo;
6import android.graphics.Typeface;
7import android.net.Uri;
8import android.text.Spannable;
9import android.text.SpannableString;
10import android.text.style.ForegroundColorSpan;
11import android.text.style.StyleSpan;
12import android.util.DisplayMetrics;
13import android.view.View;
14import android.view.View.OnClickListener;
15import android.view.View.OnLongClickListener;
16import android.view.ViewGroup;
17import android.widget.ArrayAdapter;
18import android.widget.Button;
19import android.widget.ImageView;
20import android.widget.LinearLayout;
21import android.widget.TextView;
22import android.widget.Toast;
23
24import java.util.List;
25
26import eu.siacs.conversations.R;
27import eu.siacs.conversations.entities.Account;
28import eu.siacs.conversations.entities.Contact;
29import eu.siacs.conversations.entities.Conversation;
30import eu.siacs.conversations.entities.Downloadable;
31import eu.siacs.conversations.entities.DownloadableFile;
32import eu.siacs.conversations.entities.Message;
33import eu.siacs.conversations.entities.Message.ImageParams;
34import eu.siacs.conversations.ui.ConversationActivity;
35import eu.siacs.conversations.utils.UIHelper;
36import eu.siacs.conversations.xmpp.jid.Jid;
37
38public class MessageAdapter extends ArrayAdapter<Message> {
39
40 private static final int SENT = 0;
41 private static final int RECEIVED = 1;
42 private static final int STATUS = 2;
43 private static final int NULL = 3;
44
45 private ConversationActivity activity;
46
47 private DisplayMetrics metrics;
48
49 private OnContactPictureClicked mOnContactPictureClickedListener;
50 private OnContactPictureLongClicked mOnContactPictureLongClickedListener;
51
52 private OnLongClickListener openContextMenu = new OnLongClickListener() {
53
54 @Override
55 public boolean onLongClick(View v) {
56 v.showContextMenu();
57 return true;
58 }
59 };
60
61 public MessageAdapter(ConversationActivity activity, List<Message> messages) {
62 super(activity, 0, messages);
63 this.activity = activity;
64 metrics = getContext().getResources().getDisplayMetrics();
65 }
66
67 public void setOnContactPictureClicked(OnContactPictureClicked listener) {
68 this.mOnContactPictureClickedListener = listener;
69 }
70
71 public void setOnContactPictureLongClicked(
72 OnContactPictureLongClicked listener) {
73 this.mOnContactPictureLongClickedListener = listener;
74 }
75
76 @Override
77 public int getViewTypeCount() {
78 return 4;
79 }
80
81 @Override
82 public int getItemViewType(int position) {
83 if (getItem(position).wasMergedIntoPrevious()) {
84 return NULL;
85 } else if (getItem(position).getType() == Message.TYPE_STATUS) {
86 return STATUS;
87 } else if (getItem(position).getStatus() <= Message.STATUS_RECEIVED) {
88 return RECEIVED;
89 } else {
90 return SENT;
91 }
92 }
93
94 private void displayStatus(ViewHolder viewHolder, Message message) {
95 String filesize = null;
96 String info = null;
97 boolean error = false;
98 if (viewHolder.indicatorReceived != null) {
99 viewHolder.indicatorReceived.setVisibility(View.GONE);
100 }
101 boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI
102 && message.getMergedStatus() <= Message.STATUS_RECEIVED;
103 if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE || message.getDownloadable() != null) {
104 ImageParams params = message.getImageParams();
105 if (params.size > (1.5 * 1024 * 1024)) {
106 filesize = params.size / (1024 * 1024)+ " MB";
107 } else if (params.size > 0) {
108 filesize = params.size / 1024 + " KB";
109 }
110 if (message.getDownloadable() != null && message.getDownloadable().getStatus() == Downloadable.STATUS_FAILED) {
111 error = true;
112 }
113 }
114 switch (message.getMergedStatus()) {
115 case Message.STATUS_WAITING:
116 info = getContext().getString(R.string.waiting);
117 break;
118 case Message.STATUS_UNSEND:
119 Downloadable d = message.getDownloadable();
120 if (d!=null) {
121 info = getContext().getString(R.string.sending_file,d.getProgress());
122 } else {
123 info = getContext().getString(R.string.sending);
124 }
125 break;
126 case Message.STATUS_OFFERED:
127 info = getContext().getString(R.string.offering);
128 break;
129 case Message.STATUS_SEND_RECEIVED:
130 if (activity.indicateReceived()) {
131 viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
132 }
133 break;
134 case Message.STATUS_SEND_DISPLAYED:
135 if (activity.indicateReceived()) {
136 viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
137 }
138 break;
139 case Message.STATUS_SEND_FAILED:
140 info = getContext().getString(R.string.send_failed);
141 error = true;
142 break;
143 default:
144 if (multiReceived) {
145 Contact contact = message.getContact();
146 if (contact != null) {
147 info = contact.getDisplayName();
148 } else {
149 info = getDisplayedMucCounterpart(message.getCounterpart());
150 }
151 }
152 break;
153 }
154 if (error) {
155 viewHolder.time.setTextColor(activity.getWarningTextColor());
156 } else {
157 viewHolder.time.setTextColor(activity.getSecondaryTextColor());
158 }
159 if (message.getEncryption() == Message.ENCRYPTION_NONE) {
160 viewHolder.indicator.setVisibility(View.GONE);
161 } else {
162 viewHolder.indicator.setVisibility(View.VISIBLE);
163 }
164
165 String formatedTime = UIHelper.readableTimeDifferenceFull(getContext(),
166 message.getMergedTimeSent());
167 if (message.getStatus() <= Message.STATUS_RECEIVED) {
168 if ((filesize != null) && (info != null)) {
169 viewHolder.time.setText(filesize + " \u00B7 " + info);
170 } else if ((filesize == null) && (info != null)) {
171 viewHolder.time.setText(formatedTime + " \u00B7 " + info);
172 } else if ((filesize != null) && (info == null)) {
173 viewHolder.time.setText(formatedTime + " \u00B7 " + filesize);
174 } else {
175 viewHolder.time.setText(formatedTime);
176 }
177 } else {
178 if ((filesize != null) && (info != null)) {
179 viewHolder.time.setText(filesize + " \u00B7 " + info);
180 } else if ((filesize == null) && (info != null)) {
181 if (error) {
182 viewHolder.time.setText(info + " \u00B7 " + formatedTime);
183 } else {
184 viewHolder.time.setText(info);
185 }
186 } else if ((filesize != null) && (info == null)) {
187 viewHolder.time.setText(filesize + " \u00B7 " + formatedTime);
188 } else {
189 viewHolder.time.setText(formatedTime);
190 }
191 }
192 }
193
194 private void displayInfoMessage(ViewHolder viewHolder, String text) {
195 if (viewHolder.download_button != null) {
196 viewHolder.download_button.setVisibility(View.GONE);
197 }
198 viewHolder.image.setVisibility(View.GONE);
199 viewHolder.messageBody.setVisibility(View.VISIBLE);
200 viewHolder.messageBody.setText(text);
201 viewHolder.messageBody.setTextColor(activity.getSecondaryTextColor());
202 viewHolder.messageBody.setTypeface(null, Typeface.ITALIC);
203 viewHolder.messageBody.setTextIsSelectable(false);
204 }
205
206 private void displayDecryptionFailed(ViewHolder viewHolder) {
207 if (viewHolder.download_button != null) {
208 viewHolder.download_button.setVisibility(View.GONE);
209 }
210 viewHolder.image.setVisibility(View.GONE);
211 viewHolder.messageBody.setVisibility(View.VISIBLE);
212 viewHolder.messageBody.setText(getContext().getString(
213 R.string.decryption_failed));
214 viewHolder.messageBody.setTextColor(activity.getWarningTextColor());
215 viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
216 viewHolder.messageBody.setTextIsSelectable(false);
217 }
218
219 private void displayTextMessage(ViewHolder viewHolder, Message message) {
220 if (viewHolder.download_button != null) {
221 viewHolder.download_button.setVisibility(View.GONE);
222 }
223 viewHolder.image.setVisibility(View.GONE);
224 viewHolder.messageBody.setVisibility(View.VISIBLE);
225 if (message.getBody() != null) {
226 if (message.getType() != Message.TYPE_PRIVATE) {
227 viewHolder.messageBody.setText(message.getMergedBody());
228 } else {
229 String privateMarker;
230 if (message.getStatus() <= Message.STATUS_RECEIVED) {
231 privateMarker = activity
232 .getString(R.string.private_message);
233 } else {
234 final String to;
235 if (message.getCounterpart() != null) {
236 to = message.getCounterpart().getResourcepart();
237 } else {
238 to = "";
239 }
240 privateMarker = activity.getString(R.string.private_message_to, to);
241 }
242 SpannableString span = new SpannableString(privateMarker + " "
243 + message.getBody());
244 span.setSpan(
245 new ForegroundColorSpan(activity
246 .getSecondaryTextColor()), 0, privateMarker
247 .length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
248 span.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
249 privateMarker.length(),
250 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
251 viewHolder.messageBody.setText(span);
252 }
253 } else {
254 viewHolder.messageBody.setText("");
255 }
256 viewHolder.messageBody.setTextColor(activity.getPrimaryTextColor());
257 viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
258 viewHolder.messageBody.setTextIsSelectable(true);
259 }
260
261 private void displayDownloadableMessage(ViewHolder viewHolder,
262 final Message message, String text) {
263 viewHolder.image.setVisibility(View.GONE);
264 viewHolder.messageBody.setVisibility(View.GONE);
265 viewHolder.download_button.setVisibility(View.VISIBLE);
266 viewHolder.download_button.setText(text);
267 viewHolder.download_button.setOnClickListener(new OnClickListener() {
268
269 @Override
270 public void onClick(View v) {
271 startDownloadable(message);
272 }
273 });
274 viewHolder.download_button.setOnLongClickListener(openContextMenu);
275 }
276
277 private void displayOpenableMessage(ViewHolder viewHolder,final Message message) {
278 final DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
279 viewHolder.image.setVisibility(View.GONE);
280 viewHolder.messageBody.setVisibility(View.GONE);
281 viewHolder.download_button.setVisibility(View.VISIBLE);
282 viewHolder.download_button.setText(activity.getString(R.string.open_file,file.getMimeType()));
283 viewHolder.download_button.setOnClickListener(new OnClickListener() {
284
285 @Override
286 public void onClick(View v) {
287 openDownloadable(file);
288 }
289 });
290 viewHolder.download_button.setOnLongClickListener(openContextMenu);
291 }
292
293 private void displayImageMessage(ViewHolder viewHolder,
294 final Message message) {
295 if (viewHolder.download_button != null) {
296 viewHolder.download_button.setVisibility(View.GONE);
297 }
298 viewHolder.messageBody.setVisibility(View.GONE);
299 viewHolder.image.setVisibility(View.VISIBLE);
300 ImageParams params = message.getImageParams();
301 double target = metrics.density * 288;
302 int scalledW;
303 int scalledH;
304 if (params.width <= params.height) {
305 scalledW = (int) (params.width / ((double) params.height / target));
306 scalledH = (int) target;
307 } else {
308 scalledW = (int) target;
309 scalledH = (int) (params.height / ((double) params.width / target));
310 }
311 viewHolder.image.setLayoutParams(new LinearLayout.LayoutParams(
312 scalledW, scalledH));
313 activity.loadBitmap(message, viewHolder.image);
314 viewHolder.image.setOnClickListener(new OnClickListener() {
315
316 @Override
317 public void onClick(View v) {
318 Intent intent = new Intent(Intent.ACTION_VIEW);
319 intent.setDataAndType(activity.xmppConnectionService
320 .getFileBackend().getJingleFileUri(message), "image/*");
321 getContext().startActivity(intent);
322 }
323 });
324 viewHolder.image.setOnLongClickListener(openContextMenu);
325 }
326
327 private String getDisplayedMucCounterpart(final Jid counterpart) {
328 if (counterpart==null) {
329 return "";
330 } else if (!counterpart.isBareJid()) {
331 return counterpart.getResourcepart();
332 } else {
333 return counterpart.toString();
334 }
335 }
336
337 @Override
338 public View getView(int position, View view, ViewGroup parent) {
339 final Message message = getItem(position);
340 final Conversation conversation = message.getConversation();
341 final Account account = conversation.getAccount();
342 final int type = getItemViewType(position);
343 ViewHolder viewHolder;
344 if (view == null) {
345 viewHolder = new ViewHolder();
346 switch (type) {
347 case NULL:
348 view = activity.getLayoutInflater().inflate(
349 R.layout.message_null, parent, false);
350 break;
351 case SENT:
352 view = activity.getLayoutInflater().inflate(
353 R.layout.message_sent, parent, false);
354 viewHolder.message_box = (LinearLayout) view
355 .findViewById(R.id.message_box);
356 viewHolder.contact_picture = (ImageView) view
357 .findViewById(R.id.message_photo);
358 viewHolder.download_button = (Button) view
359 .findViewById(R.id.download_button);
360 viewHolder.indicator = (ImageView) view
361 .findViewById(R.id.security_indicator);
362 viewHolder.image = (ImageView) view
363 .findViewById(R.id.message_image);
364 viewHolder.messageBody = (TextView) view
365 .findViewById(R.id.message_body);
366 viewHolder.time = (TextView) view
367 .findViewById(R.id.message_time);
368 viewHolder.indicatorReceived = (ImageView) view
369 .findViewById(R.id.indicator_received);
370 break;
371 case RECEIVED:
372 view = activity.getLayoutInflater().inflate(
373 R.layout.message_received, parent, false);
374 viewHolder.message_box = (LinearLayout) view
375 .findViewById(R.id.message_box);
376 viewHolder.contact_picture = (ImageView) view
377 .findViewById(R.id.message_photo);
378 viewHolder.download_button = (Button) view
379 .findViewById(R.id.download_button);
380 viewHolder.indicator = (ImageView) view
381 .findViewById(R.id.security_indicator);
382 viewHolder.image = (ImageView) view
383 .findViewById(R.id.message_image);
384 viewHolder.messageBody = (TextView) view
385 .findViewById(R.id.message_body);
386 viewHolder.time = (TextView) view
387 .findViewById(R.id.message_time);
388 viewHolder.indicatorReceived = (ImageView) view
389 .findViewById(R.id.indicator_received);
390 break;
391 case STATUS:
392 view = activity.getLayoutInflater().inflate(
393 R.layout.message_status, parent, false);
394 viewHolder.contact_picture = (ImageView) view
395 .findViewById(R.id.message_photo);
396 break;
397 default:
398 viewHolder = null;
399 break;
400 }
401 view.setTag(viewHolder);
402 } else {
403 viewHolder = (ViewHolder) view.getTag();
404 if (viewHolder == null) {
405 return view;
406 }
407 }
408
409 if (type == STATUS) {
410 if (conversation.getMode() == Conversation.MODE_SINGLE) {
411 viewHolder.contact_picture.setImageBitmap(activity
412 .avatarService().get(conversation.getContact(),
413 activity.getPixel(32)));
414 viewHolder.contact_picture.setAlpha(0.5f);
415 viewHolder.contact_picture
416 .setOnClickListener(new OnClickListener() {
417
418 @Override
419 public void onClick(View v) {
420 String name = conversation.getName();
421 String read = getContext()
422 .getString(
423 R.string.contact_has_read_up_to_this_point,
424 name);
425 Toast.makeText(getContext(), read,
426 Toast.LENGTH_SHORT).show();
427 }
428 });
429
430 }
431 return view;
432 } else if (type == NULL) {
433 if (position == getCount() - 1) {
434 view.getLayoutParams().height = 1;
435 } else {
436 view.getLayoutParams().height = 0;
437
438 }
439 view.setLayoutParams(view.getLayoutParams());
440 return view;
441 } else if (viewHolder.messageBody == null || viewHolder.image == null) {
442 return view; //avoiding weird platform bugs
443 } else if (type == RECEIVED) {
444 Contact contact = message.getContact();
445 if (contact != null) {
446 viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(contact, activity.getPixel(48)));
447 } else if (conversation.getMode() == Conversation.MODE_MULTI) {
448 viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(getDisplayedMucCounterpart(message.getCounterpart()),
449 activity.getPixel(48)));
450 }
451 } else if (type == SENT) {
452 viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(account, activity.getPixel(48)));
453 }
454
455 viewHolder.contact_picture
456 .setOnClickListener(new OnClickListener() {
457
458 @Override
459 public void onClick(View v) {
460 if (MessageAdapter.this.mOnContactPictureClickedListener != null) {
461 MessageAdapter.this.mOnContactPictureClickedListener
462 .onContactPictureClicked(message);
463 }
464
465 }
466 });
467 viewHolder.contact_picture
468 .setOnLongClickListener(new OnLongClickListener() {
469
470 @Override
471 public boolean onLongClick(View v) {
472 if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) {
473 MessageAdapter.this.mOnContactPictureLongClickedListener
474 .onContactPictureLongClicked(message);
475 return true;
476 } else {
477 return false;
478 }
479 }
480 });
481
482 if (message.getDownloadable() != null && message.getDownloadable().getStatus() != Downloadable.STATUS_UPLOADING) {
483 Downloadable d = message.getDownloadable();
484 if (d.getStatus() == Downloadable.STATUS_DOWNLOADING) {
485 if (message.getType() == Message.TYPE_FILE) {
486 displayInfoMessage(viewHolder,activity.getString(R.string.receiving_file,d.getMimeType(),d.getProgress()));
487 } else {
488 displayInfoMessage(viewHolder,activity.getString(R.string.receiving_image,d.getProgress()));
489 }
490 } else if (d.getStatus() == Downloadable.STATUS_CHECKING) {
491 displayInfoMessage(viewHolder,activity.getString(R.string.checking_image));
492 } else if (d.getStatus() == Downloadable.STATUS_DELETED) {
493 if (message.getType() == Message.TYPE_FILE) {
494 displayInfoMessage(viewHolder, activity.getString(R.string.file_deleted));
495 } else {
496 displayInfoMessage(viewHolder, activity.getString(R.string.image_file_deleted));
497 }
498 } else if (d.getStatus() == Downloadable.STATUS_OFFER) {
499 if (message.getType() == Message.TYPE_FILE) {
500 displayDownloadableMessage(viewHolder,message,activity.getString(R.string.download_file,d.getMimeType()));
501 } else {
502 displayDownloadableMessage(viewHolder, message,activity.getString(R.string.download_image));
503 }
504 } else if (d.getStatus() == Downloadable.STATUS_OFFER_CHECK_FILESIZE) {
505 displayDownloadableMessage(viewHolder, message,activity.getString(R.string.check_image_filesize));
506 } else if (d.getStatus() == Downloadable.STATUS_FAILED) {
507 if (message.getType() == Message.TYPE_FILE) {
508 displayInfoMessage(viewHolder, activity.getString(R.string.file_transmission_failed));
509 } else {
510 displayInfoMessage(viewHolder, activity.getString(R.string.image_transmission_failed));
511 }
512 }
513 } else if (message.getType() == Message.TYPE_IMAGE && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
514 displayImageMessage(viewHolder, message);
515 } else if (message.getType() == Message.TYPE_FILE && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
516 if (message.getImageParams().width > 0) {
517 displayImageMessage(viewHolder,message);
518 } else {
519 displayOpenableMessage(viewHolder, message);
520 }
521 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
522 if (activity.hasPgp()) {
523 displayInfoMessage(viewHolder,activity.getString(R.string.encrypted_message));
524 } else {
525 displayInfoMessage(viewHolder,
526 activity.getString(R.string.install_openkeychain));
527 if (viewHolder != null) {
528 viewHolder.message_box
529 .setOnClickListener(new OnClickListener() {
530
531 @Override
532 public void onClick(View v) {
533 activity.showInstallPgpDialog();
534 }
535 });
536 }
537 }
538 } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
539 displayDecryptionFailed(viewHolder);
540 } else {
541 displayTextMessage(viewHolder, message);
542 }
543
544 displayStatus(viewHolder, message);
545
546 return view;
547 }
548
549 public void startDownloadable(Message message) {
550 Downloadable downloadable = message.getDownloadable();
551 if (downloadable != null) {
552 if (!downloadable.start()) {
553 Toast.makeText(activity, R.string.not_connected_try_again,
554 Toast.LENGTH_SHORT).show();
555 }
556 }
557 }
558
559 public void openDownloadable(DownloadableFile file) {
560 if (!file.exists()) {
561 Toast.makeText(activity,R.string.file_deleted,Toast.LENGTH_SHORT).show();
562 return;
563 }
564 Intent openIntent = new Intent(Intent.ACTION_VIEW);
565 openIntent.setDataAndType(Uri.fromFile(file), file.getMimeType());
566 PackageManager manager = activity.getPackageManager();
567 List<ResolveInfo> infos = manager.queryIntentActivities(openIntent, 0);
568 if (infos.size() > 0) {
569 getContext().startActivity(openIntent);
570 } else {
571 Toast.makeText(activity,R.string.no_application_found_to_open_file,Toast.LENGTH_SHORT).show();
572 }
573 }
574
575 public interface OnContactPictureClicked {
576 public void onContactPictureClicked(Message message);
577 }
578
579 public interface OnContactPictureLongClicked {
580 public void onContactPictureLongClicked(Message message);
581 }
582
583 private static class ViewHolder {
584
585 protected LinearLayout message_box;
586 protected Button download_button;
587 protected ImageView image;
588 protected ImageView indicator;
589 protected ImageView indicatorReceived;
590 protected TextView time;
591 protected TextView messageBody;
592 protected ImageView contact_picture;
593
594 }
595}