Commit 402069be by zhangchengbo

fix:解决消息通知栏短信回复后点击信息进入信息详情页,手机号显示明文问题

parent 7e0cfcc7
......@@ -21,6 +21,7 @@ import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.google.gson.Gson
import com.secspace.sms.R
import com.secspace.sms.databases.MessagesDatabase
import com.secspace.sms.helpers.*
......@@ -35,9 +36,9 @@ import com.secspace.sms.messaging.SmsSender
import com.secspace.sms.models.*
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.BlockedNumber
import com.simplemobiletools.commons.models.PhoneNumber
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.commons.utils.PhoneUtilsCommon
import java.io.FileNotFoundException
val Context.config: Config get() = Config.newInstance(applicationContext)
......@@ -843,13 +844,13 @@ fun Context.getThreadId(addresses: Set<String>): Long {
}
}
fun Context.showReceivedMessageNotification(messageId: Long, address: String, body: String, threadId: Long, bitmap: Bitmap?) {
fun Context.showReceivedMessageNotification(messageId: Long, address: String, body: String, threadId: Long, bitmap: Bitmap?, normalNumber: String = "") {
val privateCursor = getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
ensureBackgroundThread {
val senderName = getNameFromAddress(address, privateCursor)
Handler(Looper.getMainLooper()).post {
notificationHelper.showMessageNotification(messageId, address, body, threadId, bitmap, senderName)
notificationHelper.showMessageNotification(messageId, address, body, threadId, bitmap, senderName, normalNumber = normalNumber)
}
}
}
......@@ -978,15 +979,22 @@ fun Context.deleteSmsDraft(threadId: Long) {
}
}
fun Context.updateLastConversationMessage(threadId: Long) {
fun Context.updateLastConversationMessage(threadId: Long, isNeedEnCode: Boolean = false) {
val uri = Threads.CONTENT_URI
val selection = "${Threads._ID} = ?"
val selectionArgs = arrayOf(threadId.toString())
try {
contentResolver.delete(uri, selection, selectionArgs)
val newConversation = getConversations(threadId)[0]
Log.d("TAG", "updateLastConversationMessage: isNeedEnCode = $isNeedEnCode")
if (isNeedEnCode) {
newConversation.isSFNumber = true
newConversation.title = PhoneUtilsCommon.phoneConvert(newConversation.title)
}
Log.d("TAG", "updateLastConversationMessage: newConversation = ${Gson().toJson(newConversation)}")
insertOrUpdateConversation(newConversation)
} catch (e: Exception) {
Log.d("TAG", "updateLastConversationMessage: Exception message= ${e.message}")
}
}
......
......@@ -43,10 +43,12 @@ class NotificationHelper(private val context: Context) {
threadId: Long,
bitmap: Bitmap?,
sender: String?,
alertOnlyOnce: Boolean = false
alertOnlyOnce: Boolean = false,
normalNumber :String = ""
) {
maybeCreateChannel(name = context.getString(R.string.channel_received_sms))
// Log.d("TAG", "showReceivedMessageNotification: messageId = $messageId address = $address body = $body threadId = $threadId sender = $sender normalNumber = $normalNumber")
val notificationId = threadId.hashCode()
val contentIntent = Intent(context, ThreadActivity::class.java).apply {
putExtra(THREAD_ID, threadId)
......@@ -78,7 +80,12 @@ class NotificationHelper(private val context: Context) {
val replyIntent = Intent(context, DirectReplyReceiver::class.java).apply {
putExtra(THREAD_ID, threadId)
putExtra(THREAD_NUMBER, address)
putExtra(THREAD_TITLE, address)
if (address.contains("*")) {
putExtra(THREAD_NUMBER, normalNumber)
}else{
putExtra(THREAD_NUMBER, address)
}
}
val replyPendingIntent =
......
......@@ -6,26 +6,37 @@ import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.core.app.RemoteInput
import com.secspace.sms.extensions.*
import com.secspace.sms.helpers.REPLY
import com.secspace.sms.helpers.THREAD_ID
import com.secspace.sms.helpers.THREAD_NUMBER
import com.secspace.sms.helpers.THREAD_TITLE
import com.secspace.sms.messaging.sendMessageCompat
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
class DirectReplyReceiver : BroadcastReceiver() {
private val TAG = DirectReplyReceiver::class.java.simpleName
@SuppressLint("MissingPermission")
override fun onReceive(context: Context, intent: Intent) {
val address = intent.getStringExtra(THREAD_NUMBER)
//正常/掩码手机号
val aesPhoneNumber = intent.getStringExtra(THREAD_TITLE) ?: ""
Log.d(TAG, "onReceive: aesPhoneNumber = $aesPhoneNumber")
var address = intent.getStringExtra(THREAD_NUMBER) ?: ""
Log.d(TAG, "onReceive: address = $address")
val threadId = intent.getLongExtra(THREAD_ID, 0L)
var body = RemoteInput.getResultsFromIntent(intent)?.getCharSequence(REPLY)?.toString() ?: return
body = context.removeDiacriticsIfNeeded(body)
if (address != null) {
if (address.isNotBlank()) {
if(address.contains("+86")){
address = address.replace("+86","")
}
var subscriptionId: Int? = null
val availableSIMs = context.subscriptionManagerCompat().activeSubscriptionInfoList
if ((availableSIMs?.size ?: 0) > 1) {
......@@ -41,11 +52,18 @@ class DirectReplyReceiver : BroadcastReceiver() {
try {
context.sendMessageCompat(body, listOf(address), subscriptionId, emptyList())
val message = context.getMessages(threadId, getImageResolutions = false, includeScheduledMessages = false, limit = 1).lastOrNull()
// Log.d(TAG, "onReceive: message = ${Gson().toJson(message)}")
if (message != null) {
context.messagesDB.insertOrUpdate(message)
messageId = message.id
context.updateLastConversationMessage(threadId)
Log.d(TAG, "onReceive: messageId = $messageId")
val containsResult = aesPhoneNumber.contains("*")
Log.d(TAG, "onReceive: 手机号不包含 [*] = $containsResult")
if (containsResult) {
context.updateLastConversationMessage(threadId, true)
} else {
context.updateLastConversationMessage(threadId, false)
}
}
} catch (e: Exception) {
context.showErrorToast(e)
......@@ -54,7 +72,12 @@ class DirectReplyReceiver : BroadcastReceiver() {
val photoUri = SimpleContactsHelper(context).getPhotoUriFromPhoneNumber(address)
val bitmap = context.getNotificationBitmap(photoUri)
Handler(Looper.getMainLooper()).post {
context.notificationHelper.showMessageNotification(messageId, address, body, threadId, bitmap, sender = null, alertOnlyOnce = true)
if (aesPhoneNumber.contains("*")) {
Log.d(TAG, "handleMessage: 掩码号码")
context.notificationHelper.showMessageNotification(messageId, aesPhoneNumber, body, threadId, bitmap, sender = null, alertOnlyOnce = true, normalNumber = address)
} else {
context.notificationHelper.showMessageNotification(messageId, address, body, threadId, bitmap, sender = null, alertOnlyOnce = true)
}
}
context.markThreadMessagesRead(threadId)
......
......@@ -27,12 +27,10 @@ import com.secspace.sms.util.PhoneFromUtil
import com.secspace.sms.util.PhoneUtils
import com.secspace.sms.util.SmsCountUtil
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.PhoneNumber
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.commons.models.contacts.Contact
import com.simplemobiletools.commons.util.GsonUtil
/**
......@@ -105,7 +103,7 @@ class SmsReceiver : BroadcastReceiver() {
* 2.未找到再从 通话记录中查找
*/
private fun findConversationWithPhoneNumber(context: Context, phoneNumber: String, body: String, threadId: Long, confirmCallBack: (String) -> Unit) {
var lastNumber = ""
var lastNumberOrName = ""
Log.d(TAG, "SmsReceiver phoneNumber:${phoneNumber} body = $body threadId = $threadId")
val hasPerMissionResult = context.hasPermission(PERMISSION_READ_CALL_LOG)
if (hasPerMissionResult) {
......@@ -116,13 +114,13 @@ class SmsReceiver : BroadcastReceiver() {
val listContains: List<Message> = listMessageData.filter { msg -> msg.body.contains("\u200B") }
if (listContains.isNotEmpty()) {
PhoneUtils.phoneNumberFormat(phoneNumber) { itNumber ->
lastNumber = itNumber
lastNumberOrName = itNumber
}
}
}
//2
if (lastNumber.isEmpty()) {
if (lastNumberOrName.isEmpty()) {
RecentsHelper(context = context).getRecentCalls(false, Int.MAX_VALUE) { recents: List<RecentCall> ->
val listCallRecent: List<RecentCall> = recents.distinctBy { it.phoneNumber }
Log.d(TAG, "SmsReceiver最近通话 recents:${GsonUtil.parseListToJson(listCallRecent)}")
......@@ -136,22 +134,38 @@ class SmsReceiver : BroadcastReceiver() {
AES.decrypt(it.phoneNumber) == phoneNumber
}
}
Log.d(TAG, "SmsReceiverfindResult:${findResult} phoneNumber = ${phoneNumber.trim()}")
Log.d(TAG, "SmsReceiver findResult:${findResult} phoneNumber = ${phoneNumber.trim()}")
//从最近通话记录中找到了号码 进行掩码操作
if (findResult > 0) {
PhoneUtils.phoneNumberFormat(phoneNumber) { itNumber ->
lastNumber = itNumber
lastNumberOrName = itNumber
}
ContactsHelper(mContent).getContacts(showOnlyContactsWithNumbers = true) { contacts ->
if (contacts.isEmpty())return@getContacts
Log.d(TAG, "SmsReceiver contacts = ${Gson().toJson(contacts)}")
val listPhoneNumbers = contacts.flatMap { it.phoneNumbers }.map { it.normalizedNumber.ifBlank { it.value } }
Log.d(TAG, "SmsReceiver listPhoneNumbers = ${Gson().toJson(listPhoneNumbers)}")
if (!listPhoneNumbers.contains(phoneNumber)) return@getContacts
listPhoneNumbers.forEachIndexed { index, number ->
if (number == phoneNumber) {
lastNumberOrName = contacts[index].name
Log.d(TAG, "SmsReceiver lastNumberOrName = $lastNumberOrName 通讯录名称是 = ${contacts[index].name}")
return@forEachIndexed
}
}
}
}
}
}
}
Log.d(TAG, "SmsReceiver lastNumber = $lastNumber")
Log.d(TAG, "SmsReceiver lastNumberOrName = $lastNumberOrName")
}
if (lastNumber == "") {
if (lastNumberOrName == "") {
confirmCallBack.invoke(phoneNumber)
} else {
confirmCallBack.invoke(lastNumber)
confirmCallBack.invoke(lastNumberOrName)
}
}
......
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