From 312f5bd0a82e4b69c6291779dbb662eb7b3859b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 24 Jul 2023 11:03:48 +0200 Subject: [PATCH 1/5] Prevent duplication of messages on resend This prevents duplication by ensuring that message is just updated in case of SMS, instead of creating a new entry. In case of MMS, due to the way it is sent internally, we delete original message once we get result of the new one. This closes #554 --- .../smsmessenger/activities/ThreadActivity.kt | 10 +++++-- .../smsmessenger/messaging/Messaging.kt | 10 +++---- .../smsmessenger/messaging/MessagingUtils.kt | 26 ++++++++++++++----- .../smsmessenger/receivers/MmsSentReceiver.kt | 8 ++++++ 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index 82e2003a..c5194b56 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -107,6 +107,7 @@ class ThreadActivity : SimpleActivity() { private var wasProtectionHandled = false private var isScheduledMessage: Boolean = false + private var messageToResend: Long? = null private var scheduledMessage: Message? = null private lateinit var scheduledDateTime: DateTime @@ -291,6 +292,7 @@ class ThreadActivity : SimpleActivity() { super.onActivityResult(requestCode, resultCode, resultData) if (resultCode != Activity.RESULT_OK) return val data = resultData?.data + messageToResend = null if (requestCode == CAPTURE_PHOTO_INTENT && capturedImageUri != null) { addAttachment(capturedImageUri!!) @@ -492,7 +494,10 @@ class ThreadActivity : SimpleActivity() { private fun handleItemClick(any: Any) { when { any is Message && any.isScheduled -> showScheduledMessageInfo(any) - any is ThreadError -> thread_type_message.setText(any.messageText) + any is ThreadError -> { + thread_type_message.setText(any.messageText) + messageToResend = any.messageId + } } } @@ -635,6 +640,7 @@ class ThreadActivity : SimpleActivity() { thread_send_message.isClickable = false thread_type_message.onTextChangeListener { + messageToResend = null checkSendMessageAvailability() val messageString = if (config.useSimpleCharacters) { it.normalizeString() @@ -1334,7 +1340,7 @@ class ThreadActivity : SimpleActivity() { try { refreshedSinceSent = false - sendMessageCompat(text, addresses, subscriptionId, attachments) + sendMessageCompat(text, addresses, subscriptionId, attachments, messageToResend) ensureBackgroundThread { val messageIds = messages.map { it.id } val messages = getMessages(threadId, getImageResolutions = true, limit = maxOf(1, attachments.size)) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/messaging/Messaging.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/messaging/Messaging.kt index e02a1a6c..9a391395 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/messaging/Messaging.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/messaging/Messaging.kt @@ -32,7 +32,7 @@ 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, subId: Int?, attachments: List) { +fun Context.sendMessageCompat(text: String, addresses: List, subId: Int?, attachments: List, messageId: Long? = null) { val settings = getSendMessageSettings() if (subId != null) { settings.subscriptionId = subId @@ -47,18 +47,18 @@ fun Context.sendMessageCompat(text: String, addresses: List, subId: Int? if (attachments.size > 1) { for (i in 0 until lastIndex) { val attachment = attachments[i] - messagingUtils.sendMmsMessage("", addresses, attachment, settings) + messagingUtils.sendMmsMessage("", addresses, attachment, settings, messageId) } } val lastAttachment = attachments[lastIndex] - messagingUtils.sendMmsMessage(text, addresses, lastAttachment, settings) + messagingUtils.sendMmsMessage(text, addresses, lastAttachment, settings, messageId) } else { - messagingUtils.sendMmsMessage(text, addresses, null, settings) + messagingUtils.sendMmsMessage(text, addresses, null, settings, messageId) } } else { try { - messagingUtils.sendSmsMessage(text, addresses.toSet(), settings.subscriptionId, settings.deliveryReports) + messagingUtils.sendSmsMessage(text, addresses.toSet(), settings.subscriptionId, settings.deliveryReports, messageId) } catch (e: SmsException) { when (e.errorCode) { EMPTY_DESTINATION_ADDRESS -> toast(id = R.string.empty_destination_address, length = LENGTH_LONG) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/messaging/MessagingUtils.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/messaging/MessagingUtils.kt index 8d79ea7d..211efadf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/messaging/MessagingUtils.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/messaging/MessagingUtils.kt @@ -31,7 +31,7 @@ class MessagingUtils(val context: Context) { */ private fun insertSmsMessage( subId: Int, dest: String, text: String, timestamp: Long, threadId: Long, - status: Int = Sms.STATUS_NONE, type: Int = Sms.MESSAGE_TYPE_OUTBOX + status: Int = Sms.STATUS_NONE, type: Int = Sms.MESSAGE_TYPE_OUTBOX, messageId: Long? = null ): Uri { val response: Uri? val values = ContentValues().apply { @@ -58,7 +58,18 @@ class MessagingUtils(val context: Context) { } try { - response = context.contentResolver.insert(Sms.CONTENT_URI, values) + 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}") + } else { + response = null + } + } else { + response = context.contentResolver.insert(Sms.CONTENT_URI, values) + } } catch (e: Exception) { throw SmsException(ERROR_PERSISTING_MESSAGE, e) } @@ -67,7 +78,7 @@ 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, subId: Int, requireDeliveryReport: Boolean + text: String, addresses: Set, subId: Int, requireDeliveryReport: Boolean, messageId: Long? = null ) { if (addresses.size > 1) { // insert a dummy message for this thread if it is a group message @@ -76,7 +87,8 @@ class MessagingUtils(val context: Context) { insertSmsMessage( subId = subId, dest = mergedAddresses, text = text, timestamp = System.currentTimeMillis(), threadId = broadCastThreadId, - status = Sms.Sent.STATUS_COMPLETE, type = Sms.Sent.MESSAGE_TYPE_SENT + status = Sms.Sent.STATUS_COMPLETE, type = Sms.Sent.MESSAGE_TYPE_SENT, + messageId = messageId ) } @@ -84,7 +96,8 @@ class MessagingUtils(val context: Context) { val threadId = context.getThreadId(address) val messageUri = insertSmsMessage( subId = subId, dest = address, text = text, - timestamp = System.currentTimeMillis(), threadId = threadId + timestamp = System.currentTimeMillis(), threadId = threadId, + messageId = messageId ) try { context.smsSender.sendMessage( @@ -133,7 +146,7 @@ class MessagingUtils(val context: Context) { } @Deprecated("TODO: Move/rewrite MMS code into the app.") - fun sendMmsMessage(text: String, addresses: List, attachment: Attachment?, settings: Settings) { + fun sendMmsMessage(text: String, addresses: List, attachment: Attachment?, settings: Settings, messageId: Long? = null) { val transaction = Transaction(context, settings) val message = Message(text, addresses.toTypedArray()) @@ -158,6 +171,7 @@ class MessagingUtils(val context: Context) { } val mmsSentIntent = Intent(context, MmsSentReceiver::class.java) + mmsSentIntent.putExtra(MmsSentReceiver.EXTRA_ORIGINAL_RESENT_MESSAGE_ID, messageId) transaction.setExplicitBroadcastForSentMms(mmsSentIntent) try { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsSentReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsSentReceiver.kt index e3c92eef..b2650622 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsSentReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsSentReceiver.kt @@ -11,6 +11,7 @@ import android.widget.Toast import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.smsmessenger.R +import com.simplemobiletools.smsmessenger.extensions.deleteMessage import com.simplemobiletools.smsmessenger.helpers.refreshMessages import java.io.File @@ -19,6 +20,7 @@ class MmsSentReceiver : SendStatusReceiver() { override fun updateAndroidDatabase(context: Context, intent: Intent, receiverResultCode: Int) { val uri = Uri.parse(intent.getStringExtra(EXTRA_CONTENT_URI)) + val originalResentMessageId = intent.getLongExtra(EXTRA_ORIGINAL_RESENT_MESSAGE_ID, -1L) val messageBox = if (receiverResultCode == Activity.RESULT_OK) { Telephony.Mms.MESSAGE_BOX_SENT } else { @@ -37,6 +39,11 @@ class MmsSentReceiver : SendStatusReceiver() { context.showErrorToast(e) } + // In case of resent message, delete original to prevent duplication + if (originalResentMessageId != -1L) { + context.deleteMessage(originalResentMessageId, true) + } + val filePath = intent.getStringExtra(EXTRA_FILE_PATH) if (filePath != null) { File(filePath).delete() @@ -50,5 +57,6 @@ class MmsSentReceiver : SendStatusReceiver() { companion object { private const val EXTRA_CONTENT_URI = "content_uri" private const val EXTRA_FILE_PATH = "file_path" + const val EXTRA_ORIGINAL_RESENT_MESSAGE_ID = "original_message_id" } } From 8d2b93793aa1f69b4609604b9dd30ce67ce96195 Mon Sep 17 00:00:00 2001 From: solokot Date: Sun, 23 Jul 2023 06:53:01 +0000 Subject: [PATCH 2/5] Translated using Weblate (Russian) Currently translated at 100.0% (100 of 100 strings) Translation: Simple Mobile Tools/Simple SMS Messenger Translate-URL: https://hosted.weblate.org/projects/simple-mobile-tools/simple-sms-messenger/ru/ --- app/src/main/res/values-ru/strings.xml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index d95f4e1d..9a810549 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -62,16 +62,16 @@ Я Разархивировать - Delete all archived conversations - Archive - Show archived conversations + Удалить все архивные переписки + Архивная + Показывать архивные переписки Архивировать - No archived conversations have been found - The archive has been emptied successfully - Are you sure you want to empty the archive? All archived conversations will be permanently lost. + Нет архивных переписок + Архив успешно очищен + Очистить архив\? Все архивные переписки будут безвозвратно потеряны. Удалить все сообщения в этой переписке\? - Are you sure you want to archive %s? + Архивировать %s\? %d переписку From 94ef53eba27c5f3adcb18983f773a4a341eefd52 Mon Sep 17 00:00:00 2001 From: "Josep M. Ferrer" Date: Sun, 23 Jul 2023 05:22:07 +0000 Subject: [PATCH 3/5] Translated using Weblate (Catalan) Currently translated at 100.0% (100 of 100 strings) Translation: Simple Mobile Tools/Simple SMS Messenger Translate-URL: https://hosted.weblate.org/projects/simple-mobile-tools/simple-sms-messenger/ca/ --- app/src/main/res/values-ca/strings.xml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/main/res/values-ca/strings.xml b/app/src/main/res/values-ca/strings.xml index fbf7b9c0..abe4148d 100644 --- a/app/src/main/res/values-ca/strings.xml +++ b/app/src/main/res/values-ca/strings.xml @@ -59,17 +59,17 @@ Marcar com no llegit Me - Unarchive - Delete all archived conversations - Archive - Show archived conversations - Archive - No archived conversations have been found - The archive has been emptied successfully - Are you sure you want to empty the archive? All archived conversations will be permanently lost. + Desarxivar + Suprimeix totes les converses arxivades + Arxiva + Mostra les converses arxivades + Arxiu + No s\'ha trobat cap conversa arxivada + L\'arxiu s\'ha buidat correctament + Segur que voleu buidar l\'arxiu\? Totes les converses arxivades es perdran permanentment. Confirmeu que voleu suprimir tots els missatges d\'aquesta conversa\? - Are you sure you want to archive %s? + Segur que voleu arxivar %s\? %d conversa @@ -128,4 +128,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - + \ No newline at end of file From 461b1efbdebff0cb8e6ad89ea740e435fb232999 Mon Sep 17 00:00:00 2001 From: Sergio Marques Date: Mon, 24 Jul 2023 15:50:07 +0000 Subject: [PATCH 4/5] Translated using Weblate (Portuguese) Currently translated at 100.0% (100 of 100 strings) Translation: Simple Mobile Tools/Simple SMS Messenger Translate-URL: https://hosted.weblate.org/projects/simple-mobile-tools/simple-sms-messenger/pt/ --- app/src/main/res/values-pt/strings.xml | 38 +++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index da820c9a..c53faab3 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -49,10 +49,10 @@ Enviar agora Message details - Sender - Receiver - Sent at - Received at + Remetente + Destinatário + Enviada a + Recebida a SMS recebida Nova mensagem @@ -60,17 +60,17 @@ Marcar como não lida Eu - Unarchive - Delete all archived conversations - Archive - Show archived conversations - Archive - No archived conversations have been found - The archive has been emptied successfully - Are you sure you want to empty the archive? All archived conversations will be permanently lost. + Desarquivar + Eliminar todas as conversas arquivadas + Arquivo + Mostrar conversas arquivadas + Arquivo + Não existem conversas arquivadas + O arquivo foi limpo com sucesso + Tem a certeza de que pretende limpar o arquivo\? Todas as conversas arquivadas serão eliminadas. Tem a certeza de que pretende apagar todas as mensagens desta conversa\? - Are you sure you want to archive %s? + Tem a certeza de que pretende arquivar %s\? %d conversa @@ -84,11 +84,11 @@ %d mensagens - Keyword - Blocked keywords - Manage blocked keywords - You are not blocking any keywords. You may add keywords here to block all messages containing them. - Add a blocked keyword + Palavra-chave + Palavras-chave bloqueadas + Gerir palavras-chave bloqueadas + Não existem quaisquer palavras-chave bloqueadas. Pode adicionar as palavras-chave aqui para poder bloquear todas as mensagens que as contenham. + Adicionar palavra-chave de bloqueio Notificação no ecrã de bloqueio Remetente e mensagem Apenas remetente @@ -131,4 +131,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - + \ No newline at end of file From fb1b784a8006f57fd6851ed80a8ced6bb3406f5e Mon Sep 17 00:00:00 2001 From: en2sv Date: Mon, 24 Jul 2023 19:11:04 +0000 Subject: [PATCH 5/5] Translated using Weblate (Swedish) Currently translated at 100.0% (100 of 100 strings) Translation: Simple Mobile Tools/Simple SMS Messenger Translate-URL: https://hosted.weblate.org/projects/simple-mobile-tools/simple-sms-messenger/sv/ --- app/src/main/res/values-sv/strings.xml | 38 +++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index 038dfed4..dd34deab 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -59,17 +59,17 @@ Markera som oläst Jag - Unarchive - Delete all archived conversations - Archive - Show archived conversations - Archive - No archived conversations have been found - The archive has been emptied successfully - Are you sure you want to empty the archive? All archived conversations will be permanently lost. + Ångra arkivering + Ta bort alla arkiverade konversationer + Arkiv + Visa arkiverade konversationer + Arkivera + Inga arkiverade konversationer hittades + Arkivet har tömts + Är du säker på att du vill tömma arkivet\? Alla arkiverade konversationer tas bort permanent. Är du säker på att du vill ta bort alla meddelanden i konversationen\? - Are you sure you want to archive %s? + Är du säker på att du vill arkivera %s\? %d konversation @@ -81,11 +81,11 @@ %d meddelanden - Keyword - Blocked keywords - Manage blocked keywords - You are not blocking any keywords. You may add keywords here to block all messages containing them. - Add a blocked keyword + Nyckelord + Blockerade nyckelord + Hantera blockerade nyckelord + Du blockerar inte några nyckelord. Du kan lägga till nyckelord här för att blockera alla meddelanden som innehåller dem. + Lägg till ett blockerat nyckelord Synlighet för aviseringar på låsskärmen Avsändare och meddelande Endast avsändare @@ -109,10 +109,10 @@ Kan inte skicka meddelande utan nummer Det går inte att spara meddelandet på telefonen - Kunde inte skicka meddelande, ingen nätverkstjänst - Misslyckades att skicka meddelande, radion avstängd - Misslyckades att skicka meddelande, nätoperatörsfel - Misslyckades att skicka meddelande, felkod: %1$d + Det gick inte att skicka meddelandet, tjänsten är inte tillgänglig + Det gick inte att skicka meddelandet, radion är avstängd + Det gick inte att skicka meddelandet, operatörsfel + Det gick inte att skicka meddelandet, felkod: %d Det går inte att svara på korta koder som den här Du kan svara på korta koder med siffror som \"503501\", men inte på koder som innehåller bokstäver och siffror som \"AB-CD0\". Bilagans storlek överskrider maxgränsen för mms @@ -128,4 +128,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - + \ No newline at end of file