use of org.telegram.ui.Components.URLSpanBotCommand in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessageObject method addUrlsByPattern.
public static void addUrlsByPattern(boolean isOut, CharSequence charSequence, boolean botCommands, int patternType, int duration, boolean check) {
try {
Matcher matcher;
if (patternType == 3 || patternType == 4) {
if (videoTimeUrlPattern == null) {
videoTimeUrlPattern = Pattern.compile("\\b(?:(\\d{1,2}):)?(\\d{1,3}):([0-5][0-9])\\b");
}
matcher = videoTimeUrlPattern.matcher(charSequence);
} else if (patternType == 1) {
if (instagramUrlPattern == null) {
instagramUrlPattern = Pattern.compile("(^|\\s|\\()@[a-zA-Z\\d_.]{1,32}|(^|\\s|\\()#[\\w.]+");
}
matcher = instagramUrlPattern.matcher(charSequence);
} else {
if (urlPattern == null) {
urlPattern = Pattern.compile("(^|\\s)/[a-zA-Z@\\d_]{1,255}|(^|\\s|\\()@[a-zA-Z\\d_]{1,32}|(^|\\s|\\()#[^0-9][\\w.]+|(^|\\s)\\$[A-Z]{3,8}([ ,.]|$)");
}
matcher = urlPattern.matcher(charSequence);
}
Spannable spannable = (Spannable) charSequence;
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
URLSpanNoUnderline url = null;
if (patternType == 3 || patternType == 4) {
URLSpan[] spans = spannable.getSpans(start, end, URLSpan.class);
if (spans != null && spans.length > 0) {
continue;
}
int count = matcher.groupCount();
int s1 = matcher.start(1);
int e1 = matcher.end(1);
int s2 = matcher.start(2);
int e2 = matcher.end(2);
int s3 = matcher.start(3);
int e3 = matcher.end(3);
int minutes = Utilities.parseInt(charSequence.subSequence(s2, e2));
int seconds = Utilities.parseInt(charSequence.subSequence(s3, e3));
int hours = s1 >= 0 && e1 >= 0 ? Utilities.parseInt(charSequence.subSequence(s1, e1)) : -1;
seconds += minutes * 60;
if (hours > 0) {
seconds += hours * 60 * 60;
}
if (seconds > duration) {
continue;
}
if (patternType == 3) {
url = new URLSpanNoUnderline("video?" + seconds);
} else {
url = new URLSpanNoUnderline("audio?" + seconds);
}
} else {
char ch = charSequence.charAt(start);
if (patternType != 0) {
if (ch != '@' && ch != '#') {
start++;
}
ch = charSequence.charAt(start);
if (ch != '@' && ch != '#') {
continue;
}
} else {
if (ch != '@' && ch != '#' && ch != '/' && ch != '$') {
start++;
}
}
if (patternType == 1) {
if (ch == '@') {
url = new URLSpanNoUnderline("https://instagram.com/" + charSequence.subSequence(start + 1, end).toString());
} else {
url = new URLSpanNoUnderline("https://www.instagram.com/explore/tags/" + charSequence.subSequence(start + 1, end).toString());
}
} else if (patternType == 2) {
if (ch == '@') {
url = new URLSpanNoUnderline("https://twitter.com/" + charSequence.subSequence(start + 1, end).toString());
} else {
url = new URLSpanNoUnderline("https://twitter.com/hashtag/" + charSequence.subSequence(start + 1, end).toString());
}
} else {
if (charSequence.charAt(start) == '/') {
if (botCommands) {
url = new URLSpanBotCommand(charSequence.subSequence(start, end).toString(), isOut ? 1 : 0);
}
} else {
url = new URLSpanNoUnderline(charSequence.subSequence(start, end).toString());
}
}
}
if (url != null) {
if (check) {
ClickableSpan[] spans = spannable.getSpans(start, end, ClickableSpan.class);
if (spans != null && spans.length > 0) {
spannable.removeSpan(spans[0]);
}
}
spannable.setSpan(url, start, end, 0);
}
}
} catch (Exception e) {
FileLog.e(e);
}
}
Aggregations