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.content.res.ColorStateList;
   8import android.graphics.Typeface;
   9import android.os.Build;
  10import android.text.Spannable;
  11import android.text.SpannableString;
  12import android.text.SpannableStringBuilder;
  13import android.text.format.DateUtils;
  14import android.text.style.ForegroundColorSpan;
  15import android.text.style.RelativeSizeSpan;
  16import android.text.style.StyleSpan;
  17import android.util.DisplayMetrics;
  18import android.view.View;
  19import android.view.ViewGroup;
  20import android.view.WindowManager;
  21import android.widget.ArrayAdapter;
  22import android.widget.ImageView;
  23import android.widget.LinearLayout;
  24import android.widget.RelativeLayout;
  25import android.widget.TextView;
  26import android.widget.Toast;
  27import androidx.annotation.AttrRes;
  28import androidx.annotation.ColorInt;
  29import androidx.annotation.DrawableRes;
  30import androidx.annotation.NonNull;
  31import androidx.annotation.Nullable;
  32import androidx.constraintlayout.widget.ConstraintLayout;
  33import androidx.core.app.ActivityCompat;
  34import androidx.core.content.ContextCompat;
  35import androidx.core.widget.ImageViewCompat;
  36import com.google.android.material.button.MaterialButton;
  37import com.google.android.material.chip.ChipGroup;
  38import com.google.android.material.color.MaterialColors;
  39import com.google.android.material.dialog.MaterialAlertDialogBuilder;
  40import com.google.common.base.Joiner;
  41import com.google.common.base.Strings;
  42import com.google.common.collect.Collections2;
  43import com.google.common.collect.ImmutableList;
  44import eu.siacs.conversations.AppSettings;
  45import eu.siacs.conversations.Config;
  46import eu.siacs.conversations.R;
  47import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
  48import eu.siacs.conversations.entities.Account;
  49import eu.siacs.conversations.entities.Conversation;
  50import eu.siacs.conversations.entities.Conversational;
  51import eu.siacs.conversations.entities.DownloadableFile;
  52import eu.siacs.conversations.entities.Message;
  53import eu.siacs.conversations.entities.Message.FileParams;
  54import eu.siacs.conversations.entities.RtpSessionStatus;
  55import eu.siacs.conversations.entities.Transferable;
  56import eu.siacs.conversations.persistance.FileBackend;
  57import eu.siacs.conversations.services.MessageArchiveService;
  58import eu.siacs.conversations.services.NotificationService;
  59import eu.siacs.conversations.ui.Activities;
  60import eu.siacs.conversations.ui.BindingAdapters;
  61import eu.siacs.conversations.ui.ConversationFragment;
  62import eu.siacs.conversations.ui.ConversationsActivity;
  63import eu.siacs.conversations.ui.XmppActivity;
  64import eu.siacs.conversations.ui.service.AudioPlayer;
  65import eu.siacs.conversations.ui.text.DividerSpan;
  66import eu.siacs.conversations.ui.text.QuoteSpan;
  67import eu.siacs.conversations.ui.util.Attachment;
  68import eu.siacs.conversations.ui.util.AvatarWorkerTask;
  69import eu.siacs.conversations.ui.util.MyLinkify;
  70import eu.siacs.conversations.ui.util.QuoteHelper;
  71import eu.siacs.conversations.ui.util.ViewUtil;
  72import eu.siacs.conversations.ui.widget.ClickableMovementMethod;
  73import eu.siacs.conversations.utils.CryptoHelper;
  74import eu.siacs.conversations.utils.Emoticons;
  75import eu.siacs.conversations.utils.GeoHelper;
  76import eu.siacs.conversations.utils.MessageUtils;
  77import eu.siacs.conversations.utils.StylingHelper;
  78import eu.siacs.conversations.utils.TimeFrameUtils;
  79import eu.siacs.conversations.utils.UIHelper;
  80import eu.siacs.conversations.xmpp.Jid;
  81import eu.siacs.conversations.xmpp.mam.MamReference;
  82import java.net.URI;
  83import java.util.Arrays;
  84import java.util.Collection;
  85import java.util.List;
  86import java.util.Locale;
  87import java.util.regex.Matcher;
  88import java.util.regex.Pattern;
  89
  90public class MessageAdapter extends ArrayAdapter<Message> {
  91
  92    public static final String DATE_SEPARATOR_BODY = "DATE_SEPARATOR";
  93    private static final int SENT = 0;
  94    private static final int RECEIVED = 1;
  95    private static final int STATUS = 2;
  96    private static final int DATE_SEPARATOR = 3;
  97    private static final int RTP_SESSION = 4;
  98    private final XmppActivity activity;
  99    private final AudioPlayer audioPlayer;
 100    private List<String> highlightedTerm = null;
 101    private final DisplayMetrics metrics;
 102    private OnContactPictureClicked mOnContactPictureClickedListener;
 103    private OnContactPictureLongClicked mOnContactPictureLongClickedListener;
 104    private BubbleDesign bubbleDesign = new BubbleDesign(false, false);
 105    private final boolean mForceNames;
 106
 107    public MessageAdapter(
 108            final XmppActivity activity, final List<Message> messages, final boolean forceNames) {
 109        super(activity, 0, messages);
 110        this.audioPlayer = new AudioPlayer(this);
 111        this.activity = activity;
 112        metrics = getContext().getResources().getDisplayMetrics();
 113        updatePreferences();
 114        this.mForceNames = forceNames;
 115    }
 116
 117    public MessageAdapter(final XmppActivity activity, final List<Message> messages) {
 118        this(activity, messages, false);
 119    }
 120
 121    private static void resetClickListener(View... views) {
 122        for (View view : views) {
 123            view.setOnClickListener(null);
 124        }
 125    }
 126
 127    public void flagScreenOn() {
 128        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 129    }
 130
 131    public void flagScreenOff() {
 132        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 133    }
 134
 135    public void setVolumeControl(final int stream) {
 136        activity.setVolumeControlStream(stream);
 137    }
 138
 139    public void setOnContactPictureClicked(OnContactPictureClicked listener) {
 140        this.mOnContactPictureClickedListener = listener;
 141    }
 142
 143    public Activity getActivity() {
 144        return activity;
 145    }
 146
 147    public void setOnContactPictureLongClicked(OnContactPictureLongClicked listener) {
 148        this.mOnContactPictureLongClickedListener = listener;
 149    }
 150
 151    @Override
 152    public int getViewTypeCount() {
 153        return 5;
 154    }
 155
 156    private static int getItemViewType(final Message message) {
 157        if (message.getType() == Message.TYPE_STATUS) {
 158            if (DATE_SEPARATOR_BODY.equals(message.getBody())) {
 159                return DATE_SEPARATOR;
 160            } else {
 161                return STATUS;
 162            }
 163        } else if (message.getType() == Message.TYPE_RTP_SESSION) {
 164            return RTP_SESSION;
 165        } else if (message.getStatus() <= Message.STATUS_RECEIVED) {
 166            return RECEIVED;
 167        } else {
 168            return SENT;
 169        }
 170    }
 171
 172    @Override
 173    public int getItemViewType(final int position) {
 174        return getItemViewType(getItem(position));
 175    }
 176
 177    private void displayStatus(
 178            final ViewHolder viewHolder,
 179            final Message message,
 180            final int type,
 181            final BubbleColor bubbleColor) {
 182        final int mergedStatus = message.getStatus();
 183        final boolean error;
 184        if (viewHolder.indicatorReceived != null) {
 185            viewHolder.indicatorReceived.setVisibility(View.GONE);
 186        }
 187        final Transferable transferable = message.getTransferable();
 188        final boolean multiReceived =
 189                message.getConversation().getMode() == Conversation.MODE_MULTI
 190                        && mergedStatus <= Message.STATUS_RECEIVED;
 191        final String fileSize;
 192        if (message.isFileOrImage()
 193                || transferable != null
 194                || MessageUtils.unInitiatedButKnownSize(message)) {
 195            final FileParams params = message.getFileParams();
 196            fileSize = params.size != null ? UIHelper.filesizeToString(params.size) : null;
 197            if (message.getStatus() == Message.STATUS_SEND_FAILED
 198                    || (transferable != null
 199                            && (transferable.getStatus() == Transferable.STATUS_FAILED
 200                                    || transferable.getStatus()
 201                                            == Transferable.STATUS_CANCELLED))) {
 202                error = true;
 203            } else {
 204                error = message.getStatus() == Message.STATUS_SEND_FAILED;
 205            }
 206        } else {
 207            fileSize = null;
 208            error = message.getStatus() == Message.STATUS_SEND_FAILED;
 209        }
 210        if (type == SENT) {
 211            final @DrawableRes Integer receivedIndicator =
 212                    getMessageStatusAsDrawable(message, mergedStatus);
 213            if (receivedIndicator == null) {
 214                viewHolder.indicatorReceived.setVisibility(View.INVISIBLE);
 215            } else {
 216                viewHolder.indicatorReceived.setImageResource(receivedIndicator);
 217                if (mergedStatus == Message.STATUS_SEND_FAILED) {
 218                    setImageTintError(viewHolder.indicatorReceived);
 219                } else {
 220                    setImageTint(viewHolder.indicatorReceived, bubbleColor);
 221                }
 222                viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
 223            }
 224        }
 225        final var additionalStatusInfo = getAdditionalStatusInfo(message, mergedStatus);
 226
 227        if (error && type == SENT) {
 228            viewHolder.time.setTextColor(
 229                    MaterialColors.getColor(
 230                            viewHolder.time, com.google.android.material.R.attr.colorError));
 231        } else {
 232            setTextColor(viewHolder.time, bubbleColor);
 233        }
 234        if (message.getEncryption() == Message.ENCRYPTION_NONE) {
 235            viewHolder.indicator.setVisibility(View.GONE);
 236        } else {
 237            boolean verified = false;
 238            if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
 239                final FingerprintStatus status =
 240                        message.getConversation()
 241                                .getAccount()
 242                                .getAxolotlService()
 243                                .getFingerprintTrust(message.getFingerprint());
 244                if (status != null && status.isVerified()) {
 245                    verified = true;
 246                }
 247            }
 248            if (verified) {
 249                viewHolder.indicator.setImageResource(R.drawable.ic_verified_user_24dp);
 250            } else {
 251                viewHolder.indicator.setImageResource(R.drawable.ic_lock_24dp);
 252            }
 253            if (error && type == SENT) {
 254                setImageTintError(viewHolder.indicator);
 255            } else {
 256                setImageTint(viewHolder.indicator, bubbleColor);
 257            }
 258            viewHolder.indicator.setVisibility(View.VISIBLE);
 259        }
 260
 261        if (viewHolder.edit_indicator != null) {
 262            if (message.edited()) {
 263                viewHolder.edit_indicator.setVisibility(View.VISIBLE);
 264                if (error && type == SENT) {
 265                    setImageTintError(viewHolder.edit_indicator);
 266                } else {
 267                    setImageTint(viewHolder.edit_indicator, bubbleColor);
 268                }
 269            } else {
 270                viewHolder.edit_indicator.setVisibility(View.GONE);
 271            }
 272        }
 273
 274        final String formattedTime =
 275                UIHelper.readableTimeDifferenceFull(getContext(), message.getTimeSent());
 276        final String bodyLanguage = message.getBodyLanguage();
 277        final ImmutableList.Builder<String> timeInfoBuilder = new ImmutableList.Builder<>();
 278        if (message.getStatus() <= Message.STATUS_RECEIVED) {
 279            timeInfoBuilder.add(formattedTime);
 280            if (fileSize != null) {
 281                timeInfoBuilder.add(fileSize);
 282            }
 283            if (mForceNames || multiReceived) {
 284                final String displayName = UIHelper.getMessageDisplayName(message);
 285                if (displayName != null) {
 286                    timeInfoBuilder.add(displayName);
 287                }
 288            }
 289            if (bodyLanguage != null) {
 290                timeInfoBuilder.add(bodyLanguage.toUpperCase(Locale.US));
 291            }
 292        } else {
 293            if (bodyLanguage != null) {
 294                timeInfoBuilder.add(bodyLanguage.toUpperCase(Locale.US));
 295            }
 296            if (fileSize != null) {
 297                timeInfoBuilder.add(fileSize);
 298            }
 299            // for space reasons we display only 'additional status info' (send progress or concrete
 300            // failure reason) or the time
 301            if (additionalStatusInfo != null) {
 302                timeInfoBuilder.add(additionalStatusInfo);
 303            } else {
 304                timeInfoBuilder.add(formattedTime);
 305            }
 306        }
 307        final var timeInfo = timeInfoBuilder.build();
 308        viewHolder.time.setText(Joiner.on(" \u00B7 ").join(timeInfo));
 309    }
 310
 311    public static @DrawableRes Integer getMessageStatusAsDrawable(
 312            final Message message, final int status) {
 313        final var transferable = message.getTransferable();
 314        return switch (status) {
 315            case Message.STATUS_WAITING -> R.drawable.ic_more_horiz_24dp;
 316            case Message.STATUS_UNSEND -> transferable == null ? null : R.drawable.ic_upload_24dp;
 317            case Message.STATUS_SEND -> R.drawable.ic_done_24dp;
 318            case Message.STATUS_SEND_RECEIVED, Message.STATUS_SEND_DISPLAYED ->
 319                    R.drawable.ic_done_all_24dp;
 320            case Message.STATUS_SEND_FAILED -> {
 321                final String errorMessage = message.getErrorMessage();
 322                if (Message.ERROR_MESSAGE_CANCELLED.equals(errorMessage)) {
 323                    yield R.drawable.ic_cancel_24dp;
 324                } else {
 325                    yield R.drawable.ic_error_24dp;
 326                }
 327            }
 328            case Message.STATUS_OFFERED -> R.drawable.ic_p2p_24dp;
 329            default -> null;
 330        };
 331    }
 332
 333    @Nullable
 334    private String getAdditionalStatusInfo(final Message message, final int mergedStatus) {
 335        final String additionalStatusInfo;
 336        if (mergedStatus == Message.STATUS_SEND_FAILED) {
 337            final String errorMessage = Strings.nullToEmpty(message.getErrorMessage());
 338            final String[] errorParts = errorMessage.split("\\u001f", 2);
 339            if (errorParts.length == 2 && errorParts[0].equals("file-too-large")) {
 340                additionalStatusInfo = getContext().getString(R.string.file_too_large);
 341            } else {
 342                additionalStatusInfo = null;
 343            }
 344        } else if (mergedStatus == Message.STATUS_UNSEND) {
 345            final var transferable = message.getTransferable();
 346            if (transferable == null) {
 347                return null;
 348            }
 349            return getContext().getString(R.string.sending_file, transferable.getProgress());
 350        } else {
 351            additionalStatusInfo = null;
 352        }
 353        return additionalStatusInfo;
 354    }
 355
 356    private void displayInfoMessage(
 357            ViewHolder viewHolder, CharSequence text, final BubbleColor bubbleColor) {
 358        viewHolder.download_button.setVisibility(View.GONE);
 359        viewHolder.audioPlayer.setVisibility(View.GONE);
 360        viewHolder.image.setVisibility(View.GONE);
 361        viewHolder.messageBody.setVisibility(View.VISIBLE);
 362        viewHolder.messageBody.setText(text);
 363        viewHolder.messageBody.setTextColor(
 364                bubbleToOnSurfaceVariant(viewHolder.messageBody, bubbleColor));
 365        viewHolder.messageBody.setTextIsSelectable(false);
 366    }
 367
 368    private void displayEmojiMessage(
 369            final ViewHolder viewHolder, final String body, final BubbleColor bubbleColor) {
 370        viewHolder.download_button.setVisibility(View.GONE);
 371        viewHolder.audioPlayer.setVisibility(View.GONE);
 372        viewHolder.image.setVisibility(View.GONE);
 373        viewHolder.messageBody.setVisibility(View.VISIBLE);
 374        setTextColor(viewHolder.messageBody, bubbleColor);
 375        final Spannable span = new SpannableString(body);
 376        float size = Emoticons.isEmoji(body) ? 3.0f : 2.0f;
 377        span.setSpan(
 378                new RelativeSizeSpan(size), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 379        viewHolder.messageBody.setText(span);
 380    }
 381
 382    private void applyQuoteSpan(
 383            final TextView textView,
 384            SpannableStringBuilder body,
 385            int start,
 386            int end,
 387            final BubbleColor bubbleColor) {
 388        if (start > 1 && !"\n\n".equals(body.subSequence(start - 2, start).toString())) {
 389            body.insert(start++, "\n");
 390            body.setSpan(
 391                    new DividerSpan(false), start - 2, start, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 392            end++;
 393        }
 394        if (end < body.length() - 1 && !"\n\n".equals(body.subSequence(end, end + 2).toString())) {
 395            body.insert(end, "\n");
 396            body.setSpan(new DividerSpan(false), end, end + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 397        }
 398        final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
 399        body.setSpan(
 400                new QuoteSpan(bubbleToOnSurfaceVariant(textView, bubbleColor), metrics),
 401                start,
 402                end,
 403                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 404    }
 405
 406    /**
 407     * Applies QuoteSpan to group of lines which starts with > or » characters. Appends likebreaks
 408     * and applies DividerSpan to them to show a padding between quote and text.
 409     */
 410    private boolean handleTextQuotes(
 411            final TextView textView,
 412            final SpannableStringBuilder body,
 413            final BubbleColor bubbleColor) {
 414        boolean startsWithQuote = false;
 415        int quoteDepth = 1;
 416        while (QuoteHelper.bodyContainsQuoteStart(body) && quoteDepth <= Config.QUOTE_MAX_DEPTH) {
 417            char previous = '\n';
 418            int lineStart = -1;
 419            int lineTextStart = -1;
 420            int quoteStart = -1;
 421            for (int i = 0; i <= body.length(); i++) {
 422                char current = body.length() > i ? body.charAt(i) : '\n';
 423                if (lineStart == -1) {
 424                    if (previous == '\n') {
 425                        if (i < body.length() && QuoteHelper.isPositionQuoteStart(body, i)) {
 426                            // Line start with quote
 427                            lineStart = i;
 428                            if (quoteStart == -1) quoteStart = i;
 429                            if (i == 0) startsWithQuote = true;
 430                        } else if (quoteStart >= 0) {
 431                            // Line start without quote, apply spans there
 432                            applyQuoteSpan(textView, body, quoteStart, i - 1, bubbleColor);
 433                            quoteStart = -1;
 434                        }
 435                    }
 436                } else {
 437                    // Remove extra spaces between > and first character in the line
 438                    // > character will be removed too
 439                    if (current != ' ' && lineTextStart == -1) {
 440                        lineTextStart = i;
 441                    }
 442                    if (current == '\n') {
 443                        body.delete(lineStart, lineTextStart);
 444                        i -= lineTextStart - lineStart;
 445                        if (i == lineStart) {
 446                            // Avoid empty lines because span over empty line can be hidden
 447                            body.insert(i++, " ");
 448                        }
 449                        lineStart = -1;
 450                        lineTextStart = -1;
 451                    }
 452                }
 453                previous = current;
 454            }
 455            if (quoteStart >= 0) {
 456                // Apply spans to finishing open quote
 457                applyQuoteSpan(textView, body, quoteStart, body.length(), bubbleColor);
 458            }
 459            quoteDepth++;
 460        }
 461        return startsWithQuote;
 462    }
 463
 464    private void displayTextMessage(
 465            final ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor) {
 466        viewHolder.download_button.setVisibility(View.GONE);
 467        viewHolder.image.setVisibility(View.GONE);
 468        viewHolder.audioPlayer.setVisibility(View.GONE);
 469        viewHolder.messageBody.setVisibility(View.VISIBLE);
 470        setTextColor(viewHolder.messageBody, bubbleColor);
 471        setTextSize(viewHolder.messageBody, this.bubbleDesign.largeFont);
 472        viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
 473
 474        if (message.getBody() != null) {
 475            final String nick = UIHelper.getMessageDisplayName(message);
 476            final boolean hasMeCommand = message.hasMeCommand();
 477            final var rawBody = message.getBody();
 478            final SpannableStringBuilder body;
 479            if (rawBody.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
 480                body = new SpannableStringBuilder(rawBody, 0, Config.MAX_DISPLAY_MESSAGE_CHARS);
 481                body.append("");
 482            } else {
 483                body = new SpannableStringBuilder(rawBody);
 484            }
 485            if (hasMeCommand) {
 486                body.replace(0, Message.ME_COMMAND.length(), String.format("%s ", nick));
 487            }
 488            boolean startsWithQuote = handleTextQuotes(viewHolder.messageBody, body, bubbleColor);
 489            if (!message.isPrivateMessage()) {
 490                if (hasMeCommand) {
 491                    body.setSpan(
 492                            new StyleSpan(Typeface.BOLD_ITALIC),
 493                            0,
 494                            nick.length(),
 495                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 496                }
 497            } else {
 498                String privateMarker;
 499                if (message.getStatus() <= Message.STATUS_RECEIVED) {
 500                    privateMarker = activity.getString(R.string.private_message);
 501                } else {
 502                    Jid cp = message.getCounterpart();
 503                    privateMarker =
 504                            activity.getString(
 505                                    R.string.private_message_to,
 506                                    Strings.nullToEmpty(cp == null ? null : cp.getResource()));
 507                }
 508                body.insert(0, privateMarker);
 509                int privateMarkerIndex = privateMarker.length();
 510                if (startsWithQuote) {
 511                    body.insert(privateMarkerIndex, "\n\n");
 512                    body.setSpan(
 513                            new DividerSpan(false),
 514                            privateMarkerIndex,
 515                            privateMarkerIndex + 2,
 516                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 517                } else {
 518                    body.insert(privateMarkerIndex, " ");
 519                }
 520                body.setSpan(
 521                        new ForegroundColorSpan(
 522                                bubbleToOnSurfaceVariant(viewHolder.messageBody, bubbleColor)),
 523                        0,
 524                        privateMarkerIndex,
 525                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 526                body.setSpan(
 527                        new StyleSpan(Typeface.BOLD),
 528                        0,
 529                        privateMarkerIndex,
 530                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 531                if (hasMeCommand) {
 532                    body.setSpan(
 533                            new StyleSpan(Typeface.BOLD_ITALIC),
 534                            privateMarkerIndex + 1,
 535                            privateMarkerIndex + 1 + nick.length(),
 536                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 537                }
 538            }
 539            if (message.getConversation().getMode() == Conversation.MODE_MULTI
 540                    && message.getStatus() == Message.STATUS_RECEIVED) {
 541                if (message.getConversation() instanceof Conversation conversation) {
 542                    Pattern pattern =
 543                            NotificationService.generateNickHighlightPattern(
 544                                    conversation.getMucOptions().getActualNick());
 545                    Matcher matcher = pattern.matcher(body);
 546                    while (matcher.find()) {
 547                        body.setSpan(
 548                                new StyleSpan(Typeface.BOLD),
 549                                matcher.start(),
 550                                matcher.end(),
 551                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 552                    }
 553                }
 554            }
 555            Matcher matcher = Emoticons.getEmojiPattern(body).matcher(body);
 556            while (matcher.find()) {
 557                if (matcher.start() < matcher.end()) {
 558                    body.setSpan(
 559                            new RelativeSizeSpan(1.2f),
 560                            matcher.start(),
 561                            matcher.end(),
 562                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 563                }
 564            }
 565
 566            StylingHelper.format(body, viewHolder.messageBody.getCurrentTextColor());
 567            MyLinkify.addLinks(body, true);
 568            if (highlightedTerm != null) {
 569                StylingHelper.highlight(viewHolder.messageBody, body, highlightedTerm);
 570            }
 571            viewHolder.messageBody.setAutoLinkMask(0);
 572            viewHolder.messageBody.setText(body);
 573            viewHolder.messageBody.setMovementMethod(ClickableMovementMethod.getInstance());
 574        } else {
 575            viewHolder.messageBody.setText("");
 576            viewHolder.messageBody.setTextIsSelectable(false);
 577        }
 578    }
 579
 580    private void displayDownloadableMessage(
 581            ViewHolder viewHolder,
 582            final Message message,
 583            String text,
 584            final BubbleColor bubbleColor) {
 585        toggleWhisperInfo(viewHolder, message, bubbleColor);
 586        viewHolder.image.setVisibility(View.GONE);
 587        viewHolder.audioPlayer.setVisibility(View.GONE);
 588        viewHolder.download_button.setVisibility(View.VISIBLE);
 589        viewHolder.download_button.setText(text);
 590        final var attachment = Attachment.of(message);
 591        final @DrawableRes int imageResource = MediaAdapter.getImageDrawable(attachment);
 592        viewHolder.download_button.setIconResource(imageResource);
 593        viewHolder.download_button.setOnClickListener(
 594                v -> ConversationFragment.downloadFile(activity, message));
 595    }
 596
 597    private void displayOpenableMessage(
 598            ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor) {
 599        toggleWhisperInfo(viewHolder, message, bubbleColor);
 600        viewHolder.image.setVisibility(View.GONE);
 601        viewHolder.audioPlayer.setVisibility(View.GONE);
 602        viewHolder.download_button.setVisibility(View.VISIBLE);
 603        viewHolder.download_button.setText(
 604                activity.getString(
 605                        R.string.open_x_file,
 606                        UIHelper.getFileDescriptionString(activity, message)));
 607        final var attachment = Attachment.of(message);
 608        final @DrawableRes int imageResource = MediaAdapter.getImageDrawable(attachment);
 609        viewHolder.download_button.setIconResource(imageResource);
 610        viewHolder.download_button.setOnClickListener(v -> openDownloadable(message));
 611    }
 612
 613    private void displayLocationMessage(
 614            ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor) {
 615        toggleWhisperInfo(viewHolder, message, bubbleColor);
 616        viewHolder.image.setVisibility(View.GONE);
 617        viewHolder.audioPlayer.setVisibility(View.GONE);
 618        viewHolder.download_button.setVisibility(View.VISIBLE);
 619        viewHolder.download_button.setText(R.string.show_location);
 620        final var attachment = Attachment.of(message);
 621        final @DrawableRes int imageResource = MediaAdapter.getImageDrawable(attachment);
 622        viewHolder.download_button.setIconResource(imageResource);
 623        viewHolder.download_button.setOnClickListener(v -> showLocation(message));
 624    }
 625
 626    private void displayAudioMessage(
 627            ViewHolder viewHolder, Message message, final BubbleColor bubbleColor) {
 628        toggleWhisperInfo(viewHolder, message, bubbleColor);
 629        viewHolder.image.setVisibility(View.GONE);
 630        viewHolder.download_button.setVisibility(View.GONE);
 631        final RelativeLayout audioPlayer = viewHolder.audioPlayer;
 632        audioPlayer.setVisibility(View.VISIBLE);
 633        AudioPlayer.ViewHolder.get(audioPlayer).setBubbleColor(bubbleColor);
 634        this.audioPlayer.init(audioPlayer, message);
 635    }
 636
 637    private void displayMediaPreviewMessage(
 638            ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor) {
 639        toggleWhisperInfo(viewHolder, message, bubbleColor);
 640        viewHolder.download_button.setVisibility(View.GONE);
 641        viewHolder.audioPlayer.setVisibility(View.GONE);
 642        viewHolder.image.setVisibility(View.VISIBLE);
 643        final FileParams params = message.getFileParams();
 644        final float target = activity.getResources().getDimension(R.dimen.image_preview_width);
 645        final int scaledW;
 646        final int scaledH;
 647        if (Math.max(params.height, params.width) * metrics.density <= target) {
 648            scaledW = (int) (params.width * metrics.density);
 649            scaledH = (int) (params.height * metrics.density);
 650        } else if (Math.max(params.height, params.width) <= target) {
 651            scaledW = params.width;
 652            scaledH = params.height;
 653        } else if (params.width <= params.height) {
 654            scaledW = (int) (params.width / ((double) params.height / target));
 655            scaledH = (int) target;
 656        } else {
 657            scaledW = (int) target;
 658            scaledH = (int) (params.height / ((double) params.width / target));
 659        }
 660        final LinearLayout.LayoutParams layoutParams =
 661                new LinearLayout.LayoutParams(scaledW, scaledH);
 662        viewHolder.image.setLayoutParams(layoutParams);
 663        activity.loadBitmap(message, viewHolder.image);
 664        viewHolder.image.setOnClickListener(v -> openDownloadable(message));
 665    }
 666
 667    private void toggleWhisperInfo(
 668            ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor) {
 669        if (message.isPrivateMessage()) {
 670            final String privateMarker;
 671            if (message.getStatus() <= Message.STATUS_RECEIVED) {
 672                privateMarker = activity.getString(R.string.private_message);
 673            } else {
 674                Jid cp = message.getCounterpart();
 675                privateMarker =
 676                        activity.getString(
 677                                R.string.private_message_to,
 678                                Strings.nullToEmpty(cp == null ? null : cp.getResource()));
 679            }
 680            final SpannableString body = new SpannableString(privateMarker);
 681            body.setSpan(
 682                    new ForegroundColorSpan(
 683                            bubbleToOnSurfaceVariant(viewHolder.messageBody, bubbleColor)),
 684                    0,
 685                    privateMarker.length(),
 686                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 687            body.setSpan(
 688                    new StyleSpan(Typeface.BOLD),
 689                    0,
 690                    privateMarker.length(),
 691                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 692            viewHolder.messageBody.setText(body);
 693            viewHolder.messageBody.setVisibility(View.VISIBLE);
 694        } else {
 695            viewHolder.messageBody.setVisibility(View.GONE);
 696        }
 697    }
 698
 699    private void loadMoreMessages(Conversation conversation) {
 700        conversation.setLastClearHistory(0, null);
 701        activity.xmppConnectionService.updateConversation(conversation);
 702        conversation.setHasMessagesLeftOnServer(true);
 703        conversation.setFirstMamReference(null);
 704        long timestamp = conversation.getLastMessageTransmitted().getTimestamp();
 705        if (timestamp == 0) {
 706            timestamp = System.currentTimeMillis();
 707        }
 708        conversation.messagesLoaded.set(true);
 709        MessageArchiveService.Query query =
 710                activity.xmppConnectionService
 711                        .getMessageArchiveService()
 712                        .query(conversation, new MamReference(0), timestamp, false);
 713        if (query != null) {
 714            Toast.makeText(activity, R.string.fetching_history_from_server, Toast.LENGTH_LONG)
 715                    .show();
 716        } else {
 717            Toast.makeText(
 718                            activity,
 719                            R.string.not_fetching_history_retention_period,
 720                            Toast.LENGTH_SHORT)
 721                    .show();
 722        }
 723    }
 724
 725    @Override
 726    public View getView(final int position, View view, final @NonNull ViewGroup parent) {
 727        final Message message = getItem(position);
 728        final boolean omemoEncryption = message.getEncryption() == Message.ENCRYPTION_AXOLOTL;
 729        final boolean isInValidSession =
 730                message.isValidInSession() && (!omemoEncryption || message.isTrusted());
 731        final Conversational conversation = message.getConversation();
 732        final Account account = conversation.getAccount();
 733        final int type = getItemViewType(message);
 734        ViewHolder viewHolder;
 735        if (view == null) {
 736            viewHolder = new ViewHolder();
 737            switch (type) {
 738                case DATE_SEPARATOR:
 739                    view =
 740                            activity.getLayoutInflater()
 741                                    .inflate(R.layout.item_message_date_bubble, parent, false);
 742                    viewHolder.status_message = view.findViewById(R.id.message_body);
 743                    viewHolder.message_box = view.findViewById(R.id.message_box);
 744                    break;
 745                case RTP_SESSION:
 746                    view =
 747                            activity.getLayoutInflater()
 748                                    .inflate(R.layout.item_message_rtp_session, parent, false);
 749                    viewHolder.status_message = view.findViewById(R.id.message_body);
 750                    viewHolder.message_box = view.findViewById(R.id.message_box);
 751                    viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
 752                    break;
 753                case SENT:
 754                    view =
 755                            activity.getLayoutInflater()
 756                                    .inflate(R.layout.item_message_sent, parent, false);
 757                    viewHolder.root = (ConstraintLayout) view;
 758                    viewHolder.message_box = view.findViewById(R.id.message_box);
 759                    viewHolder.contact_picture = view.findViewById(R.id.message_photo);
 760                    viewHolder.download_button = view.findViewById(R.id.download_button);
 761                    viewHolder.indicator = view.findViewById(R.id.security_indicator);
 762                    viewHolder.edit_indicator = view.findViewById(R.id.edit_indicator);
 763                    viewHolder.image = view.findViewById(R.id.message_image);
 764                    viewHolder.messageBody = view.findViewById(R.id.message_body);
 765                    viewHolder.time = view.findViewById(R.id.message_time);
 766                    viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
 767                    viewHolder.audioPlayer = view.findViewById(R.id.audio_player);
 768                    viewHolder.reactions = view.findViewById(R.id.reactions);
 769                    break;
 770                case RECEIVED:
 771                    view =
 772                            activity.getLayoutInflater()
 773                                    .inflate(R.layout.item_message_received, parent, false);
 774                    viewHolder.root = (ConstraintLayout) view;
 775                    viewHolder.message_box = view.findViewById(R.id.message_box);
 776                    viewHolder.contact_picture = view.findViewById(R.id.message_photo);
 777                    viewHolder.download_button = view.findViewById(R.id.download_button);
 778                    viewHolder.indicator = view.findViewById(R.id.security_indicator);
 779                    viewHolder.edit_indicator = view.findViewById(R.id.edit_indicator);
 780                    viewHolder.image = view.findViewById(R.id.message_image);
 781                    viewHolder.messageBody = view.findViewById(R.id.message_body);
 782                    viewHolder.time = view.findViewById(R.id.message_time);
 783                    viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
 784                    viewHolder.encryption = view.findViewById(R.id.message_encryption);
 785                    viewHolder.audioPlayer = view.findViewById(R.id.audio_player);
 786                    viewHolder.reactions = view.findViewById(R.id.reactions);
 787                    break;
 788                case STATUS:
 789                    view =
 790                            activity.getLayoutInflater()
 791                                    .inflate(R.layout.item_message_status, parent, false);
 792                    viewHolder.contact_picture = view.findViewById(R.id.message_photo);
 793                    viewHolder.status_message = view.findViewById(R.id.status_message);
 794                    viewHolder.load_more_messages = view.findViewById(R.id.load_more_messages);
 795                    break;
 796                default:
 797                    throw new AssertionError("Unknown view type");
 798            }
 799            view.setTag(viewHolder);
 800        } else {
 801            viewHolder = (ViewHolder) view.getTag();
 802            if (viewHolder == null) {
 803                return view;
 804            }
 805        }
 806
 807        final boolean colorfulBackground = this.bubbleDesign.colorfulChatBubbles;
 808        final BubbleColor bubbleColor;
 809        if (type == RECEIVED) {
 810            if (isInValidSession) {
 811                bubbleColor = colorfulBackground ? BubbleColor.SECONDARY : BubbleColor.SURFACE;
 812            } else {
 813                bubbleColor = BubbleColor.WARNING;
 814            }
 815        } else {
 816            bubbleColor = colorfulBackground ? BubbleColor.TERTIARY : BubbleColor.SURFACE_HIGH;
 817        }
 818
 819        if (type == DATE_SEPARATOR) {
 820            if (UIHelper.today(message.getTimeSent())) {
 821                viewHolder.status_message.setText(R.string.today);
 822            } else if (UIHelper.yesterday(message.getTimeSent())) {
 823                viewHolder.status_message.setText(R.string.yesterday);
 824            } else {
 825                viewHolder.status_message.setText(
 826                        DateUtils.formatDateTime(
 827                                activity,
 828                                message.getTimeSent(),
 829                                DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
 830            }
 831            if (colorfulBackground) {
 832                setBackgroundTint(viewHolder.message_box, BubbleColor.PRIMARY);
 833                setTextColor(viewHolder.status_message, BubbleColor.PRIMARY);
 834            } else {
 835                setBackgroundTint(viewHolder.message_box, BubbleColor.SURFACE_HIGH);
 836                setTextColor(viewHolder.status_message, BubbleColor.SURFACE_HIGH);
 837            }
 838            return view;
 839        } else if (type == RTP_SESSION) {
 840            final boolean received = message.getStatus() <= Message.STATUS_RECEIVED;
 841            final RtpSessionStatus rtpSessionStatus = RtpSessionStatus.of(message.getBody());
 842            final long duration = rtpSessionStatus.duration;
 843            if (received) {
 844                if (duration > 0) {
 845                    viewHolder.status_message.setText(
 846                            activity.getString(
 847                                    R.string.incoming_call_duration_timestamp,
 848                                    TimeFrameUtils.resolve(activity, duration),
 849                                    UIHelper.readableTimeDifferenceFull(
 850                                            activity, message.getTimeSent())));
 851                } else if (rtpSessionStatus.successful) {
 852                    viewHolder.status_message.setText(R.string.incoming_call);
 853                } else {
 854                    viewHolder.status_message.setText(
 855                            activity.getString(
 856                                    R.string.missed_call_timestamp,
 857                                    UIHelper.readableTimeDifferenceFull(
 858                                            activity, message.getTimeSent())));
 859                }
 860            } else {
 861                if (duration > 0) {
 862                    viewHolder.status_message.setText(
 863                            activity.getString(
 864                                    R.string.outgoing_call_duration_timestamp,
 865                                    TimeFrameUtils.resolve(activity, duration),
 866                                    UIHelper.readableTimeDifferenceFull(
 867                                            activity, message.getTimeSent())));
 868                } else {
 869                    viewHolder.status_message.setText(
 870                            activity.getString(
 871                                    R.string.outgoing_call_timestamp,
 872                                    UIHelper.readableTimeDifferenceFull(
 873                                            activity, message.getTimeSent())));
 874                }
 875            }
 876            if (colorfulBackground) {
 877                setBackgroundTint(viewHolder.message_box, BubbleColor.SECONDARY);
 878                setTextColor(viewHolder.status_message, BubbleColor.SECONDARY);
 879                setImageTint(viewHolder.indicatorReceived, BubbleColor.SECONDARY);
 880            } else {
 881                setBackgroundTint(viewHolder.message_box, BubbleColor.SURFACE_HIGH);
 882                setTextColor(viewHolder.status_message, BubbleColor.SURFACE_HIGH);
 883                setImageTint(viewHolder.indicatorReceived, BubbleColor.SURFACE_HIGH);
 884            }
 885            viewHolder.indicatorReceived.setImageResource(
 886                    RtpSessionStatus.getDrawable(received, rtpSessionStatus.successful));
 887            return view;
 888        } else if (type == STATUS) {
 889            if ("LOAD_MORE".equals(message.getBody())) {
 890                viewHolder.status_message.setVisibility(View.GONE);
 891                viewHolder.contact_picture.setVisibility(View.GONE);
 892                viewHolder.load_more_messages.setVisibility(View.VISIBLE);
 893                viewHolder.load_more_messages.setOnClickListener(
 894                        v -> loadMoreMessages((Conversation) message.getConversation()));
 895            } else {
 896                viewHolder.status_message.setVisibility(View.VISIBLE);
 897                viewHolder.load_more_messages.setVisibility(View.GONE);
 898                viewHolder.status_message.setText(message.getBody());
 899                boolean showAvatar;
 900                if (conversation.getMode() == Conversation.MODE_SINGLE) {
 901                    showAvatar = true;
 902                    AvatarWorkerTask.loadAvatar(
 903                            message, viewHolder.contact_picture, R.dimen.avatar_on_status_message);
 904                } else if (message.getCounterpart() != null
 905                        || message.getTrueCounterpart() != null
 906                        || (message.getCounterparts() != null
 907                                && !message.getCounterparts().isEmpty())) {
 908                    showAvatar = true;
 909                    AvatarWorkerTask.loadAvatar(
 910                            message, viewHolder.contact_picture, R.dimen.avatar_on_status_message);
 911                } else {
 912                    showAvatar = false;
 913                }
 914                if (showAvatar) {
 915                    viewHolder.contact_picture.setAlpha(0.5f);
 916                    viewHolder.contact_picture.setVisibility(View.VISIBLE);
 917                } else {
 918                    viewHolder.contact_picture.setVisibility(View.GONE);
 919                }
 920            }
 921            return view;
 922        } else {
 923            // sent and received bubbles
 924            final var mergeIntoTop = mergeIntoTop(position, message);
 925            final var mergeIntoBottom = mergeIntoBottom(position, message);
 926            final var requiresAvatar = type == SENT ? !mergeIntoBottom : !mergeIntoTop;
 927            setBubblePadding(viewHolder.root, mergeIntoTop, mergeIntoBottom);
 928            setRequiresAvatar(viewHolder, requiresAvatar);
 929            viewHolder.message_box.setClipToOutline(true);
 930            AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar);
 931        }
 932
 933        resetClickListener(viewHolder.message_box, viewHolder.messageBody);
 934
 935        viewHolder.contact_picture.setOnClickListener(
 936                v -> {
 937                    if (MessageAdapter.this.mOnContactPictureClickedListener != null) {
 938                        MessageAdapter.this.mOnContactPictureClickedListener
 939                                .onContactPictureClicked(message);
 940                    }
 941                });
 942        viewHolder.contact_picture.setOnLongClickListener(
 943                v -> {
 944                    if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) {
 945                        MessageAdapter.this.mOnContactPictureLongClickedListener
 946                                .onContactPictureLongClicked(v, message);
 947                        return true;
 948                    } else {
 949                        return false;
 950                    }
 951                });
 952
 953        final Transferable transferable = message.getTransferable();
 954        final boolean unInitiatedButKnownSize = MessageUtils.unInitiatedButKnownSize(message);
 955        if (unInitiatedButKnownSize
 956                || message.isDeleted()
 957                || (transferable != null
 958                        && transferable.getStatus() != Transferable.STATUS_UPLOADING)) {
 959            if (unInitiatedButKnownSize
 960                    || transferable != null
 961                            && transferable.getStatus() == Transferable.STATUS_OFFER) {
 962                displayDownloadableMessage(
 963                        viewHolder,
 964                        message,
 965                        activity.getString(
 966                                R.string.download_x_file,
 967                                UIHelper.getFileDescriptionString(activity, message)),
 968                        bubbleColor);
 969            } else if (transferable != null
 970                    && transferable.getStatus() == Transferable.STATUS_OFFER_CHECK_FILESIZE) {
 971                displayDownloadableMessage(
 972                        viewHolder,
 973                        message,
 974                        activity.getString(
 975                                R.string.check_x_filesize,
 976                                UIHelper.getFileDescriptionString(activity, message)),
 977                        bubbleColor);
 978            } else {
 979                displayInfoMessage(
 980                        viewHolder,
 981                        UIHelper.getMessagePreview(activity, message).first,
 982                        bubbleColor);
 983            }
 984        } else if (message.isFileOrImage()
 985                && message.getEncryption() != Message.ENCRYPTION_PGP
 986                && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
 987            if (message.getFileParams().width > 0 && message.getFileParams().height > 0) {
 988                displayMediaPreviewMessage(viewHolder, message, bubbleColor);
 989            } else if (message.getFileParams().runtime > 0) {
 990                displayAudioMessage(viewHolder, message, bubbleColor);
 991            } else {
 992                displayOpenableMessage(viewHolder, message, bubbleColor);
 993            }
 994        } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 995            if (account.isPgpDecryptionServiceConnected()) {
 996                if (conversation instanceof Conversation
 997                        && !account.hasPendingPgpIntent((Conversation) conversation)) {
 998                    displayInfoMessage(
 999                            viewHolder,
1000                            activity.getString(R.string.message_decrypting),
1001                            bubbleColor);
1002                } else {
1003                    displayInfoMessage(
1004                            viewHolder, activity.getString(R.string.pgp_message), bubbleColor);
1005                }
1006            } else {
1007                displayInfoMessage(
1008                        viewHolder, activity.getString(R.string.install_openkeychain), bubbleColor);
1009                viewHolder.message_box.setOnClickListener(this::promptOpenKeychainInstall);
1010                viewHolder.messageBody.setOnClickListener(this::promptOpenKeychainInstall);
1011            }
1012        } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
1013            displayInfoMessage(
1014                    viewHolder, activity.getString(R.string.decryption_failed), bubbleColor);
1015        } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE) {
1016            displayInfoMessage(
1017                    viewHolder,
1018                    activity.getString(R.string.not_encrypted_for_this_device),
1019                    bubbleColor);
1020        } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
1021            displayInfoMessage(
1022                    viewHolder, activity.getString(R.string.omemo_decryption_failed), bubbleColor);
1023        } else {
1024            if (message.isGeoUri()) {
1025                displayLocationMessage(viewHolder, message, bubbleColor);
1026            } else if (message.bodyIsOnlyEmojis() && message.getType() != Message.TYPE_PRIVATE) {
1027                displayEmojiMessage(viewHolder, message.getBody().trim(), bubbleColor);
1028            } else if (message.treatAsDownloadable()) {
1029                try {
1030                    final URI uri = new URI(message.getBody());
1031                    displayDownloadableMessage(
1032                            viewHolder,
1033                            message,
1034                            activity.getString(
1035                                    R.string.check_x_filesize_on_host,
1036                                    UIHelper.getFileDescriptionString(activity, message),
1037                                    uri.getHost()),
1038                            bubbleColor);
1039                } catch (Exception e) {
1040                    displayDownloadableMessage(
1041                            viewHolder,
1042                            message,
1043                            activity.getString(
1044                                    R.string.check_x_filesize,
1045                                    UIHelper.getFileDescriptionString(activity, message)),
1046                            bubbleColor);
1047                }
1048            } else {
1049                displayTextMessage(viewHolder, message, bubbleColor);
1050            }
1051        }
1052
1053        setBackgroundTint(viewHolder.message_box, bubbleColor);
1054        setTextColor(viewHolder.messageBody, bubbleColor);
1055
1056        if (type == RECEIVED) {
1057            setTextColor(viewHolder.encryption, bubbleColor);
1058            if (isInValidSession) {
1059                viewHolder.encryption.setVisibility(View.GONE);
1060            } else {
1061                viewHolder.encryption.setVisibility(View.VISIBLE);
1062                if (omemoEncryption && !message.isTrusted()) {
1063                    viewHolder.encryption.setText(R.string.not_trusted);
1064                } else {
1065                    viewHolder.encryption.setText(
1066                            CryptoHelper.encryptionTypeToText(message.getEncryption()));
1067                }
1068            }
1069            BindingAdapters.setReactionsOnReceived(
1070                    viewHolder.reactions,
1071                    message.getAggregatedReactions(),
1072                    reactions -> sendReactions(message, reactions),
1073                    emoji -> showDetailedReaction(message, emoji),
1074                    () -> addReaction(message));
1075        } else if (type == SENT) {
1076            BindingAdapters.setReactionsOnSent(
1077                    viewHolder.reactions,
1078                    message.getAggregatedReactions(),
1079                    reactions -> sendReactions(message, reactions),
1080                    emoji -> showDetailedReaction(message, emoji));
1081        }
1082
1083        displayStatus(viewHolder, message, type, bubbleColor);
1084        return view;
1085    }
1086
1087    private void setBubblePadding(
1088            final ConstraintLayout root,
1089            final boolean mergeIntoTop,
1090            final boolean mergeIntoBottom) {
1091        final var resources = root.getResources();
1092        final var horizontal = resources.getDimensionPixelSize(R.dimen.bubble_horizontal_padding);
1093        final int top =
1094                resources.getDimensionPixelSize(
1095                        mergeIntoTop
1096                                ? R.dimen.bubble_vertical_padding_minimum
1097                                : R.dimen.bubble_vertical_padding);
1098        final int bottom =
1099                resources.getDimensionPixelSize(
1100                        mergeIntoBottom
1101                                ? R.dimen.bubble_vertical_padding_minimum
1102                                : R.dimen.bubble_vertical_padding);
1103        root.setPadding(horizontal, top, horizontal, bottom);
1104    }
1105
1106    private void setRequiresAvatar(final ViewHolder viewHolder, final boolean requiresAvatar) {
1107        final var layoutParams = viewHolder.contact_picture.getLayoutParams();
1108        if (requiresAvatar) {
1109            final var resources = viewHolder.contact_picture.getResources();
1110            final var avatarSize = resources.getDimensionPixelSize(R.dimen.bubble_avatar_size);
1111            layoutParams.height = avatarSize;
1112            viewHolder.contact_picture.setVisibility(View.VISIBLE);
1113            viewHolder.message_box.setMinimumHeight(avatarSize);
1114        } else {
1115            layoutParams.height = 0;
1116            viewHolder.contact_picture.setVisibility(View.INVISIBLE);
1117            viewHolder.message_box.setMinimumHeight(0);
1118        }
1119        viewHolder.contact_picture.setLayoutParams(layoutParams);
1120    }
1121
1122    private boolean mergeIntoTop(final int position, final Message message) {
1123        if (position < 0) {
1124            return false;
1125        }
1126        final var top = getItem(position - 1);
1127        return merge(top, message);
1128    }
1129
1130    private boolean mergeIntoBottom(final int position, final Message message) {
1131        final Message bottom;
1132        try {
1133            bottom = getItem(position + 1);
1134        } catch (final IndexOutOfBoundsException e) {
1135            return false;
1136        }
1137        return merge(message, bottom);
1138    }
1139
1140    private static boolean merge(final Message a, final Message b) {
1141        if (getItemViewType(a) != getItemViewType(b)) {
1142            return false;
1143        }
1144        if (a.getConversation().getMode() == Conversation.MODE_MULTI
1145                && a.getStatus() == Message.STATUS_RECEIVED) {
1146            final var occupantIdA = a.getOccupantId();
1147            final var occupantIdB = b.getOccupantId();
1148            if (occupantIdA != null && occupantIdB != null) {
1149                if (!occupantIdA.equals(occupantIdB)) {
1150                    return false;
1151                }
1152            }
1153            final var counterPartA = a.getCounterpart();
1154            final var counterPartB = b.getCounterpart();
1155            if (counterPartA == null || !counterPartA.equals(counterPartB)) {
1156                return false;
1157            }
1158        }
1159        return b.getTimeSent() - a.getTimeSent() <= Config.MESSAGE_MERGE_WINDOW;
1160    }
1161
1162    private boolean showDetailedReaction(final Message message, final String emoji) {
1163        final var c = message.getConversation();
1164        if (c instanceof Conversation conversation && c.getMode() == Conversational.MODE_MULTI) {
1165            final var reactions =
1166                    Collections2.filter(
1167                            message.getReactions(), r -> r.normalizedReaction().equals(emoji));
1168            final var mucOptions = conversation.getMucOptions();
1169            final var users = mucOptions.findUsers(reactions);
1170            if (users.isEmpty()) {
1171                return true;
1172            }
1173            final MaterialAlertDialogBuilder dialogBuilder =
1174                    new MaterialAlertDialogBuilder(activity);
1175            dialogBuilder.setTitle(emoji);
1176            dialogBuilder.setMessage(UIHelper.concatNames(users));
1177            dialogBuilder.create().show();
1178            return true;
1179        } else {
1180            return false;
1181        }
1182    }
1183
1184    private void sendReactions(final Message message, final Collection<String> reactions) {
1185        if (activity.xmppConnectionService.sendReactions(message, reactions)) {
1186            return;
1187        }
1188        Toast.makeText(activity, R.string.could_not_add_reaction, Toast.LENGTH_LONG).show();
1189    }
1190
1191    private void addReaction(final Message message) {
1192        activity.addReaction(
1193                message,
1194                reactions -> {
1195                    if (activity.xmppConnectionService.sendReactions(message, reactions)) {
1196                        return;
1197                    }
1198                    Toast.makeText(activity, R.string.could_not_add_reaction, Toast.LENGTH_LONG)
1199                            .show();
1200                });
1201    }
1202
1203    private void promptOpenKeychainInstall(View view) {
1204        activity.showInstallPgpDialog();
1205    }
1206
1207    public FileBackend getFileBackend() {
1208        return activity.xmppConnectionService.getFileBackend();
1209    }
1210
1211    public void stopAudioPlayer() {
1212        audioPlayer.stop();
1213    }
1214
1215    public void unregisterListenerInAudioPlayer() {
1216        audioPlayer.unregisterListener();
1217    }
1218
1219    public void startStopPending() {
1220        audioPlayer.startStopPending();
1221    }
1222
1223    public void openDownloadable(Message message) {
1224        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
1225                && ContextCompat.checkSelfPermission(
1226                                activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
1227                        != PackageManager.PERMISSION_GRANTED) {
1228            ConversationFragment.registerPendingMessage(activity, message);
1229            ActivityCompat.requestPermissions(
1230                    activity,
1231                    new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
1232                    ConversationsActivity.REQUEST_OPEN_MESSAGE);
1233            return;
1234        }
1235        final DownloadableFile file =
1236                activity.xmppConnectionService.getFileBackend().getFile(message);
1237        ViewUtil.view(activity, file);
1238    }
1239
1240    private void showLocation(Message message) {
1241        for (Intent intent : GeoHelper.createGeoIntentsFromMessage(activity, message)) {
1242            if (intent.resolveActivity(getContext().getPackageManager()) != null) {
1243                getContext().startActivity(intent);
1244                return;
1245            }
1246        }
1247        Toast.makeText(
1248                        activity,
1249                        R.string.no_application_found_to_display_location,
1250                        Toast.LENGTH_SHORT)
1251                .show();
1252    }
1253
1254    public void updatePreferences() {
1255        final AppSettings appSettings = new AppSettings(activity);
1256        this.bubbleDesign =
1257                new BubbleDesign(appSettings.isColorfulChatBubbles(), appSettings.isLargeFont());
1258    }
1259
1260    public void setHighlightedTerm(List<String> terms) {
1261        this.highlightedTerm = terms == null ? null : StylingHelper.filterHighlightedWords(terms);
1262    }
1263
1264    public interface OnContactPictureClicked {
1265        void onContactPictureClicked(Message message);
1266    }
1267
1268    public interface OnContactPictureLongClicked {
1269        void onContactPictureLongClicked(View v, Message message);
1270    }
1271
1272    private static void setBackgroundTint(final View view, final BubbleColor bubbleColor) {
1273        view.setBackgroundTintList(bubbleToColorStateList(view, bubbleColor));
1274    }
1275
1276    private static ColorStateList bubbleToColorStateList(
1277            final View view, final BubbleColor bubbleColor) {
1278        final @AttrRes int colorAttributeResId =
1279                switch (bubbleColor) {
1280                    case SURFACE ->
1281                            Activities.isNightMode(view.getContext())
1282                                    ? com.google.android.material.R.attr.colorSurfaceContainerHigh
1283                                    : com.google.android.material.R.attr.colorSurfaceContainerLow;
1284                    case SURFACE_HIGH ->
1285                            Activities.isNightMode(view.getContext())
1286                                    ? com.google.android.material.R.attr
1287                                            .colorSurfaceContainerHighest
1288                                    : com.google.android.material.R.attr.colorSurfaceContainerHigh;
1289                    case PRIMARY -> com.google.android.material.R.attr.colorPrimaryContainer;
1290                    case SECONDARY -> com.google.android.material.R.attr.colorSecondaryContainer;
1291                    case TERTIARY -> com.google.android.material.R.attr.colorTertiaryContainer;
1292                    case WARNING -> com.google.android.material.R.attr.colorErrorContainer;
1293                };
1294        return ColorStateList.valueOf(MaterialColors.getColor(view, colorAttributeResId));
1295    }
1296
1297    public static void setImageTint(final ImageView imageView, final BubbleColor bubbleColor) {
1298        ImageViewCompat.setImageTintList(
1299                imageView, bubbleToOnSurfaceColorStateList(imageView, bubbleColor));
1300    }
1301
1302    public static void setImageTintError(final ImageView imageView) {
1303        ImageViewCompat.setImageTintList(
1304                imageView,
1305                ColorStateList.valueOf(
1306                        MaterialColors.getColor(
1307                                imageView, com.google.android.material.R.attr.colorError)));
1308    }
1309
1310    public static void setTextColor(final TextView textView, final BubbleColor bubbleColor) {
1311        final var color = bubbleToOnSurfaceColor(textView, bubbleColor);
1312        textView.setTextColor(color);
1313        if (BubbleColor.SURFACES.contains(bubbleColor)) {
1314            textView.setLinkTextColor(
1315                    MaterialColors.getColor(
1316                            textView, com.google.android.material.R.attr.colorPrimary));
1317        } else {
1318            textView.setLinkTextColor(color);
1319        }
1320    }
1321
1322    private static void setTextSize(final TextView textView, final boolean largeFont) {
1323        if (largeFont) {
1324            textView.setTextAppearance(
1325                    com.google.android.material.R.style.TextAppearance_Material3_BodyLarge);
1326        } else {
1327            textView.setTextAppearance(
1328                    com.google.android.material.R.style.TextAppearance_Material3_BodyMedium);
1329        }
1330    }
1331
1332    private static @ColorInt int bubbleToOnSurfaceVariant(
1333            final View view, final BubbleColor bubbleColor) {
1334        final @AttrRes int colorAttributeResId;
1335        if (BubbleColor.SURFACES.contains(bubbleColor)) {
1336            colorAttributeResId = com.google.android.material.R.attr.colorOnSurfaceVariant;
1337        } else {
1338            colorAttributeResId = bubbleToOnSurface(bubbleColor);
1339        }
1340        return MaterialColors.getColor(view, colorAttributeResId);
1341    }
1342
1343    private static @ColorInt int bubbleToOnSurfaceColor(
1344            final View view, final BubbleColor bubbleColor) {
1345        return MaterialColors.getColor(view, bubbleToOnSurface(bubbleColor));
1346    }
1347
1348    public static ColorStateList bubbleToOnSurfaceColorStateList(
1349            final View view, final BubbleColor bubbleColor) {
1350        return ColorStateList.valueOf(bubbleToOnSurfaceColor(view, bubbleColor));
1351    }
1352
1353    private static @AttrRes int bubbleToOnSurface(final BubbleColor bubbleColor) {
1354        return switch (bubbleColor) {
1355            case SURFACE, SURFACE_HIGH -> com.google.android.material.R.attr.colorOnSurface;
1356            case PRIMARY -> com.google.android.material.R.attr.colorOnPrimaryContainer;
1357            case SECONDARY -> com.google.android.material.R.attr.colorOnSecondaryContainer;
1358            case TERTIARY -> com.google.android.material.R.attr.colorOnTertiaryContainer;
1359            case WARNING -> com.google.android.material.R.attr.colorOnErrorContainer;
1360        };
1361    }
1362
1363    public enum BubbleColor {
1364        SURFACE,
1365        SURFACE_HIGH,
1366        PRIMARY,
1367        SECONDARY,
1368        TERTIARY,
1369        WARNING;
1370
1371        private static final Collection<BubbleColor> SURFACES =
1372                Arrays.asList(BubbleColor.SURFACE, BubbleColor.SURFACE_HIGH);
1373    }
1374
1375    private static class BubbleDesign {
1376        public final boolean colorfulChatBubbles;
1377        public final boolean largeFont;
1378
1379        private BubbleDesign(final boolean colorfulChatBubbles, final boolean largeFont) {
1380            this.colorfulChatBubbles = colorfulChatBubbles;
1381            this.largeFont = largeFont;
1382        }
1383    }
1384
1385    private static class ViewHolder {
1386
1387        private ConstraintLayout root;
1388        public MaterialButton load_more_messages;
1389        public ImageView edit_indicator;
1390        public RelativeLayout audioPlayer;
1391        protected LinearLayout message_box;
1392        protected MaterialButton download_button;
1393        protected ImageView image;
1394        protected ImageView indicator;
1395        protected ImageView indicatorReceived;
1396        protected TextView time;
1397        protected TextView messageBody;
1398        protected ImageView contact_picture;
1399        protected TextView status_message;
1400        protected TextView encryption;
1401        protected ChipGroup reactions;
1402    }
1403}