MessageAdapter.java

   1package eu.siacs.conversations.ui.adapter;
   2
   3import android.Manifest;
   4import android.app.Activity;
   5import android.content.Intent;
   6import android.content.pm.PackageManager;
   7import android.graphics.PorterDuff;
   8import android.graphics.drawable.Drawable;
   9import android.content.res.ColorStateList;
  10import android.graphics.Typeface;
  11import android.net.Uri;
  12import android.os.AsyncTask;
  13import android.os.Build;
  14import android.preference.PreferenceManager;
  15import android.text.Editable;
  16import android.text.Spanned;
  17import android.text.Spannable;
  18import android.text.SpannableString;
  19import android.text.SpannableStringBuilder;
  20import android.text.style.ImageSpan;
  21import android.text.style.ClickableSpan;
  22import android.text.format.DateUtils;
  23import android.text.style.ForegroundColorSpan;
  24import android.text.style.RelativeSizeSpan;
  25import android.text.style.StyleSpan;
  26import android.text.style.URLSpan;
  27import android.util.DisplayMetrics;
  28import android.util.LruCache;
  29import android.view.accessibility.AccessibilityEvent;
  30import android.view.Gravity;
  31import android.view.MotionEvent;
  32import android.view.View;
  33import android.view.ViewGroup;
  34import android.view.WindowManager;
  35import android.widget.ArrayAdapter;
  36import android.widget.ImageView;
  37import android.widget.LinearLayout;
  38import android.widget.ListAdapter;
  39import android.widget.ListView;
  40import android.widget.RelativeLayout;
  41import android.widget.TextView;
  42import android.widget.Toast;
  43
  44import androidx.annotation.AttrRes;
  45import androidx.annotation.ColorInt;
  46import androidx.annotation.DrawableRes;
  47import androidx.annotation.NonNull;
  48import androidx.annotation.Nullable;
  49import androidx.core.app.ActivityCompat;
  50import androidx.core.content.ContextCompat;
  51import androidx.core.content.res.ResourcesCompat;
  52import androidx.core.widget.ImageViewCompat;
  53
  54import com.google.android.material.imageview.ShapeableImageView;
  55import com.google.android.material.shape.CornerFamily;
  56import com.google.android.material.shape.ShapeAppearanceModel;
  57
  58import com.cheogram.android.BobTransfer;
  59import com.cheogram.android.MessageTextActionModeCallback;
  60import com.cheogram.android.SwipeDetector;
  61import com.cheogram.android.WebxdcPage;
  62import com.cheogram.android.WebxdcUpdate;
  63
  64import com.google.android.material.button.MaterialButton;
  65import com.google.android.material.color.MaterialColors;
  66import com.google.common.base.Joiner;
  67import com.google.common.base.Strings;
  68import com.google.common.collect.ImmutableList;
  69
  70import com.lelloman.identicon.view.GithubIdenticonView;
  71
  72import io.ipfs.cid.Cid;
  73
  74import java.io.IOException;
  75import java.net.URI;
  76import java.net.URISyntaxException;
  77import java.security.NoSuchAlgorithmException;
  78import java.util.HashMap;
  79import java.util.List;
  80import java.util.Map;
  81import java.util.Locale;
  82import java.util.regex.Matcher;
  83import java.util.regex.Pattern;
  84
  85import me.saket.bettermovementmethod.BetterLinkMovementMethod;
  86
  87import net.fellbaum.jemoji.EmojiManager;
  88
  89import eu.siacs.conversations.AppSettings;
  90import eu.siacs.conversations.Config;
  91import eu.siacs.conversations.R;
  92import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
  93import eu.siacs.conversations.entities.Account;
  94import eu.siacs.conversations.entities.Contact;
  95import eu.siacs.conversations.entities.Conversation;
  96import eu.siacs.conversations.entities.Conversational;
  97import eu.siacs.conversations.entities.DownloadableFile;
  98import eu.siacs.conversations.entities.Message.FileParams;
  99import eu.siacs.conversations.entities.Message;
 100import eu.siacs.conversations.entities.MucOptions;
 101import eu.siacs.conversations.entities.Roster;
 102import eu.siacs.conversations.entities.RtpSessionStatus;
 103import eu.siacs.conversations.entities.Transferable;
 104import eu.siacs.conversations.persistance.FileBackend;
 105import eu.siacs.conversations.services.MessageArchiveService;
 106import eu.siacs.conversations.services.NotificationService;
 107import eu.siacs.conversations.ui.Activities;
 108import eu.siacs.conversations.ui.ConversationFragment;
 109import eu.siacs.conversations.ui.ConversationsActivity;
 110import eu.siacs.conversations.ui.XmppActivity;
 111import eu.siacs.conversations.ui.service.AudioPlayer;
 112import eu.siacs.conversations.ui.text.DividerSpan;
 113import eu.siacs.conversations.ui.text.QuoteSpan;
 114import eu.siacs.conversations.ui.util.Attachment;
 115import eu.siacs.conversations.ui.util.AvatarWorkerTask;
 116import eu.siacs.conversations.ui.util.MyLinkify;
 117import eu.siacs.conversations.ui.util.QuoteHelper;
 118import eu.siacs.conversations.ui.util.ShareUtil;
 119import eu.siacs.conversations.ui.util.ViewUtil;
 120import eu.siacs.conversations.utils.CryptoHelper;
 121import eu.siacs.conversations.utils.Emoticons;
 122import eu.siacs.conversations.utils.GeoHelper;
 123import eu.siacs.conversations.utils.MessageUtils;
 124import eu.siacs.conversations.utils.StylingHelper;
 125import eu.siacs.conversations.utils.TimeFrameUtils;
 126import eu.siacs.conversations.utils.UIHelper;
 127import eu.siacs.conversations.xmpp.Jid;
 128import eu.siacs.conversations.xmpp.mam.MamReference;
 129import eu.siacs.conversations.xml.Element;
 130
 131import java.net.URI;
 132import java.util.Arrays;
 133import java.util.Collection;
 134import java.util.List;
 135import java.util.Locale;
 136import java.util.regex.Matcher;
 137import java.util.regex.Pattern;
 138
 139public class MessageAdapter extends ArrayAdapter<Message> {
 140
 141    public static final String DATE_SEPARATOR_BODY = "DATE_SEPARATOR";
 142    private static final int SENT = 0;
 143    private static final int RECEIVED = 1;
 144    private static final int STATUS = 2;
 145    private static final int DATE_SEPARATOR = 3;
 146    private static final int RTP_SESSION = 4;
 147    private final XmppActivity activity;
 148    private final AudioPlayer audioPlayer;
 149    private List<String> highlightedTerm = null;
 150    private final DisplayMetrics metrics;
 151    private ConversationFragment mConversationFragment = null;
 152    private OnContactPictureClicked mOnContactPictureClickedListener;
 153    private OnContactPictureClicked mOnMessageBoxClickedListener;
 154    private OnContactPictureClicked mOnMessageBoxSwipedListener;
 155    private OnContactPictureLongClicked mOnContactPictureLongClickedListener;
 156    private OnInlineImageLongClicked mOnInlineImageLongClickedListener;
 157    private boolean mUseGreenBackground = false;
 158    private BubbleDesign bubbleDesign = new BubbleDesign(false, false);
 159    private final boolean mForceNames;
 160    private final Map<String, WebxdcUpdate> lastWebxdcUpdate = new HashMap<>();
 161    private String selectionUuid = null;
 162
 163    public MessageAdapter(
 164            final XmppActivity activity, final List<Message> messages, final boolean forceNames) {
 165        super(activity, 0, messages);
 166        this.audioPlayer = new AudioPlayer(this);
 167        this.activity = activity;
 168        metrics = getContext().getResources().getDisplayMetrics();
 169        updatePreferences();
 170        this.mForceNames = forceNames;
 171    }
 172
 173    public MessageAdapter(final XmppActivity activity, final List<Message> messages) {
 174        this(activity, messages, false);
 175    }
 176
 177    private static void resetClickListener(View... views) {
 178        for (View view : views) {
 179            if (view != null) view.setOnClickListener(null);
 180        }
 181    }
 182
 183    public void flagScreenOn() {
 184        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 185    }
 186
 187    public void flagScreenOff() {
 188        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 189    }
 190
 191    public void setVolumeControl(final int stream) {
 192        activity.setVolumeControlStream(stream);
 193    }
 194
 195    public void setOnContactPictureClicked(OnContactPictureClicked listener) {
 196        this.mOnContactPictureClickedListener = listener;
 197    }
 198
 199    public void setOnMessageBoxClicked(OnContactPictureClicked listener) {
 200        this.mOnMessageBoxClickedListener = listener;
 201    }
 202
 203    public void setOnMessageBoxSwiped(OnContactPictureClicked listener) {
 204        this.mOnMessageBoxSwipedListener = listener;
 205    }
 206
 207    public void setConversationFragment(ConversationFragment frag) {
 208        mConversationFragment = frag;
 209    }
 210
 211    public void quoteText(String text) {
 212        if (mConversationFragment != null) mConversationFragment.quoteText(text);
 213    }
 214
 215    public boolean hasSelection() {
 216        return selectionUuid != null;
 217    }
 218
 219    public Activity getActivity() {
 220        return activity;
 221    }
 222
 223    public void setOnContactPictureLongClicked(OnContactPictureLongClicked listener) {
 224        this.mOnContactPictureLongClickedListener = listener;
 225    }
 226
 227    public void setOnInlineImageLongClicked(OnInlineImageLongClicked listener) {
 228        this.mOnInlineImageLongClickedListener = listener;
 229    }
 230
 231    @Override
 232    public int getViewTypeCount() {
 233        return 5;
 234    }
 235
 236    private int getItemViewType(Message message) {
 237        if (message.getType() == Message.TYPE_STATUS) {
 238            if (DATE_SEPARATOR_BODY.equals(message.getBody())) {
 239                return DATE_SEPARATOR;
 240            } else {
 241                return STATUS;
 242            }
 243        } else if (message.getType() == Message.TYPE_RTP_SESSION) {
 244            return RTP_SESSION;
 245        } else if (message.getStatus() <= Message.STATUS_RECEIVED) {
 246            return RECEIVED;
 247        } else {
 248            return SENT;
 249        }
 250    }
 251
 252    @Override
 253    public int getItemViewType(int position) {
 254        return this.getItemViewType(getItem(position));
 255    }
 256
 257    private void displayStatus(
 258            final ViewHolder viewHolder,
 259            final Message message,
 260            final int type,
 261            final BubbleColor bubbleColor) {
 262        final int mergedStatus = message.getMergedStatus();
 263        final boolean error;
 264        if (viewHolder.indicatorReceived != null) {
 265            viewHolder.indicatorReceived.setVisibility(View.GONE);
 266        }
 267        final Transferable transferable = message.getTransferable();
 268        final boolean multiReceived =
 269                message.getConversation().getMode() == Conversation.MODE_MULTI
 270                        && mergedStatus <= Message.STATUS_RECEIVED;
 271        final String fileSize;
 272        if (message.isFileOrImage()
 273                || transferable != null
 274                || MessageUtils.unInitiatedButKnownSize(message)) {
 275            final FileParams params = message.getFileParams();
 276            fileSize = params.size != null ? UIHelper.filesizeToString(params.size) : null;
 277            if (message.getStatus() == Message.STATUS_SEND_FAILED
 278                    || (transferable != null
 279                            && (transferable.getStatus() == Transferable.STATUS_FAILED
 280                                    || transferable.getStatus()
 281                                            == Transferable.STATUS_CANCELLED))) {
 282                error = true;
 283            } else {
 284                error = message.getStatus() == Message.STATUS_SEND_FAILED;
 285            }
 286        } else {
 287            fileSize = null;
 288            error = message.getStatus() == Message.STATUS_SEND_FAILED;
 289        }
 290        if (type == SENT) {
 291            final @DrawableRes Integer receivedIndicator =
 292                    getMessageStatusAsDrawable(message, mergedStatus);
 293            if (receivedIndicator == null) {
 294                viewHolder.indicatorReceived.setVisibility(View.INVISIBLE);
 295            } else {
 296                viewHolder.indicatorReceived.setImageResource(receivedIndicator);
 297                if (mergedStatus == Message.STATUS_SEND_FAILED) {
 298                    setImageTintError(viewHolder.indicatorReceived);
 299                } else {
 300                    setImageTint(viewHolder.indicatorReceived, bubbleColor);
 301                }
 302                viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
 303            }
 304        }
 305        final var additionalStatusInfo = getAdditionalStatusInfo(message, mergedStatus);
 306
 307        if (error && type == SENT) {
 308            viewHolder.time.setTextColor(
 309                    MaterialColors.getColor(
 310                            viewHolder.time, com.google.android.material.R.attr.colorError));
 311        } else {
 312            setTextColor(viewHolder.time, bubbleColor);
 313        }
 314        setTextColor(viewHolder.subject, bubbleColor);
 315        if (message.getEncryption() == Message.ENCRYPTION_NONE) {
 316            viewHolder.indicator.setVisibility(View.GONE);
 317        } else {
 318            boolean verified = false;
 319            if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
 320                final FingerprintStatus status =
 321                        message.getConversation()
 322                                .getAccount()
 323                                .getAxolotlService()
 324                                .getFingerprintTrust(message.getFingerprint());
 325                if (status != null && status.isVerified()) {
 326                    verified = true;
 327                }
 328            }
 329            if (verified) {
 330                viewHolder.indicator.setImageResource(R.drawable.ic_verified_user_24dp);
 331            } else {
 332                viewHolder.indicator.setImageResource(R.drawable.ic_lock_24dp);
 333            }
 334            if (error && type == SENT) {
 335                setImageTintError(viewHolder.indicator);
 336            } else {
 337                setImageTint(viewHolder.indicator, bubbleColor);
 338            }
 339            viewHolder.indicator.setVisibility(View.VISIBLE);
 340        }
 341
 342        if (viewHolder.edit_indicator != null) {
 343            if (message.edited()) {
 344                viewHolder.edit_indicator.setVisibility(View.VISIBLE);
 345                if (error && type == SENT) {
 346                    setImageTintError(viewHolder.edit_indicator);
 347                } else {
 348                    setImageTint(viewHolder.edit_indicator, bubbleColor);
 349                }
 350            } else {
 351                viewHolder.edit_indicator.setVisibility(View.GONE);
 352            }
 353        }
 354
 355        final String formattedTime =
 356                UIHelper.readableTimeDifferenceFull(getContext(), message.getMergedTimeSent());
 357        final String bodyLanguage = message.getBodyLanguage();
 358        final ImmutableList.Builder<String> timeInfoBuilder = new ImmutableList.Builder<>();
 359        if (message.getStatus() <= Message.STATUS_RECEIVED) {
 360            timeInfoBuilder.add(formattedTime);
 361            if (fileSize != null) {
 362                timeInfoBuilder.add(fileSize);
 363            }
 364            if (mForceNames || multiReceived || (message.getTrueCounterpart() != null && message.getContact() != null)) {
 365                final String displayName = UIHelper.getMessageDisplayName(message);
 366                if (displayName != null) {
 367                    timeInfoBuilder.add(displayName);
 368                }
 369            }
 370            if (bodyLanguage != null) {
 371                timeInfoBuilder.add(bodyLanguage.toUpperCase(Locale.US));
 372            }
 373        } else {
 374            if (bodyLanguage != null) {
 375                timeInfoBuilder.add(bodyLanguage.toUpperCase(Locale.US));
 376            }
 377            if (fileSize != null) {
 378                timeInfoBuilder.add(fileSize);
 379            }
 380            // for space reasons we display only 'additional status info' (send progress or concrete
 381            // failure reason) or the time
 382            if (additionalStatusInfo != null) {
 383                timeInfoBuilder.add(additionalStatusInfo);
 384            } else {
 385                timeInfoBuilder.add(formattedTime);
 386            }
 387        }
 388        final var timeInfo = timeInfoBuilder.build();
 389        viewHolder.time.setText(Joiner.on(" \u00B7 ").join(timeInfo));
 390    }
 391
 392    public static @DrawableRes Integer getMessageStatusAsDrawable(
 393            final Message message, final int status) {
 394        final var transferable = message.getTransferable();
 395        return switch (status) {
 396            case Message.STATUS_WAITING -> R.drawable.ic_more_horiz_24dp;
 397            case Message.STATUS_UNSEND -> transferable == null ? null : R.drawable.ic_upload_24dp;
 398            case Message.STATUS_SEND -> R.drawable.ic_done_24dp;
 399            case Message.STATUS_SEND_RECEIVED, Message.STATUS_SEND_DISPLAYED -> R.drawable
 400                    .ic_done_all_24dp;
 401            case Message.STATUS_SEND_FAILED -> {
 402                final String errorMessage = message.getErrorMessage();
 403                if (Message.ERROR_MESSAGE_CANCELLED.equals(errorMessage)) {
 404                    yield R.drawable.ic_cancel_24dp;
 405                } else {
 406                    yield R.drawable.ic_error_24dp;
 407                }
 408            }
 409            case Message.STATUS_OFFERED -> R.drawable.ic_p2p_24dp;
 410            default -> null;
 411        };
 412    }
 413
 414    @Nullable
 415    private String getAdditionalStatusInfo(final Message message, final int mergedStatus) {
 416        final String additionalStatusInfo;
 417        if (mergedStatus == Message.STATUS_SEND_FAILED) {
 418            final String errorMessage = Strings.nullToEmpty(message.getErrorMessage());
 419            final String[] errorParts = errorMessage.split("\\u001f", 2);
 420            if (errorParts.length == 2 && errorParts[0].equals("file-too-large")) {
 421                additionalStatusInfo = getContext().getString(R.string.file_too_large);
 422            } else {
 423                additionalStatusInfo = null;
 424            }
 425        } else if (mergedStatus == Message.STATUS_UNSEND) {
 426            final var transferable = message.getTransferable();
 427            if (transferable == null) {
 428                return null;
 429            }
 430            return getContext().getString(R.string.sending_file, transferable.getProgress());
 431        } else {
 432            additionalStatusInfo = null;
 433        }
 434        return additionalStatusInfo;
 435    }
 436
 437    private void displayInfoMessage(
 438            ViewHolder viewHolder, CharSequence text, final BubbleColor bubbleColor) {
 439        viewHolder.download_button.setVisibility(View.GONE);
 440        viewHolder.audioPlayer.setVisibility(View.GONE);
 441        viewHolder.image.setVisibility(View.GONE);
 442        viewHolder.messageBody.setVisibility(View.VISIBLE);
 443        viewHolder.messageBody.setText(text);
 444        viewHolder.messageBody.setTextColor(
 445                bubbleToOnSurfaceVariant(viewHolder.messageBody, bubbleColor));
 446        viewHolder.messageBody.setTextIsSelectable(false);
 447    }
 448
 449    private void displayEmojiMessage(
 450            final ViewHolder viewHolder, final SpannableStringBuilder body, final BubbleColor bubbleColor) {
 451        viewHolder.download_button.setVisibility(View.GONE);
 452        viewHolder.audioPlayer.setVisibility(View.GONE);
 453        viewHolder.image.setVisibility(View.GONE);
 454        viewHolder.messageBody.setVisibility(View.VISIBLE);
 455        setTextColor(viewHolder.messageBody, bubbleColor);
 456        ImageSpan[] imageSpans = body.getSpans(0, body.length(), ImageSpan.class);
 457        float size = imageSpans.length == 1 || Emoticons.isEmoji(body.toString()) ? 3.0f : 2.0f;
 458        body.setSpan(
 459                new RelativeSizeSpan(size), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 460        viewHolder.messageBody.setText(body);
 461    }
 462
 463    private void applyQuoteSpan(
 464            final TextView textView,
 465            Editable body,
 466            int start,
 467            int end,
 468            final BubbleColor bubbleColor,
 469            final boolean makeEdits) {
 470        if (makeEdits && start > 1 && !"\n\n".equals(body.subSequence(start - 2, start).toString())) {
 471            body.insert(start++, "\n");
 472            body.setSpan(
 473                    new DividerSpan(false), start - 2, start, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 474            end++;
 475        }
 476        if (makeEdits && end < body.length() - 1 && !"\n\n".equals(body.subSequence(end, end + 2).toString())) {
 477            body.insert(end, "\n");
 478            body.setSpan(
 479                new DividerSpan(false),
 480                end,
 481                end + ("\n".equals(body.subSequence(end + 1, end + 2).toString()) ? 2 : 1),
 482                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
 483            );
 484        }
 485        final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
 486        body.setSpan(
 487                new QuoteSpan(bubbleToOnSurfaceVariant(textView, bubbleColor), metrics),
 488                start,
 489                end,
 490                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 491    }
 492
 493    public boolean handleTextQuotes(final TextView textView, final Editable body) {
 494        return handleTextQuotes(textView, body, true);
 495    }
 496
 497    public boolean handleTextQuotes(final TextView textView, final Editable body, final boolean deleteMarkers) {
 498        final boolean colorfulBackground = this.bubbleDesign.colorfulChatBubbles;
 499        final BubbleColor bubbleColor = colorfulBackground ? (deleteMarkers ? BubbleColor.SECONDARY : BubbleColor.TERTIARY) : BubbleColor.SURFACE;
 500        return handleTextQuotes(textView, body, bubbleColor, deleteMarkers);
 501    }
 502
 503    /**
 504     * Applies QuoteSpan to group of lines which starts with > or » characters. Appends likebreaks
 505     * and applies DividerSpan to them to show a padding between quote and text.
 506     */
 507    public boolean handleTextQuotes(
 508            final TextView textView,
 509            final Editable body,
 510            final BubbleColor bubbleColor,
 511            final boolean deleteMarkers) {
 512        boolean startsWithQuote = false;
 513        int quoteDepth = 1;
 514        while (QuoteHelper.bodyContainsQuoteStart(body) && quoteDepth <= Config.QUOTE_MAX_DEPTH) {
 515            char previous = '\n';
 516            int lineStart = -1;
 517            int lineTextStart = -1;
 518            int quoteStart = -1;
 519            int skipped = 0;
 520            for (int i = 0; i <= body.length(); i++) {
 521                if (!deleteMarkers && QuoteHelper.isRelativeSizeSpanned(body, i)) {
 522                    skipped++;
 523                    continue;
 524                }
 525                char current = body.length() > i ? body.charAt(i) : '\n';
 526                if (lineStart == -1) {
 527                    if (previous == '\n') {
 528                        if (i < body.length() && QuoteHelper.isPositionQuoteStart(body, i)) {
 529                            // Line start with quote
 530                            lineStart = i;
 531                            if (quoteStart == -1) quoteStart = i - skipped;
 532                            if (i == 0) startsWithQuote = true;
 533                        } else if (quoteStart >= 0) {
 534                            // Line start without quote, apply spans there
 535                            applyQuoteSpan(textView, body, quoteStart, i - 1, bubbleColor, deleteMarkers);
 536                            quoteStart = -1;
 537                        }
 538                    }
 539                } else {
 540                    // Remove extra spaces between > and first character in the line
 541                    // > character will be removed too
 542                    if (current != ' ' && lineTextStart == -1) {
 543                        lineTextStart = i;
 544                    }
 545                    if (current == '\n') {
 546                        if (deleteMarkers) {
 547                            i -= lineTextStart - lineStart;
 548                            body.delete(lineStart, lineTextStart);
 549                            if (i == lineStart) {
 550                                // Avoid empty lines because span over empty line can be hidden
 551                                body.insert(i++, " ");
 552                            }
 553                        } else {
 554                            body.setSpan(new RelativeSizeSpan(i - (lineTextStart - lineStart) == lineStart ? 1 : 0), lineStart, lineTextStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE | StylingHelper.XHTML_REMOVE << Spanned.SPAN_USER_SHIFT);
 555                        }
 556                        lineStart = -1;
 557                        lineTextStart = -1;
 558                    }
 559                }
 560                previous = current;
 561                skipped = 0;
 562            }
 563            if (quoteStart >= 0) {
 564                // Apply spans to finishing open quote
 565                applyQuoteSpan(textView, body, quoteStart, body.length(), bubbleColor, deleteMarkers);
 566            }
 567            quoteDepth++;
 568        }
 569        return startsWithQuote;
 570    }
 571
 572    private SpannableStringBuilder getSpannableBody(final Message message) {
 573        Drawable fallbackImg = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_photo_24dp, null);
 574        return message.getMergedBody((cid) -> {
 575            try {
 576                DownloadableFile f = activity.xmppConnectionService.getFileForCid(cid);
 577                if (f == null || !f.canRead()) {
 578                    if (!message.trusted() && !message.getConversation().canInferPresence()) return null;
 579
 580                    try {
 581                        new BobTransfer(BobTransfer.uri(cid), message.getConversation().getAccount(), message.getCounterpart(), activity.xmppConnectionService).start();
 582                    } catch (final NoSuchAlgorithmException | URISyntaxException e) { }
 583                    return null;
 584                }
 585
 586                Drawable d = activity.xmppConnectionService.getFileBackend().getThumbnail(f, activity.getResources(), (int) (metrics.density * 288), true);
 587                if (d == null) {
 588                    new ThumbnailTask().execute(f);
 589                }
 590                return d;
 591            } catch (final IOException e) {
 592                return null;
 593            }
 594        }, fallbackImg);
 595    }
 596
 597    private void displayTextMessage(
 598            final ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor, final int type) {
 599        viewHolder.download_button.setVisibility(View.GONE);
 600        viewHolder.image.setVisibility(View.GONE);
 601        viewHolder.audioPlayer.setVisibility(View.GONE);
 602        viewHolder.messageBody.setVisibility(View.VISIBLE);
 603        setTextColor(viewHolder.messageBody, bubbleColor);
 604        setTextSize(viewHolder.messageBody, this.bubbleDesign.largeFont);
 605
 606        final ViewGroup.LayoutParams layoutParams = viewHolder.messageBody.getLayoutParams();
 607        layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
 608        viewHolder.messageBody.setLayoutParams(layoutParams);
 609
 610        viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
 611
 612        if (message.getBody() != null && !message.getBody().equals("")) {
 613            viewHolder.messageBody.setTextIsSelectable(true);
 614            viewHolder.messageBody.setVisibility(View.VISIBLE);
 615            final String nick = UIHelper.getMessageDisplayName(message);
 616            SpannableStringBuilder body = getSpannableBody(message);
 617            final var processMarkup = body.getSpans(0, body.length(), Message.PlainTextSpan.class).length > 0;
 618            boolean hasMeCommand = message.hasMeCommand();
 619            if (hasMeCommand) {
 620                body = body.replace(0, Message.ME_COMMAND.length(), nick + " ");
 621            }
 622            if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
 623                body = new SpannableStringBuilder(body, 0, Config.MAX_DISPLAY_MESSAGE_CHARS);
 624                body.append("\u2026");
 625            }
 626            Message.MergeSeparator[] mergeSeparators =
 627                    body.getSpans(0, body.length(), Message.MergeSeparator.class);
 628            for (Message.MergeSeparator mergeSeparator : mergeSeparators) {
 629                int start = body.getSpanStart(mergeSeparator);
 630                int end = body.getSpanEnd(mergeSeparator);
 631                body.setSpan(new DividerSpan(true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 632            }
 633            for (final android.text.style.QuoteSpan quote : body.getSpans(0, body.length(), android.text.style.QuoteSpan.class)) {
 634                int start = body.getSpanStart(quote);
 635                int end = body.getSpanEnd(quote);
 636                body.removeSpan(quote);
 637                applyQuoteSpan(viewHolder.messageBody, body, start, end, bubbleColor, true);
 638            }
 639            boolean startsWithQuote = processMarkup ? handleTextQuotes(viewHolder.messageBody, body, bubbleColor, true) : false;
 640            if (!message.isPrivateMessage()) {
 641                if (hasMeCommand) {
 642                    body.setSpan(
 643                            new StyleSpan(Typeface.BOLD_ITALIC),
 644                            0,
 645                            nick.length(),
 646                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 647                }
 648            } else {
 649                String privateMarker;
 650                if (message.getStatus() <= Message.STATUS_RECEIVED) {
 651                    privateMarker = activity.getString(R.string.private_message);
 652                } else {
 653                    Jid cp = message.getCounterpart();
 654                    privateMarker =
 655                            activity.getString(
 656                                    R.string.private_message_to,
 657                                    Strings.nullToEmpty(cp == null ? null : cp.getResource()));
 658                }
 659                body.insert(0, privateMarker);
 660                int privateMarkerIndex = privateMarker.length();
 661                if (startsWithQuote) {
 662                    body.insert(privateMarkerIndex, "\n\n");
 663                    body.setSpan(
 664                            new DividerSpan(false),
 665                            privateMarkerIndex,
 666                            privateMarkerIndex + 2,
 667                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 668                } else {
 669                    body.insert(privateMarkerIndex, " ");
 670                }
 671                body.setSpan(
 672                        new ForegroundColorSpan(
 673                                bubbleToOnSurfaceVariant(viewHolder.messageBody, bubbleColor)),
 674                        0,
 675                        privateMarkerIndex,
 676                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 677                body.setSpan(
 678                        new StyleSpan(Typeface.BOLD),
 679                        0,
 680                        privateMarkerIndex,
 681                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 682                if (hasMeCommand) {
 683                    body.setSpan(
 684                            new StyleSpan(Typeface.BOLD_ITALIC),
 685                            privateMarkerIndex + 1,
 686                            privateMarkerIndex + 1 + nick.length(),
 687                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 688                }
 689            }
 690            if (message.getConversation().getMode() == Conversation.MODE_MULTI
 691                    && message.getStatus() == Message.STATUS_RECEIVED) {
 692                if (message.getConversation() instanceof Conversation conversation) {
 693                    Pattern pattern =
 694                            NotificationService.generateNickHighlightPattern(
 695                                    conversation.getMucOptions().getActualNick());
 696                    Matcher matcher = pattern.matcher(body);
 697                    while (matcher.find()) {
 698                        body.setSpan(
 699                                new StyleSpan(Typeface.BOLD),
 700                                matcher.start(),
 701                                matcher.end(),
 702                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 703                    }
 704
 705                    pattern = NotificationService.generateNickHighlightPattern(conversation.getMucOptions().getActualName());
 706                    matcher = pattern.matcher(body);
 707                    while (matcher.find()) {
 708                        body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 709                    }
 710                }
 711            }
 712            for (final var emoji : EmojiManager.extractEmojisInOrderWithIndex(body.toString())) {
 713                    body.setSpan(
 714                            new RelativeSizeSpan(1.2f),
 715                            emoji.getCharIndex(),
 716                            emoji.getCharIndex() + emoji.getEmoji().getEmoji().length(),
 717                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 718            }
 719
 720            if (processMarkup) StylingHelper.format(body, viewHolder.messageBody.getCurrentTextColor());
 721            MyLinkify.addLinks(body, message.getConversation().getAccount(), message.getConversation().getJid());
 722            if (highlightedTerm != null) {
 723                StylingHelper.highlight(viewHolder.messageBody, body, highlightedTerm);
 724            }
 725
 726            viewHolder.messageBody.setAutoLinkMask(0);
 727            viewHolder.messageBody.setText(body);
 728            BetterLinkMovementMethod method = new BetterLinkMovementMethod() {
 729                @Override
 730                protected void dispatchUrlLongClick(TextView tv, ClickableSpan span) {
 731                    if (span instanceof URLSpan || mOnInlineImageLongClickedListener == null) {
 732                        tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0));
 733                        super.dispatchUrlLongClick(tv, span);
 734                        return;
 735                    }
 736
 737                    Spannable body = (Spannable) tv.getText();
 738                    ImageSpan[] imageSpans = body.getSpans(body.getSpanStart(span), body.getSpanEnd(span), ImageSpan.class);
 739                    if (imageSpans.length > 0) {
 740                        Uri uri = Uri.parse(imageSpans[0].getSource());
 741                        Cid cid = BobTransfer.cid(uri);
 742                        if (cid == null) return;
 743                        if (mOnInlineImageLongClickedListener.onInlineImageLongClicked(cid)) {
 744                            tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0));
 745                        }
 746                    }
 747                }
 748            };
 749            method.setOnLinkLongClickListener((tv, url) -> {
 750                tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0));
 751                ShareUtil.copyLinkToClipboard(activity, url);
 752                return true;
 753            });
 754            viewHolder.messageBody.setMovementMethod(method);
 755        } else {
 756            viewHolder.messageBody.setText("");
 757            viewHolder.messageBody.setTextIsSelectable(false);
 758            toggleWhisperInfo(viewHolder, message, bubbleColor);
 759        }
 760    }
 761
 762    private void displayDownloadableMessage(
 763            ViewHolder viewHolder,
 764            final Message message,
 765            String text,
 766            final BubbleColor bubbleColor, final int type) {
 767        displayTextMessage(viewHolder, message, bubbleColor, type);
 768        viewHolder.image.setVisibility(View.GONE);
 769        List<Element> thumbs = message.getFileParams() != null ? message.getFileParams().getThumbnails() : null;
 770        if (thumbs != null && !thumbs.isEmpty()) {
 771            for (Element thumb : thumbs) {
 772                Uri uri = Uri.parse(thumb.getAttribute("uri"));
 773                if (uri.getScheme().equals("data")) {
 774                    String[] parts = uri.getSchemeSpecificPart().split(",", 2);
 775                    parts = parts[0].split(";");
 776                    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;
 777                } else if (uri.getScheme().equals("cid")) {
 778                    Cid cid = BobTransfer.cid(uri);
 779                    if (cid == null) continue;
 780                    DownloadableFile f = activity.xmppConnectionService.getFileForCid(cid);
 781                    if (f == null || !f.canRead()) {
 782                        if (!message.trusted() && !message.getConversation().canInferPresence()) continue;
 783
 784                        try {
 785                            new BobTransfer(BobTransfer.uri(cid), message.getConversation().getAccount(), message.getCounterpart(), activity.xmppConnectionService).start();
 786                        } catch (final NoSuchAlgorithmException | URISyntaxException e) { }
 787                        continue;
 788                    }
 789                } else {
 790                    continue;
 791                }
 792
 793                int width = message.getFileParams().width;
 794                if (width < 1 && thumb.getAttribute("width") != null) width = Integer.parseInt(thumb.getAttribute("width"));
 795                if (width < 1) width = 1920;
 796
 797                int height = message.getFileParams().height;
 798                if (height < 1 && thumb.getAttribute("height") != null) height = Integer.parseInt(thumb.getAttribute("height"));
 799                if (height < 1) height = 1080;
 800
 801                viewHolder.image.setVisibility(View.VISIBLE);
 802                imagePreviewLayout(width, height, viewHolder.image, true, type, viewHolder);
 803                activity.loadBitmap(message, viewHolder.image);
 804                viewHolder.image.setOnClickListener(v -> ConversationFragment.downloadFile(activity, message));
 805
 806                break;
 807            }
 808        }
 809        viewHolder.audioPlayer.setVisibility(View.GONE);
 810        viewHolder.download_button.setVisibility(View.VISIBLE);
 811        viewHolder.download_button.setText(text);
 812        final var attachment = Attachment.of(message);
 813        final @DrawableRes int imageResource = MediaAdapter.getImageDrawable(attachment);
 814        viewHolder.download_button.setIconResource(imageResource);
 815        viewHolder.download_button.setOnClickListener(
 816                v -> ConversationFragment.downloadFile(activity, message));
 817    }
 818
 819    private void displayWebxdcMessage(ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor, final int type) {
 820        Cid webxdcCid = message.getFileParams().getCids().get(0);
 821        WebxdcPage webxdc = new WebxdcPage(activity, webxdcCid, message, activity.xmppConnectionService);
 822        displayTextMessage(viewHolder, message, bubbleColor, type);
 823        viewHolder.image.setVisibility(View.GONE);
 824        viewHolder.audioPlayer.setVisibility(View.GONE);
 825        viewHolder.download_button.setVisibility(View.VISIBLE);
 826        viewHolder.download_button.setText("Open " + webxdc.getName());
 827        viewHolder.download_button.setOnClickListener(v -> {
 828            Conversation conversation = (Conversation) message.getConversation();
 829            if (!conversation.switchToSession("webxdc\0" + message.getUuid())) {
 830                conversation.startWebxdc(webxdc);
 831            }
 832        });
 833
 834        final WebxdcUpdate lastUpdate;
 835        synchronized(lastWebxdcUpdate) { lastUpdate = lastWebxdcUpdate.get(message.getUuid()); }
 836        if (lastUpdate == null) {
 837            new Thread(() -> {
 838                final WebxdcUpdate update = activity.xmppConnectionService.findLastWebxdcUpdate(message);
 839                if (update != null) {
 840                    synchronized(lastWebxdcUpdate) { lastWebxdcUpdate.put(message.getUuid(), update); }
 841                    activity.xmppConnectionService.updateConversationUi();
 842                }
 843            }).start();
 844        } else {
 845            if (lastUpdate != null && (lastUpdate.getSummary() != null || lastUpdate.getDocument() != null)) {
 846                viewHolder.messageBody.setVisibility(View.VISIBLE);
 847                viewHolder.messageBody.setText(
 848                    (lastUpdate.getDocument() == null ? "" : lastUpdate.getDocument() + "\n") +
 849                    (lastUpdate.getSummary() == null ? "" : lastUpdate.getSummary())
 850                );
 851            }
 852        }
 853
 854        final LruCache<String, Drawable> cache = activity.xmppConnectionService.getDrawableCache();
 855        final Drawable d = cache.get("webxdc:icon:" + webxdcCid);
 856        if (d == null) {
 857            new Thread(() -> {
 858                Drawable icon = webxdc.getIcon();
 859                if (icon != null) {
 860                    cache.put("webxdc:icon:" + webxdcCid, icon);
 861                    activity.xmppConnectionService.updateConversationUi();
 862                }
 863            }).start();
 864        } else {
 865            viewHolder.image.setVisibility(View.VISIBLE);
 866            viewHolder.image.setImageDrawable(d);
 867        }
 868    }
 869
 870    private void displayOpenableMessage(
 871            ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor, final int type) {
 872        displayTextMessage(viewHolder, message, bubbleColor, type);
 873        viewHolder.image.setVisibility(View.GONE);
 874        viewHolder.audioPlayer.setVisibility(View.GONE);
 875        viewHolder.download_button.setVisibility(View.VISIBLE);
 876        viewHolder.download_button.setText(
 877                activity.getString(
 878                        R.string.open_x_file,
 879                        UIHelper.getFileDescriptionString(activity, message)));
 880        final var attachment = Attachment.of(message);
 881        final @DrawableRes int imageResource = MediaAdapter.getImageDrawable(attachment);
 882        viewHolder.download_button.setIconResource(imageResource);
 883        viewHolder.download_button.setOnClickListener(v -> openDownloadable(message));
 884    }
 885
 886    private void displayLocationMessage(
 887            ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor, final int type) {
 888        displayTextMessage(viewHolder, message, bubbleColor, type);
 889        viewHolder.image.setVisibility(View.GONE);
 890        viewHolder.audioPlayer.setVisibility(View.GONE);
 891        viewHolder.download_button.setVisibility(View.VISIBLE);
 892        viewHolder.download_button.setText(R.string.show_location);
 893        final var attachment = Attachment.of(message);
 894        final @DrawableRes int imageResource = MediaAdapter.getImageDrawable(attachment);
 895        viewHolder.download_button.setIconResource(imageResource);
 896        viewHolder.download_button.setOnClickListener(v -> showLocation(message));
 897    }
 898
 899    private void displayAudioMessage(
 900            ViewHolder viewHolder, Message message, final BubbleColor bubbleColor, final int type) {
 901        displayTextMessage(viewHolder, message, bubbleColor, type);
 902        viewHolder.image.setVisibility(View.GONE);
 903        viewHolder.download_button.setVisibility(View.GONE);
 904        final RelativeLayout audioPlayer = viewHolder.audioPlayer;
 905        audioPlayer.setVisibility(View.VISIBLE);
 906        AudioPlayer.ViewHolder.get(audioPlayer).setBubbleColor(bubbleColor);
 907        this.audioPlayer.init(audioPlayer, message);
 908    }
 909
 910    private void displayMediaPreviewMessage(
 911            ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor, final int type) {
 912        displayTextMessage(viewHolder, message, bubbleColor, type);
 913        viewHolder.download_button.setVisibility(View.GONE);
 914        viewHolder.audioPlayer.setVisibility(View.GONE);
 915        viewHolder.image.setVisibility(View.VISIBLE);
 916        final FileParams params = message.getFileParams();
 917        imagePreviewLayout(params.width, params.height, viewHolder.image, viewHolder.messageBody.getVisibility() != View.GONE, type, viewHolder);
 918        activity.loadBitmap(message, viewHolder.image);
 919        viewHolder.image.setOnClickListener(v -> openDownloadable(message));
 920    }
 921
 922    private void imagePreviewLayout(int w, int h, ShapeableImageView image, boolean withOther, int type, ViewHolder viewHolder) {
 923        final float target = activity.getResources().getDimension(R.dimen.image_preview_width);
 924        final int scaledW;
 925        final int scaledH;
 926        if (Math.max(h, w) * metrics.density <= target) {
 927            scaledW = (int) (w * metrics.density);
 928            scaledH = (int) (h * metrics.density);
 929        } else if (Math.max(h, w) <= target) {
 930            scaledW = w;
 931            scaledH = h;
 932        } else if (w <= h) {
 933            scaledW = (int) (w / ((double) h / target));
 934            scaledH = (int) target;
 935        } else {
 936            scaledW = (int) target;
 937            scaledH = (int) (h / ((double) w / target));
 938        }
 939        final var small = withOther ? scaledW < target : scaledW < 110 * metrics.density;
 940        final LinearLayout.LayoutParams layoutParams =
 941                new LinearLayout.LayoutParams(scaledW, scaledH);
 942        image.setLayoutParams(layoutParams);
 943
 944        final var bubbleRadius = activity.getResources().getDimension(R.dimen.bubble_radius);
 945        var shape = new ShapeAppearanceModel.Builder().setTopRightCorner(CornerFamily.ROUNDED, bubbleRadius);
 946        if (type == SENT) {
 947            shape = shape.setTopLeftCorner(CornerFamily.ROUNDED, bubbleRadius);
 948        }
 949        if (small) {
 950            final var imageRadius = activity.getResources().getDimension(R.dimen.image_radius);
 951            shape = shape.setAllCorners(CornerFamily.ROUNDED, imageRadius);
 952            image.setPadding(0, (int)(8 * metrics.density), 0, 0);
 953        } else {
 954            image.setPadding(0, 0, 0, 0);
 955        }
 956        image.setShapeAppearanceModel(shape.build());
 957
 958        if (!small) {
 959            final ViewGroup.LayoutParams blayoutParams = viewHolder.messageBody.getLayoutParams();
 960            blayoutParams.width = (int) (target - (22 * metrics.density));
 961            viewHolder.messageBody.setLayoutParams(blayoutParams);
 962        }
 963    }
 964
 965    private void toggleWhisperInfo(
 966            ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor) {
 967        if (message.isPrivateMessage()) {
 968            final String privateMarker;
 969            if (message.getStatus() <= Message.STATUS_RECEIVED) {
 970                privateMarker = activity.getString(R.string.private_message);
 971            } else {
 972                Jid cp = message.getCounterpart();
 973                privateMarker =
 974                        activity.getString(
 975                                R.string.private_message_to,
 976                                Strings.nullToEmpty(cp == null ? null : cp.getResource()));
 977            }
 978            final SpannableString body = new SpannableString(privateMarker);
 979            body.setSpan(
 980                    new ForegroundColorSpan(
 981                            bubbleToOnSurfaceVariant(viewHolder.messageBody, bubbleColor)),
 982                    0,
 983                    privateMarker.length(),
 984                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 985            body.setSpan(
 986                    new StyleSpan(Typeface.BOLD),
 987                    0,
 988                    privateMarker.length(),
 989                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 990            viewHolder.messageBody.setText(body);
 991            viewHolder.messageBody.setVisibility(View.VISIBLE);
 992        } else {
 993            viewHolder.messageBody.setVisibility(View.GONE);
 994        }
 995    }
 996
 997    private void loadMoreMessages(Conversation conversation) {
 998        conversation.setLastClearHistory(0, null);
 999        activity.xmppConnectionService.updateConversation(conversation);
1000        conversation.setHasMessagesLeftOnServer(true);
1001        conversation.setFirstMamReference(null);
1002        long timestamp = conversation.getLastMessageTransmitted().getTimestamp();
1003        if (timestamp == 0) {
1004            timestamp = System.currentTimeMillis();
1005        }
1006        conversation.messagesLoaded.set(true);
1007        MessageArchiveService.Query query =
1008                activity.xmppConnectionService
1009                        .getMessageArchiveService()
1010                        .query(conversation, new MamReference(0), timestamp, false);
1011        if (query != null) {
1012            Toast.makeText(activity, R.string.fetching_history_from_server, Toast.LENGTH_LONG)
1013                    .show();
1014        } else {
1015            Toast.makeText(
1016                            activity,
1017                            R.string.not_fetching_history_retention_period,
1018                            Toast.LENGTH_SHORT)
1019                    .show();
1020        }
1021    }
1022
1023    @Override
1024    public View getView(final int position, View view, final @NonNull ViewGroup parent) {
1025        final Message message = getItem(position);
1026        final boolean omemoEncryption = message.getEncryption() == Message.ENCRYPTION_AXOLOTL;
1027        final boolean isInValidSession =
1028                message.isValidInSession() && (!omemoEncryption || message.isTrusted());
1029        final Conversational conversation = message.getConversation();
1030        final Account account = conversation.getAccount();
1031        final List<Element> commands = message.getCommands();
1032        final int type = getItemViewType(position);
1033        ViewHolder viewHolder;
1034        if (view == null) {
1035            viewHolder = new ViewHolder();
1036            switch (type) {
1037                case DATE_SEPARATOR:
1038                    view =
1039                            activity.getLayoutInflater()
1040                                    .inflate(R.layout.item_message_date_bubble, parent, false);
1041                    viewHolder.status_message = view.findViewById(R.id.message_body);
1042                    viewHolder.message_box = view.findViewById(R.id.message_box);
1043                    break;
1044                case RTP_SESSION:
1045                    view =
1046                            activity.getLayoutInflater()
1047                                    .inflate(R.layout.item_message_rtp_session, parent, false);
1048                    viewHolder.status_message = view.findViewById(R.id.message_body);
1049                    viewHolder.message_box = view.findViewById(R.id.message_box);
1050                    viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
1051                    break;
1052                case SENT:
1053                    view = activity.getLayoutInflater().inflate(R.layout.item_message_sent, parent, false);
1054                    viewHolder.status_line = view.findViewById(R.id.status_line);
1055                    viewHolder.message_box_inner = view.findViewById(R.id.message_box_inner);
1056                    viewHolder.message_box = view.findViewById(R.id.message_box);
1057                    viewHolder.contact_picture = view.findViewById(R.id.message_photo);
1058                    viewHolder.download_button = view.findViewById(R.id.download_button);
1059                    viewHolder.indicator = view.findViewById(R.id.security_indicator);
1060                    viewHolder.edit_indicator = view.findViewById(R.id.edit_indicator);
1061                    viewHolder.image = view.findViewById(R.id.message_image);
1062                    viewHolder.messageBody = view.findViewById(R.id.message_body);
1063                    viewHolder.time = view.findViewById(R.id.message_time);
1064                    viewHolder.subject = view.findViewById(R.id.message_subject);
1065                    viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
1066                    viewHolder.audioPlayer = view.findViewById(R.id.audio_player);
1067                    viewHolder.thread_identicon = view.findViewById(R.id.thread_identicon);
1068                    break;
1069                case RECEIVED:
1070                    view = activity.getLayoutInflater().inflate(R.layout.item_message_received, parent, false);
1071                    viewHolder.status_line = view.findViewById(R.id.status_line);
1072                    viewHolder.message_box_inner = view.findViewById(R.id.message_box_inner);
1073                    viewHolder.message_box = view.findViewById(R.id.message_box);
1074                    viewHolder.contact_picture = view.findViewById(R.id.message_photo);
1075                    viewHolder.download_button = view.findViewById(R.id.download_button);
1076                    viewHolder.indicator = view.findViewById(R.id.security_indicator);
1077                    viewHolder.edit_indicator = view.findViewById(R.id.edit_indicator);
1078                    viewHolder.image = view.findViewById(R.id.message_image);
1079                    viewHolder.messageBody = view.findViewById(R.id.message_body);
1080                    viewHolder.time = view.findViewById(R.id.message_time);
1081                    viewHolder.subject = view.findViewById(R.id.message_subject);
1082                    viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
1083                    viewHolder.encryption = view.findViewById(R.id.message_encryption);
1084                    viewHolder.audioPlayer = view.findViewById(R.id.audio_player);
1085                    viewHolder.commands_list = view.findViewById(R.id.commands_list);
1086                    viewHolder.thread_identicon = view.findViewById(R.id.thread_identicon);
1087                    break;
1088                case STATUS:
1089                    view =
1090                            activity.getLayoutInflater()
1091                                    .inflate(R.layout.item_message_status, parent, false);
1092                    viewHolder.contact_picture = view.findViewById(R.id.message_photo);
1093                    viewHolder.status_message = view.findViewById(R.id.status_message);
1094                    viewHolder.load_more_messages = view.findViewById(R.id.load_more_messages);
1095                    break;
1096                default:
1097                    throw new AssertionError("Unknown view type");
1098            }
1099            view.setTag(viewHolder);
1100        } else {
1101            viewHolder = (ViewHolder) view.getTag();
1102            if (viewHolder == null) {
1103                return view;
1104            }
1105        }
1106
1107        if (viewHolder.messageBody != null) {
1108            viewHolder.messageBody.setCustomSelectionActionModeCallback(new MessageTextActionModeCallback(this, viewHolder.messageBody));
1109        }
1110
1111        if (viewHolder.thread_identicon != null) {
1112            viewHolder.thread_identicon.setVisibility(View.GONE);
1113            final Element thread = message.getThread();
1114            if (thread != null) {
1115                final String threadId = thread.getContent();
1116                if (threadId != null) {
1117                    viewHolder.thread_identicon.setVisibility(View.VISIBLE);
1118                    viewHolder.thread_identicon.setColor(UIHelper.getColorForName(threadId));
1119                    viewHolder.thread_identicon.setHash(UIHelper.identiconHash(threadId));
1120                }
1121            }
1122        }
1123
1124        final var black = MaterialColors.getColor(view, com.google.android.material.R.attr.colorSecondaryContainer) == view.getContext().getColor(android.R.color.black);
1125        final boolean colorfulBackground = this.bubbleDesign.colorfulChatBubbles;
1126        final BubbleColor bubbleColor;
1127        if (type == RECEIVED) {
1128            if (isInValidSession) {
1129                bubbleColor = colorfulBackground  || black ? BubbleColor.SECONDARY : BubbleColor.SURFACE;
1130            } else {
1131                bubbleColor = BubbleColor.WARNING;
1132            }
1133        } else {
1134            if (!colorfulBackground && black) {
1135                bubbleColor = BubbleColor.SECONDARY;
1136            } else {
1137                bubbleColor = colorfulBackground ? BubbleColor.TERTIARY : BubbleColor.SURFACE_HIGH;
1138            }
1139        }
1140
1141        if (type == DATE_SEPARATOR) {
1142            if (UIHelper.today(message.getTimeSent())) {
1143                viewHolder.status_message.setText(R.string.today);
1144            } else if (UIHelper.yesterday(message.getTimeSent())) {
1145                viewHolder.status_message.setText(R.string.yesterday);
1146            } else {
1147                viewHolder.status_message.setText(
1148                        DateUtils.formatDateTime(
1149                                activity,
1150                                message.getTimeSent(),
1151                                DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
1152            }
1153            if (colorfulBackground) {
1154                setBackgroundTint(viewHolder.message_box, BubbleColor.PRIMARY);
1155                setTextColor(viewHolder.status_message, BubbleColor.PRIMARY);
1156            } else {
1157                setBackgroundTint(viewHolder.message_box, BubbleColor.SURFACE_HIGH);
1158                setTextColor(viewHolder.status_message, BubbleColor.SURFACE_HIGH);
1159            }
1160            return view;
1161        } else if (type == RTP_SESSION) {
1162            final boolean received = message.getStatus() <= Message.STATUS_RECEIVED;
1163            final RtpSessionStatus rtpSessionStatus = RtpSessionStatus.of(message.getBody());
1164            final long duration = rtpSessionStatus.duration;
1165            final String callTime = UIHelper.readableTimeDifferenceFull(activity, message.getTimeSent());
1166            if (received) {
1167                if (duration > 0) {
1168                    viewHolder.status_message.setText(
1169                            activity.getString(
1170                                    R.string.incoming_call_duration_timestamp,
1171                                    TimeFrameUtils.resolve(activity, duration),
1172                                    UIHelper.readableTimeDifferenceFull(
1173                                            activity, message.getTimeSent())));
1174                } else if (rtpSessionStatus.successful) {
1175                    viewHolder.status_message.setText(activity.getString(R.string.incoming_call_timestamp, callTime));
1176                } else {
1177                    viewHolder.status_message.setText(
1178                            activity.getString(
1179                                    R.string.missed_call_timestamp,
1180                                    UIHelper.readableTimeDifferenceFull(
1181                                            activity, message.getTimeSent())));
1182                }
1183            } else {
1184                if (duration > 0) {
1185                    viewHolder.status_message.setText(
1186                            activity.getString(
1187                                    R.string.outgoing_call_duration_timestamp,
1188                                    TimeFrameUtils.resolve(activity, duration),
1189                                    UIHelper.readableTimeDifferenceFull(
1190                                            activity, message.getTimeSent())));
1191                } else {
1192                    viewHolder.status_message.setText(
1193                            activity.getString(
1194                                    R.string.outgoing_call_timestamp,
1195                                    UIHelper.readableTimeDifferenceFull(
1196                                            activity, message.getTimeSent())));
1197                }
1198            }
1199            if (colorfulBackground) {
1200                setBackgroundTint(viewHolder.message_box, BubbleColor.SECONDARY);
1201                setTextColor(viewHolder.status_message, BubbleColor.SECONDARY);
1202                setImageTint(viewHolder.indicatorReceived, BubbleColor.SECONDARY);
1203            } else {
1204                setBackgroundTint(viewHolder.message_box, BubbleColor.SURFACE_HIGH);
1205                setTextColor(viewHolder.status_message, BubbleColor.SURFACE_HIGH);
1206                setImageTint(viewHolder.indicatorReceived, BubbleColor.SURFACE_HIGH);
1207            }
1208            viewHolder.indicatorReceived.setImageResource(
1209                    RtpSessionStatus.getDrawable(received, rtpSessionStatus.successful));
1210            return view;
1211        } else if (type == STATUS) {
1212            if ("LOAD_MORE".equals(message.getBody())) {
1213                viewHolder.status_message.setVisibility(View.GONE);
1214                viewHolder.contact_picture.setVisibility(View.GONE);
1215                viewHolder.load_more_messages.setVisibility(View.VISIBLE);
1216                viewHolder.load_more_messages.setOnClickListener(
1217                        v -> loadMoreMessages((Conversation) message.getConversation()));
1218            } else {
1219                viewHolder.status_message.setVisibility(View.VISIBLE);
1220                viewHolder.load_more_messages.setVisibility(View.GONE);
1221                viewHolder.status_message.setText(message.getBody());
1222                boolean showAvatar;
1223                if (conversation.getMode() == Conversation.MODE_SINGLE) {
1224                    showAvatar = true;
1225                    AvatarWorkerTask.loadAvatar(
1226                            message, viewHolder.contact_picture, R.dimen.avatar_on_status_message);
1227                } else if (message.getCounterpart() != null
1228                        || message.getTrueCounterpart() != null
1229                        || (message.getCounterparts() != null
1230                                && message.getCounterparts().size() > 0)) {
1231                    showAvatar = true;
1232                    AvatarWorkerTask.loadAvatar(
1233                            message, viewHolder.contact_picture, R.dimen.avatar_on_status_message);
1234                } else {
1235                    showAvatar = false;
1236                }
1237                if (showAvatar) {
1238                    viewHolder.contact_picture.setAlpha(0.5f);
1239                    viewHolder.contact_picture.setVisibility(View.VISIBLE);
1240                } else {
1241                    viewHolder.contact_picture.setVisibility(View.GONE);
1242                }
1243            }
1244            return view;
1245        } else {
1246            // viewHolder.message_box.setClipToOutline(true); This eats the bubble tails on A14 for some reason
1247            AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar);
1248        }
1249
1250        resetClickListener(viewHolder.message_box, viewHolder.messageBody);
1251
1252        viewHolder.message_box.setOnClickListener(v -> {
1253            if (MessageAdapter.this.mOnMessageBoxClickedListener != null) {
1254                MessageAdapter.this.mOnMessageBoxClickedListener
1255                        .onContactPictureClicked(message);
1256            }
1257        });
1258        SwipeDetector swipeDetector = new SwipeDetector((action) -> {
1259            if (action == SwipeDetector.Action.LR && MessageAdapter.this.mOnMessageBoxSwipedListener != null) {
1260                MessageAdapter.this.mOnMessageBoxSwipedListener.onContactPictureClicked(message);
1261            }
1262        });
1263        viewHolder.message_box.setOnTouchListener(swipeDetector);
1264        viewHolder.image.setOnTouchListener(swipeDetector);
1265        viewHolder.time.setOnTouchListener(swipeDetector);
1266
1267        // Treat touch-up as click so we don't have to touch twice
1268        // (touch twice is because it's waiting to see if you double-touch for text selection)
1269        viewHolder.messageBody.setOnTouchListener((v, event) -> {
1270            if (event.getAction() == MotionEvent.ACTION_UP) {
1271                if (MessageAdapter.this.mOnMessageBoxClickedListener != null) {
1272                    MessageAdapter.this.mOnMessageBoxClickedListener
1273                        .onContactPictureClicked(message);
1274                }
1275            }
1276
1277            swipeDetector.onTouch(v, event);
1278
1279            return false;
1280        });
1281        viewHolder.messageBody.setOnClickListener(v -> {
1282            if (MessageAdapter.this.mOnMessageBoxClickedListener != null) {
1283                MessageAdapter.this.mOnMessageBoxClickedListener
1284                        .onContactPictureClicked(message);
1285            }
1286        });
1287        viewHolder.contact_picture.setOnClickListener(v -> {
1288            if (MessageAdapter.this.mOnContactPictureClickedListener != null) {
1289                MessageAdapter.this.mOnContactPictureClickedListener
1290                        .onContactPictureClicked(message);
1291            }
1292
1293        });
1294        viewHolder.contact_picture.setOnLongClickListener(v -> {
1295            if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) {
1296                MessageAdapter.this.mOnContactPictureLongClickedListener
1297                        .onContactPictureLongClicked(v, message);
1298                return true;
1299            } else {
1300                return false;
1301            }
1302        });
1303        viewHolder.messageBody.setAccessibilityDelegate(null);
1304
1305        boolean footerWrap = false;
1306
1307        final Transferable transferable = message.getTransferable();
1308        final boolean unInitiatedButKnownSize = MessageUtils.unInitiatedButKnownSize(message);
1309
1310        final boolean muted = message.getStatus() == Message.STATUS_RECEIVED && conversation.getMode() == Conversation.MODE_MULTI && activity.xmppConnectionService.isMucUserMuted(new MucOptions.User(null, conversation.getJid(), message.getOccupantId(), null, null));
1311        if (muted) {
1312            // Muted MUC participant
1313            displayInfoMessage(viewHolder, "Muted", bubbleColor);
1314        } else if (unInitiatedButKnownSize || message.isDeleted() || (transferable != null && transferable.getStatus() != Transferable.STATUS_UPLOADING)) {
1315            if (unInitiatedButKnownSize || (message.isDeleted() && message.getModerated() == null) || transferable != null && transferable.getStatus() == Transferable.STATUS_OFFER) {
1316                displayDownloadableMessage(viewHolder, message, activity.getString(R.string.download_x_file, UIHelper.getFileDescriptionString(activity, message)), bubbleColor, type);
1317            } else if (transferable != null && transferable.getStatus() == Transferable.STATUS_OFFER_CHECK_FILESIZE) {
1318                displayDownloadableMessage(viewHolder, message, activity.getString(R.string.check_x_filesize, UIHelper.getFileDescriptionString(activity, message)), bubbleColor, type);
1319            } else {
1320                displayInfoMessage(viewHolder, UIHelper.getMessagePreview(activity.xmppConnectionService, message).first, bubbleColor);
1321            }
1322        } else if (message.isFileOrImage()
1323                && message.getEncryption() != Message.ENCRYPTION_PGP
1324                && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
1325            if (message.getFileParams().width > 0 && message.getFileParams().height > 0) {
1326                displayMediaPreviewMessage(viewHolder, message, bubbleColor, type);
1327                if (!black && viewHolder.image.getLayoutParams().width > metrics.density * 110) {
1328                    footerWrap = true;
1329                }
1330            } else if (message.getFileParams().runtime > 0) {
1331                displayAudioMessage(viewHolder, message, bubbleColor, type);
1332            } else if ("application/xdc+zip".equals(message.getFileParams().getMediaType()) && message.getConversation() instanceof Conversation && message.getThread() != null && !message.getFileParams().getCids().isEmpty()) {
1333                displayWebxdcMessage(viewHolder, message, bubbleColor, type);
1334            } else {
1335                displayOpenableMessage(viewHolder, message, bubbleColor, type);
1336            }
1337        } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
1338            if (account.isPgpDecryptionServiceConnected()) {
1339                if (conversation instanceof Conversation
1340                        && !account.hasPendingPgpIntent((Conversation) conversation)) {
1341                    displayInfoMessage(
1342                            viewHolder,
1343                            activity.getString(R.string.message_decrypting),
1344                            bubbleColor);
1345                } else {
1346                    displayInfoMessage(
1347                            viewHolder, activity.getString(R.string.pgp_message), bubbleColor);
1348                }
1349            } else {
1350                displayInfoMessage(
1351                        viewHolder, activity.getString(R.string.install_openkeychain), bubbleColor);
1352                viewHolder.message_box.setOnClickListener(this::promptOpenKeychainInstall);
1353                viewHolder.messageBody.setOnClickListener(this::promptOpenKeychainInstall);
1354            }
1355        } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
1356            displayInfoMessage(
1357                    viewHolder, activity.getString(R.string.decryption_failed), bubbleColor);
1358        } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE) {
1359            displayInfoMessage(
1360                    viewHolder,
1361                    activity.getString(R.string.not_encrypted_for_this_device),
1362                    bubbleColor);
1363        } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
1364            displayInfoMessage(
1365                    viewHolder, activity.getString(R.string.omemo_decryption_failed), bubbleColor);
1366        } else {
1367            if (message.isGeoUri()) {
1368                displayLocationMessage(viewHolder, message, bubbleColor, type);
1369            } else if (message.treatAsDownloadable()) {
1370                try {
1371                    final URI uri = message.getOob();
1372                    displayDownloadableMessage(viewHolder,
1373                            message,
1374                            activity.getString(
1375                                    R.string.check_x_filesize_on_host,
1376                                    UIHelper.getFileDescriptionString(activity, message),
1377                                    uri.getHost()),
1378                            bubbleColor, type);
1379                } catch (Exception e) {
1380                    displayDownloadableMessage(
1381                            viewHolder,
1382                            message,
1383                            activity.getString(
1384                                    R.string.check_x_filesize,
1385                                    UIHelper.getFileDescriptionString(activity, message)),
1386                            bubbleColor, type);
1387                }
1388            } else if (message.bodyIsOnlyEmojis() && message.getType() != Message.TYPE_PRIVATE) {
1389                displayEmojiMessage(viewHolder, getSpannableBody(message), bubbleColor);
1390            } else {
1391                displayTextMessage(viewHolder, message, bubbleColor, message.getType());
1392            }
1393        }
1394
1395        viewHolder.message_box_inner.setMinimumWidth(footerWrap ? (int) (110 * metrics.density) : 0);
1396        LinearLayout.LayoutParams statusParams = (LinearLayout.LayoutParams) viewHolder.status_line.getLayoutParams();
1397        statusParams.width = footerWrap ? ViewGroup.LayoutParams.MATCH_PARENT : ViewGroup.LayoutParams.WRAP_CONTENT;
1398        viewHolder.status_line.setLayoutParams(statusParams);
1399
1400        setBackgroundTint(viewHolder.message_box, bubbleColor);
1401        setTextColor(viewHolder.messageBody, bubbleColor);
1402        viewHolder.messageBody.setLinkTextColor(bubbleToOnSurfaceColor(viewHolder.messageBody, bubbleColor));
1403
1404        if (type == RECEIVED) {
1405            if (!muted && commands != null && conversation instanceof Conversation) {
1406                CommandButtonAdapter adapter = new CommandButtonAdapter(activity);
1407                adapter.addAll(commands);
1408                viewHolder.commands_list.setAdapter(adapter);
1409                viewHolder.commands_list.setVisibility(View.VISIBLE);
1410                viewHolder.commands_list.setOnItemClickListener((p, v, pos, id) -> {
1411                    final Element command = adapter.getItem(pos);
1412                    activity.startCommand(conversation.getAccount(), command.getAttributeAsJid("jid"), command.getAttribute("node"));
1413                });
1414            } else {
1415                // It's unclear if we can set this to null...
1416                ListAdapter adapter = viewHolder.commands_list.getAdapter();
1417                if (adapter instanceof ArrayAdapter) {
1418                    ((ArrayAdapter<?>) adapter).clear();
1419                }
1420                viewHolder.commands_list.setVisibility(View.GONE);
1421                viewHolder.commands_list.setOnItemClickListener(null);
1422            }
1423
1424            setTextColor(viewHolder.encryption, bubbleColor);
1425
1426            if (isInValidSession) {
1427                viewHolder.encryption.setVisibility(View.GONE);
1428            } else {
1429                viewHolder.encryption.setVisibility(View.VISIBLE);
1430                if (omemoEncryption && !message.isTrusted()) {
1431                    viewHolder.encryption.setText(R.string.not_trusted);
1432                } else {
1433                    viewHolder.encryption.setText(
1434                            CryptoHelper.encryptionTypeToText(message.getEncryption()));
1435                }
1436            }
1437        }
1438
1439        if (type == RECEIVED || type == SENT) {
1440            String subject = message.getSubject();
1441            if (subject == null && message.getThread() != null) {
1442                final var thread = ((Conversation) message.getConversation()).getThread(message.getThread().getContent());
1443                if (thread != null) subject = thread.getSubject();
1444            }
1445            if (muted || subject == null) {
1446                viewHolder.subject.setVisibility(View.GONE);
1447            } else {
1448                viewHolder.subject.setVisibility(View.VISIBLE);
1449                viewHolder.subject.setText(subject);
1450            }
1451        }
1452
1453        displayStatus(viewHolder, message, type, bubbleColor);
1454
1455        viewHolder.messageBody.setAccessibilityDelegate(new View.AccessibilityDelegate() {
1456            @Override
1457            public void sendAccessibilityEvent(View host, int eventType) {
1458                super.sendAccessibilityEvent(host, eventType);
1459                if (eventType == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED) {
1460                    if (viewHolder.messageBody.hasSelection()) {
1461                        selectionUuid = message.getUuid();
1462                    } else if (message.getUuid() != null && message.getUuid().equals(selectionUuid)) {
1463                        selectionUuid = null;
1464                    }
1465                }
1466            }
1467        });
1468
1469        return view;
1470    }
1471
1472    private void promptOpenKeychainInstall(View view) {
1473        activity.showInstallPgpDialog();
1474    }
1475
1476    public FileBackend getFileBackend() {
1477        return activity.xmppConnectionService.getFileBackend();
1478    }
1479
1480    public void stopAudioPlayer() {
1481        audioPlayer.stop();
1482    }
1483
1484    public void unregisterListenerInAudioPlayer() {
1485        audioPlayer.unregisterListener();
1486    }
1487
1488    public void startStopPending() {
1489        audioPlayer.startStopPending();
1490    }
1491
1492    public void openDownloadable(Message message) {
1493        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
1494                && ContextCompat.checkSelfPermission(
1495                                activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
1496                        != PackageManager.PERMISSION_GRANTED) {
1497            ConversationFragment.registerPendingMessage(activity, message);
1498            ActivityCompat.requestPermissions(
1499                    activity,
1500                    new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
1501                    ConversationsActivity.REQUEST_OPEN_MESSAGE);
1502            return;
1503        }
1504        final DownloadableFile file =
1505                activity.xmppConnectionService.getFileBackend().getFile(message);
1506        ViewUtil.view(activity, file);
1507    }
1508
1509    private void showLocation(Message message) {
1510        for (Intent intent : GeoHelper.createGeoIntentsFromMessage(activity, message)) {
1511            if (intent.resolveActivity(getContext().getPackageManager()) != null) {
1512                getContext().startActivity(intent);
1513                return;
1514            }
1515        }
1516        Toast.makeText(
1517                        activity,
1518                        R.string.no_application_found_to_display_location,
1519                        Toast.LENGTH_SHORT)
1520                .show();
1521    }
1522
1523    public void updatePreferences() {
1524        final AppSettings appSettings = new AppSettings(activity);
1525        this.bubbleDesign =
1526                new BubbleDesign(appSettings.isColorfulChatBubbles(), appSettings.isLargeFont());
1527    }
1528
1529    public void setHighlightedTerm(List<String> terms) {
1530        this.highlightedTerm = terms == null ? null : StylingHelper.filterHighlightedWords(terms);
1531    }
1532
1533    public interface OnContactPictureClicked {
1534        void onContactPictureClicked(Message message);
1535    }
1536
1537    public interface OnContactPictureLongClicked {
1538        void onContactPictureLongClicked(View v, Message message);
1539    }
1540
1541    public interface OnInlineImageLongClicked {
1542        boolean onInlineImageLongClicked(Cid cid);
1543    }
1544
1545    private static void setBackgroundTint(final View view, final BubbleColor bubbleColor) {
1546        view.setBackgroundTintList(bubbleToColorStateList(view, bubbleColor));
1547    }
1548
1549    private static ColorStateList bubbleToColorStateList(
1550            final View view, final BubbleColor bubbleColor) {
1551        final @AttrRes int colorAttributeResId =
1552                switch (bubbleColor) {
1553                    case SURFACE -> Activities.isNightMode(view.getContext())
1554                            ? com.google.android.material.R.attr.colorSurfaceContainerHigh
1555                            : com.google.android.material.R.attr.colorSurfaceContainerLow;
1556                    case SURFACE_HIGH -> Activities.isNightMode(view.getContext())
1557                            ? com.google.android.material.R.attr.colorSurfaceContainerHighest
1558                            : com.google.android.material.R.attr.colorSurfaceContainerHigh;
1559                    case PRIMARY -> com.google.android.material.R.attr.colorPrimaryContainer;
1560                    case SECONDARY -> com.google.android.material.R.attr.colorSecondaryContainer;
1561                    case TERTIARY -> com.google.android.material.R.attr.colorTertiaryContainer;
1562                    case WARNING -> com.google.android.material.R.attr.colorErrorContainer;
1563                };
1564        return ColorStateList.valueOf(MaterialColors.getColor(view, colorAttributeResId));
1565    }
1566
1567    public static void setImageTint(final ImageView imageView, final BubbleColor bubbleColor) {
1568        ImageViewCompat.setImageTintList(
1569                imageView, bubbleToOnSurfaceColorStateList(imageView, bubbleColor));
1570    }
1571
1572    public static void setImageTintError(final ImageView imageView) {
1573        ImageViewCompat.setImageTintList(
1574                imageView,
1575                ColorStateList.valueOf(
1576                        MaterialColors.getColor(
1577                                imageView, com.google.android.material.R.attr.colorError)));
1578    }
1579
1580    public static void setTextColor(final TextView textView, final BubbleColor bubbleColor) {
1581        final var color = bubbleToOnSurfaceColor(textView, bubbleColor);
1582        textView.setTextColor(color);
1583        if (BubbleColor.SURFACES.contains(bubbleColor)) {
1584            textView.setLinkTextColor(
1585                    MaterialColors.getColor(
1586                            textView, com.google.android.material.R.attr.colorPrimary));
1587        } else {
1588            textView.setLinkTextColor(color);
1589        }
1590    }
1591
1592    private static void setTextSize(final TextView textView, final boolean largeFont) {
1593        if (largeFont) {
1594            textView.setTextAppearance(
1595                    com.google.android.material.R.style.TextAppearance_Material3_BodyLarge);
1596            textView.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 18);
1597        } else {
1598            textView.setTextAppearance(
1599                    com.google.android.material.R.style.TextAppearance_Material3_BodyMedium);
1600        }
1601    }
1602
1603    private static @ColorInt int bubbleToOnSurfaceVariant(
1604            final View view, final BubbleColor bubbleColor) {
1605        final @AttrRes int colorAttributeResId;
1606        if (BubbleColor.SURFACES.contains(bubbleColor)) {
1607            colorAttributeResId = com.google.android.material.R.attr.colorOnSurfaceVariant;
1608        } else {
1609            colorAttributeResId = bubbleToOnSurface(bubbleColor);
1610        }
1611        return MaterialColors.getColor(view, colorAttributeResId);
1612    }
1613
1614    private static @ColorInt int bubbleToOnSurfaceColor(
1615            final View view, final BubbleColor bubbleColor) {
1616        return MaterialColors.getColor(view, bubbleToOnSurface(bubbleColor));
1617    }
1618
1619    public static ColorStateList bubbleToOnSurfaceColorStateList(
1620            final View view, final BubbleColor bubbleColor) {
1621        return ColorStateList.valueOf(bubbleToOnSurfaceColor(view, bubbleColor));
1622    }
1623
1624    private static @AttrRes int bubbleToOnSurface(final BubbleColor bubbleColor) {
1625        return switch (bubbleColor) {
1626            case SURFACE, SURFACE_HIGH -> com.google.android.material.R.attr.colorOnSurface;
1627            case PRIMARY -> com.google.android.material.R.attr.colorOnPrimaryContainer;
1628            case SECONDARY -> com.google.android.material.R.attr.colorOnSecondaryContainer;
1629            case TERTIARY -> com.google.android.material.R.attr.colorOnTertiaryContainer;
1630            case WARNING -> com.google.android.material.R.attr.colorOnErrorContainer;
1631        };
1632    }
1633
1634    public enum BubbleColor {
1635        SURFACE,
1636        SURFACE_HIGH,
1637        PRIMARY,
1638        SECONDARY,
1639        TERTIARY,
1640        WARNING;
1641
1642        private static final Collection<BubbleColor> SURFACES =
1643                Arrays.asList(BubbleColor.SURFACE, BubbleColor.SURFACE_HIGH);
1644    }
1645
1646    private static class BubbleDesign {
1647        public final boolean colorfulChatBubbles;
1648        public final boolean largeFont;
1649
1650        private BubbleDesign(final boolean colorfulChatBubbles, final boolean largeFont) {
1651            this.colorfulChatBubbles = colorfulChatBubbles;
1652            this.largeFont = largeFont;
1653        }
1654    }
1655
1656    private static class ViewHolder {
1657
1658        public MaterialButton load_more_messages;
1659        public ImageView edit_indicator;
1660        public RelativeLayout audioPlayer;
1661        protected View status_line;
1662        protected LinearLayout message_box;
1663        protected View message_box_inner;
1664        protected MaterialButton download_button;
1665        protected ShapeableImageView image;
1666        protected ImageView indicator;
1667        protected ImageView indicatorReceived;
1668        protected TextView time;
1669        protected TextView subject;
1670        protected TextView messageBody;
1671        protected ImageView contact_picture;
1672        protected TextView status_message;
1673        protected TextView encryption;
1674        protected ListView commands_list;
1675        protected GithubIdenticonView thread_identicon;
1676    }
1677
1678    class ThumbnailTask extends AsyncTask<DownloadableFile, Void, Drawable[]> {
1679        @Override
1680        protected Drawable[] doInBackground(DownloadableFile... params) {
1681            if (isCancelled()) return null;
1682
1683            Drawable[] d = new Drawable[params.length];
1684            for (int i = 0; i < params.length; i++) {
1685                try {
1686                    d[i] = activity.xmppConnectionService.getFileBackend().getThumbnail(params[i], activity.getResources(), (int) (metrics.density * 288), false);
1687                } catch (final IOException e) {
1688                    d[i] = null;
1689                }
1690            }
1691
1692            return d;
1693        }
1694
1695        @Override
1696        protected void onPostExecute(final Drawable[] d) {
1697            if (isCancelled()) return;
1698            activity.xmppConnectionService.updateConversationUi();
1699        }
1700    }
1701}