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. */ /** 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() val settings = getSendMessageSettings()
if (subId != null) { if (subId != null) {
settings.subscriptionId = subId settings.subscriptionId = subId
} }
val messagingUtils = messagingUtils 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) { if (isMms) {
// we send all MMS attachments separately to reduces the chances of hitting provider MMS limit. // we send all MMS attachments separately to reduces the chances of hitting provider MMS limit.
if (attachments.isNotEmpty()) { if (attachments.isNotEmpty()) {
@ -58,11 +65,25 @@ fun Context.sendMessageCompat(text: String, addresses: List<String>, subId: Int?
} }
} else { } else {
try { 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) { } catch (e: SmsException) {
when (e.errorCode) { when (e.errorCode) {
EMPTY_DESTINATION_ADDRESS -> toast(id = R.string.empty_destination_address, length = LENGTH_LONG) EMPTY_DESTINATION_ADDRESS -> toast(
ERROR_PERSISTING_MESSAGE -> toast(id = R.string.unable_to_save_message, length = LENGTH_LONG) 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( ERROR_SENDING_MESSAGE -> toast(
msg = getString(R.string.unknown_error_occurred_sending_message, e.errorCode), msg = getString(R.string.unknown_error_occurred_sending_message, e.errorCode),
length = LENGTH_LONG 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. * Insert an SMS to the given URI with thread_id specified.
*/ */
private fun insertSmsMessage( private fun insertSmsMessage(
subId: Int, dest: String, text: String, timestamp: Long, threadId: Long, subId: Int,
status: Int = Sms.STATUS_NONE, type: Int = Sms.MESSAGE_TYPE_OUTBOX, messageId: Long? = null dest: String,
text: String,
timestamp: Long,
threadId: Long,
status: Int = Sms.STATUS_NONE,
type: Int = Sms.MESSAGE_TYPE_OUTBOX,
messageId: Long? = null
): Uri { ): Uri {
val response: Uri? val response: Uri?
val values = ContentValues().apply { val values = ContentValues().apply {
@ -61,11 +67,13 @@ class MessagingUtils(val context: Context) {
if (messageId != null) { if (messageId != null) {
val selection = "${Sms._ID} = ?" val selection = "${Sms._ID} = ?"
val selectionArgs = arrayOf(messageId.toString()) val selectionArgs = arrayOf(messageId.toString())
val count = context.contentResolver.update(Sms.CONTENT_URI, values, selection, selectionArgs) val count = context.contentResolver.update(
if (count > 0) { Sms.CONTENT_URI, values, selection, selectionArgs
response = Uri.parse("${Sms.CONTENT_URI}/${messageId}") )
response = if (count > 0) {
Uri.parse("${Sms.CONTENT_URI}/${messageId}")
} else { } else {
response = null null
} }
} else { } else {
response = context.contentResolver.insert(Sms.CONTENT_URI, values) 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. */ /** Send an SMS message given [text] and [addresses]. A [SmsException] is thrown in case any errors occur. */
fun sendSmsMessage( 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) { if (addresses.size > 1) {
// insert a dummy message for this thread if it is a group message // 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.") @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 transaction = Transaction(context, settings)
val message = Message(text, addresses.toTypedArray()) val message = Message(text, addresses.toTypedArray())