properly mark read MMS messages too, not just SMS

This commit is contained in:
tibbi
2020-04-10 18:51:47 +02:00
parent 7a883ce280
commit a791b681aa
3 changed files with 31 additions and 6 deletions

View File

@@ -251,7 +251,7 @@ class ThreadActivity : SimpleActivity() {
if (!it.read) { if (!it.read) {
hadUnreadItems = true hadUnreadItems = true
markSMSRead(it.id) markMessageRead(it)
} }
} }
@@ -316,6 +316,14 @@ class ThreadActivity : SimpleActivity() {
showSelectedContacts() showSelectedContacts()
} }
private fun markMessageRead(message: Message) {
if (message.isMMS) {
markMMSRead(message.id)
} else {
markSMSRead(message.id)
}
}
@Subscribe(threadMode = ThreadMode.ASYNC) @Subscribe(threadMode = ThreadMode.ASYNC)
fun refreshMessages(event: Events.RefreshMessages) { fun refreshMessages(event: Events.RefreshMessages) {
messages = getMessages(threadId) messages = getMessages(threadId)

View File

@@ -60,7 +60,8 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
val read = cursor.getIntValue(Sms.READ) == 1 val read = cursor.getIntValue(Sms.READ) == 1
val thread = cursor.getIntValue(Sms.THREAD_ID) val thread = cursor.getIntValue(Sms.THREAD_ID)
val participant = Contact(0, senderName, "", senderNumber, false) val participant = Contact(0, senderName, "", senderNumber, false)
val message = Message(id, body, type, arrayListOf(participant), date, read, thread, null) val isMMS = false
val message = Message(id, body, type, arrayListOf(participant), date, read, thread, isMMS, null)
messages.add(message) messages.add(message)
} }
@@ -75,7 +76,7 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
} }
// as soon as a message contains multiple recipients it count as an MMS instead of SMS // as soon as a message contains multiple recipients it count as an MMS instead of SMS
fun Context.getMMS(threadId: Int? = null): ArrayList<Message> { fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<Message> {
val uri = Mms.CONTENT_URI val uri = Mms.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
Mms._ID, Mms._ID,
@@ -99,15 +100,16 @@ fun Context.getMMS(threadId: Int? = null): ArrayList<Message> {
val messages = ArrayList<Message>() val messages = ArrayList<Message>()
val contactsMap = HashMap<Int, Contact>() val contactsMap = HashMap<Int, Contact>()
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor -> queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor ->
val id = cursor.getIntValue(Mms._ID) val id = cursor.getIntValue(Mms._ID)
val type = cursor.getIntValue(Mms.MESSAGE_BOX) val type = cursor.getIntValue(Mms.MESSAGE_BOX)
val date = cursor.getLongValue(Mms.DATE).toInt() val date = cursor.getLongValue(Mms.DATE).toInt()
val read = cursor.getIntValue(Mms.READ) == 1 val read = cursor.getIntValue(Mms.READ) == 1
val thread = cursor.getIntValue(Mms.THREAD_ID) val thread = cursor.getIntValue(Mms.THREAD_ID)
val participants = getThreadParticipants(thread, contactsMap) val participants = getThreadParticipants(thread, contactsMap)
val isMMS = true
val attachment = getMmsAttachment(id) val attachment = getMmsAttachment(id)
val message = Message(id, attachment?.text ?: "", type, participants, date, read, thread, attachment) val message = Message(id, attachment?.text ?: "", type, participants, date, read, thread, isMMS, attachment)
messages.add(message) messages.add(message)
participants.forEach { participants.forEach {
@@ -149,6 +151,11 @@ fun Context.getMmsAttachment(id: Int): MessageAttachment? {
return attachment return attachment
} }
fun Context.getLatestMMS(): Message? {
val sortOrder = "${Mms.DATE} DESC LIMIT 1"
return getMMS(sortOrder = sortOrder).firstOrNull()
}
fun Context.getThreadParticipants(threadId: Int, contactsMap: HashMap<Int, Contact>?): ArrayList<Contact> { fun Context.getThreadParticipants(threadId: Int, contactsMap: HashMap<Int, Contact>?): ArrayList<Contact> {
val uri = Uri.parse("${MmsSms.CONTENT_CONVERSATIONS_URI}?simple=true") val uri = Uri.parse("${MmsSms.CONTENT_CONVERSATIONS_URI}?simple=true")
val projection = arrayOf( val projection = arrayOf(
@@ -369,6 +376,16 @@ fun Context.markSMSRead(id: Int) {
contentResolver.update(uri, contentValues, selection, selectionArgs) contentResolver.update(uri, contentValues, selection, selectionArgs)
} }
fun Context.markMMSRead(id: Int) {
val uri = Mms.CONTENT_URI
val contentValues = ContentValues().apply {
put(Mms.READ, 1)
}
val selection = "${Mms._ID} = ? AND ${Mms.READ} = ?"
val selectionArgs = arrayOf(id.toString(), "0")
contentResolver.update(uri, contentValues, selection, selectionArgs)
}
@SuppressLint("NewApi") @SuppressLint("NewApi")
fun Context.getThreadId(address: String): Long { fun Context.getThreadId(address: String): Long {
return if (isMarshmallowPlus()) { return if (isMarshmallowPlus()) {

View File

@@ -5,7 +5,7 @@ import com.simplemobiletools.smsmessenger.extensions.getThreadTitle
data class Message( data class Message(
val id: Int, val body: String, val type: Int, val participants: ArrayList<Contact>, val date: Int, val read: Boolean, val thread: Int, val id: Int, val body: String, val type: Int, val participants: ArrayList<Contact>, val date: Int, val read: Boolean, val thread: Int,
val attachment: MessageAttachment? val isMMS: Boolean, val attachment: MessageAttachment?
) : ThreadItem() { ) : ThreadItem() {
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX