Minor code improvement

This commit is contained in:
Naveen Singh 2025-01-04 15:51:44 +05:30
parent fcf68ca977
commit d4d914abf4
No known key found for this signature in database
GPG key ID: AF5D43C216778C0B
2 changed files with 52 additions and 13 deletions

View file

@ -32,14 +32,21 @@ fun Context.isLongMmsMessage(text: String, settings: Settings = getSendMessageSe
}
/** Sends the message using the in-app SmsManager API wrappers if it's an SMS or using android-smsmms for MMS. */
fun Context.sendMessageCompat(text: String, addresses: List<String>, subId: Int?, attachments: List<Attachment>, messageId: Long? = null) {
fun Context.sendMessageCompat(
text: String,
addresses: List<String>,
subId: Int?,
attachments: List<Attachment>,
messageId: Long? = null
) {
val settings = getSendMessageSettings()
if (subId != null) {
settings.subscriptionId = subId
}
val messagingUtils = messagingUtils
val isMms = attachments.isNotEmpty() || isLongMmsMessage(text, settings) || addresses.size > 1 && settings.group
val isMms = attachments.isNotEmpty() || isLongMmsMessage(text, settings)
|| addresses.size > 1 && settings.group
if (isMms) {
// we send all MMS attachments separately to reduces the chances of hitting provider MMS limit.
if (attachments.isNotEmpty()) {
@ -58,11 +65,25 @@ fun Context.sendMessageCompat(text: String, addresses: List<String>, subId: Int?
}
} else {
try {
messagingUtils.sendSmsMessage(text, addresses.toSet(), settings.subscriptionId, settings.deliveryReports, messageId)
messagingUtils.sendSmsMessage(
text = text,
addresses = addresses.toSet(),
subId = settings.subscriptionId,
requireDeliveryReport = settings.deliveryReports,
messageId = messageId
)
} catch (e: SmsException) {
when (e.errorCode) {
EMPTY_DESTINATION_ADDRESS -> toast(id = R.string.empty_destination_address, length = LENGTH_LONG)
ERROR_PERSISTING_MESSAGE -> toast(id = R.string.unable_to_save_message, length = LENGTH_LONG)
EMPTY_DESTINATION_ADDRESS -> toast(
id = R.string.empty_destination_address,
length = LENGTH_LONG
)
ERROR_PERSISTING_MESSAGE -> toast(
id = R.string.unable_to_save_message,
length = LENGTH_LONG
)
ERROR_SENDING_MESSAGE -> toast(
msg = getString(R.string.unknown_error_occurred_sending_message, e.errorCode),
length = LENGTH_LONG

View file

@ -30,8 +30,14 @@ class MessagingUtils(val context: Context) {
* Insert an SMS to the given URI with thread_id specified.
*/
private fun insertSmsMessage(
subId: Int, dest: String, text: String, timestamp: Long, threadId: Long,
status: Int = Sms.STATUS_NONE, type: Int = Sms.MESSAGE_TYPE_OUTBOX, messageId: Long? = null
subId: Int,
dest: String,
text: String,
timestamp: Long,
threadId: Long,
status: Int = Sms.STATUS_NONE,
type: Int = Sms.MESSAGE_TYPE_OUTBOX,
messageId: Long? = null
): Uri {
val response: Uri?
val values = ContentValues().apply {
@ -61,11 +67,13 @@ class MessagingUtils(val context: Context) {
if (messageId != null) {
val selection = "${Sms._ID} = ?"
val selectionArgs = arrayOf(messageId.toString())
val count = context.contentResolver.update(Sms.CONTENT_URI, values, selection, selectionArgs)
if (count > 0) {
response = Uri.parse("${Sms.CONTENT_URI}/${messageId}")
val count = context.contentResolver.update(
Sms.CONTENT_URI, values, selection, selectionArgs
)
response = if (count > 0) {
Uri.parse("${Sms.CONTENT_URI}/${messageId}")
} else {
response = null
null
}
} else {
response = context.contentResolver.insert(Sms.CONTENT_URI, values)
@ -78,7 +86,11 @@ class MessagingUtils(val context: Context) {
/** Send an SMS message given [text] and [addresses]. A [SmsException] is thrown in case any errors occur. */
fun sendSmsMessage(
text: String, addresses: Set<String>, subId: Int, requireDeliveryReport: Boolean, messageId: Long? = null
text: String,
addresses: Set<String>,
subId: Int,
requireDeliveryReport: Boolean,
messageId: Long? = null
) {
if (addresses.size > 1) {
// insert a dummy message for this thread if it is a group message
@ -146,7 +158,13 @@ class MessagingUtils(val context: Context) {
}
@Deprecated("TODO: Move/rewrite MMS code into the app.")
fun sendMmsMessage(text: String, addresses: List<String>, attachment: Attachment?, settings: Settings, messageId: Long? = null) {
fun sendMmsMessage(
text: String,
addresses: List<String>,
attachment: Attachment?,
settings: Settings,
messageId: Long? = null
) {
val transaction = Transaction(context, settings)
val message = Message(text, addresses.toTypedArray())