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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
|
package de.pixart.messenger.ui.adapter;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.text.util.Linkify;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.RejectedExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.pixart.messenger.Config;
import de.pixart.messenger.R;
import de.pixart.messenger.crypto.axolotl.FingerprintStatus;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.entities.Conversation;
import de.pixart.messenger.entities.DownloadableFile;
import de.pixart.messenger.entities.Message;
import de.pixart.messenger.entities.Message.FileParams;
import de.pixart.messenger.entities.Transferable;
import de.pixart.messenger.persistance.FileBackend;
import de.pixart.messenger.services.MessageArchiveService;
import de.pixart.messenger.services.NotificationService;
import de.pixart.messenger.ui.ConversationActivity;
import de.pixart.messenger.ui.ShowFullscreenMessageActivity;
import de.pixart.messenger.ui.text.DividerSpan;
import de.pixart.messenger.ui.text.QuoteSpan;
import de.pixart.messenger.ui.widget.ClickableMovementMethod;
import de.pixart.messenger.ui.widget.CopyTextView;
import de.pixart.messenger.ui.widget.ListSelectionManager;
import de.pixart.messenger.utils.CryptoHelper;
import de.pixart.messenger.utils.GeoHelper;
import de.pixart.messenger.utils.Patterns;
import de.pixart.messenger.utils.UIHelper;
import ezvcard.Ezvcard;
import ezvcard.VCard;
import nl.changer.audiowife.AudioWife;
public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextView.CopyHandler {
private static final int SENT = 0;
private static final int RECEIVED = 1;
private static final int STATUS = 2;
private static final Pattern XMPP_PATTERN = Pattern
.compile("xmpp\\:(?:(?:["
+ Patterns.GOOD_IRI_CHAR
+ "\\;\\/\\?\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])"
+ "|(?:\\%[a-fA-F0-9]{2}))+");
private static final Linkify.TransformFilter WEBURL_TRANSFORM_FILTER = new Linkify.TransformFilter() {
@Override
public String transformUrl(Matcher matcher, String url) {
if (url == null) {
return null;
}
final String lcUrl = url.toLowerCase(Locale.US);
if (lcUrl.startsWith("http://") || lcUrl.startsWith("https://")) {
return url;
} else {
return "http://" + url;
}
}
};
private static final Linkify.MatchFilter WEBURL_MATCH_FILTER = new Linkify.MatchFilter() {
@Override
public boolean acceptMatch(CharSequence charSequence, int start, int end) {
return start < 1 || charSequence.charAt(start - 1) != '@';
}
};
private ConversationActivity activity;
private DisplayMetrics metrics;
private AudioWife audioWife;
private OnContactPictureClicked mOnContactPictureClickedListener;
private OnContactPictureLongClicked mOnContactPictureLongClickedListener;
private boolean mIndicateReceived = false;
private OnQuoteListener onQuoteListener;
private final ListSelectionManager listSelectionManager = new ListSelectionManager();
private HashMap<Integer, AudioWife> audioPlayer;
private boolean mUseWhiteBackground = false;
public MessageAdapter(ConversationActivity activity, List<Message> messages) {
super(activity, 0, messages);
this.activity = activity;
metrics = getContext().getResources().getDisplayMetrics();
updatePreferences();
}
public void setOnContactPictureClicked(OnContactPictureClicked listener) {
this.mOnContactPictureClickedListener = listener;
}
public void setOnContactPictureLongClicked(
OnContactPictureLongClicked listener) {
this.mOnContactPictureLongClickedListener = listener;
}
public void setOnQuoteListener(OnQuoteListener listener) {
this.onQuoteListener = listener;
}
@Override
public int getViewTypeCount() {
return 3;
}
public int getItemViewType(Message message) {
if (message.getType() == Message.TYPE_STATUS) {
return STATUS;
} else if (message.getStatus() <= Message.STATUS_RECEIVED) {
return RECEIVED;
}
return SENT;
}
@Override
public int getItemViewType(int position) {
return getItemViewType(getItem(position));
}
private int getMessageTextColor(boolean onDark, boolean primary) {
if (onDark) {
return activity.getResources().getColor(primary ? R.color.dark : R.color.primary);
} else {
return activity.getResources().getColor(primary ? R.color.dark : R.color.primary);
}
}
private void displayStatus(ViewHolder viewHolder, final Message message, int type, boolean darkBackground, boolean inValidSession) {
String filesize = null;
String info = null;
boolean error = false;
if (viewHolder.indicatorReceived != null) {
viewHolder.indicatorReceived.setVisibility(View.GONE);
viewHolder.indicatorRead.setVisibility(View.GONE);
}
if (viewHolder.edit_indicator != null) {
if (message.edited()) {
viewHolder.edit_indicator.setVisibility(View.VISIBLE);
} else {
viewHolder.edit_indicator.setVisibility(View.GONE);
}
}
boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI
&& message.getMergedStatus() <= Message.STATUS_RECEIVED;
if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE || message.getTransferable() != null) {
FileParams params = message.getFileParams();
if (params.size > (1 * 1024 * 1024)) {
filesize = params.size / (1024 * 1024) + " MiB";
} else if (params.size > (1 * 1024)) {
filesize = params.size / 1024 + " KiB";
} else if (params.size > 0) {
filesize = "1 KiB";
}
if (message.getTransferable() != null && message.getTransferable().getStatus() == Transferable.STATUS_FAILED) {
error = true;
}
}
switch (message.getMergedStatus()) {
case Message.STATUS_WAITING:
info = getContext().getString(R.string.waiting);
break;
case Message.STATUS_UNSEND:
Transferable d = message.getTransferable();
if (d != null) {
info = getContext().getString(R.string.sending_file, d.getProgress());
} else {
info = getContext().getString(R.string.sending);
}
break;
case Message.STATUS_OFFERED:
info = getContext().getString(R.string.offering);
break;
case Message.STATUS_SEND_RECEIVED:
if (mIndicateReceived) {
viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
}
break;
case Message.STATUS_SEND_DISPLAYED:
if (mIndicateReceived) {
viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
viewHolder.indicatorRead.setVisibility(View.VISIBLE);
}
break;
case Message.STATUS_SEND_FAILED:
info = getContext().getString(R.string.send_failed);
error = true;
break;
default:
if (multiReceived) {
info = UIHelper.getMessageDisplayName(message);
}
break;
}
if (error && type == SENT) {
viewHolder.time.setTextColor(activity.getWarningTextColor());
DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
if (file.exists()) {
viewHolder.resend_button.setVisibility(View.VISIBLE);
}
viewHolder.resend_button.setText(R.string.send_again);
viewHolder.resend_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_resend_grey600_48dp, 0, 0, 0);
viewHolder.resend_button.setOnClickListener(new OnClickListener() {
final Message mMessage = message;
@Override
public void onClick(View v) {
activity.mConversationFragment.resendMessage(mMessage);
}
});
} else if (!error && type == SENT) {
viewHolder.resend_button.setVisibility(View.GONE);
} else {
viewHolder.time.setTextColor(this.getMessageTextColor(darkBackground, false));
}
if (message.getEncryption() == Message.ENCRYPTION_NONE) {
viewHolder.indicator.setVisibility(View.GONE);
} else {
boolean verified = false;
if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
FingerprintStatus status = message.getConversation()
.getAccount().getAxolotlService().getFingerprintTrust(
message.getFingerprint());
if (status != null && status.isTrustedAndActive()) {
verified = true;
}
}
if (verified) {
viewHolder.indicator.setImageResource(R.drawable.ic_verified_user_blue_18dp);
} else {
viewHolder.indicator.setImageResource(R.drawable.ic_lock_blue_18dp);
}
viewHolder.indicator.setAlpha(0.7f);
viewHolder.indicator.setVisibility(View.VISIBLE);
}
String formatedTime = UIHelper.readableTimeDifferenceFull(getContext(),
message.getMergedTimeSent());
if (message.getStatus() <= Message.STATUS_RECEIVED) {
if ((filesize != null) && (info != null)) {
viewHolder.time.setText(formatedTime + " \u00B7 " + 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, String text, boolean darkBackground) {
viewHolder.aw_player.setVisibility(View.GONE);
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.VISIBLE);
viewHolder.messageBody.setText(text);
viewHolder.messageBody.setTextColor(getMessageTextColor(darkBackground, false));
viewHolder.messageBody.setTypeface(null, Typeface.ITALIC);
viewHolder.messageBody.setTextIsSelectable(false);
}
private void displayDecryptionFailed(ViewHolder viewHolder, boolean darkBackground) {
viewHolder.aw_player.setVisibility(View.GONE);
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(getMessageTextColor(darkBackground, false));
viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
viewHolder.messageBody.setTextIsSelectable(false);
}
private void displayHeartMessage(final ViewHolder viewHolder, final String body) {
viewHolder.aw_player.setVisibility(View.GONE);
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.VISIBLE);
viewHolder.messageBody.setIncludeFontPadding(false);
Spannable span = new SpannableString(body);
span.setSpan(new RelativeSizeSpan(4.0f), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new ForegroundColorSpan(activity.getWarningTextColor()), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
viewHolder.messageBody.setText(span);
}
private void displayXmppMessage(final ViewHolder viewHolder, final String body) {
String contact = body.toLowerCase();
contact = contact.split(":")[1];
contact = contact.split("\\?")[0];
String add_contact = activity.getString(R.string.add_to_contact_list) + " (" + contact + ")";
viewHolder.aw_player.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.VISIBLE);
viewHolder.download_button.setText(add_contact);
viewHolder.download_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(body));
activity.startActivity(intent);
}
});
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE);
}
private int applyQuoteSpan(SpannableStringBuilder body, int start, int end, boolean darkBackground) {
if (start > 1 && !"\n\n".equals(body.subSequence(start - 2, start).toString())) {
body.insert(start++, "\n");
body.setSpan(new DividerSpan(false), start - 2, start, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
end++;
}
if (end < body.length() - 1 && !"\n\n".equals(body.subSequence(end, end + 2).toString())) {
body.insert(end, "\n");
body.setSpan(new DividerSpan(false), end, end + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
int color = darkBackground ? this.getMessageTextColor(darkBackground, false)
: getContext().getResources().getColor(R.color.bubble);
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
body.setSpan(new QuoteSpan(color, metrics), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return 0;
}
/**
* Applies QuoteSpan to group of lines which starts with > or » characters.
* Appends likebreaks and applies DividerSpan to them to show a padding between quote and text.
*/
private boolean handleTextQuotes(SpannableStringBuilder body, boolean darkBackground) {
boolean startsWithQuote = false;
char previous = '\n';
int lineStart = -1;
int lineTextStart = -1;
int quoteStart = -1;
for (int i = 0; i <= body.length(); i++) {
char current = body.length() > i ? body.charAt(i) : '\n';
if (lineStart == -1) {
if (previous == '\n') {
if ((current == '>' && !UIHelper.isPositionFollowedByNumber(body, i)) || current == '\u00bb') {
// Line start with quote
lineStart = i;
if (quoteStart == -1) quoteStart = i;
if (i == 0) startsWithQuote = true;
} else if (quoteStart >= 0) {
// Line start without quote, apply spans there
applyQuoteSpan(body, quoteStart, i - 1, darkBackground);
quoteStart = -1;
}
}
} else {
// Remove extra spaces between > and first character in the line
// > character will be removed too
if (current != ' ' && lineTextStart == -1) {
lineTextStart = i;
}
if (current == '\n') {
body.delete(lineStart, lineTextStart);
i -= lineTextStart - lineStart;
if (i == lineStart) {
// Avoid empty lines because span over empty line can be hidden
body.insert(i++, " ");
}
lineStart = -1;
lineTextStart = -1;
}
}
previous = current;
}
if (quoteStart >= 0) {
// Apply spans to finishing open quote
applyQuoteSpan(body, quoteStart, body.length(), darkBackground);
}
return startsWithQuote;
}
private void displayTextMessage(final ViewHolder viewHolder, final Message message, boolean darkBackground) {
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.VISIBLE);
viewHolder.messageBody.setIncludeFontPadding(true);
if (message.getBody() != null) {
final String nick = UIHelper.getMessageDisplayName(message);
SpannableStringBuilder body = message.getMergedBody();
boolean hasMeCommand = message.hasMeCommand();
if (hasMeCommand) {
body = body.replace(0, Message.ME_COMMAND.length(), nick + " ");
}
if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
body = new SpannableStringBuilder(body, 0, Config.MAX_DISPLAY_MESSAGE_CHARS);
body.append("\u2026");
}
Message.MergeSeparator[] mergeSeparators = body.getSpans(0, body.length(), Message.MergeSeparator.class);
for (Message.MergeSeparator mergeSeparator : mergeSeparators) {
int start = body.getSpanStart(mergeSeparator);
int end = body.getSpanEnd(mergeSeparator);
body.setSpan(new DividerSpan(true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
boolean startsWithQuote = handleTextQuotes(body, darkBackground);
if (message.getType() != Message.TYPE_PRIVATE) {
if (hasMeCommand) {
body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else {
String privateMarker;
if (message.getStatus() <= Message.STATUS_RECEIVED) {
privateMarker = activity
.getString(R.string.private_message);
} else {
final String to;
if (message.getCounterpart() != null) {
to = message.getCounterpart().getResourcepart();
} else {
to = "";
}
privateMarker = activity.getString(R.string.private_message_to, to);
}
body.insert(0, privateMarker);
int privateMarkerIndex = privateMarker.length();
if (startsWithQuote) {
body.insert(privateMarkerIndex, "\n\n");
body.setSpan(new DividerSpan(false), privateMarkerIndex, privateMarkerIndex + 2,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
body.insert(privateMarkerIndex, " ");
}
body.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground, false)), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
body.setSpan(new StyleSpan(Typeface.BOLD), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (hasMeCommand) {
body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), privateMarkerIndex + 1, privateMarkerIndex + 1 + nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
if (message.getConversation().getMode() == Conversation.MODE_MULTI && message.getStatus() == Message.STATUS_RECEIVED) {
Pattern pattern = NotificationService.generateNickHighlightPattern(message.getConversation().getMucOptions().getActualNick());
Matcher matcher = pattern.matcher(body);
while (matcher.find()) {
body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
Linkify.addLinks(body, XMPP_PATTERN, "xmpp");
Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER);
Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
viewHolder.messageBody.setAutoLinkMask(0);
viewHolder.messageBody.setText(body);
viewHolder.messageBody.setTextIsSelectable(true);
viewHolder.messageBody.setMovementMethod(ClickableMovementMethod.getInstance());
listSelectionManager.onUpdate(viewHolder.messageBody, message);
} else {
viewHolder.messageBody.setText("");
viewHolder.messageBody.setTextIsSelectable(false);
}
viewHolder.messageBody.setTextColor(this.getMessageTextColor(darkBackground, true));
viewHolder.messageBody.setLinkTextColor(this.getMessageTextColor(darkBackground, true));
viewHolder.messageBody.setHighlightColor(activity.getResources().getColor(darkBackground ? R.color.grey800 : R.color.grey500));
viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
}
private void displayDownloadableMessage(ViewHolder viewHolder, final Message message, String text) {
viewHolder.aw_player.setVisibility(View.GONE);
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.VISIBLE);
viewHolder.download_button.setText(text);
viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_download_grey600_48dp, 0, 0, 0);
viewHolder.download_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
activity.startDownloadable(message);
}
});
}
private void displayAudioMessage(ViewHolder viewHolder, final Message message, int position) {
if (audioPlayer == null) audioPlayer = new HashMap<>();
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE);
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.aw_player.setVisibility(View.VISIBLE);
Uri audioFile = Uri.fromFile(activity.xmppConnectionService.getFileBackend().getFile(message));
audioWife = audioPlayer.get(position);
if (audioWife == null) {
audioWife = new AudioWife();
audioWife.init(getContext(), audioFile);
audioPlayer.put(position, audioWife);
RelativeLayout vg = new RelativeLayout(activity);
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
audioWife.useDefaultUi(vg, layoutInflater);
viewHolder.aw_player.addView(audioWife.getPlayerUi());
} else {
audioWife.cleanPlayerUi();
viewHolder.aw_player.addView(audioWife.getPlayerUi());
}
}
private void displayOpenableMessage(ViewHolder viewHolder, final Message message) {
viewHolder.aw_player.setVisibility(View.GONE);
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.VISIBLE);
String mimeType = message.getMimeType();
String fullName = "";
if (mimeType != null) {
if (message.getMimeType().contains("pdf")) {
viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_file_pdf_grey600_48dp, 0, 0, 0);
} else if (message.getMimeType().contains("vcard")) {
File file = new File(activity.xmppConnectionService.getFileBackend().getFile(message).toString());
VCard vcard = null;
String name = null;
String version = null;
try {
vcard = Ezvcard.parse(file).first();
} catch (IOException e) {
e.printStackTrace();
}
if (vcard != null) {
version = vcard.getVersion().toString();
Log.d(Config.LOGTAG, "VCard version: " + version);
name = vcard.getFormattedName().getValue();
fullName = " (" + name + ")";
}
viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_account_card_details_grey600_48dp, 0, 0, 0);
} else if (message.getMimeType().contains("calendar")) {
viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_calendar_grey600_48dp, 0, 0, 0);
} else if (message.getMimeType().equals("application/vnd.android.package-archive")) {
String file = activity.xmppConnectionService.getFileBackend().getFile(message).toString();
Log.d(Config.LOGTAG, "APK path: " + file);
try {
PackageManager pm = getContext().getPackageManager();
PackageInfo pi = pm.getPackageArchiveInfo(file, 0);
pi.applicationInfo.sourceDir = file;
pi.applicationInfo.publicSourceDir = file;
Drawable APKicon = pi.applicationInfo.loadIcon(pm);
String AppName = (String) pi.applicationInfo.loadLabel(pm);
String AppVersion = (String) pi.versionName;
Log.d(Config.LOGTAG, "APK name: " + AppName);
fullName = " (" + AppName + " " + AppVersion + ")";
} catch (Exception e) {
//ignored
}
viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_file_grey600_48dp, 0, 0, 0);
} else {
viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_file_grey600_48dp, 0, 0, 0);
}
}
viewHolder.download_button.setText(activity.getString(R.string.open_x_file, UIHelper.getFileDescriptionString(activity, message) + fullName));
viewHolder.download_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openDownloadable(message);
}
});
}
private void displayLocationMessage(ViewHolder viewHolder, final Message message) {
viewHolder.aw_player.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE);
String url = GeoHelper.MapPreviewUri(message);
viewHolder.image.setVisibility(View.VISIBLE);
viewHolder.image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showLocation(message);
}
});
Glide
.with(activity)
.load(Uri.parse(url))
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter()
.placeholder(R.drawable.ic_map_marker_grey600_48dp)
.error(R.drawable.ic_map_marker_grey600_48dp)
.into(viewHolder.image);
viewHolder.image.setMaxWidth(500);
viewHolder.image.setAdjustViewBounds(true);
viewHolder.download_button.setVisibility(View.GONE);
viewHolder.download_button.setText(R.string.show_location);
viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_map_marker_grey600_48dp, 0, 0, 0);
viewHolder.download_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showLocation(message);
}
});
}
private void displayImageMessage(ViewHolder viewHolder,
final Message message) {
viewHolder.aw_player.setVisibility(View.GONE);
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.messageBody.setVisibility(View.GONE);
viewHolder.image.setVisibility(View.VISIBLE);
FileParams params = message.getFileParams();
double target = metrics.density * 200;
int scaledW;
int scaledH;
if (Math.max(params.height, params.width) * metrics.density <= target) {
scaledW = (int) (params.width * metrics.density);
scaledH = (int) (params.height * metrics.density);
} else if (Math.max(params.height, params.width) <= target) {
scaledW = params.width;
scaledH = params.height;
} else if (params.width <= params.height) {
scaledW = (int) (params.width / ((double) params.height / target));
scaledH = (int) target;
} else {
scaledW = (int) target;
scaledH = (int) (params.height / ((double) params.width / target));
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(scaledW, scaledH);
layoutParams.setMargins(0, (int) (metrics.density * 4), 0, (int) (metrics.density * 4));
viewHolder.image.setLayoutParams(layoutParams);
activity.loadBitmap(message, viewHolder.image);
viewHolder.image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openDownloadable(message);
}
});
}
private void loadMoreMessages(Conversation conversation) {
conversation.setLastClearHistory(0);
activity.xmppConnectionService.updateConversation(conversation);
conversation.setHasMessagesLeftOnServer(true);
conversation.setFirstMamReference(null);
long timestamp = conversation.getLastMessageTransmitted();
if (timestamp == 0) {
timestamp = System.currentTimeMillis();
}
conversation.messagesLoaded.set(true);
MessageArchiveService.Query query = activity.xmppConnectionService.getMessageArchiveService().query(conversation, 0, timestamp);
if (query != null) {
Toast.makeText(activity, R.string.fetching_history_from_server, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(activity, R.string.not_fetching_history_retention_period, Toast.LENGTH_SHORT).show();
}
}
@Override
public View getView(int position, View unused, ViewGroup parent) {
final Message message = getItem(position);
final boolean omemoEncryption = message.getEncryption() == Message.ENCRYPTION_AXOLOTL;
final boolean isInValidSession = message.isValidInSession() && (!omemoEncryption || message.isTrusted());
final Conversation conversation = message.getConversation();
final Account account = conversation.getAccount();
final int type = getItemViewType(position);
ViewHolder viewHolder;
View view;
viewHolder = new ViewHolder();
switch (type) {
case SENT:
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.aw_player = (ViewGroup) view.findViewById(R.id.aw_player);
viewHolder.download_button = (Button) view
.findViewById(R.id.download_button);
viewHolder.resend_button = (Button) view
.findViewById(R.id.resend_button);
viewHolder.indicator = (ImageView) view
.findViewById(R.id.security_indicator);
viewHolder.edit_indicator = (ImageView) view.findViewById(R.id.edit_indicator);
viewHolder.image = (ImageView) view
.findViewById(R.id.message_image);
viewHolder.messageBody = (CopyTextView) view
.findViewById(R.id.message_body);
viewHolder.time = (TextView) view
.findViewById(R.id.message_time);
viewHolder.indicatorReceived = (ImageView) view
.findViewById(R.id.indicator_received);
viewHolder.indicatorRead = (ImageView) view
.findViewById(R.id.indicator_read);
break;
case RECEIVED:
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.aw_player = (ViewGroup) view.findViewById(R.id.aw_player);
viewHolder.download_button = (Button) view
.findViewById(R.id.download_button);
viewHolder.indicator = (ImageView) view
.findViewById(R.id.security_indicator);
viewHolder.edit_indicator = (ImageView) view.findViewById(R.id.edit_indicator);
viewHolder.image = (ImageView) view
.findViewById(R.id.message_image);
viewHolder.messageBody = (CopyTextView) view
.findViewById(R.id.message_body);
viewHolder.time = (TextView) view
.findViewById(R.id.message_time);
viewHolder.indicatorReceived = (ImageView) view
.findViewById(R.id.indicator_received);
viewHolder.encryption = (TextView) view.findViewById(R.id.message_encryption);
break;
case STATUS:
view = activity.getLayoutInflater().inflate(R.layout.message_status, parent, false);
viewHolder.contact_picture = (ImageView) view.findViewById(R.id.message_photo);
viewHolder.status_message = (TextView) view.findViewById(R.id.status_message);
viewHolder.load_more_messages = (Button) view.findViewById(R.id.load_more_messages);
break;
default:
view = new View(getContext());
viewHolder = null;
break;
}
if (viewHolder.messageBody != null) {
listSelectionManager.onCreate(viewHolder.messageBody, new MessageBodyActionModeCallback(viewHolder.messageBody));
viewHolder.messageBody.setCopyHandler(this);
}
view.setTag(viewHolder);
if (viewHolder == null) {
return view;
}
boolean darkBackground = (type == SENT && (!isInValidSession || !mUseWhiteBackground));
if (type == STATUS) {
if ("LOAD_MORE".equals(message.getBody())) {
viewHolder.status_message.setVisibility(View.GONE);
viewHolder.contact_picture.setVisibility(View.GONE);
viewHolder.load_more_messages.setVisibility(View.VISIBLE);
viewHolder.load_more_messages.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loadMoreMessages(message.getConversation());
}
});
} else {
viewHolder.status_message.setVisibility(View.VISIBLE);
viewHolder.contact_picture.setVisibility(View.VISIBLE);
viewHolder.load_more_messages.setVisibility(View.GONE);
if (conversation.getMode() == Conversation.MODE_SINGLE) {
viewHolder.contact_picture.setImageBitmap(activity
.avatarService().get(conversation.getContact(),
activity.getPixel(32)));
viewHolder.contact_picture.setAlpha(0.5f);
}
viewHolder.status_message.setText(message.getBody());
}
return view;
} else {
loadAvatar(message, viewHolder.contact_picture);
}
viewHolder.contact_picture
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (MessageAdapter.this.mOnContactPictureClickedListener != null) {
MessageAdapter.this.mOnContactPictureClickedListener
.onContactPictureClicked(message);
}
}
});
viewHolder.contact_picture
.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) {
MessageAdapter.this.mOnContactPictureLongClickedListener
.onContactPictureLongClicked(message);
return true;
} else {
return false;
}
}
});
final Transferable transferable = message.getTransferable();
String mimeType = message.getMimeType();
if (transferable != null && transferable.getStatus() != Transferable.STATUS_UPLOADING) {
if (transferable.getStatus() == Transferable.STATUS_OFFER) {
displayDownloadableMessage(viewHolder, message, activity.getString(R.string.download_x_file, UIHelper.getFileDescriptionString(activity, message)));
} else if (transferable.getStatus() == Transferable.STATUS_OFFER_CHECK_FILESIZE) {
displayDownloadableMessage(viewHolder, message, activity.getString(R.string.check_x_filesize, UIHelper.getFileDescriptionString(activity, message)));
} else {
displayInfoMessage(viewHolder, UIHelper.getMessagePreview(activity, message).first, darkBackground);
}
} else if (message.getType() == Message.TYPE_IMAGE && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
displayImageMessage(viewHolder, message);
} else if (message.getType() == Message.TYPE_FILE && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
if (message.getFileParams().width > 0) {
displayImageMessage(viewHolder, message);
} else {
if (mimeType != null) {
if (message.getMimeType().startsWith("audio/")) {
displayAudioMessage(viewHolder, message, position);
} else displayOpenableMessage(viewHolder, message);
} else displayOpenableMessage(viewHolder, message);
}
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
if (account.isPgpDecryptionServiceConnected()) {
if (!account.hasPendingPgpIntent(conversation)) {
displayInfoMessage(viewHolder, activity.getString(R.string.message_decrypting), darkBackground);
} else {
displayInfoMessage(viewHolder, activity.getString(R.string.pgp_message), darkBackground);
}
} else {
displayInfoMessage(viewHolder, activity.getString(R.string.install_openkeychain), darkBackground);
if (viewHolder != null) {
viewHolder.message_box
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
activity.showInstallPgpDialog();
}
});
}
}
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
displayDecryptionFailed(viewHolder, darkBackground);
} else {
if (GeoHelper.isGeoUri(message.getBody())) {
displayLocationMessage(viewHolder, message);
} else if (message.bodyIsHeart()) {
displayHeartMessage(viewHolder, message.getBody().trim());
} else if (message.bodyIsXmpp()) {
displayXmppMessage(viewHolder, message.getBody().trim());
} else if (message.treatAsDownloadable() == Message.Decision.MUST ||
message.treatAsDownloadable() == Message.Decision.SHOULD) {
try {
URL url = new URL(message.getBody());
displayDownloadableMessage(viewHolder,
message,
activity.getString(R.string.check_x_filesize_on_host,
UIHelper.getFileDescriptionString(activity, message),
url.getHost()));
} catch (Exception e) {
displayDownloadableMessage(viewHolder,
message,
activity.getString(R.string.check_x_filesize,
UIHelper.getFileDescriptionString(activity, message)));
}
} else {
displayTextMessage(viewHolder, message, darkBackground);
}
}
if (type == RECEIVED) {
if (isInValidSession) {
viewHolder.message_box.setBackgroundResource(R.drawable.message_bubble_received);
viewHolder.encryption.setVisibility(View.GONE);
} else {
viewHolder.message_box.setBackgroundResource(R.drawable.message_bubble_received_warning);
viewHolder.encryption.setVisibility(View.VISIBLE);
if (omemoEncryption && !message.isTrusted()) {
viewHolder.encryption.setText(R.string.not_trusted);
} else {
viewHolder.encryption.setText(CryptoHelper.encryptionTypeToText(message.getEncryption()));
}
}
}
if (type == SENT) {
if (mUseWhiteBackground) {
viewHolder.message_box.setBackgroundResource(R.drawable.message_bubble_sent_white);
} else {
viewHolder.message_box.setBackgroundResource(R.drawable.message_bubble_sent);
}
}
displayStatus(viewHolder, message, type, darkBackground, isInValidSession);
return view;
}
@Override
public void notifyDataSetChanged() {
listSelectionManager.onBeforeNotifyDataSetChanged();
super.notifyDataSetChanged();
listSelectionManager.onAfterNotifyDataSetChanged();
}
private String transformText(CharSequence text, int start, int end, boolean forCopy) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Object copySpan = new Object();
builder.setSpan(copySpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
DividerSpan[] dividerSpans = builder.getSpans(0, builder.length(), DividerSpan.class);
for (DividerSpan dividerSpan : dividerSpans) {
builder.replace(builder.getSpanStart(dividerSpan), builder.getSpanEnd(dividerSpan),
dividerSpan.isLarge() ? "\n\n" : "\n");
}
start = builder.getSpanStart(copySpan);
end = builder.getSpanEnd(copySpan);
if (start == -1 || end == -1) return "";
builder = new SpannableStringBuilder(builder, start, end);
if (forCopy) {
QuoteSpan[] quoteSpans = builder.getSpans(0, builder.length(), QuoteSpan.class);
for (QuoteSpan quoteSpan : quoteSpans) {
builder.insert(builder.getSpanStart(quoteSpan), "> ");
}
}
return builder.toString();
}
@Override
public String transformTextForCopy(CharSequence text, int start, int end) {
if (text instanceof Spanned) {
return transformText(text, start, end, true);
} else {
return text.toString().substring(start, end);
}
}
public interface OnQuoteListener {
public void onQuote(String text);
}
private class MessageBodyActionModeCallback implements ActionMode.Callback {
private final TextView textView;
public MessageBodyActionModeCallback(TextView textView) {
this.textView = textView;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
if (onQuoteListener != null) {
int quoteResId = activity.getThemeResource(R.attr.icon_quote, R.drawable.ic_action_reply);
// 3rd item is placed after "copy" item
menu.add(0, android.R.id.button1, 3, R.string.quote).setIcon(quoteResId)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
return false;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == android.R.id.button1) {
int start = textView.getSelectionStart();
int end = textView.getSelectionEnd();
if (end > start) {
String text = transformText(textView.getText(), start, end, false);
if (onQuoteListener != null) {
onQuoteListener.onQuote(text);
}
mode.finish();
}
return true;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {}
}
public void openDownloadable(Message message) {
DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
if (!file.exists()) {
Toast.makeText(activity, R.string.file_deleted, Toast.LENGTH_SHORT).show();
return;
}
String mime = file.getMimeType();
if (mime.startsWith("image/")) {
Intent intent = new Intent(getContext(), ShowFullscreenMessageActivity.class);
intent.putExtra("image", Uri.fromFile(file));
try {
activity.startActivity(intent);
return;
} catch (ActivityNotFoundException e) {
//ignored
}
} else if (mime.startsWith("video/")) {
Intent intent = new Intent(getContext(), ShowFullscreenMessageActivity.class);
intent.putExtra("video", Uri.fromFile(file));
try {
activity.startActivity(intent);
return;
} catch (ActivityNotFoundException e) {
//ignored
}
}
Intent openIntent = new Intent(Intent.ACTION_VIEW);
if (mime == null) {
mime = "*/*";
}
Uri uri;
try {
uri = FileBackend.getUriForFile(activity, file);
} catch (SecurityException e) {
Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
return;
}
openIntent.setDataAndType(uri, mime);
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PackageManager manager = activity.getPackageManager();
List<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0);
if (info.size() == 0) {
openIntent.setDataAndType(uri,"*/*");
}
try {
getContext().startActivity(openIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
}
}
public void showLocation(Message message) {
for (Intent intent : GeoHelper.createGeoIntentsFromMessage(message)) {
if (intent.resolveActivity(getContext().getPackageManager()) != null) {
getContext().startActivity(intent);
return;
}
}
Toast.makeText(activity, R.string.no_application_found_to_display_location, Toast.LENGTH_SHORT).show();
}
public void updatePreferences() {
this.mIndicateReceived = activity.indicateReceived();
this.mUseWhiteBackground = activity.useWhiteBackground();
}
public TextView getMessageBody(View view) {
final Object tag = view.getTag();
if (tag instanceof ViewHolder) {
final ViewHolder viewHolder = (ViewHolder) tag;
return viewHolder.messageBody;
}
return null;
}
public interface OnContactPictureClicked {
void onContactPictureClicked(Message message);
}
public interface OnContactPictureLongClicked {
void onContactPictureLongClicked(Message message);
}
private static class ViewHolder {
protected LinearLayout message_box;
protected Button download_button;
protected Button resend_button;
protected ViewGroup aw_player;
protected ImageView image;
protected ImageView indicator;
protected ImageView indicatorReceived;
protected ImageView indicatorRead;
protected TextView time;
protected CopyTextView messageBody;
protected ImageView contact_picture;
protected TextView status_message;
protected TextView encryption;
public Button load_more_messages;
public ImageView edit_indicator;
}
class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Message message = null;
public BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(Message... params) {
return activity.avatarService().get(params[0], activity.getPixel(48), isCancelled());
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && !isCancelled()) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setBackgroundColor(0x00000000);
}
}
}
}
public void loadAvatar(Message message, ImageView imageView) {
if (cancelPotentialWork(message, imageView)) {
final Bitmap bm = activity.avatarService().get(message, activity.getPixel(48), true);
if (bm != null) {
cancelPotentialWork(message, imageView);
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} else {
imageView.setBackgroundColor(UIHelper.getColorForName(UIHelper.getMessageDisplayName(message)));
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
imageView.setImageDrawable(asyncDrawable);
try {
task.execute(message);
} catch (final RejectedExecutionException ignored) {
}
}
}
}
public static boolean cancelPotentialWork(Message message, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final Message oldMessage = bitmapWorkerTask.message;
if (oldMessage == null || message != oldMessage) {
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;
}
static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}
}
|