Mark sms sent status failed when any exceptions occur

This commit is contained in:
Naveen 2023-01-06 19:13:09 +05:30
parent 6e6c65ad6a
commit 71e5e06c60
2 changed files with 41 additions and 37 deletions

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.smsmessenger.messaging
import android.annotation.SuppressLint
import android.app.Activity
import android.content.ContentValues
import android.content.Context
@ -83,10 +84,43 @@ class MessagingUtils(val context: Context) {
subId = subId, dest = address, text = text,
timestamp = System.currentTimeMillis(), threadId = threadId
)
context.smsSender.sendMessage(
subId = subId, destination = address, body = text, serviceCenter = null,
requireDeliveryReport = requireDeliveryReport, messageUri = messageUri
)
try {
context.smsSender.sendMessage(
subId = subId, destination = address, body = text, serviceCenter = null,
requireDeliveryReport = requireDeliveryReport, messageUri = messageUri
)
} catch (e: Exception) {
updateSmsMessageSendingStatus(messageUri, Sms.Outbox.MESSAGE_TYPE_FAILED)
throw e // propagate error to caller
}
}
}
fun updateSmsMessageSendingStatus(messageUri: Uri?, type: Int) {
val resolver = context.contentResolver
val values = ContentValues().apply {
put(Sms.Outbox.TYPE, type)
}
try {
if (messageUri != null) {
resolver.update(messageUri, values, null, null)
} else {
// mark latest sms as sent, need to check if this is still necessary (or reliable)
// as this was taken from android-smsmms. The messageUri shouldn't be null anyway
val cursor = resolver.query(Sms.Outbox.CONTENT_URI, null, null, null, null)
cursor?.use {
if (cursor.moveToFirst()) {
@SuppressLint("Range")
val id = cursor.getString(cursor.getColumnIndex(Sms.Outbox._ID))
val selection = "${Sms._ID} = ?"
val selectionArgs = arrayOf(id.toString())
resolver.update(Sms.Outbox.CONTENT_URI, values, selection, selectionArgs)
}
}
}
} catch (e: Exception) {
context.showErrorToast(e)
}
}

View File

@ -1,8 +1,6 @@
package com.simplemobiletools.smsmessenger.receivers
import android.annotation.SuppressLint
import android.app.Activity
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.net.Uri
@ -12,7 +10,6 @@ import android.provider.Telephony.Sms
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner
import com.simplemobiletools.commons.extensions.getMyContactsCursor
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.extensions.*
import com.simplemobiletools.smsmessenger.helpers.refreshMessages
@ -24,47 +21,20 @@ class SmsStatusSentReceiver : SendStatusReceiver() {
override fun updateAndroidDatabase(context: Context, intent: Intent, receiverResultCode: Int) {
val messageUri: Uri? = intent.data
val resultCode = resultCode
val messagingUtils = context.messagingUtils
val type = if (resultCode == Activity.RESULT_OK) {
Sms.MESSAGE_TYPE_SENT
} else {
Sms.MESSAGE_TYPE_FAILED
}
updateSmsMessageSendingStatus(context, messageUri, type)
context.messagingUtils.maybeShowErrorToast(
messagingUtils.updateSmsMessageSendingStatus(messageUri, type)
messagingUtils.maybeShowErrorToast(
resultCode = resultCode,
errorCode = intent.getIntExtra(EXTRA_ERROR_CODE, NO_ERROR_CODE)
)
}
private fun updateSmsMessageSendingStatus(context: Context, messageUri: Uri?, type: Int) {
val resolver = context.contentResolver
val values = ContentValues().apply {
put(Sms.Outbox.TYPE, type)
}
try {
if (messageUri != null) {
resolver.update(messageUri, values, null, null)
} else {
// mark latest sms as sent, need to check if this is still necessary (or reliable)
val cursor = resolver.query(Sms.Outbox.CONTENT_URI, null, null, null, null)
cursor?.use {
if (cursor.moveToFirst()) {
@SuppressLint("Range")
val id = cursor.getString(cursor.getColumnIndex(Sms.Outbox._ID))
val selection = "${Sms._ID} = ?"
val selectionArgs = arrayOf(id.toString())
resolver.update(Sms.Outbox.CONTENT_URI, values, selection, selectionArgs)
}
}
}
} catch (e: Exception) {
context.showErrorToast(e)
}
}
override fun updateAppDatabase(context: Context, intent: Intent, receiverResultCode: Int) {
val messageUri = intent.data
if (messageUri != null) {