Commit ed0f930f by zhangchengbo

fix:过滤重复插入数据库Message

parent f19e250f
......@@ -13,12 +13,14 @@ import com.secspace.sms.extensions.*
import com.secspace.sms.helpers.refreshMessages
import com.secspace.sms.models.Conversation
import com.secspace.sms.models.Message
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.extensions.getLongValue
import com.simplemobiletools.commons.extensions.getMyContactsCursor
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.extensions.isNumberBlocked
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.PhoneNumber
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.commons.util.GsonUtil
class SmsSendObserver(private val context: Context, handler: Handler?) : ContentObserver(handler) {
private val TAG = "SmsObserver"
......@@ -30,29 +32,35 @@ class SmsSendObserver(private val context: Context, handler: Handler?) : Content
override fun onChange(selfChange: Boolean) {
super.onChange(selfChange)
Log.d(TAG, "SmsObserver: onChange ")
val cursor = context.contentResolver.query(
Uri.parse("content://sms/sent"),
SMS_PROJECTION,
" type=?",
arrayOf("2"),
// "date DESC LIMIT 1"
sortOrder
)
if (cursor == null) return
if (cursor.moveToFirst()) {
val body = cursor.getStringValue(Sms.BODY)
val address = cursor.getString(cursor.getColumnIndex("address")).toString()
val date = /*System.currentTimeMillis()*/ cursor.getString(cursor.getColumnIndex("date")).toLong()
val address = cursor.getStringValue(Sms.ADDRESS) //cursor.getString(cursor.getColumnIndex("address")).toString()
val date = cursor.getLongValue(Sms.DATE) //cursor.getString(cursor.getColumnIndex("date")).toLong()
val threadId = context.getThreadId(address)
com.secspace.log.Log.d(TAG, "获取当前日期 threadId = $threadId")
// val dateTime = cursor.getStringValue(Sms.DATE)
// Log.d(TAG, "SmsObserver: dateTime = $dateTime ")
// Log.d(TAG, "SmsObserver: address = $address threadIds = $threadId body = $body ")
ensureBackgroundThread {
val listContainsDate: List<Int> = context.messagesDB.getAll().map { it.date }.distinct()
val convDate = (date / 1000).toInt()
// com.secspace.log.Log.d(TAG, "获取当前日期 convDate = $convDate")
// com.secspace.log.Log.d(TAG, "获取数据库Date集合 isContains = ${listContainsDate.contains(convDate)} listContainsDate = $listContainsDate")
handleMessage(context, address, body, date, threadId, Telephony.Sms.MESSAGE_TYPE_SENT, 0, 0)
if (!listContainsDate.contains(convDate) || listContainsDate.isEmpty()) {
handleMessage(context, address, body, date, threadId, Telephony.Sms.MESSAGE_TYPE_SENT, 0, 0)
}
cursor.close()
}
}
cursor.close()
}
......@@ -71,15 +79,11 @@ class SmsSendObserver(private val context: Context, handler: Handler?) : Content
if (!context.isNumberBlocked(address)) {
val privateCursor = context.getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
ensureBackgroundThread {
val conversation: Conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread
// Log.d(TAG, "SmsObserver handleMessage conversation:${GsonUtil.parseBeanToJson(conversation)}")
try {
context.insertOrUpdateConversation(conversation)
} catch (ignored: Exception) {
}
val senderName = context.getNameFromAddress(address, privateCursor)
val phoneNumber = PhoneNumber(address, 0, "", address)
......@@ -93,7 +97,6 @@ class SmsSendObserver(private val context: Context, handler: Handler?) : Content
newMessageId = msgResult.last().id
newMessageId++
}
// Log.d(TAG, "SmsObserver handleMessage newMessageId:$newMessageId")
val message =
Message(
newMessageId,
......
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