Merge branch 'main' into adb/issue-4049

This commit is contained in:
adb
2025-11-22 22:28:03 +01:00
committed by GitHub
@@ -11,6 +11,26 @@ import java.util.regex.Pattern;
public class Linkifier {
private static final Pattern CMD_PATTERN = Pattern.compile("(?<=^|\\s)/[a-zA-Z][a-zA-Z@\\d_/.-]{0,254}");
private static final Pattern PROXY_PATTERN = Pattern.compile("(?<=^|\\s)(SOCKS5|socks5|ss|SS):[^ \\n]+");
private static final Pattern PHONE_PATTERN
= Pattern.compile( // sdd = space, dot, or dash
"(?<=^|\\s|\\.|\\()" // no letter at start
+ "(\\+[0-9]+[\\- \\.]*)?" // +<digits><sdd>*
+ "(\\([0-9]+\\)[\\- \\.]*)?" // (<digits>)<sdd>*
+ "([0-9][0-9\\- \\.]{3,}[0-9])" // <digit><digit|sdd>+<digit> (5 characters min)
+ "(?=$|\\s|\\.|\\))"); // no letter at end
private static int brokenPhoneLinkifier = -1;
private static boolean internalPhoneLinkifierNeeded() {
if (brokenPhoneLinkifier == -1) { // unset
if(Linkify.addLinks(new SpannableString("a100b"), Linkify.PHONE_NUMBERS)) {
brokenPhoneLinkifier = 1; // true
} else {
brokenPhoneLinkifier = 0; // false
}
}
return brokenPhoneLinkifier == 1;
}
private static void replaceURLSpan(SpannableString messageBody) {
URLSpan[] urlSpans = messageBody.getSpans(0, messageBody.length(), URLSpan.class);
@@ -33,8 +53,18 @@ public class Linkifier {
replaceURLSpan(messageBody); // replace URLSpan so that it is not removed on the next addLinks() call
}
int flags;
if (internalPhoneLinkifierNeeded()) {
if (Linkify.addLinks(messageBody, PHONE_PATTERN, "tel:", Linkify.sPhoneNumberMatchFilter, Linkify.sPhoneNumberTransformFilter)) {
replaceURLSpan(messageBody); // replace URLSpan so that it is not removed on the next addLinks() call
}
flags = Linkify.EMAIL_ADDRESSES|Linkify.WEB_URLS;
} else {
flags = Linkify.EMAIL_ADDRESSES|Linkify.WEB_URLS|Linkify.PHONE_NUMBERS;
}
// linkyfiy urls etc., this removes all existing URLSpan
if (Linkify.addLinks(messageBody, Linkify.EMAIL_ADDRESSES|Linkify.WEB_URLS|Linkify.PHONE_NUMBERS)) {
if (Linkify.addLinks(messageBody, flags)) {
replaceURLSpan(messageBody);
}