mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-02-21 22:17:45 +01:00
properly mark read MMS messages too, not just SMS
This commit is contained in:
parent
7a883ce280
commit
a791b681aa
@ -251,7 +251,7 @@ class ThreadActivity : SimpleActivity() {
|
||||
|
||||
if (!it.read) {
|
||||
hadUnreadItems = true
|
||||
markSMSRead(it.id)
|
||||
markMessageRead(it)
|
||||
}
|
||||
}
|
||||
|
||||
@ -316,6 +316,14 @@ class ThreadActivity : SimpleActivity() {
|
||||
showSelectedContacts()
|
||||
}
|
||||
|
||||
private fun markMessageRead(message: Message) {
|
||||
if (message.isMMS) {
|
||||
markMMSRead(message.id)
|
||||
} else {
|
||||
markSMSRead(message.id)
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.ASYNC)
|
||||
fun refreshMessages(event: Events.RefreshMessages) {
|
||||
messages = getMessages(threadId)
|
||||
|
@ -60,7 +60,8 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
|
||||
val read = cursor.getIntValue(Sms.READ) == 1
|
||||
val thread = cursor.getIntValue(Sms.THREAD_ID)
|
||||
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)
|
||||
}
|
||||
|
||||
@ -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
|
||||
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 projection = arrayOf(
|
||||
Mms._ID,
|
||||
@ -99,15 +100,16 @@ fun Context.getMMS(threadId: Int? = null): ArrayList<Message> {
|
||||
|
||||
val messages = ArrayList<Message>()
|
||||
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 type = cursor.getIntValue(Mms.MESSAGE_BOX)
|
||||
val date = cursor.getLongValue(Mms.DATE).toInt()
|
||||
val read = cursor.getIntValue(Mms.READ) == 1
|
||||
val thread = cursor.getIntValue(Mms.THREAD_ID)
|
||||
val participants = getThreadParticipants(thread, contactsMap)
|
||||
val isMMS = true
|
||||
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)
|
||||
|
||||
participants.forEach {
|
||||
@ -149,6 +151,11 @@ fun Context.getMmsAttachment(id: Int): MessageAttachment? {
|
||||
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> {
|
||||
val uri = Uri.parse("${MmsSms.CONTENT_CONVERSATIONS_URI}?simple=true")
|
||||
val projection = arrayOf(
|
||||
@ -369,6 +376,16 @@ fun Context.markSMSRead(id: Int) {
|
||||
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")
|
||||
fun Context.getThreadId(address: String): Long {
|
||||
return if (isMarshmallowPlus()) {
|
||||
|
@ -5,7 +5,7 @@ import com.simplemobiletools.smsmessenger.extensions.getThreadTitle
|
||||
|
||||
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 attachment: MessageAttachment?
|
||||
val isMMS: Boolean, val attachment: MessageAttachment?
|
||||
) : ThreadItem() {
|
||||
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user