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