Updated bubbles color + added call on click in SIP URI in chat

This commit is contained in:
Sylvain Berfini 2023-10-31 10:30:41 +01:00
parent a919f5edbc
commit 5e069033b3
13 changed files with 125 additions and 28 deletions

View file

@ -308,7 +308,7 @@ class ConversationFragment : GenericFragment() {
layout.setCopyClickListener {
Log.i("$TAG Copying message text into clipboard")
val text = chatMessageModel.text
val text = chatMessageModel.text.value?.toString()
val clipboard = requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val label = "Message"
clipboard.setPrimaryClip(ClipData.newPlainText(label, text))

View file

@ -19,9 +19,11 @@
*/
package org.linphone.ui.main.chat.model
import android.text.Spannable
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.lifecycle.MutableLiveData
import java.util.regex.Pattern
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.core.Address
import org.linphone.core.ChatMessage
@ -31,6 +33,7 @@ import org.linphone.core.tools.Log
import org.linphone.ui.main.contacts.model.ContactAvatarModel
import org.linphone.utils.Event
import org.linphone.utils.LinphoneUtils
import org.linphone.utils.PatternClickableSpan
import org.linphone.utils.TimestampUtils
class ChatMessageModel @WorkerThread constructor(
@ -53,7 +56,7 @@ class ChatMessageModel @WorkerThread constructor(
val statusIcon = MutableLiveData<Int>()
val text = LinphoneUtils.getTextDescribingMessage(chatMessage)
val text = MutableLiveData<Spannable>()
val timestamp = chatMessage.time
@ -92,6 +95,42 @@ class ChatMessageModel @WorkerThread constructor(
chatMessage.addListener(chatMessageListener)
statusIcon.postValue(LinphoneUtils.getChatIconResId(chatMessage.state))
updateReactionsList()
var textFound = false
for (content in chatMessage.contents) {
if (content.isText) {
val textContent = content.utf8Text.orEmpty().trim()
val spannable = Spannable.Factory.getInstance().newSpannable(textContent)
text.postValue(
PatternClickableSpan()
.add(
Pattern.compile(
"(?:<?sips?:)[a-zA-Z0-9+_.\\-]+(?:@([a-zA-Z0-9+_.\\-;=~]+))+(>)?"
),
object : PatternClickableSpan.SpannableClickedListener {
@UiThread
override fun onSpanClicked(text: String) {
coreContext.postOnCoreThread {
Log.i("$TAG Clicked on SIP URI: $text")
val address = coreContext.core.interpretUrl(text)
if (address != null) {
coreContext.startCall(address)
} else {
Log.w("$TAG Failed to parse [$text] as SIP URI")
}
}
}
}
).build(spannable)
)
textFound = true
}
}
if (!textFound) {
val describe = LinphoneUtils.getTextDescribingMessage(chatMessage)
val spannable = Spannable.Factory.getInstance().newSpannable(describe)
text.postValue(spannable)
}
}
@WorkerThread

View file

@ -23,9 +23,10 @@ import android.content.Context
import android.text.method.LinkMovementMethod
import android.util.AttributeSet
import android.view.View.MeasureSpec.*
import androidx.annotation.UiThread
import androidx.appcompat.widget.AppCompatTextView
import kotlin.math.ceil
@UiThread
class ChatBubbleTextView : AppCompatTextView {
constructor(context: Context) : super(context)
@ -42,20 +43,4 @@ class ChatBubbleTextView : AppCompatTextView {
// Required for PatternClickableSpan
movementMethod = LinkMovementMethod.getInstance()
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val lines = layout.lineCount
if (lines > 1 && getMode(widthMeasureSpec) != EXACTLY) {
val textWidth = (0 until lines).maxOf(layout::getLineWidth)
val padding = compoundPaddingLeft + compoundPaddingRight
val w = ceil(textWidth).toInt() + padding
if (w < measuredWidth) {
val newWidthMeasureSpec = makeMeasureSpec(w, AT_MOST)
super.onMeasure(newWidthMeasureSpec, heightMeasureSpec)
}
}
}
}

View file

@ -0,0 +1,73 @@
/*
* Copyright (c) 2010-2022 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.utils
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import java.util.regex.Pattern
class PatternClickableSpan {
private var patterns: ArrayList<SpannablePatternItem> = ArrayList()
inner class SpannablePatternItem(
var pattern: Pattern,
var listener: SpannableClickedListener
)
interface SpannableClickedListener {
fun onSpanClicked(text: String)
}
inner class StyledClickableSpan(var item: SpannablePatternItem) : ClickableSpan() {
override fun onClick(widget: View) {
val tv = widget as TextView
val span = tv.text as Spanned
val start = span.getSpanStart(this)
val end = span.getSpanEnd(this)
val text = span.subSequence(start, end)
item.listener.onSpanClicked(text.toString())
}
}
fun add(
pattern: Pattern,
listener: SpannableClickedListener
): PatternClickableSpan {
patterns.add(SpannablePatternItem(pattern, listener))
return this
}
fun build(editable: CharSequence?): SpannableStringBuilder {
val ssb = SpannableStringBuilder(editable)
for (item in patterns) {
val matcher = item.pattern.matcher(ssb)
while (matcher.find()) {
val start = matcher.start()
val end = matcher.end()
val url = StyledClickableSpan(item)
ssb.setSpan(url, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
return ssb
}
}

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:topRightRadius="16dp" android:bottomRightRadius="16dp" android:bottomLeftRadius="16dp" />
<solid android:color="@color/gray_100"/>
<solid android:color="@color/gray_main2_100"/>
</shape>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="16dp" />
<solid android:color="@color/gray_100"/>
<solid android:color="@color/gray_main2_100"/>
</shape>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:topRightRadius="16dp" android:topLeftRadius="16dp" />
<solid android:color="@color/gray_200"/>
<solid android:color="@color/gray_main2_100"/>
</shape>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:topLeftRadius="16dp" android:topRightRadius="16dp" android:bottomLeftRadius="16dp" />
<solid android:color="@color/gray_main2_100"/>
<solid android:color="@color/orange_main_100"/>
</shape>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="16dp" />
<solid android:color="@color/gray_main2_100"/>
<solid android:color="@color/orange_main_100"/>
</shape>

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="@color/gray_100"/>
<solid android:color="@color/gray_main2_100"/>
<stroke android:color="@color/white" android:width="2dp" />
</shape>

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="@color/gray_main2_100"/>
<solid android:color="@color/orange_main_100"/>
<stroke android:color="@color/white" android:width="2dp" />
</shape>

View file

@ -121,7 +121,7 @@
app:layout_constraintTop_toBottomOf="@id/reply"
app:layout_constraintBottom_toBottomOf="@id/date_time"/>
<androidx.appcompat.widget.AppCompatTextView
<org.linphone.ui.main.chat.view.ChatBubbleTextView
style="@style/default_text_style"
android:id="@+id/text_message"
android:layout_width="wrap_content"

View file

@ -52,7 +52,7 @@
app:layout_constraintEnd_toEndOf="@id/reply"
app:layout_constraintBottom_toBottomOf="@id/background" />
<androidx.appcompat.widget.AppCompatTextView
<org.linphone.ui.main.chat.view.ChatBubbleTextView
style="@style/default_text_style"
android:onClick="@{scrollToRepliedMessageClickListener}"
android:id="@+id/reply"