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