use of org.telegram.ui.Components.URLSpanReplacement in project Telegram-FOSS by Telegram-FOSS-Team.
the class ChatActivity method applyDraftMaybe.
private void applyDraftMaybe(boolean canClear) {
if (chatActivityEnterView == null || chatMode != 0) {
return;
}
TLRPC.DraftMessage draftMessage = getMediaDataController().getDraft(dialog_id, threadMessageId);
TLRPC.Message draftReplyMessage = draftMessage != null && draftMessage.reply_to_msg_id != 0 ? getMediaDataController().getDraftMessage(dialog_id, threadMessageId) : null;
if (chatActivityEnterView.getFieldText() == null) {
if (draftMessage != null) {
chatActivityEnterView.setWebPage(null, !draftMessage.no_webpage);
CharSequence message;
if (!draftMessage.entities.isEmpty()) {
SpannableStringBuilder stringBuilder = SpannableStringBuilder.valueOf(draftMessage.message);
MediaDataController.sortEntities(draftMessage.entities);
for (int a = 0; a < draftMessage.entities.size(); a++) {
TLRPC.MessageEntity entity = draftMessage.entities.get(a);
if (entity instanceof TLRPC.TL_inputMessageEntityMentionName || entity instanceof TLRPC.TL_messageEntityMentionName) {
long user_id;
if (entity instanceof TLRPC.TL_inputMessageEntityMentionName) {
user_id = ((TLRPC.TL_inputMessageEntityMentionName) entity).user_id.user_id;
} else {
user_id = ((TLRPC.TL_messageEntityMentionName) entity).user_id;
}
if (entity.offset + entity.length < stringBuilder.length() && stringBuilder.charAt(entity.offset + entity.length) == ' ') {
entity.length++;
}
stringBuilder.setSpan(new URLSpanUserMention("" + user_id, 3), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (entity instanceof TLRPC.TL_messageEntityCode || entity instanceof TLRPC.TL_messageEntityPre) {
TextStyleSpan.TextStyleRun run = new TextStyleSpan.TextStyleRun();
run.flags |= TextStyleSpan.FLAG_STYLE_MONO;
MediaDataController.addStyleToText(new TextStyleSpan(run), entity.offset, entity.offset + entity.length, stringBuilder, true);
} else if (entity instanceof TLRPC.TL_messageEntityBold) {
TextStyleSpan.TextStyleRun run = new TextStyleSpan.TextStyleRun();
run.flags |= TextStyleSpan.FLAG_STYLE_BOLD;
MediaDataController.addStyleToText(new TextStyleSpan(run), entity.offset, entity.offset + entity.length, stringBuilder, true);
} else if (entity instanceof TLRPC.TL_messageEntityItalic) {
TextStyleSpan.TextStyleRun run = new TextStyleSpan.TextStyleRun();
run.flags |= TextStyleSpan.FLAG_STYLE_ITALIC;
MediaDataController.addStyleToText(new TextStyleSpan(run), entity.offset, entity.offset + entity.length, stringBuilder, true);
} else if (entity instanceof TLRPC.TL_messageEntityStrike) {
TextStyleSpan.TextStyleRun run = new TextStyleSpan.TextStyleRun();
run.flags |= TextStyleSpan.FLAG_STYLE_STRIKE;
MediaDataController.addStyleToText(new TextStyleSpan(run), entity.offset, entity.offset + entity.length, stringBuilder, true);
} else if (entity instanceof TLRPC.TL_messageEntityUnderline) {
TextStyleSpan.TextStyleRun run = new TextStyleSpan.TextStyleRun();
run.flags |= TextStyleSpan.FLAG_STYLE_UNDERLINE;
MediaDataController.addStyleToText(new TextStyleSpan(run), entity.offset, entity.offset + entity.length, stringBuilder, true);
} else if (entity instanceof TLRPC.TL_messageEntityTextUrl) {
stringBuilder.setSpan(new URLSpanReplacement(entity.url), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (entity instanceof TLRPC.TL_messageEntitySpoiler) {
TextStyleSpan.TextStyleRun run = new TextStyleSpan.TextStyleRun();
run.flags |= TextStyleSpan.FLAG_STYLE_SPOILER;
MediaDataController.addStyleToText(new TextStyleSpan(run), entity.offset, entity.offset + entity.length, stringBuilder, true);
}
}
message = stringBuilder;
} else {
message = draftMessage.message;
}
chatActivityEnterView.setFieldText(message);
if (getArguments().getBoolean("hasUrl", false)) {
chatActivityEnterView.setSelection(draftMessage.message.indexOf('\n') + 1);
AndroidUtilities.runOnUIThread(() -> {
if (chatActivityEnterView != null) {
chatActivityEnterView.setFieldFocused(true);
chatActivityEnterView.openKeyboard();
}
}, 700);
}
}
} else if (canClear && draftMessage == null) {
chatActivityEnterView.setFieldText("");
hideFieldPanel(true);
}
if (replyingMessageObject == null && draftReplyMessage != null) {
replyingMessageObject = new MessageObject(currentAccount, draftReplyMessage, getMessagesController().getUsers(), false, false);
showFieldPanelForReply(replyingMessageObject);
}
}
use of org.telegram.ui.Components.URLSpanReplacement in project Telegram-FOSS by Telegram-FOSS-Team.
the class ChatActivity method searchLinks.
private void searchLinks(final CharSequence charSequence, final boolean force) {
if (currentEncryptedChat != null && getMessagesController().secretWebpagePreview == 0 || editingMessageObject != null && !editingMessageObject.isWebpage()) {
return;
}
if (force && foundWebPage != null) {
if (foundWebPage.url != null) {
int index = TextUtils.indexOf(charSequence, foundWebPage.url);
char lastChar = 0;
boolean lenEqual = false;
if (index == -1) {
if (foundWebPage.display_url != null) {
index = TextUtils.indexOf(charSequence, foundWebPage.display_url);
lenEqual = index != -1 && index + foundWebPage.display_url.length() == charSequence.length();
lastChar = index != -1 && !lenEqual ? charSequence.charAt(index + foundWebPage.display_url.length()) : 0;
}
} else {
lenEqual = index + foundWebPage.url.length() == charSequence.length();
lastChar = !lenEqual ? charSequence.charAt(index + foundWebPage.url.length()) : 0;
}
if (index != -1 && (lenEqual || lastChar == ' ' || lastChar == ',' || lastChar == '.' || lastChar == '!' || lastChar == '/')) {
return;
}
}
pendingLinkSearchString = null;
foundUrls = null;
showFieldPanelForWebPage(false, foundWebPage, false);
}
final MessagesController messagesController = getMessagesController();
Utilities.searchQueue.postRunnable(() -> {
if (linkSearchRequestId != 0) {
getConnectionsManager().cancelRequest(linkSearchRequestId, true);
linkSearchRequestId = 0;
}
ArrayList<CharSequence> urls = null;
CharSequence textToCheck;
try {
Matcher m = AndroidUtilities.WEB_URL.matcher(charSequence);
while (m.find()) {
if (m.start() > 0) {
if (charSequence.charAt(m.start() - 1) == '@') {
continue;
}
}
if (urls == null) {
urls = new ArrayList<>();
}
urls.add(charSequence.subSequence(m.start(), m.end()));
}
if (charSequence instanceof Spannable) {
URLSpanReplacement[] spans = ((Spannable) charSequence).getSpans(0, charSequence.length(), URLSpanReplacement.class);
if (spans != null && spans.length > 0) {
if (urls == null) {
urls = new ArrayList<>();
}
for (int a = 0; a < spans.length; a++) {
urls.add(spans[a].getURL());
}
}
}
if (urls != null && foundUrls != null && urls.size() == foundUrls.size()) {
boolean clear = true;
for (int a = 0; a < urls.size(); a++) {
if (!TextUtils.equals(urls.get(a), foundUrls.get(a))) {
clear = false;
}
}
if (clear) {
return;
}
}
foundUrls = urls;
if (urls == null) {
AndroidUtilities.runOnUIThread(() -> {
if (foundWebPage != null) {
showFieldPanelForWebPage(false, foundWebPage, false);
foundWebPage = null;
}
});
return;
}
textToCheck = TextUtils.join(" ", urls);
} catch (Exception e) {
FileLog.e(e);
String text = charSequence.toString().toLowerCase();
if (charSequence.length() < 13 || !text.contains("http://") && !text.contains("https://")) {
AndroidUtilities.runOnUIThread(() -> {
if (foundWebPage != null) {
showFieldPanelForWebPage(false, foundWebPage, false);
foundWebPage = null;
}
});
return;
}
textToCheck = charSequence;
}
if (currentEncryptedChat != null && messagesController.secretWebpagePreview == 2) {
AndroidUtilities.runOnUIThread(() -> {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity(), themeDelegate);
builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), (dialog, which) -> {
messagesController.secretWebpagePreview = 1;
MessagesController.getGlobalMainSettings().edit().putInt("secretWebpage2", getMessagesController().secretWebpagePreview).commit();
foundUrls = null;
searchLinks(charSequence, force);
});
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
builder.setMessage(LocaleController.getString("SecretLinkPreviewAlert", R.string.SecretLinkPreviewAlert));
showDialog(builder.create());
messagesController.secretWebpagePreview = 0;
MessagesController.getGlobalMainSettings().edit().putInt("secretWebpage2", messagesController.secretWebpagePreview).commit();
});
return;
}
final TLRPC.TL_messages_getWebPagePreview req = new TLRPC.TL_messages_getWebPagePreview();
if (textToCheck instanceof String) {
req.message = (String) textToCheck;
} else {
req.message = textToCheck.toString();
}
linkSearchRequestId = getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
linkSearchRequestId = 0;
if (error == null) {
if (response instanceof TLRPC.TL_messageMediaWebPage) {
foundWebPage = ((TLRPC.TL_messageMediaWebPage) response).webpage;
if (foundWebPage instanceof TLRPC.TL_webPage || foundWebPage instanceof TLRPC.TL_webPagePending) {
if (foundWebPage instanceof TLRPC.TL_webPagePending) {
pendingLinkSearchString = req.message;
}
if (currentEncryptedChat != null && foundWebPage instanceof TLRPC.TL_webPagePending) {
foundWebPage.url = req.message;
}
showFieldPanelForWebPage(true, foundWebPage, false);
} else {
if (foundWebPage != null) {
showFieldPanelForWebPage(false, foundWebPage, false);
foundWebPage = null;
}
}
} else {
if (foundWebPage != null) {
showFieldPanelForWebPage(false, foundWebPage, false);
foundWebPage = null;
}
}
}
}));
getConnectionsManager().bindRequestToGuid(linkSearchRequestId, classGuid);
});
}
Aggregations