From 21a8ffd7d9dba74390cacc3da8f2721d1b61f0f8 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sat, 9 Nov 2024 22:25:01 +0100 Subject: [PATCH] If whole mesage is a known URI with an icon, make button (cherry picked from commit ae157db4d9455610b3c9f4f8d809e8cba48e498a) --- .../siacs/conversations/entities/Message.java | 15 +++++++++++ .../ui/adapter/MessageAdapter.java | 27 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index 19dea19dd..acd556f16 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -4,6 +4,7 @@ import android.content.ContentValues; import android.database.Cursor; import android.graphics.drawable.Drawable; import android.graphics.Color; +import android.net.Uri; import android.os.Build; import android.text.Html; import android.text.Spannable; @@ -63,6 +64,7 @@ import eu.siacs.conversations.ui.util.QuoteHelper; import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.Emoticons; import eu.siacs.conversations.utils.GeoHelper; +import eu.siacs.conversations.utils.Patterns; import eu.siacs.conversations.utils.MessageUtils; import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.utils.StringUtils; @@ -168,6 +170,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable private Collection reactions = Collections.emptyList(); private Boolean isGeoUri = null; + private Uri wholeIsKnownURI = null; private Boolean isEmojisOnly = null; private Boolean treatAsDownloadable = null; private FileParams fileParams = null; @@ -636,6 +639,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable private synchronized void setBodyPreserveXHTML(String body) { this.body = body; this.isGeoUri = null; + this.wholeIsKnownURI = null; this.isEmojisOnly = null; this.treatAsDownloadable = null; } @@ -656,6 +660,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable public synchronized void appendBody(String append) { this.body += append; this.isGeoUri = null; + this.wholeIsKnownURI = null; this.isEmojisOnly = null; this.treatAsDownloadable = null; } @@ -1444,6 +1449,16 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable return isGeoUri; } + public synchronized Uri wholeIsKnownURI() { + if (wholeIsKnownURI != null) return wholeIsKnownURI; + + if (Patterns.BITCOIN_URI.matcher(body).matches() ||Patterns.BITCOINCASH_URI.matcher(body).matches() || Patterns.MONERO_URI.matcher(body).matches()) { + wholeIsKnownURI = Uri.parse(body.replace(":", "://")); // hack to make query parser work + } + + return wholeIsKnownURI; + } + protected List getSims() { return payloads.stream().filter(el -> el.getName().equals("reference") && el.getNamespace().equals("urn:xmpp:reference:0") && diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 85d63fc07..b099c361a 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -935,6 +935,29 @@ public class MessageAdapter extends ArrayAdapter { viewHolder.download_button.setOnClickListener(v -> openDownloadable(message)); } + private void displayURIMessage( + ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor, final int type) { + displayTextMessage(viewHolder, message, bubbleColor, type); + viewHolder.messageBody.setVisibility(View.GONE); + viewHolder.image.setVisibility(View.GONE); + viewHolder.audioPlayer.setVisibility(View.GONE); + viewHolder.download_button.setVisibility(View.VISIBLE); + final var uri = message.wholeIsKnownURI(); + final var amount = uri.getQueryParameter("amount"); + final var formattedAmount = amount == null || amount.equals("") ? "" : amount + " "; + if ("bitcoin".equals(uri.getScheme())) { + viewHolder.download_button.setIconResource(R.drawable.bitcoin_24dp); + viewHolder.download_button.setText("Send " + formattedAmount + "Bitcoin"); + } else if ("bitcoincash".equals(uri.getScheme())) { + viewHolder.download_button.setIconResource(R.drawable.bitcoin_cash_24dp); + viewHolder.download_button.setText("Send " + formattedAmount + "Bitcoin Cash"); + } else if ("monero".equals(uri.getScheme())) { + viewHolder.download_button.setIconResource(R.drawable.monero_24dp); + viewHolder.download_button.setText("Send " + formattedAmount + "Monero"); + } + viewHolder.download_button.setOnClickListener(v -> new FixedURLSpan(message.getRawBody()).onClick(v)); + } + private void displayLocationMessage( ViewHolder viewHolder, final Message message, final BubbleColor bubbleColor, final int type) { displayTextMessage(viewHolder, message, bubbleColor, type); @@ -1537,7 +1560,9 @@ public class MessageAdapter extends ArrayAdapter { displayInfoMessage( viewHolder, activity.getString(R.string.omemo_decryption_failed), bubbleColor); } else { - if (message.isGeoUri()) { + if (message.wholeIsKnownURI() != null) { + displayURIMessage(viewHolder, message, bubbleColor, type); + } else if (message.isGeoUri()) { displayLocationMessage(viewHolder, message, bubbleColor, type); } else if (message.treatAsDownloadable()) { try {