1package eu.siacs.conversations.ui.adapter;
2
3import android.Manifest;
4import android.app.Activity;
5import android.content.Intent;
6import android.content.SharedPreferences;
7import android.content.pm.PackageManager;
8import android.graphics.PorterDuff;
9import android.graphics.drawable.Drawable;
10import android.graphics.Typeface;
11import android.net.Uri;
12import android.os.AsyncTask;
13import android.os.Build;
14import android.preference.PreferenceManager;
15import android.text.Spannable;
16import android.text.SpannableString;
17import android.text.SpannableStringBuilder;
18import android.text.style.ImageSpan;
19import android.text.style.ClickableSpan;
20import android.text.format.DateUtils;
21import android.text.style.ForegroundColorSpan;
22import android.text.style.RelativeSizeSpan;
23import android.text.style.StyleSpan;
24import android.text.style.URLSpan;
25import android.util.DisplayMetrics;
26import android.util.LruCache;
27import android.view.accessibility.AccessibilityEvent;
28import android.view.MotionEvent;
29import android.view.View;
30import android.view.ViewGroup;
31import android.view.WindowManager;
32import android.widget.ArrayAdapter;
33import android.widget.Button;
34import android.widget.ImageView;
35import android.widget.LinearLayout;
36import android.widget.ListAdapter;
37import android.widget.ListView;
38import android.widget.RelativeLayout;
39import android.widget.TextView;
40import android.widget.Toast;
41
42import androidx.core.app.ActivityCompat;
43import androidx.core.content.ContextCompat;
44import androidx.core.content.res.ResourcesCompat;
45
46import com.cheogram.android.BobTransfer;
47import com.cheogram.android.MessageTextActionModeCallback;
48import com.cheogram.android.SwipeDetector;
49import com.cheogram.android.WebxdcPage;
50import com.cheogram.android.WebxdcUpdate;
51
52import com.google.common.base.Strings;
53
54import com.lelloman.identicon.view.GithubIdenticonView;
55
56import java.io.IOException;
57import java.net.URI;
58import java.net.URISyntaxException;
59import java.security.NoSuchAlgorithmException;
60import java.util.HashMap;
61import java.util.List;
62import java.util.Map;
63import java.util.Locale;
64import java.util.regex.Matcher;
65import java.util.regex.Pattern;
66
67import io.ipfs.cid.Cid;
68
69import me.saket.bettermovementmethod.BetterLinkMovementMethod;
70
71import eu.siacs.conversations.Config;
72import eu.siacs.conversations.R;
73import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
74import eu.siacs.conversations.entities.Account;
75import eu.siacs.conversations.entities.Contact;
76import eu.siacs.conversations.entities.Conversation;
77import eu.siacs.conversations.entities.Conversational;
78import eu.siacs.conversations.entities.DownloadableFile;
79import eu.siacs.conversations.entities.Message.FileParams;
80import eu.siacs.conversations.entities.Message;
81import eu.siacs.conversations.entities.Roster;
82import eu.siacs.conversations.entities.RtpSessionStatus;
83import eu.siacs.conversations.entities.Transferable;
84import eu.siacs.conversations.persistance.FileBackend;
85import eu.siacs.conversations.services.MessageArchiveService;
86import eu.siacs.conversations.services.NotificationService;
87import eu.siacs.conversations.ui.ConversationFragment;
88import eu.siacs.conversations.ui.ConversationsActivity;
89import eu.siacs.conversations.ui.XmppActivity;
90import eu.siacs.conversations.ui.service.AudioPlayer;
91import eu.siacs.conversations.ui.text.DividerSpan;
92import eu.siacs.conversations.ui.text.QuoteSpan;
93import eu.siacs.conversations.ui.util.AvatarWorkerTask;
94import eu.siacs.conversations.ui.util.MyLinkify;
95import eu.siacs.conversations.ui.util.QuoteHelper;
96import eu.siacs.conversations.ui.util.ShareUtil;
97import eu.siacs.conversations.ui.util.StyledAttributes;
98import eu.siacs.conversations.ui.util.ViewUtil;
99import eu.siacs.conversations.utils.CryptoHelper;
100import eu.siacs.conversations.utils.Emoticons;
101import eu.siacs.conversations.utils.GeoHelper;
102import eu.siacs.conversations.utils.MessageUtils;
103import eu.siacs.conversations.utils.StylingHelper;
104import eu.siacs.conversations.utils.TimeFrameUtils;
105import eu.siacs.conversations.utils.UIHelper;
106import eu.siacs.conversations.xmpp.Jid;
107import eu.siacs.conversations.xmpp.mam.MamReference;
108import eu.siacs.conversations.xml.Element;
109
110public class MessageAdapter extends ArrayAdapter<Message> {
111
112 public static final String DATE_SEPARATOR_BODY = "DATE_SEPARATOR";
113 private static final int SENT = 0;
114 private static final int RECEIVED = 1;
115 private static final int STATUS = 2;
116 private static final int DATE_SEPARATOR = 3;
117 private static final int RTP_SESSION = 4;
118 private final XmppActivity activity;
119 private final AudioPlayer audioPlayer;
120 private List<String> highlightedTerm = null;
121 private final DisplayMetrics metrics;
122 private ConversationFragment mConversationFragment = null;
123 private OnContactPictureClicked mOnContactPictureClickedListener;
124 private OnContactPictureClicked mOnMessageBoxClickedListener;
125 private OnContactPictureClicked mOnMessageBoxSwipedListener;
126 private OnContactPictureLongClicked mOnContactPictureLongClickedListener;
127 private OnInlineImageLongClicked mOnInlineImageLongClickedListener;
128 private boolean mUseGreenBackground = false;
129 private final boolean mForceNames;
130 private final Map<String, WebxdcUpdate> lastWebxdcUpdate = new HashMap<>();
131 private String selectionUuid = null;
132
133 public MessageAdapter(final XmppActivity activity, final List<Message> messages, final boolean forceNames) {
134 super(activity, 0, messages);
135 this.audioPlayer = new AudioPlayer(this);
136 this.activity = activity;
137 metrics = getContext().getResources().getDisplayMetrics();
138 updatePreferences();
139 this.mForceNames = forceNames;
140 }
141
142 public MessageAdapter(final XmppActivity activity, final List<Message> messages) {
143 this(activity, messages, false);
144 }
145
146 private static void resetClickListener(View... views) {
147 for (View view : views) {
148 if (view != null) view.setOnClickListener(null);
149 }
150 }
151
152 public void flagScreenOn() {
153 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
154 }
155
156 public void flagScreenOff() {
157 activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
158 }
159
160 public void setVolumeControl(final int stream) {
161 activity.setVolumeControlStream(stream);
162 }
163
164 public void setOnContactPictureClicked(OnContactPictureClicked listener) {
165 this.mOnContactPictureClickedListener = listener;
166 }
167
168 public void setOnMessageBoxClicked(OnContactPictureClicked listener) {
169 this.mOnMessageBoxClickedListener = listener;
170 }
171
172 public void setOnMessageBoxSwiped(OnContactPictureClicked listener) {
173 this.mOnMessageBoxSwipedListener = listener;
174 }
175
176 public void setConversationFragment(ConversationFragment frag) {
177 mConversationFragment = frag;
178 }
179
180 public void quoteText(String text) {
181 if (mConversationFragment != null) mConversationFragment.quoteText(text);
182 }
183
184 public boolean hasSelection() {
185 return selectionUuid != null;
186 }
187
188 public Activity getActivity() {
189 return activity;
190 }
191
192 public void setOnContactPictureLongClicked(
193 OnContactPictureLongClicked listener) {
194 this.mOnContactPictureLongClickedListener = listener;
195 }
196
197 public void setOnInlineImageLongClicked(OnInlineImageLongClicked listener) {
198 this.mOnInlineImageLongClickedListener = listener;
199 }
200
201 @Override
202 public int getViewTypeCount() {
203 return 5;
204 }
205
206 private int getItemViewType(Message message) {
207 if (message.getType() == Message.TYPE_STATUS) {
208 if (DATE_SEPARATOR_BODY.equals(message.getBody())) {
209 return DATE_SEPARATOR;
210 } else {
211 return STATUS;
212 }
213 } else if (message.getType() == Message.TYPE_RTP_SESSION) {
214 return RTP_SESSION;
215 } else if (message.getStatus() <= Message.STATUS_RECEIVED) {
216 return RECEIVED;
217 } else {
218 return SENT;
219 }
220 }
221
222 @Override
223 public int getItemViewType(int position) {
224 return this.getItemViewType(getItem(position));
225 }
226
227 private int getMessageTextColor(boolean onDark, boolean primary) {
228 if (onDark) {
229 return ContextCompat.getColor(activity, primary ? R.color.white : R.color.white70);
230 } else {
231 return ContextCompat.getColor(activity, primary ? R.color.black87 : R.color.black54);
232 }
233 }
234
235 private void displayStatus(ViewHolder viewHolder, Message message, int type, boolean darkBackground) {
236 String filesize = null;
237 String info = null;
238 boolean error = false;
239 if (viewHolder.indicatorReceived != null) {
240 viewHolder.indicatorReceived.setVisibility(View.GONE);
241 }
242
243 if (viewHolder.edit_indicator != null) {
244 if (message.edited() && message.getModerated() == null) {
245 viewHolder.edit_indicator.setVisibility(View.VISIBLE);
246 viewHolder.edit_indicator.setImageResource(darkBackground ? R.drawable.ic_mode_edit_white_18dp : R.drawable.ic_mode_edit_black_18dp);
247 viewHolder.edit_indicator.setAlpha(darkBackground ? 0.7f : 0.57f);
248 } else {
249 viewHolder.edit_indicator.setVisibility(View.GONE);
250 }
251 }
252 final Transferable transferable = message.getTransferable();
253 boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI
254 && message.getMergedStatus() <= Message.STATUS_RECEIVED;
255 if (message.isFileOrImage() || transferable != null || MessageUtils.unInitiatedButKnownSize(message)) {
256 FileParams params = message.getFileParams();
257 filesize = params.size != null ? UIHelper.filesizeToString(params.size) : null;
258 if (transferable != null && (transferable.getStatus() == Transferable.STATUS_FAILED || transferable.getStatus() == Transferable.STATUS_CANCELLED)) {
259 error = true;
260 }
261 }
262 switch (message.getMergedStatus()) {
263 case Message.STATUS_WAITING:
264 info = getContext().getString(R.string.waiting);
265 break;
266 case Message.STATUS_UNSEND:
267 if (transferable != null) {
268 info = getContext().getString(R.string.sending_file, transferable.getProgress());
269 } else {
270 info = getContext().getString(R.string.sending);
271 }
272 break;
273 case Message.STATUS_OFFERED:
274 info = getContext().getString(R.string.offering);
275 break;
276 case Message.STATUS_SEND_RECEIVED:
277 case Message.STATUS_SEND_DISPLAYED:
278 if (viewHolder.indicatorReceived != null) {
279 viewHolder.indicatorReceived.setImageResource(darkBackground ? R.drawable.ic_done_white_18dp : R.drawable.ic_done_black_18dp);
280 viewHolder.indicatorReceived.setAlpha(darkBackground ? 0.7f : 0.57f);
281 viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
282 }
283 break;
284 case Message.STATUS_SEND_FAILED:
285 final String errorMessage = message.getErrorMessage();
286 if (Message.ERROR_MESSAGE_CANCELLED.equals(errorMessage)) {
287 info = getContext().getString(R.string.cancelled);
288 } else if (errorMessage != null) {
289 final String[] errorParts = errorMessage.split("\\u001f", 2);
290 if (errorParts.length == 2) {
291 switch (errorParts[0]) {
292 case "file-too-large":
293 info = getContext().getString(R.string.file_too_large);
294 break;
295 default:
296 info = getContext().getString(R.string.send_failed);
297 break;
298 }
299 } else {
300 info = getContext().getString(R.string.send_failed);
301 }
302 } else {
303 info = getContext().getString(R.string.send_failed);
304 }
305 error = true;
306 break;
307 default:
308 if (mForceNames || multiReceived || (message.getTrueCounterpart() != null && message.getContact() != null)) {
309 info = UIHelper.getMessageDisplayName(message);
310 }
311 break;
312 }
313 if (error && type == SENT) {
314 if (darkBackground) {
315 viewHolder.time.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption_Warning_OnDark);
316 } else {
317 viewHolder.time.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption_Warning);
318 }
319 } else {
320 if (darkBackground) {
321 viewHolder.time.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption_OnDark);
322 } else {
323 viewHolder.time.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption);
324 }
325 viewHolder.time.setTextColor(this.getMessageTextColor(darkBackground, false));
326 }
327 if (message.getEncryption() == Message.ENCRYPTION_NONE) {
328 viewHolder.indicator.setVisibility(View.GONE);
329 } else {
330 boolean verified = false;
331 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
332 final FingerprintStatus status = message.getConversation()
333 .getAccount().getAxolotlService().getFingerprintTrust(
334 message.getFingerprint());
335 if (status != null && status.isVerified()) {
336 verified = true;
337 }
338 }
339 if (verified) {
340 viewHolder.indicator.setImageResource(darkBackground ? R.drawable.ic_verified_user_white_18dp : R.drawable.ic_verified_user_black_18dp);
341 } else {
342 viewHolder.indicator.setImageResource(darkBackground ? R.drawable.ic_lock_white_18dp : R.drawable.ic_lock_black_18dp);
343 }
344 if (darkBackground) {
345 viewHolder.indicator.setAlpha(0.7f);
346 } else {
347 viewHolder.indicator.setAlpha(0.57f);
348 }
349 viewHolder.indicator.setVisibility(View.VISIBLE);
350 }
351
352 final String formattedTime = UIHelper.readableTimeDifferenceFull(getContext(), message.getMergedTimeSent());
353 final String bodyLanguage = message.getBodyLanguage();
354 final String bodyLanguageInfo = bodyLanguage == null ? "" : String.format(" \u00B7 %s", bodyLanguage.toUpperCase(Locale.US));
355 if (message.getStatus() <= Message.STATUS_RECEIVED) {
356 if ((filesize != null) && (info != null)) {
357 viewHolder.time.setText(formattedTime + " \u00B7 " + filesize + " \u00B7 " + info + bodyLanguageInfo);
358 } else if ((filesize == null) && (info != null)) {
359 viewHolder.time.setText(formattedTime + " \u00B7 " + info + bodyLanguageInfo);
360 } else if ((filesize != null) && (info == null)) {
361 viewHolder.time.setText(formattedTime + " \u00B7 " + filesize + bodyLanguageInfo);
362 } else {
363 viewHolder.time.setText(formattedTime + bodyLanguageInfo);
364 }
365 } else {
366 if ((filesize != null) && (info != null)) {
367 viewHolder.time.setText(filesize + " \u00B7 " + info + bodyLanguageInfo);
368 } else if ((filesize == null) && (info != null)) {
369 if (error) {
370 viewHolder.time.setText(info + " \u00B7 " + formattedTime + bodyLanguageInfo);
371 } else {
372 viewHolder.time.setText(info);
373 }
374 } else if ((filesize != null) && (info == null)) {
375 viewHolder.time.setText(filesize + " \u00B7 " + formattedTime + bodyLanguageInfo);
376 } else {
377 viewHolder.time.setText(formattedTime + bodyLanguageInfo);
378 }
379 }
380 }
381
382 private void displayInfoMessage(ViewHolder viewHolder, CharSequence text, boolean darkBackground, final Message message, int type) {
383 displayDownloadableMessage(viewHolder, message, "", darkBackground, type);
384 int imageVisibility = viewHolder.image.getVisibility();
385 displayInfoMessage(viewHolder, text, darkBackground);
386 viewHolder.image.setVisibility(imageVisibility);
387 }
388
389 private void displayInfoMessage(ViewHolder viewHolder, CharSequence text, boolean darkBackground) {
390 viewHolder.download_button.setVisibility(View.GONE);
391 viewHolder.audioPlayer.setVisibility(View.GONE);
392 viewHolder.image.setVisibility(View.GONE);
393 viewHolder.messageBody.setVisibility(View.VISIBLE);
394 viewHolder.messageBody.setText(text);
395 if (darkBackground) {
396 viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1_Secondary_OnDark);
397 } else {
398 viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1_Secondary);
399 }
400 viewHolder.messageBody.setTextIsSelectable(false);
401 }
402
403 private void displayEmojiMessage(final ViewHolder viewHolder, final SpannableStringBuilder body, final boolean darkBackground) {
404 viewHolder.download_button.setVisibility(View.GONE);
405 viewHolder.audioPlayer.setVisibility(View.GONE);
406 viewHolder.image.setVisibility(View.GONE);
407 viewHolder.messageBody.setVisibility(View.VISIBLE);
408 if (darkBackground) {
409 viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1_Emoji_OnDark);
410 } else {
411 viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1_Emoji);
412 }
413 ImageSpan[] imageSpans = body.getSpans(0, body.length(), ImageSpan.class);
414 float size = imageSpans.length == 1 || Emoticons.isEmoji(body.toString()) ? 3.0f : 2.0f;
415 body.setSpan(new RelativeSizeSpan(size), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
416 viewHolder.messageBody.setText(body);
417 }
418
419 private void applyQuoteSpan(SpannableStringBuilder body, int start, int end, boolean darkBackground) {
420 if (start > 1 && !"\n\n".equals(body.subSequence(start - 2, start).toString())) {
421 body.insert(start++, "\n");
422 body.setSpan(
423 new DividerSpan(false),
424 start - ("\n".equals(body.subSequence(start - 2, start - 1).toString()) ? 2 : 1),
425 start,
426 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
427 );
428 end++;
429 }
430 if (end < body.length() - 1 && !"\n\n".equals(body.subSequence(end, end + 2).toString())) {
431 body.insert(end, "\n");
432 body.setSpan(
433 new DividerSpan(false),
434 end,
435 end + ("\n".equals(body.subSequence(end + 1, end + 2).toString()) ? 2 : 1),
436 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
437 );
438 }
439 int color = darkBackground ? this.getMessageTextColor(darkBackground, false)
440 : ContextCompat.getColor(activity, R.color.green700_desaturated);
441 DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
442 body.setSpan(new QuoteSpan(color, metrics), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
443 }
444
445 /**
446 * Applies QuoteSpan to group of lines which starts with > or » characters.
447 * Appends likebreaks and applies DividerSpan to them to show a padding between quote and text.
448 */
449 public boolean handleTextQuotes(SpannableStringBuilder body, boolean darkBackground) {
450 boolean startsWithQuote = false;
451 int quoteDepth = 1;
452 while (QuoteHelper.bodyContainsQuoteStart(body) && quoteDepth <= Config.QUOTE_MAX_DEPTH) {
453 char previous = '\n';
454 int lineStart = -1;
455 int lineTextStart = -1;
456 int quoteStart = -1;
457 for (int i = 0; i <= body.length(); i++) {
458 char current = body.length() > i ? body.charAt(i) : '\n';
459 if (lineStart == -1) {
460 if (previous == '\n') {
461 if (i < body.length() && QuoteHelper.isPositionQuoteStart(body, i)) {
462 // Line start with quote
463 lineStart = i;
464 if (quoteStart == -1) quoteStart = i;
465 if (i == 0) startsWithQuote = true;
466 } else if (quoteStart >= 0) {
467 // Line start without quote, apply spans there
468 applyQuoteSpan(body, quoteStart, i - 1, darkBackground);
469 quoteStart = -1;
470 }
471 }
472 } else {
473 // Remove extra spaces between > and first character in the line
474 // > character will be removed too
475 if (current != ' ' && lineTextStart == -1) {
476 lineTextStart = i;
477 }
478 if (current == '\n') {
479 body.delete(lineStart, lineTextStart);
480 i -= lineTextStart - lineStart;
481 if (i == lineStart) {
482 // Avoid empty lines because span over empty line can be hidden
483 body.insert(i++, " ");
484 }
485 lineStart = -1;
486 lineTextStart = -1;
487 }
488 }
489 previous = current;
490 }
491 if (quoteStart >= 0) {
492 // Apply spans to finishing open quote
493 applyQuoteSpan(body, quoteStart, body.length(), darkBackground);
494 }
495 quoteDepth++;
496 }
497 return startsWithQuote;
498 }
499
500 private SpannableStringBuilder getSpannableBody(final Message message) {
501 Drawable fallbackImg = ResourcesCompat.getDrawable(activity.getResources(), activity.getThemeResource(R.attr.ic_attach_photo, R.drawable.ic_attach_photo), null);
502 return message.getMergedBody((cid) -> {
503 try {
504 DownloadableFile f = activity.xmppConnectionService.getFileForCid(cid);
505 if (f == null || !f.canRead()) {
506 if (!message.trusted() && !message.getConversation().canInferPresence()) return null;
507
508 try {
509 new BobTransfer(BobTransfer.uri(cid), message.getConversation().getAccount(), message.getCounterpart(), activity.xmppConnectionService).start();
510 } catch (final NoSuchAlgorithmException | URISyntaxException e) { }
511 return null;
512 }
513
514 Drawable d = activity.xmppConnectionService.getFileBackend().getThumbnail(f, activity.getResources(), (int) (metrics.density * 288), true);
515 if (d == null) {
516 new ThumbnailTask().execute(f);
517 }
518 return d;
519 } catch (final IOException e) {
520 return fallbackImg;
521 }
522 }, fallbackImg);
523 }
524
525 private void displayTextMessage(final ViewHolder viewHolder, final Message message, boolean darkBackground, int type) {
526 viewHolder.download_button.setVisibility(View.GONE);
527 viewHolder.image.setVisibility(View.GONE);
528 viewHolder.audioPlayer.setVisibility(View.GONE);
529 viewHolder.messageBody.setVisibility(View.GONE);
530
531 if (darkBackground) {
532 viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1_OnDark);
533 } else {
534 viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1);
535 }
536 viewHolder.messageBody.setHighlightColor(ContextCompat.getColor(activity, darkBackground
537 ? (type == SENT || !mUseGreenBackground ? R.color.black26 : R.color.grey800) : R.color.grey500));
538 viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
539
540 if (message.getBody() != null && !message.getBody().equals("")) {
541 viewHolder.messageBody.setTextIsSelectable(true);
542 viewHolder.messageBody.setVisibility(View.VISIBLE);
543 final String nick = UIHelper.getMessageDisplayName(message);
544 SpannableStringBuilder body = getSpannableBody(message);
545 boolean hasMeCommand = message.hasMeCommand();
546 if (hasMeCommand) {
547 body = body.replace(0, Message.ME_COMMAND.length(), nick + " ");
548 }
549 if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
550 body = new SpannableStringBuilder(body, 0, Config.MAX_DISPLAY_MESSAGE_CHARS);
551 body.append("\u2026");
552 }
553 Message.MergeSeparator[] mergeSeparators = body.getSpans(0, body.length(), Message.MergeSeparator.class);
554 for (Message.MergeSeparator mergeSeparator : mergeSeparators) {
555 int start = body.getSpanStart(mergeSeparator);
556 int end = body.getSpanEnd(mergeSeparator);
557 body.setSpan(new DividerSpan(true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
558 }
559 for (final android.text.style.QuoteSpan quote : body.getSpans(0, body.length(), android.text.style.QuoteSpan.class)) {
560 int start = body.getSpanStart(quote);
561 int end = body.getSpanEnd(quote);
562 body.removeSpan(quote);
563 applyQuoteSpan(body, start, end, darkBackground);
564 }
565 boolean startsWithQuote = handleTextQuotes(body, darkBackground);
566 if (!message.isPrivateMessage()) {
567 if (hasMeCommand) {
568 body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, nick.length(),
569 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
570 }
571 } else {
572 String privateMarker;
573 if (message.getStatus() <= Message.STATUS_RECEIVED) {
574 privateMarker = activity.getString(R.string.private_message);
575 } else {
576 Jid cp = message.getCounterpart();
577 privateMarker = activity.getString(R.string.private_message_to, Strings.nullToEmpty(cp == null ? null : cp.getResource()));
578 }
579 body.insert(0, privateMarker);
580 int privateMarkerIndex = privateMarker.length();
581 if (startsWithQuote) {
582 body.insert(privateMarkerIndex, "\n\n");
583 body.setSpan(new DividerSpan(false), privateMarkerIndex, privateMarkerIndex + 2,
584 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
585 } else {
586 body.insert(privateMarkerIndex, " ");
587 }
588 body.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground, false)), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
589 body.setSpan(new StyleSpan(Typeface.BOLD), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
590 if (hasMeCommand) {
591 body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), privateMarkerIndex + 1,
592 privateMarkerIndex + 1 + nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
593 }
594 }
595 if (message.getConversation().getMode() == Conversation.MODE_MULTI && message.getStatus() == Message.STATUS_RECEIVED) {
596 if (message.getConversation() instanceof Conversation) {
597 final Conversation conversation = (Conversation) message.getConversation();
598 Pattern pattern = NotificationService.generateNickHighlightPattern(conversation.getMucOptions().getActualNick());
599 Matcher matcher = pattern.matcher(body);
600 while (matcher.find()) {
601 body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
602 }
603
604 pattern = NotificationService.generateNickHighlightPattern(conversation.getMucOptions().getActualName());
605 matcher = pattern.matcher(body);
606 while (matcher.find()) {
607 body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
608 }
609 }
610 }
611 Matcher matcher = Emoticons.getEmojiPattern(body).matcher(body);
612 while (matcher.find()) {
613 if (matcher.start() < matcher.end()) {
614 body.setSpan(new RelativeSizeSpan(1.2f), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
615 }
616 }
617
618 StylingHelper.format(body, viewHolder.messageBody.getCurrentTextColor());
619 if (highlightedTerm != null) {
620 StylingHelper.highlight(activity, body, highlightedTerm, StylingHelper.isDarkText(viewHolder.messageBody));
621 }
622 MyLinkify.addLinks(body, message.getConversation().getAccount(), message.getConversation().getJid());
623 viewHolder.messageBody.setAutoLinkMask(0);
624 viewHolder.messageBody.setText(body);
625 BetterLinkMovementMethod method = new BetterLinkMovementMethod() {
626 @Override
627 protected void dispatchUrlLongClick(TextView tv, ClickableSpan span) {
628 if (span instanceof URLSpan || mOnInlineImageLongClickedListener == null) {
629 tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0));
630 super.dispatchUrlLongClick(tv, span);
631 return;
632 }
633
634 Spannable body = (Spannable) tv.getText();
635 ImageSpan[] imageSpans = body.getSpans(body.getSpanStart(span), body.getSpanEnd(span), ImageSpan.class);
636 if (imageSpans.length > 0) {
637 Uri uri = Uri.parse(imageSpans[0].getSource());
638 Cid cid = BobTransfer.cid(uri);
639 if (cid == null) return;
640 if (mOnInlineImageLongClickedListener.onInlineImageLongClicked(cid)) {
641 tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0));
642 }
643 }
644 }
645 };
646 method.setOnLinkLongClickListener((tv, url) -> {
647 tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0));
648 ShareUtil.copyLinkToClipboard(activity, url);
649 return true;
650 });
651 viewHolder.messageBody.setMovementMethod(method);
652 } else {
653 viewHolder.messageBody.setText("");
654 viewHolder.messageBody.setTextIsSelectable(false);
655 toggleWhisperInfo(viewHolder, message, darkBackground);
656 }
657 }
658
659 private void displayDownloadableMessage(ViewHolder viewHolder, final Message message, String text, final boolean darkBackground, final int type) {
660 displayTextMessage(viewHolder, message, darkBackground, type);
661 viewHolder.image.setVisibility(View.GONE);
662 List<Element> thumbs = message.getFileParams() != null ? message.getFileParams().getThumbnails() : null;
663 if (thumbs != null && !thumbs.isEmpty()) {
664 for (Element thumb : thumbs) {
665 Uri uri = Uri.parse(thumb.getAttribute("uri"));
666 if (uri.getScheme().equals("data")) {
667 String[] parts = uri.getSchemeSpecificPart().split(",", 2);
668 parts = parts[0].split(";");
669 if (!parts[0].equals("image/blurhash") && !parts[0].equals("image/thumbhash") && !parts[0].equals("image/jpeg") && !parts[0].equals("image/png") && !parts[0].equals("image/webp") && !parts[0].equals("image/gif")) continue;
670 } else if (uri.getScheme().equals("cid")) {
671 Cid cid = BobTransfer.cid(uri);
672 if (cid == null) continue;
673 DownloadableFile f = activity.xmppConnectionService.getFileForCid(cid);
674 if (f == null || !f.canRead()) {
675 if (!message.trusted() && !message.getConversation().canInferPresence()) continue;
676
677 try {
678 new BobTransfer(BobTransfer.uri(cid), message.getConversation().getAccount(), message.getCounterpart(), activity.xmppConnectionService).start();
679 } catch (final NoSuchAlgorithmException | URISyntaxException e) { }
680 continue;
681 }
682 } else {
683 continue;
684 }
685
686 int width = message.getFileParams().width;
687 if (width < 1 && thumb.getAttribute("width") != null) width = Integer.parseInt(thumb.getAttribute("width"));
688 if (width < 1) width = 1920;
689
690 int height = message.getFileParams().height;
691 if (height < 1 && thumb.getAttribute("height") != null) height = Integer.parseInt(thumb.getAttribute("height"));
692 if (height < 1) height = 1080;
693
694 viewHolder.image.setVisibility(View.VISIBLE);
695 imagePreviewLayout(width, height, viewHolder.image, message.getBody() != null && message.getBody().length() > 0);
696 activity.loadBitmap(message, viewHolder.image);
697 viewHolder.image.setOnClickListener(v -> ConversationFragment.downloadFile(activity, message));
698
699 break;
700 }
701 }
702 viewHolder.audioPlayer.setVisibility(View.GONE);
703 viewHolder.download_button.setVisibility(View.VISIBLE);
704 viewHolder.download_button.setText(text);
705 viewHolder.download_button.setOnClickListener(v -> ConversationFragment.downloadFile(activity, message));
706 }
707
708 private void displayWebxdcMessage(ViewHolder viewHolder, final Message message, final boolean darkBackground, final int type) {
709 Cid webxdcCid = message.getFileParams().getCids().get(0);
710 WebxdcPage webxdc = new WebxdcPage(activity, webxdcCid, message, activity.xmppConnectionService);
711 displayTextMessage(viewHolder, message, darkBackground, type);
712 viewHolder.image.setVisibility(View.GONE);
713 viewHolder.audioPlayer.setVisibility(View.GONE);
714 viewHolder.download_button.setVisibility(View.VISIBLE);
715 viewHolder.download_button.setText("Open " + webxdc.getName());
716 viewHolder.download_button.setOnClickListener(v -> {
717 Conversation conversation = (Conversation) message.getConversation();
718 if (!conversation.switchToSession("webxdc\0" + message.getUuid())) {
719 conversation.startWebxdc(webxdc);
720 }
721 });
722
723 final WebxdcUpdate lastUpdate;
724 synchronized(lastWebxdcUpdate) { lastUpdate = lastWebxdcUpdate.get(message.getUuid()); }
725 if (lastUpdate == null) {
726 new Thread(() -> {
727 final WebxdcUpdate update = activity.xmppConnectionService.findLastWebxdcUpdate(message);
728 if (update != null) {
729 synchronized(lastWebxdcUpdate) { lastWebxdcUpdate.put(message.getUuid(), update); }
730 activity.xmppConnectionService.updateConversationUi();
731 }
732 }).start();
733 } else {
734 if (lastUpdate != null && (lastUpdate.getSummary() != null || lastUpdate.getDocument() != null)) {
735 viewHolder.messageBody.setVisibility(View.VISIBLE);
736 viewHolder.messageBody.setText(
737 (lastUpdate.getDocument() == null ? "" : lastUpdate.getDocument() + "\n") +
738 (lastUpdate.getSummary() == null ? "" : lastUpdate.getSummary())
739 );
740 }
741 }
742
743 final LruCache<String, Drawable> cache = activity.xmppConnectionService.getDrawableCache();
744 final Drawable d = cache.get("webxdc:icon:" + webxdcCid);
745 if (d == null) {
746 new Thread(() -> {
747 Drawable icon = webxdc.getIcon();
748 if (icon != null) {
749 cache.put("webxdc:icon:" + webxdcCid, icon);
750 activity.xmppConnectionService.updateConversationUi();
751 }
752 }).start();
753 } else {
754 viewHolder.image.setVisibility(View.VISIBLE);
755 viewHolder.image.setImageDrawable(d);
756 }
757 }
758
759 private void displayOpenableMessage(ViewHolder viewHolder, final Message message, final boolean darkBackground, final int type) {
760 displayTextMessage(viewHolder, message, darkBackground, type);
761 viewHolder.image.setVisibility(View.GONE);
762 viewHolder.audioPlayer.setVisibility(View.GONE);
763 viewHolder.download_button.setVisibility(View.VISIBLE);
764 viewHolder.download_button.setText(activity.getString(R.string.open_x_file, UIHelper.getFileDescriptionString(activity, message)));
765 viewHolder.download_button.setOnClickListener(v -> openDownloadable(message));
766 }
767
768 private void displayLocationMessage(ViewHolder viewHolder, final Message message, final boolean darkBackground, final int type) {
769 displayTextMessage(viewHolder, message, darkBackground, type);
770 viewHolder.image.setVisibility(View.GONE);
771 viewHolder.audioPlayer.setVisibility(View.GONE);
772 viewHolder.download_button.setVisibility(View.VISIBLE);
773 viewHolder.download_button.setText(R.string.show_location);
774 viewHolder.download_button.setOnClickListener(v -> showLocation(message));
775 }
776
777 private void displayAudioMessage(ViewHolder viewHolder, Message message, boolean darkBackground, final int type) {
778 displayTextMessage(viewHolder, message, darkBackground, type);
779 viewHolder.image.setVisibility(View.GONE);
780 viewHolder.download_button.setVisibility(View.GONE);
781 final RelativeLayout audioPlayer = viewHolder.audioPlayer;
782 audioPlayer.setVisibility(View.VISIBLE);
783 AudioPlayer.ViewHolder.get(audioPlayer).setDarkBackground(darkBackground);
784 this.audioPlayer.init(audioPlayer, message);
785 }
786
787 private void displayMediaPreviewMessage(ViewHolder viewHolder, final Message message, final boolean darkBackground, final int type) {
788 displayTextMessage(viewHolder, message, darkBackground, type);
789 viewHolder.download_button.setVisibility(View.GONE);
790 viewHolder.audioPlayer.setVisibility(View.GONE);
791 viewHolder.image.setVisibility(View.VISIBLE);
792 final FileParams params = message.getFileParams();
793 imagePreviewLayout(params.width, params.height, viewHolder.image, message.getBody() != null && message.getBody().length() > 0);
794 activity.loadBitmap(message, viewHolder.image);
795 viewHolder.image.setOnClickListener(v -> openDownloadable(message));
796 }
797
798 private void imagePreviewLayout(int w, int h, ImageView image, boolean topMargin) {
799 final float target = activity.getResources().getDimension(R.dimen.image_preview_width);
800 final int scaledW;
801 final int scaledH;
802 if (Math.max(h, w) * metrics.density <= target) {
803 scaledW = (int) (w * metrics.density);
804 scaledH = (int) (h * metrics.density);
805 } else if (Math.max(h, w) <= target) {
806 scaledW = w;
807 scaledH = h;
808 } else if (w <= h) {
809 scaledW = (int) (w / ((double) h / target));
810 scaledH = (int) target;
811 } else {
812 scaledW = (int) target;
813 scaledH = (int) (h / ((double) w / target));
814 }
815 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(scaledW, scaledH);
816 layoutParams.setMargins(0, topMargin ? (int) (metrics.density * 4) : 0, 0, (int) (metrics.density * 4));
817 layoutParams.gravity = Gravity.CENTER;
818 image.setLayoutParams(layoutParams);
819 }
820
821 private void toggleWhisperInfo(ViewHolder viewHolder, final Message message, final boolean darkBackground) {
822 if (message.isPrivateMessage()) {
823 final String privateMarker;
824 if (message.getStatus() <= Message.STATUS_RECEIVED) {
825 privateMarker = activity.getString(R.string.private_message);
826 } else {
827 Jid cp = message.getCounterpart();
828 privateMarker = activity.getString(R.string.private_message_to, Strings.nullToEmpty(cp == null ? null : cp.getResource()));
829 }
830 final SpannableString body = new SpannableString(privateMarker);
831 body.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground, false)), 0, privateMarker.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
832 body.setSpan(new StyleSpan(Typeface.BOLD), 0, privateMarker.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
833 viewHolder.messageBody.setText(body);
834 viewHolder.messageBody.setVisibility(View.VISIBLE);
835 } else {
836 viewHolder.messageBody.setVisibility(View.GONE);
837 }
838 }
839
840 private void loadMoreMessages(Conversation conversation) {
841 conversation.setLastClearHistory(0, null);
842 activity.xmppConnectionService.updateConversation(conversation);
843 conversation.setHasMessagesLeftOnServer(true);
844 conversation.setFirstMamReference(null);
845 long timestamp = conversation.getLastMessageTransmitted().getTimestamp();
846 if (timestamp == 0) {
847 timestamp = System.currentTimeMillis();
848 }
849 conversation.messagesLoaded.set(true);
850 MessageArchiveService.Query query = activity.xmppConnectionService.getMessageArchiveService().query(conversation, new MamReference(0), timestamp, false);
851 if (query != null) {
852 Toast.makeText(activity, R.string.fetching_history_from_server, Toast.LENGTH_LONG).show();
853 } else {
854 Toast.makeText(activity, R.string.not_fetching_history_retention_period, Toast.LENGTH_SHORT).show();
855 }
856 }
857
858 @Override
859 public View getView(int position, View view, ViewGroup parent) {
860 final Message message = getItem(position);
861 final boolean omemoEncryption = message.getEncryption() == Message.ENCRYPTION_AXOLOTL;
862 final boolean isInValidSession = message.isValidInSession() && (!omemoEncryption || message.isTrusted());
863 final Conversational conversation = message.getConversation();
864 final Account account = conversation.getAccount();
865 final List<Element> commands = message.getCommands();
866 final int type = getItemViewType(position);
867 ViewHolder viewHolder;
868 if (view == null) {
869 viewHolder = new ViewHolder();
870 switch (type) {
871 case DATE_SEPARATOR:
872 view = activity.getLayoutInflater().inflate(R.layout.message_date_bubble, parent, false);
873 viewHolder.status_message = view.findViewById(R.id.message_body);
874 viewHolder.message_box = view.findViewById(R.id.message_box);
875 viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
876 break;
877 case RTP_SESSION:
878 view = activity.getLayoutInflater().inflate(R.layout.message_rtp_session, parent, false);
879 viewHolder.status_message = view.findViewById(R.id.message_body);
880 viewHolder.message_box = view.findViewById(R.id.message_box);
881 viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
882 break;
883 case SENT:
884 view = activity.getLayoutInflater().inflate(R.layout.message_sent, parent, false);
885 viewHolder.message_box = view.findViewById(R.id.message_box);
886 viewHolder.contact_picture = view.findViewById(R.id.message_photo);
887 viewHolder.download_button = view.findViewById(R.id.download_button);
888 viewHolder.indicator = view.findViewById(R.id.security_indicator);
889 viewHolder.edit_indicator = view.findViewById(R.id.edit_indicator);
890 viewHolder.image = view.findViewById(R.id.message_image);
891 viewHolder.messageBody = view.findViewById(R.id.message_body);
892 viewHolder.time = view.findViewById(R.id.message_time);
893 viewHolder.subject = view.findViewById(R.id.message_subject);
894 viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
895 viewHolder.audioPlayer = view.findViewById(R.id.audio_player);
896 viewHolder.thread_identicon = view.findViewById(R.id.thread_identicon);
897 break;
898 case RECEIVED:
899 view = activity.getLayoutInflater().inflate(R.layout.message_received, parent, false);
900 viewHolder.message_box = view.findViewById(R.id.message_box);
901 viewHolder.contact_picture = view.findViewById(R.id.message_photo);
902 viewHolder.download_button = view.findViewById(R.id.download_button);
903 viewHolder.indicator = view.findViewById(R.id.security_indicator);
904 viewHolder.edit_indicator = view.findViewById(R.id.edit_indicator);
905 viewHolder.image = view.findViewById(R.id.message_image);
906 viewHolder.messageBody = view.findViewById(R.id.message_body);
907 viewHolder.time = view.findViewById(R.id.message_time);
908 viewHolder.subject = view.findViewById(R.id.message_subject);
909 viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
910 viewHolder.encryption = view.findViewById(R.id.message_encryption);
911 viewHolder.audioPlayer = view.findViewById(R.id.audio_player);
912 viewHolder.commands_list = view.findViewById(R.id.commands_list);
913 viewHolder.thread_identicon = view.findViewById(R.id.thread_identicon);
914 break;
915 case STATUS:
916 view = activity.getLayoutInflater().inflate(R.layout.message_status, parent, false);
917 viewHolder.contact_picture = view.findViewById(R.id.message_photo);
918 viewHolder.status_message = view.findViewById(R.id.status_message);
919 viewHolder.load_more_messages = view.findViewById(R.id.load_more_messages);
920 break;
921 default:
922 throw new AssertionError("Unknown view type");
923 }
924 view.setTag(viewHolder);
925 } else {
926 viewHolder = (ViewHolder) view.getTag();
927 if (viewHolder == null) {
928 return view;
929 }
930 }
931
932 if (viewHolder.messageBody != null) {
933 viewHolder.messageBody.setCustomSelectionActionModeCallback(new MessageTextActionModeCallback(this, viewHolder.messageBody));
934 }
935
936 if (viewHolder.thread_identicon != null) {
937 viewHolder.thread_identicon.setVisibility(View.GONE);
938 final Element thread = message.getThread();
939 if (thread != null) {
940 final String threadId = thread.getContent();
941 if (threadId != null) {
942 viewHolder.thread_identicon.setVisibility(View.VISIBLE);
943 viewHolder.thread_identicon.setColor(UIHelper.getColorForName(threadId));
944 viewHolder.thread_identicon.setHash(UIHelper.identiconHash(threadId));
945 }
946 }
947 }
948
949 boolean darkBackground = (type == RECEIVED && mUseGreenBackground) || activity.isDarkTheme();
950
951 if (type == DATE_SEPARATOR) {
952 if (UIHelper.today(message.getTimeSent())) {
953 viewHolder.status_message.setText(R.string.today);
954 } else if (UIHelper.yesterday(message.getTimeSent())) {
955 viewHolder.status_message.setText(R.string.yesterday);
956 } else {
957 viewHolder.status_message.setText(DateUtils.formatDateTime(activity, message.getTimeSent(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
958 }
959 viewHolder.message_box.setBackgroundResource(activity.isDarkTheme() ? R.drawable.date_bubble_grey : R.drawable.date_bubble_white);
960 return view;
961 } else if (type == RTP_SESSION) {
962 final boolean isDarkTheme = activity.isDarkTheme();
963 final boolean received = message.getStatus() <= Message.STATUS_RECEIVED;
964 final RtpSessionStatus rtpSessionStatus = RtpSessionStatus.of(message.getBody());
965 final long duration = rtpSessionStatus.duration;
966 final String callTime = UIHelper.readableTimeDifferenceFull(activity, message.getTimeSent());
967 if (received) {
968 if (duration > 0) {
969 viewHolder.status_message.setText(activity.getString(R.string.incoming_call_duration_timestamp, TimeFrameUtils.resolve(activity, duration), callTime));
970 } else if (rtpSessionStatus.successful) {
971 viewHolder.status_message.setText(activity.getString(R.string.incoming_call_timestamp, callTime));
972 } else {
973 viewHolder.status_message.setText(activity.getString(R.string.missed_call_timestamp, callTime));
974 }
975 } else {
976 if (duration > 0) {
977 viewHolder.status_message.setText(activity.getString(R.string.outgoing_call_duration_timestamp, TimeFrameUtils.resolve(activity, duration), callTime));
978 } else {
979 viewHolder.status_message.setText(activity.getString(R.string.outgoing_call_timestamp, callTime));
980 }
981 }
982 viewHolder.indicatorReceived.setImageResource(RtpSessionStatus.getDrawable(received, rtpSessionStatus.successful, isDarkTheme));
983 viewHolder.indicatorReceived.setAlpha(isDarkTheme ? 0.7f : 0.57f);
984 viewHolder.message_box.setBackgroundResource(isDarkTheme ? R.drawable.date_bubble_grey : R.drawable.date_bubble_white);
985 return view;
986 } else if (type == STATUS) {
987 if ("LOAD_MORE".equals(message.getBody())) {
988 viewHolder.status_message.setVisibility(View.GONE);
989 viewHolder.contact_picture.setVisibility(View.GONE);
990 viewHolder.load_more_messages.setVisibility(View.VISIBLE);
991 viewHolder.load_more_messages.setOnClickListener(v -> loadMoreMessages((Conversation) message.getConversation()));
992 } else {
993 viewHolder.status_message.setVisibility(View.VISIBLE);
994 viewHolder.load_more_messages.setVisibility(View.GONE);
995 viewHolder.status_message.setText(message.getBody());
996 boolean showAvatar;
997 if (conversation.getMode() == Conversation.MODE_SINGLE) {
998 showAvatar = true;
999 AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar_on_status_message);
1000 } else if (message.getCounterpart() != null || message.getTrueCounterpart() != null || (message.getCounterparts() != null && message.getCounterparts().size() > 0)) {
1001 showAvatar = true;
1002 AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar_on_status_message);
1003 } else {
1004 showAvatar = false;
1005 }
1006 if (showAvatar) {
1007 viewHolder.contact_picture.setAlpha(0.5f);
1008 viewHolder.contact_picture.setVisibility(View.VISIBLE);
1009 } else {
1010 viewHolder.contact_picture.setVisibility(View.GONE);
1011 }
1012 }
1013 return view;
1014 } else {
1015 AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar);
1016 }
1017
1018 resetClickListener(viewHolder.message_box, viewHolder.messageBody);
1019
1020 viewHolder.message_box.setOnClickListener(v -> {
1021 if (MessageAdapter.this.mOnMessageBoxClickedListener != null) {
1022 MessageAdapter.this.mOnMessageBoxClickedListener
1023 .onContactPictureClicked(message);
1024 }
1025 });
1026 SwipeDetector swipeDetector = new SwipeDetector((action) -> {
1027 if (action == SwipeDetector.Action.LR && MessageAdapter.this.mOnMessageBoxSwipedListener != null) {
1028 MessageAdapter.this.mOnMessageBoxSwipedListener.onContactPictureClicked(message);
1029 }
1030 });
1031 viewHolder.message_box.setOnTouchListener(swipeDetector);
1032 viewHolder.image.setOnTouchListener(swipeDetector);
1033 viewHolder.time.setOnTouchListener(swipeDetector);
1034
1035 // Treat touch-up as click so we don't have to touch twice
1036 // (touch twice is because it's waiting to see if you double-touch for text selection)
1037 viewHolder.messageBody.setOnTouchListener((v, event) -> {
1038 if (event.getAction() == MotionEvent.ACTION_UP) {
1039 if (MessageAdapter.this.mOnMessageBoxClickedListener != null) {
1040 MessageAdapter.this.mOnMessageBoxClickedListener
1041 .onContactPictureClicked(message);
1042 }
1043 }
1044
1045 swipeDetector.onTouch(v, event);
1046
1047 return false;
1048 });
1049 viewHolder.messageBody.setOnClickListener(v -> {
1050 if (MessageAdapter.this.mOnMessageBoxClickedListener != null) {
1051 MessageAdapter.this.mOnMessageBoxClickedListener
1052 .onContactPictureClicked(message);
1053 }
1054 });
1055 viewHolder.contact_picture.setOnClickListener(v -> {
1056 if (MessageAdapter.this.mOnContactPictureClickedListener != null) {
1057 MessageAdapter.this.mOnContactPictureClickedListener
1058 .onContactPictureClicked(message);
1059 }
1060
1061 });
1062 viewHolder.contact_picture.setOnLongClickListener(v -> {
1063 if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) {
1064 MessageAdapter.this.mOnContactPictureLongClickedListener
1065 .onContactPictureLongClicked(v, message);
1066 return true;
1067 } else {
1068 return false;
1069 }
1070 });
1071 viewHolder.messageBody.setAccessibilityDelegate(null);
1072
1073 final Transferable transferable = message.getTransferable();
1074 final boolean unInitiatedButKnownSize = MessageUtils.unInitiatedButKnownSize(message);
1075 if (unInitiatedButKnownSize || message.isDeleted() || (transferable != null && transferable.getStatus() != Transferable.STATUS_UPLOADING)) {
1076 if (unInitiatedButKnownSize || (message.isDeleted() && message.getModerated() == null) || transferable != null && transferable.getStatus() == Transferable.STATUS_OFFER) {
1077 displayDownloadableMessage(viewHolder, message, activity.getString(R.string.download_x_file, UIHelper.getFileDescriptionString(activity, message)), darkBackground, type);
1078 } else if (transferable != null && transferable.getStatus() == Transferable.STATUS_OFFER_CHECK_FILESIZE) {
1079 displayDownloadableMessage(viewHolder, message, activity.getString(R.string.check_x_filesize, UIHelper.getFileDescriptionString(activity, message)), darkBackground, type);
1080 } else {
1081 displayInfoMessage(viewHolder, UIHelper.getMessagePreview(activity, message).first, darkBackground, message, type);
1082 }
1083 } else if (message.isFileOrImage() && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
1084 if (message.getFileParams().width > 0 && message.getFileParams().height > 0) {
1085 displayMediaPreviewMessage(viewHolder, message, darkBackground, type);
1086 } else if (message.getFileParams().runtime > 0) {
1087 displayAudioMessage(viewHolder, message, darkBackground, type);
1088 } else if ("application/xdc+zip".equals(message.getFileParams().getMediaType()) && message.getConversation() instanceof Conversation && message.getThread() != null && !message.getFileParams().getCids().isEmpty()) {
1089 displayWebxdcMessage(viewHolder, message, darkBackground, type);
1090 } else {
1091 displayOpenableMessage(viewHolder, message, darkBackground, type);
1092 }
1093 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
1094 if (account.isPgpDecryptionServiceConnected()) {
1095 if (conversation instanceof Conversation && !account.hasPendingPgpIntent((Conversation) conversation)) {
1096 displayInfoMessage(viewHolder, activity.getString(R.string.message_decrypting), darkBackground);
1097 } else {
1098 displayInfoMessage(viewHolder, activity.getString(R.string.pgp_message), darkBackground);
1099 }
1100 } else {
1101 displayInfoMessage(viewHolder, activity.getString(R.string.install_openkeychain), darkBackground);
1102 viewHolder.message_box.setOnClickListener(this::promptOpenKeychainInstall);
1103 viewHolder.messageBody.setOnClickListener(this::promptOpenKeychainInstall);
1104 }
1105 } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
1106 displayInfoMessage(viewHolder, activity.getString(R.string.decryption_failed), darkBackground);
1107 } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE) {
1108 displayInfoMessage(viewHolder, activity.getString(R.string.not_encrypted_for_this_device), darkBackground);
1109 } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
1110 displayInfoMessage(viewHolder, activity.getString(R.string.omemo_decryption_failed), darkBackground);
1111 } else {
1112 if (message.isGeoUri()) {
1113 displayLocationMessage(viewHolder, message, darkBackground, type);
1114 } else if (message.treatAsDownloadable()) {
1115 try {
1116 final URI uri = message.getOob();
1117 displayDownloadableMessage(viewHolder,
1118 message,
1119 activity.getString(R.string.check_x_filesize_on_host,
1120 UIHelper.getFileDescriptionString(activity, message),
1121 uri.getHost()),
1122 darkBackground, type);
1123 } catch (Exception e) {
1124 displayDownloadableMessage(viewHolder,
1125 message,
1126 activity.getString(R.string.check_x_filesize,
1127 UIHelper.getFileDescriptionString(activity, message)),
1128 darkBackground, type);
1129 }
1130 } else if (message.bodyIsOnlyEmojis() && message.getType() != Message.TYPE_PRIVATE) {
1131 displayEmojiMessage(viewHolder, getSpannableBody(message), darkBackground);
1132 } else {
1133 displayTextMessage(viewHolder, message, darkBackground, type);
1134 }
1135 }
1136
1137 if (type == RECEIVED) {
1138 if (commands != null && conversation instanceof Conversation) {
1139 CommandButtonAdapter adapter = new CommandButtonAdapter(activity);
1140 adapter.addAll(commands);
1141 viewHolder.commands_list.setAdapter(adapter);
1142 viewHolder.commands_list.setVisibility(View.VISIBLE);
1143 viewHolder.commands_list.setOnItemClickListener((p, v, pos, id) -> {
1144 final Element command = adapter.getItem(pos);
1145 activity.startCommand(conversation.getAccount(), command.getAttributeAsJid("jid"), command.getAttribute("node"));
1146 });
1147 } else {
1148 // It's unclear if we can set this to null...
1149 ListAdapter adapter = viewHolder.commands_list.getAdapter();
1150 if (adapter instanceof ArrayAdapter) {
1151 ((ArrayAdapter<?>) adapter).clear();
1152 }
1153 viewHolder.commands_list.setVisibility(View.GONE);
1154 viewHolder.commands_list.setOnItemClickListener(null);
1155 }
1156
1157 if (!mUseGreenBackground) {
1158 viewHolder.message_box.getBackground().setColorFilter(
1159 StyledAttributes.getColor(activity, mUseGreenBackground ? R.attr.message_bubble_received_bg : R.attr.color_background_primary),
1160 PorterDuff.Mode.SRC_ATOP
1161 );
1162 }
1163 if (isInValidSession) {
1164 viewHolder.encryption.setVisibility(View.GONE);
1165 } else {
1166 viewHolder.encryption.setVisibility(View.VISIBLE);
1167 if (omemoEncryption && !message.isTrusted()) {
1168 viewHolder.encryption.setText(R.string.not_trusted);
1169 } else {
1170 viewHolder.encryption.setText(CryptoHelper.encryptionTypeToText(message.getEncryption()));
1171 }
1172 }
1173 }
1174
1175 if (type == RECEIVED || type == SENT) {
1176 String subject = message.getSubject();
1177 if (subject == null && message.getThread() != null) {
1178 subject = ((Conversation) message.getConversation()).getThread(message.getThread().getContent()).getSubject();
1179 }
1180 if (subject == null) {
1181 viewHolder.subject.setVisibility(View.GONE);
1182 } else {
1183 viewHolder.subject.setVisibility(View.VISIBLE);
1184 viewHolder.subject.setText(subject);
1185 }
1186 }
1187
1188 if (darkBackground) {
1189 if (viewHolder.subject != null) viewHolder.subject.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption_OnDark_Bold);
1190 if (viewHolder.encryption != null) viewHolder.encryption.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption_OnDark_Bold);
1191 } else {
1192 if (viewHolder.subject != null) viewHolder.subject.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption_Bold);
1193 if (viewHolder.encryption != null) viewHolder.encryption.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption_Bold);
1194 }
1195
1196 displayStatus(viewHolder, message, type, darkBackground);
1197
1198 viewHolder.messageBody.setAccessibilityDelegate(new View.AccessibilityDelegate() {
1199 @Override
1200 public void sendAccessibilityEvent(View host, int eventType) {
1201 super.sendAccessibilityEvent(host, eventType);
1202 if (eventType == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED) {
1203 if (viewHolder.messageBody.hasSelection()) {
1204 selectionUuid = message.getUuid();
1205 } else if (message.getUuid() != null && message.getUuid().equals(selectionUuid)) {
1206 selectionUuid = null;
1207 }
1208 }
1209 }
1210 });
1211
1212 return view;
1213 }
1214
1215 private void promptOpenKeychainInstall(View view) {
1216 activity.showInstallPgpDialog();
1217 }
1218
1219 public FileBackend getFileBackend() {
1220 return activity.xmppConnectionService.getFileBackend();
1221 }
1222
1223 public void stopAudioPlayer() {
1224 audioPlayer.stop();
1225 }
1226
1227 public void unregisterListenerInAudioPlayer() {
1228 audioPlayer.unregisterListener();
1229 }
1230
1231 public void startStopPending() {
1232 audioPlayer.startStopPending();
1233 }
1234
1235 public void openDownloadable(Message message) {
1236 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU && ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
1237 ConversationFragment.registerPendingMessage(activity, message);
1238 ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, ConversationsActivity.REQUEST_OPEN_MESSAGE);
1239 return;
1240 }
1241 final DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
1242 ViewUtil.view(activity, file);
1243 }
1244
1245 private void showLocation(Message message) {
1246 for (Intent intent : GeoHelper.createGeoIntentsFromMessage(activity, message)) {
1247 if (intent.resolveActivity(getContext().getPackageManager()) != null) {
1248 getContext().startActivity(intent);
1249 return;
1250 }
1251 }
1252 Toast.makeText(activity, R.string.no_application_found_to_display_location, Toast.LENGTH_SHORT).show();
1253 }
1254
1255 public void updatePreferences() {
1256 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(activity);
1257 this.mUseGreenBackground = p.getBoolean("use_green_background", activity.getResources().getBoolean(R.bool.use_green_background));
1258 }
1259
1260
1261 public void setHighlightedTerm(List<String> terms) {
1262 this.highlightedTerm = terms == null ? null : StylingHelper.filterHighlightedWords(terms);
1263 }
1264
1265 public interface OnContactPictureClicked {
1266 void onContactPictureClicked(Message message);
1267 }
1268
1269 public interface OnContactPictureLongClicked {
1270 void onContactPictureLongClicked(View v, Message message);
1271 }
1272
1273 public interface OnInlineImageLongClicked {
1274 boolean onInlineImageLongClicked(Cid cid);
1275 }
1276
1277 private static class ViewHolder {
1278
1279 public Button load_more_messages;
1280 public ImageView edit_indicator;
1281 public RelativeLayout audioPlayer;
1282 protected LinearLayout message_box;
1283 protected Button download_button;
1284 protected ImageView image;
1285 protected ImageView indicator;
1286 protected ImageView indicatorReceived;
1287 protected TextView time;
1288 protected TextView subject;
1289 protected TextView messageBody;
1290 protected ImageView contact_picture;
1291 protected TextView status_message;
1292 protected TextView encryption;
1293 protected ListView commands_list;
1294 protected GithubIdenticonView thread_identicon;
1295 }
1296
1297 class ThumbnailTask extends AsyncTask<DownloadableFile, Void, Drawable[]> {
1298 @Override
1299 protected Drawable[] doInBackground(DownloadableFile... params) {
1300 if (isCancelled()) return null;
1301
1302 Drawable[] d = new Drawable[params.length];
1303 for (int i = 0; i < params.length; i++) {
1304 try {
1305 d[i] = activity.xmppConnectionService.getFileBackend().getThumbnail(params[i], activity.getResources(), (int) (metrics.density * 288), false);
1306 } catch (final IOException e) {
1307 d[i] = null;
1308 }
1309 }
1310
1311 return d;
1312 }
1313
1314 @Override
1315 protected void onPostExecute(final Drawable[] d) {
1316 if (isCancelled()) return;
1317 activity.xmppConnectionService.updateConversationUi();
1318 }
1319 }
1320}