Commit 8c3c5511 by zhangchengbo

fix:1.获取最新短信 与通话记录中对比数据进行掩码操作 2.更新列表最新一条的数据

parent 1d680f4e
...@@ -305,7 +305,7 @@ class MainActivity : SimpleActivity() { ...@@ -305,7 +305,7 @@ class MainActivity : SimpleActivity() {
updateUnreadCountBadge(conversations) updateUnreadCountBadge(conversations)
// runOnUiThread { // runOnUiThread {
// setupConversations(conversations, cached = true) // setupConversations(conversations, cached = true)
getNewConversations((conversations + archived).toMutableList() as ArrayList<Conversation>) getNewConversations((conversations + archived).toMutableList() as ArrayList<Conversation>)
// } // }
conversations.forEach { conversations.forEach {
clearExpiredScheduledMessages(it.threadId) clearExpiredScheduledMessages(it.threadId)
...@@ -316,74 +316,74 @@ class MainActivity : SimpleActivity() { ...@@ -316,74 +316,74 @@ class MainActivity : SimpleActivity() {
private fun getNewConversations(cachedConversations: ArrayList<Conversation>) { private fun getNewConversations(cachedConversations: ArrayList<Conversation>) {
val privateCursor = getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true) val privateCursor = getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
val privateContacts = MyContactsContentProvider.getSimpleContacts(this@MainActivity, privateCursor) val privateContacts = MyContactsContentProvider.getSimpleContacts(this@MainActivity, privateCursor)
// Log.e(TAG, " getNewConversations privateContacts:${GsonUtil.parseListToJson(privateContacts)}") // Log.e(TAG, " getNewConversations privateContacts:${GsonUtil.parseListToJson(privateContacts)}")
//获取系统SMS DB短信列表 //获取系统SMS DB短信列表
val conversations: java.util.ArrayList<Conversation> = getConversations(privateContacts = privateContacts) val conversations: java.util.ArrayList<Conversation> = getConversations(privateContacts = privateContacts)
// Log.e(TAG, " getNewConversations 获取系统SMS DB短信列表(脏数据) conversations:${GsonUtil.parseListToJson(conversations)}") // Log.e(TAG, " getNewConversations 获取系统SMS DB短信列表(脏数据) conversations:${GsonUtil.parseListToJson(conversations)}")
//通过DB数据与cache对比threadID //通过DB数据与cache对比threadID
conversations.forEach { clonedConversation -> conversations.forEach { clonedConversation ->
val threadIds = cachedConversations.map { it.threadId } val threadIds = cachedConversations.map { it.threadId }
if (!threadIds.contains(clonedConversation.threadId)) { if (!threadIds.contains(clonedConversation.threadId)) {
conversationsDB.insertOrUpdate(clonedConversation) conversationsDB.insertOrUpdate(clonedConversation)
cachedConversations.add(clonedConversation) cachedConversations.add(clonedConversation)
}
} }
}
cachedConversations.forEach { cachedConversation -> cachedConversations.forEach { cachedConversation ->
val threadId = cachedConversation.threadId val threadId = cachedConversation.threadId
val isTemporaryThread = cachedConversation.isScheduled val isTemporaryThread = cachedConversation.isScheduled
val isConversationDeleted = !conversations.map { it.threadId }.contains(threadId) val isConversationDeleted = !conversations.map { it.threadId }.contains(threadId)
if (isConversationDeleted && !isTemporaryThread) { if (isConversationDeleted && !isTemporaryThread) {
conversationsDB.deleteThreadId(threadId) conversationsDB.deleteThreadId(threadId)
} }
val newConversation: Conversation? = conversations.find { it.phoneNumber == cachedConversation.phoneNumber } val newConversation: Conversation? = conversations.find { it.phoneNumber == cachedConversation.phoneNumber }
if (isTemporaryThread && newConversation != null) { if (isTemporaryThread && newConversation != null) {
// delete the original temporary thread and move any scheduled messages to the new thread // delete the original temporary thread and move any scheduled messages to the new thread
conversationsDB.deleteThreadId(threadId) conversationsDB.deleteThreadId(threadId)
messagesDB.getScheduledThreadMessages(threadId) messagesDB.getScheduledThreadMessages(threadId)
.forEach { message -> .forEach { message ->
messagesDB.insertOrUpdate(message.copy(threadId = newConversation.threadId)) messagesDB.insertOrUpdate(message.copy(threadId = newConversation.threadId))
} }
insertOrUpdateConversation(newConversation, cachedConversation) insertOrUpdateConversation(newConversation, cachedConversation)
}
} }
}
cachedConversations.forEach { cachedConv -> cachedConversations.forEach { cachedConv ->
val conv = conversations.find { val conv = conversations.find {
it.threadId == cachedConv.threadId && !Conversation.areContentsTheSame(cachedConv, it) it.threadId == cachedConv.threadId && !Conversation.areContentsTheSame(cachedConv, it)
} }
// Log.e(TAG, " getNewConversations 对比后的结果(新数据)222 find conv:${GsonUtil.parseBeanToJson(conv)}") // Log.e(TAG, " getNewConversations 对比后的结果(新数据)222 find conv:${GsonUtil.parseBeanToJson(conv)}")
if (conv != null && conv.isSFNumber) { if (conv != null && conv.isSFNumber) {
// Log.e(TAG, " conversations find it conv:${GsonUtil.parseBeanToJson(conv)}") // Log.e(TAG, " conversations find it conv:${GsonUtil.parseBeanToJson(conv)}")
val lastModified = maxOf(cachedConv.date, conv.date) val lastModified = maxOf(cachedConv.date, conv.date)
val conversation = conv.copy(date = lastModified) val conversation = conv.copy(date = lastModified)
// Log.e( // Log.e(
// TAG, // TAG,
// " getNewConversations 对比后的结果(新数据) isSFNumber=${conv.isSFNumber} find conversation:${GsonUtil.parseBeanToJson(conversation)}" // " getNewConversations 对比后的结果(新数据) isSFNumber=${conv.isSFNumber} find conversation:${GsonUtil.parseBeanToJson(conversation)}"
// ) // )
insertOrUpdateConversation(conversation) insertOrUpdateConversation(conversation)
}
} }
val allConversations = conversationsDB.getNonArchived() as ArrayList<Conversation> }
val allConversations = conversationsDB.getNonArchived() as ArrayList<Conversation>
// Log.e(TAG, "----MainActivity----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}") // Log.e(TAG, "----MainActivity----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}")
// runOnUiThread { // runOnUiThread {
setupConversations(allConversations) setupConversations(allConversations)
// } // }
// Log.e(TAG, "----MainActivity----handleMessage appRunCount:${config.appRunCount}") // Log.e(TAG, "----MainActivity----handleMessage appRunCount:${config.appRunCount}")
if (config.appRunCount == 1) { if (config.appRunCount == 1) {
conversations.map { it.threadId }.forEach { threadId -> conversations.map { it.threadId }.forEach { threadId ->
val messages = getMessages(threadId, getImageResolutions = false, includeScheduledMessages = false) val messages = getMessages(threadId, getImageResolutions = false, includeScheduledMessages = false)
messages.chunked(30).forEach { currentMessages: List<Message> -> messages.chunked(30).forEach { currentMessages: List<Message> ->
messagesDB.insertMessages(*currentMessages.toTypedArray()) messagesDB.insertMessages(*currentMessages.toTypedArray())
}
} }
} }
} }
}
private fun getOrCreateConversationsAdapter(): ConversationsAdapter { private fun getOrCreateConversationsAdapter(): ConversationsAdapter {
var currAdapter = binding.conversationsList.adapter var currAdapter = binding.conversationsList.adapter
...@@ -404,41 +404,31 @@ class MainActivity : SimpleActivity() { ...@@ -404,41 +404,31 @@ class MainActivity : SimpleActivity() {
return currAdapter as ConversationsAdapter return currAdapter as ConversationsAdapter
} }
private fun setupConversations(conversations: ArrayList<Conversation>, cached: Boolean = false) { /**
if (conversations.size == 0) return * 1.获取最新短信 与通话记录中对比数据进行掩码操作
* 2.更新列表最新一条的数据
val sortedConversations = conversations.sortedWith( */
compareByDescending<Conversation> { config.pinnedConversations.contains(it.threadId.toString()) } private fun setFirstConversationAndUpdateData(sortedConversations: ArrayList<Conversation>) {
.thenByDescending { it.date }
).toMutableList() as ArrayList<Conversation>
//确保排序后的短信列表是有数据的
val firstConversation = sortedConversations.first()//获取第一条未掩码会话
Log.d(TAG, " MainActivity firstConversation() 获取第一条会话 =: ${GsonUtil.parseBeanToJson(firstConversation)}")
// val smsDraft: String? = getSmsDraft(firstConversation.threadId)
// ensureBackgroundThread {
// val newConv = conversationsDB.getNonArchived()
// Log.d(TAG, " MainActivity firstConversation() newConv 获取第一条会话 =: ${GsonUtil.parseBeanToJson(newConv)}")
// }
val hasPerMissionResult = this@MainActivity.hasPermission(PERMISSION_READ_CALL_LOG) val hasPerMissionResult = this@MainActivity.hasPermission(PERMISSION_READ_CALL_LOG)
if (hasPerMissionResult) { if (hasPerMissionResult) {
Log.d(TAG, " MainActivity firstConversation() 根据线程Id查询会话 threadID=:${firstConversation.threadId} ") val firstConversation = sortedConversations.first()//获取第一条未掩码会话
Log.d(TAG, " MainActivity firstConversation() 获取第一条会话 =: ${GsonUtil.parseBeanToJson(firstConversation)}")
val phoneNumber = firstConversation.title.replace(" ", "") val phoneNumber = firstConversation.title.replace(" ", "")
if (!firstConversation.isSFNumber && phoneNumber.length == 11) { //只针对手机号
Log.d(TAG, "----MainActivity----获取第一条会话 phoneNumber:$phoneNumber") if (phoneNumber.length != 11) return
if (!firstConversation.isSFNumber) {
Log.d(TAG, "----MainActivity----获取第一条会话手机号 phoneNumber:$phoneNumber")
RecentsHelper(context = this@MainActivity).getRecentCalls(false, Int.MAX_VALUE) { recents: List<RecentCall> -> RecentsHelper(context = this@MainActivity).getRecentCalls(false, Int.MAX_VALUE) { recents: List<RecentCall> ->
val listCallRecent: List<RecentCall> = recents.distinctBy { it.phoneNumber } val listCallRecent: List<RecentCall> = recents.distinctBy { it.phoneNumber }
Log.d(TAG, "----MainActivity----最近通话记录列表 :${GsonUtil.parseListToJson(listCallRecent)}") // Log.d(TAG, "----MainActivity----最近通话记录列表 :${GsonUtil.parseListToJson(listCallRecent)}")
val findResult: RecentCall = listCallRecent.first { it.phoneNumber.trim() == phoneNumber && it.isShunFeng } val findResult: RecentCall = listCallRecent.first { it.phoneNumber.trim() == phoneNumber && it.isShunFeng }
Log.d(TAG, "----MainActivity----是否从通话记录中查找到电话号Result:${findResult != null}") Log.d(TAG, "----MainActivity----是否从通话记录中查找到电话号Result:${findResult != null}")
//从最近通话记录中找到了号码 进行掩码操作 //从最近通话记录中找到了号码 进行掩码操作
if (findResult != null) { if (findResult != null) {
PhoneUtils.phoneNumberFormat(phoneNumber) { itNumber -> PhoneUtils.phoneNumberFormat(phoneNumber) { itNumber ->
firstConversation.phoneNumber = itNumber firstConversation.phoneNumber = itNumber
firstConversation.isSFNumber = true firstConversation.isSFNumber = true
conversationsDB.insertOrUpdate(firstConversation)
Log.d( Log.d(
TAG, TAG,
"----MainActivity----手机号掩码操作 itNumber:${itNumber} phoneNumber:${firstConversation.phoneNumber} threadId:${firstConversation.threadId} " "----MainActivity----手机号掩码操作 itNumber:${itNumber} phoneNumber:${firstConversation.phoneNumber} threadId:${firstConversation.threadId} "
...@@ -447,13 +437,30 @@ class MainActivity : SimpleActivity() { ...@@ -447,13 +437,30 @@ class MainActivity : SimpleActivity() {
} }
} }
} }
//根据最新的短信threadId 获取所有短信列表 取最新一条记录
val messages: java.util.ArrayList<Message> = getMessages(firstConversation.threadId, true)
Log.e(TAG, " MainActivity getMessages() 执行查询会话 messages 数据库 =:${GsonUtil.parseListToJson(messages)} ")
val lastMsg = messages.last()
firstConversation.snippet = lastMsg.body
firstConversation.date = lastMsg.date
conversationsDB.insertOrUpdate(firstConversation)
} }
}
private fun setupConversations(conversations: ArrayList<Conversation>, cached: Boolean = false) {
if (conversations.size == 0) return
val sortedConversations = conversations.sortedWith(
compareByDescending<Conversation> { config.pinnedConversations.contains(it.threadId.toString()) }
.thenByDescending { it.date }
).toMutableList() as ArrayList<Conversation>
setFirstConversationAndUpdateData(conversations)
lifecycleScope.launch(Dispatchers.Main) { lifecycleScope.launch(Dispatchers.Main) {
// delay(1_000) // delay(1_000)
Log.d(TAG, " MainActivity setupConversations() 修改后显示短信列表数据 =: ${GsonUtil.parseListToJson(sortedConversations)}") Log.d(TAG, " MainActivity setupConversations() 修改后显示短信列表数据 =: ${GsonUtil.parseListToJson(sortedConversations)}")
// runOnUiThread { // runOnUiThread {
if (cached && config.appRunCount == 1) { if (cached && config.appRunCount == 1) {
// there are no cached conversations on the first run so we show the loading placeholder and progress until we are done loading from telephony // there are no cached conversations on the first run so we show the loading placeholder and progress until we are done loading from telephony
showOrHideProgress(conversations.isEmpty()) showOrHideProgress(conversations.isEmpty())
...@@ -634,7 +641,6 @@ class MainActivity : SimpleActivity() { ...@@ -634,7 +641,6 @@ class MainActivity : SimpleActivity() {
startActivity(Intent(applicationContext, SettingsActivity::class.java)) startActivity(Intent(applicationContext, SettingsActivity::class.java))
} }
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
fun refreshMessages(event: Events.RefreshMessages) { fun refreshMessages(event: Events.RefreshMessages) {
// Log.e(TAG, "----MainActivity----refreshMessages EventBus") // Log.e(TAG, "----MainActivity----refreshMessages EventBus")
......
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