Use the same mechanism for link copying and linkification (#4357)

Prevents copying something different from what was linked, such as in the
message "fine.gif https://example.com"
This commit is contained in:
Stephen Paul Weber 2022-08-22 02:50:26 -05:00 committed by GitHub
parent 9fdbd64d02
commit 56a6b17e7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -31,6 +31,9 @@ package eu.siacs.conversations.ui.util;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Intent; import android.content.Intent;
import android.net.Uri;
import android.text.SpannableStringBuilder;
import android.text.style.URLSpan;
import android.widget.Toast; import android.widget.Toast;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -106,11 +109,13 @@ public class ShareUtil {
} }
public static void copyLinkToClipboard(XmppActivity activity, Message message) { public static void copyLinkToClipboard(XmppActivity activity, Message message) {
String body = message.getMergedBody().toString(); SpannableStringBuilder body = message.getMergedBody();
Matcher xmppPatternMatcher = Patterns.XMPP_PATTERN.matcher(body); MyLinkify.addLinks(body, true);
if (xmppPatternMatcher.find()) { for (final URLSpan urlspan : body.getSpans(0, body.length() - 1, URLSpan.class)) {
Uri uri = Uri.parse(urlspan.getURL());
if ("xmpp".equals(uri.getScheme())) {
try { try {
Jid jid = new XmppUri(body.substring(xmppPatternMatcher.start(), xmppPatternMatcher.end())).getJid(); Jid jid = new XmppUri(uri).getJid();
if (activity.copyTextToClipboard(jid.asBareJid().toString(), R.string.account_settings_jabber_id)) { if (activity.copyTextToClipboard(jid.asBareJid().toString(), R.string.account_settings_jabber_id)) {
Toast.makeText(activity,R.string.jabber_id_copied_to_clipboard, Toast.LENGTH_SHORT).show(); Toast.makeText(activity,R.string.jabber_id_copied_to_clipboard, Toast.LENGTH_SHORT).show();
} }
@ -119,15 +124,13 @@ public class ShareUtil {
e.printStackTrace(); e.printStackTrace();
return; return;
} }
} } else {
Matcher webUrlPatternMatcher = Patterns.AUTOLINK_WEB_URL.matcher(body); if (activity.copyTextToClipboard(urlspan.getURL(),R.string.web_address)) {
if (webUrlPatternMatcher.find()) {
String url = body.substring(webUrlPatternMatcher.start(),webUrlPatternMatcher.end());
if (activity.copyTextToClipboard(url,R.string.web_address)) {
Toast.makeText(activity,R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show(); Toast.makeText(activity,R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
} }
} }
} }
}
public static boolean containsXmppUri(String body) { public static boolean containsXmppUri(String body) {
Matcher xmppPatternMatcher = Patterns.XMPP_PATTERN.matcher(body); Matcher xmppPatternMatcher = Patterns.XMPP_PATTERN.matcher(body);