mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Merge pull request #1601 from deltachat/tweak-linkify
use auto-linkify together with custom-linkify
This commit is contained in:
@@ -32,7 +32,6 @@ import android.text.style.URLSpan;
|
||||
import android.text.util.Linkify;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.TypedValue;
|
||||
import android.util.Patterns;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
@@ -67,7 +66,6 @@ import org.thoughtcrime.securesms.util.views.Stub;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -83,6 +81,7 @@ public class ConversationItem extends LinearLayout
|
||||
{
|
||||
private static final String TAG = ConversationItem.class.getSimpleName();
|
||||
|
||||
private static final Pattern CMD_PATTERN = Pattern.compile("(?<=^|\\s)/[a-zA-Z][a-zA-Z@\\d_/.-]{0,254}");
|
||||
private static final int MAX_MEASURE_CALLS = 3;
|
||||
|
||||
private DcMsg messageRecord;
|
||||
@@ -502,47 +501,32 @@ public class ConversationItem extends LinearLayout
|
||||
}
|
||||
}
|
||||
|
||||
private void replaceURLSpan(SpannableString messageBody) {
|
||||
URLSpan[] urlSpans = messageBody.getSpans(0, messageBody.length(), URLSpan.class);
|
||||
for (URLSpan urlSpan : urlSpans) {
|
||||
int start = messageBody.getSpanStart(urlSpan);
|
||||
int end = messageBody.getSpanEnd(urlSpan);
|
||||
// LongClickCopySpan must not be derived from URLSpan, otherwise links will be removed on the next addLinks() call
|
||||
messageBody.setSpan(new LongClickCopySpan(urlSpan.getURL(), this.dcChat.getId()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
}
|
||||
}
|
||||
|
||||
private SpannableString linkifyMessageBody(SpannableString messageBody, boolean shouldLinkifyAllLinks) {
|
||||
if (!shouldLinkifyAllLinks) {
|
||||
return messageBody;
|
||||
}
|
||||
boolean hasLinks = false;
|
||||
|
||||
Pattern cmdPattern = Pattern.compile("(?<=^|\\s)/[a-zA-Z][a-zA-Z@\\d_/.-]{0,254}");
|
||||
hasLinks = Linkify.addLinks(messageBody, cmdPattern, "cmd:", null, null) || hasLinks;
|
||||
|
||||
Linkify.TransformFilter urlFilter = (match, url) -> {
|
||||
if (url.startsWith("http://") || url.startsWith("https://")) {
|
||||
return url;
|
||||
} else {
|
||||
return "https://" + url;
|
||||
}
|
||||
};
|
||||
|
||||
Linkify.MatchFilter matcher = (s, start, end) -> {
|
||||
URLSpan[] urlSpans = messageBody.getSpans(start, end, URLSpan.class);
|
||||
for (URLSpan urlSpan : urlSpans) {
|
||||
int start2 = messageBody.getSpanStart(urlSpan);
|
||||
int end2 = messageBody.getSpanEnd(urlSpan);
|
||||
if ((start >= start2 && start < end2) || (end >= start2 && end < end2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
hasLinks = Linkify.addLinks(messageBody, Patterns.EMAIL_ADDRESS, "mailto:", matcher, null) || hasLinks;
|
||||
hasLinks = Linkify.addLinks(messageBody, Patterns.WEB_URL, "", matcher, urlFilter) || hasLinks;
|
||||
hasLinks = Linkify.addLinks(messageBody, Patterns.PHONE, "tel:", matcher, null) || hasLinks;
|
||||
|
||||
if (hasLinks) {
|
||||
URLSpan[] urlSpans = messageBody.getSpans(0, messageBody.length(), URLSpan.class);
|
||||
for (URLSpan urlSpan : urlSpans) {
|
||||
int start = messageBody.getSpanStart(urlSpan);
|
||||
int end = messageBody.getSpanEnd(urlSpan);
|
||||
messageBody.setSpan(new LongClickCopySpan(urlSpan.getURL(), this.dcChat.getId()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
}
|
||||
// linkify commands such as `/echo` -
|
||||
// do this first to avoid `/xkcd_123456` to be treated partly as a phone number
|
||||
if (Linkify.addLinks(messageBody, CMD_PATTERN, "cmd:", null, null)) {
|
||||
replaceURLSpan(messageBody); // replace URLSpan so that it is not removed on the next addLinks() call
|
||||
}
|
||||
|
||||
// linkyfiy urls etc., this removes all existing URLSpan
|
||||
if (Linkify.addLinks(messageBody, Linkify.EMAIL_ADDRESSES|Linkify.WEB_URLS|Linkify.PHONE_NUMBERS)) {
|
||||
replaceURLSpan(messageBody);
|
||||
}
|
||||
|
||||
return messageBody;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,19 +8,18 @@ import android.content.Intent;
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import android.text.TextPaint;
|
||||
import android.text.style.URLSpan;
|
||||
import android.text.style.ClickableSpan;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
|
||||
import org.thoughtcrime.securesms.ConversationActivity;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
|
||||
public class LongClickCopySpan extends URLSpan {
|
||||
public class LongClickCopySpan extends ClickableSpan {
|
||||
private static final String PREFIX_MAILTO = "mailto:";
|
||||
private static final String PREFIX_TEL = "tel:";
|
||||
private static final String PREFIX_CMD = "cmd:";
|
||||
@@ -28,10 +27,11 @@ public class LongClickCopySpan extends URLSpan {
|
||||
private boolean isHighlighted;
|
||||
@ColorInt
|
||||
private int highlightColor;
|
||||
private final String url;
|
||||
private int chatId;
|
||||
|
||||
public LongClickCopySpan(String url, int chatId) {
|
||||
super(url);
|
||||
this.url = url;
|
||||
this.chatId = chatId;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,6 @@ public class LongClickCopySpan extends URLSpan {
|
||||
|
||||
@Override
|
||||
public void onClick(View widget) {
|
||||
String url = getURL();
|
||||
if (url.startsWith(PREFIX_CMD)) {
|
||||
try {
|
||||
String cmd = url.substring(PREFIX_CMD.length());
|
||||
@@ -77,13 +76,12 @@ public class LongClickCopySpan extends URLSpan {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
super.onClick(widget);
|
||||
IntentUtils.showBrowserIntent(widget.getContext(), url);
|
||||
}
|
||||
}
|
||||
|
||||
void onLongClick(View widget) {
|
||||
Context context = widget.getContext();
|
||||
String url = getURL();
|
||||
|
||||
if (url.startsWith(PREFIX_CMD)) {
|
||||
copyUrl(context, url.substring(PREFIX_CMD.length()));
|
||||
|
||||
Reference in New Issue
Block a user