From 947c6faa538beb0650647982bb22e32091298d63 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 27 Jan 2021 10:41:08 +0100 Subject: [PATCH] Improved code responsible of making links clickable in chat messages --- .../chat/viewmodels/ChatMessageViewModel.kt | 13 +++--- .../main/java/org/linphone/utils/AppUtils.kt | 45 ------------------- 2 files changed, 8 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/org/linphone/activities/main/chat/viewmodels/ChatMessageViewModel.kt b/app/src/main/java/org/linphone/activities/main/chat/viewmodels/ChatMessageViewModel.kt index d67ebc72a..8524d9f3e 100644 --- a/app/src/main/java/org/linphone/activities/main/chat/viewmodels/ChatMessageViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/chat/viewmodels/ChatMessageViewModel.kt @@ -20,7 +20,9 @@ package org.linphone.activities.main.chat.viewmodels import android.os.CountDownTimer -import android.text.Spanned +import android.text.Spannable +import android.text.util.Linkify +import androidx.core.text.util.LinkifyCompat import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import kotlinx.coroutines.launch @@ -62,10 +64,7 @@ class ChatMessageViewModel( val ephemeralLifetime = MutableLiveData() - val text: Spanned? by lazy { - val textContent = chatMessage.textContent - if (textContent != null) AppUtils.getTextWithHttpLinks(textContent) else null - } + val text = MutableLiveData() private var countDownTimer: CountDownTimer? = null @@ -171,6 +170,10 @@ class ChatMessageViewModel( for (content in chatMessage.contents) { if (content.isFileTransfer || content.isFile) { list.add(ChatMessageContentViewModel(content, chatMessage, contentListener)) + } else if (content.isText) { + val spannable = Spannable.Factory.getInstance().newSpannable(content.utf8Text) + LinkifyCompat.addLinks(spannable, Linkify.WEB_URLS) + text.value = spannable } } contents.value = list diff --git a/app/src/main/java/org/linphone/utils/AppUtils.kt b/app/src/main/java/org/linphone/utils/AppUtils.kt index 8cd70e1aa..f646ab75a 100644 --- a/app/src/main/java/org/linphone/utils/AppUtils.kt +++ b/app/src/main/java/org/linphone/utils/AppUtils.kt @@ -21,12 +21,9 @@ package org.linphone.utils import android.app.Activity import android.content.* -import android.text.Spanned import android.util.TypedValue -import androidx.core.text.HtmlCompat import androidx.emoji.text.EmojiCompat import java.util.* -import java.util.regex.Pattern import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.R import org.linphone.core.tools.Log @@ -52,48 +49,6 @@ class AppUtils { return coreContext.context.resources.getDimension(id) } - fun getTextWithHttpLinks(input: String): Spanned { - var text = input - if (text.contains("<")) { - text = text.replace("<", "<") - } - if (text.contains(">")) { - text = text.replace(">", ">") - } - if (text.contains("\n")) { - text = text.replace("\n", "
") - } - if (text.contains("http://")) { - val indexHttp = text.indexOf("http://") - val indexFinHttp = - if (text.indexOf(" ", indexHttp) == -1) text.length else text.indexOf( - " ", - indexHttp - ) - val link = text.substring(indexHttp, indexFinHttp) - val linkWithoutScheme = link.replace("http://", "") - text = text.replaceFirst( - Pattern.quote(link).toRegex(), - "$linkWithoutScheme" - ) - } - if (text.contains("https://")) { - val indexHttp = text.indexOf("https://") - val indexFinHttp = - if (text.indexOf(" ", indexHttp) == -1) text.length else text.indexOf( - " ", - indexHttp - ) - val link = text.substring(indexHttp, indexFinHttp) - val linkWithoutScheme = link.replace("https://", "") - text = text.replaceFirst( - Pattern.quote(link).toRegex(), - "$linkWithoutScheme" - ) - } - return HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY) - } - fun getInitials(displayName: String, limit: Int = 2): String { if (displayName.isEmpty()) return ""