improve message marking after delivery to avoid glitching

This commit is contained in:
tibbi 2021-01-02 11:24:56 +01:00
parent da4f476bce
commit d02df0a1a2
2 changed files with 14 additions and 4 deletions

View File

@ -30,7 +30,7 @@ interface MessagesDao {
fun markThreadRead(threadId: Long)
@Query("UPDATE messages SET type = :type WHERE id = :id")
fun updateType(id: Long, type: Int)
fun updateType(id: Long, type: Int): Int
@Query("DELETE FROM messages WHERE id = :id")
fun delete(id: Long)

View File

@ -4,6 +4,8 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.provider.Telephony
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.extensions.messagesDB
@ -15,11 +17,19 @@ class SmsStatusDeliveredReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.extras?.containsKey("message_uri") == true) {
val uri = Uri.parse(intent.getStringExtra("message_uri"))
val id = uri?.lastPathSegment?.toLong() ?: 0L
val messageId = uri?.lastPathSegment?.toLong() ?: 0L
ensureBackgroundThread {
val type = Telephony.Sms.MESSAGE_TYPE_SENT
context.updateMessageType(id, type)
context.messagesDB.updateType(id, type)
context.updateMessageType(messageId, type)
val updated = context.messagesDB.updateType(messageId, type)
if (updated == 0) {
Handler(Looper.getMainLooper()).postDelayed({
ensureBackgroundThread {
context.messagesDB.updateType(messageId, type)
}
}, 2000)
}
refreshMessages()
}
}