Commit 605f0cc8 by zhangchengbo

fix:新增消息内容手机号检测并高亮显示

parent 2f3b994a
......@@ -6,6 +6,7 @@ import android.graphics.Typeface
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.util.Log
import android.util.Size
import android.util.TypedValue
import android.view.Menu
......@@ -35,10 +36,13 @@ import com.secspace.sms.helpers.*
import com.secspace.sms.models.Attachment
import com.secspace.sms.models.Message
import com.secspace.sms.models.ThreadItem
import com.secspace.sms.popup.TipPopup
import com.secspace.sms.util.SpannableStringUtil
import com.simplemobiletools.commons.adapters.MyRecyclerViewListAdapter
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.views.MyRecyclerView
import java.util.regex.Pattern
class ThreadAdapter(
activity: SimpleActivity,
......@@ -53,6 +57,28 @@ class ThreadAdapter(
private val hasMultipleSIMCards = (activity.subscriptionManagerCompat().activeSubscriptionInfoList?.size ?: 0) > 1
private val maxChatBubbleWidth = activity.usableScreenSize.x * 0.8f
private val tipsPop: TipPopup by lazy {
TipPopup(activity) {it: Int ,phoneNumber->
when(it){
1->{
activity.startCallIntent(phoneNumber)
}
2->{
activity.dialNumber(phoneNumber)
}
3->{
activity.launchSendSMSIntent(phoneNumber)
}
4->{
activity.copyToClipboard(phoneNumber)
}
5->{
activity.addNumberToContact(phoneNumber)
}
}
}
}
init {
setupDragListener(false)
setHasStableIds(true)
......@@ -153,7 +179,6 @@ class ThreadAdapter(
}
}
private fun isThreadDateTime(position: Int) = currentList.getOrNull(position) is ThreadItem.ThreadDateTime
fun updateMessages(newMessages: ArrayList<ThreadItem>, scrollPosition: Int = -1) {
......@@ -165,20 +190,43 @@ class ThreadAdapter(
}
}
private fun checkCellphone(bodyInfo: String): String {
if (bodyInfo.isEmpty()) return ""
// val str = "请关注公众号,开发工具为Android Studio 有问题请联系18698888888。"
// 将给定的正则表达式编译到模式中
val pattern = Pattern.compile("(\\d{11})")
// 创建匹配给定输入与此模式的匹配器。
val matcher = pattern.matcher(bodyInfo)
//查找字符串中是否有符合的子字符串
while (matcher.find()) {
//查找到符合的即输出
Log.d("TAG", "查询到一个符合的手机号码 = ${matcher.group()}")
return matcher.group()
}
return ""
}
private fun setupView(holder: ViewHolder, view: View, message: Message) {
ItemMessageBinding.bind(view).apply {
threadMessageHolder.isSelected = selectedKeys.contains(message.hashCode())
threadMessageBody.apply {
text = message.body
text = SpannableStringUtil.getPhoneNumberWithBody(message.body)
setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
beVisibleIf(message.body.isNotEmpty())
setOnLongClickListener {
false
}
/* setOnClickListener {
holder.viewClicked(message)
}*/
setOnClickListener {
holder.viewClicked(message)
val result = checkCellphone(message.body)
if (result.isNotEmpty()) {
tipsPop.setPhoneNumberData(result)
tipsPop.showPopupWindow()
}
}
}
if (message.isReceivedMessage()) {
......@@ -452,4 +500,5 @@ private class ThreadItemDiffCallback : DiffUtil.ItemCallback<ThreadItem>() {
is Message -> Message.areContentsTheSame(oldItem, newItem as Message)
}
}
}
package com.secspace.sms.util
import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import java.util.regex.Pattern
object SpannableStringUtil {
fun getPhoneNumberWithBody(strTel: String): SpannableString {
val ss = SpannableString(strTel)
val list = getNumbers(strTel)
if (list.isNotEmpty()) {
for (i in list.indices) {
ss.setSpan(
object : ClickableSpan() {
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.color = Color.BLUE //设置文件颜色
ds.isUnderlineText = true //设置下划线
}
override fun onClick(widget: View) {
}
},
strTel.indexOf(list[i]),
strTel.indexOf(list[i]) + 11,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
return ss
}
/**
* 将字符串中的电话号码设置点击事件和下划线
*
* @param context
* @param tv
* @param strTel
*/
fun setTelUrl(context: Context?, tv: TextView, strTel: String) {
getPhoneNumberWithBody(strTel)
tv.highlightColor = Color.TRANSPARENT //设置点击后的颜色为透明,否则会一直出现高亮
tv.text = getPhoneNumberWithBody(strTel)
tv.movementMethod = LinkMovementMethod.getInstance() //开始响应点击事件
}
/**
* 从字符串中查找电话号码字符串
*/
fun getNumbers(content: String?): List<String> {
val digitList: MutableList<String> = ArrayList()
val p = Pattern.compile("(\\d{11})")
val m = p.matcher(content)
while (m.find()) {
val find = m.group(1).toString()
digitList.add(find)
}
return digitList
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment