fix #47, mark the whole conversation as read with Mark as Read at notification

This commit is contained in:
tibbi 2020-06-17 21:45:17 +02:00
parent 26951cc979
commit a85eb77d31
5 changed files with 23 additions and 18 deletions

View File

@ -23,7 +23,9 @@ import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.ThreadActivity
import com.simplemobiletools.smsmessenger.databases.MessagesDatabase
import com.simplemobiletools.smsmessenger.helpers.*
import com.simplemobiletools.smsmessenger.helpers.Config
import com.simplemobiletools.smsmessenger.helpers.MARK_AS_READ
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.interfaces.ConversationsDao
import com.simplemobiletools.smsmessenger.models.*
import com.simplemobiletools.smsmessenger.receivers.MarkAsReadReceiver
@ -497,6 +499,18 @@ fun Context.markMessageRead(id: Int, isMMS: Boolean) {
contentResolver.update(uri, contentValues, selection, selectionArgs)
}
fun Context.markThreadMessagesRead(threadId: Int) {
arrayOf(Mms.CONTENT_URI, Sms.CONTENT_URI).forEach { uri ->
val contentValues = ContentValues().apply {
put(Sms.READ, 1)
put(Sms.SEEN, 1)
}
val selection = "${Sms.THREAD_ID} = ?"
val selectionArgs = arrayOf(threadId.toString())
contentResolver.update(uri, contentValues, selection, selectionArgs)
}
}
@SuppressLint("NewApi")
fun Context.getThreadId(address: String): Long {
return if (isMarshmallowPlus()) {
@ -524,7 +538,7 @@ fun Context.getThreadId(addresses: Set<String>): Long {
}
@SuppressLint("NewApi")
fun Context.showReceivedMessageNotification(address: String, body: String, threadID: Int, bitmap: Bitmap?, messageId: Int, isMMS: Boolean) {
fun Context.showReceivedMessageNotification(address: String, body: String, threadID: Int, bitmap: Bitmap?) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val channelId = "simple_sms_messenger"
@ -556,8 +570,6 @@ fun Context.showReceivedMessageNotification(address: String, body: String, threa
val markAsReadIntent = Intent(this, MarkAsReadReceiver::class.java).apply {
action = MARK_AS_READ
putExtra(MESSAGE_ID, messageId)
putExtra(MESSAGE_IS_MMS, isMMS)
putExtra(THREAD_ID, threadID)
}
val markAsReadPendingIntent = PendingIntent.getBroadcast(this, 0, markAsReadIntent, PendingIntent.FLAG_CANCEL_CURRENT)

View File

@ -13,8 +13,6 @@ const val USE_SIM_ID_PREFIX = "use_sim_id_"
private const val PATH = "com.simplemobiletools.smsmessenger.action."
const val MARK_AS_READ = PATH + "mark_as_read"
const val MESSAGE_ID = "message_id"
const val MESSAGE_IS_MMS = "message_is_mms"
// view types for the thread list view
const val THREAD_DATE_TIME = 1

View File

@ -6,23 +6,18 @@ import android.content.Intent
import com.simplemobiletools.commons.extensions.notificationManager
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.extensions.conversationsDB
import com.simplemobiletools.smsmessenger.extensions.markMessageRead
import com.simplemobiletools.smsmessenger.extensions.markThreadMessagesRead
import com.simplemobiletools.smsmessenger.helpers.MARK_AS_READ
import com.simplemobiletools.smsmessenger.helpers.MESSAGE_ID
import com.simplemobiletools.smsmessenger.helpers.MESSAGE_IS_MMS
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
class MarkAsReadReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
MARK_AS_READ -> {
val messageId = intent.getIntExtra(MESSAGE_ID, 0)
context.notificationManager.cancel(messageId)
val threadId = intent.getIntExtra(THREAD_ID, 0)
context.notificationManager.cancel(threadId)
ensureBackgroundThread {
val isMMS = intent.getBooleanExtra(MESSAGE_IS_MMS, false)
context.markMessageRead(messageId, isMMS)
val threadId = intent.getIntExtra(THREAD_ID, 0)
context.markThreadMessagesRead(threadId)
context.conversationsDB.markRead(threadId.toLong())
}
}

View File

@ -33,7 +33,7 @@ class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() {
null
}
context.showReceivedMessageNotification(address, mms.body, mms.thread, glideBitmap, mms.id, true)
context.showReceivedMessageNotification(address, mms.body, mms.thread, glideBitmap)
val conversation = context.getConversations(mms.thread.toLong()).firstOrNull() ?: return@ensureBackgroundThread
context.conversationsDB.insertOrUpdate(conversation)
}

View File

@ -30,8 +30,8 @@ class SmsReceiver : BroadcastReceiver() {
}
if (!context.isNumberBlocked(address)) {
val messageId = context.insertNewSMS(address, subject, body, date, read, threadId, type, subscriptionId)
context.showReceivedMessageNotification(address, body, threadId.toInt(), null, messageId, false)
context.insertNewSMS(address, subject, body, date, read, threadId, type, subscriptionId)
context.showReceivedMessageNotification(address, body, threadId.toInt(), null)
refreshMessages()
ensureBackgroundThread {