Improved code responsible of making links clickable in chat messages

This commit is contained in:
Sylvain Berfini 2021-01-27 10:41:08 +01:00
parent 53683f6fa6
commit 947c6faa53
2 changed files with 8 additions and 50 deletions

View file

@ -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<String>()
val text: Spanned? by lazy {
val textContent = chatMessage.textContent
if (textContent != null) AppUtils.getTextWithHttpLinks(textContent) else null
}
val text = MutableLiveData<Spannable>()
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

View file

@ -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("<", "&lt;")
}
if (text.contains(">")) {
text = text.replace(">", "&gt;")
}
if (text.contains("\n")) {
text = text.replace("\n", "<br>")
}
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(),
"<a href=\"$link\">$linkWithoutScheme</a>"
)
}
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(),
"<a href=\"$link\">$linkWithoutScheme</a>"
)
}
return HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY)
}
fun getInitials(displayName: String, limit: Int = 2): String {
if (displayName.isEmpty()) return ""