From 27a9cf43698413adfcc0b866f5c6eb09576c00df Mon Sep 17 00:00:00 2001 From: darthpaul Date: Tue, 21 Sep 2021 15:19:09 +0100 Subject: [PATCH] Handle cases where sms or mms does not exist in import or export --- .../smsmessenger/helpers/MessagesExporter.kt | 4 ++-- .../smsmessenger/helpers/MessagesImporter.kt | 12 ++++++++---- .../smsmessenger/helpers/MessagesReader.kt | 14 +++++++++++--- .../smsmessenger/models/ExportedMessage.kt | 4 ++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesExporter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesExporter.kt index 3b6693f3..49c2d4b6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesExporter.kt @@ -32,7 +32,7 @@ class MessagesExporter(private val context: Context) { val totalMessages = messageReader.getMessagesCount() for (threadId in conversationIds) { writer.beginObject() - if (config.exportSms) { + if (config.exportSms && messageReader.getSmsCount() > 0) { writer.name("sms") writer.beginArray() messageReader.forEachSms(threadId) { @@ -43,7 +43,7 @@ class MessagesExporter(private val context: Context) { writer.endArray() } - if (config.exportMms) { + if (config.exportMms && messageReader.getMmsCount() > 0) { writer.name("mms") writer.beginArray() messageReader.forEachMms(threadId) { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt index a24f1b76..167b191c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt @@ -16,7 +16,7 @@ import java.io.File class MessagesImporter(private val context: Context) { enum class ImportResult { - IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL + IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW } private val gson = Gson() @@ -39,18 +39,22 @@ class MessagesImporter(private val context: Context) { val json = reader.readText() val type = object : TypeToken>() {}.type val messages = gson.fromJson>(json, type) - val totalMessages = messages.flatMap { it.sms }.size + messages.flatMap { it.mms }.size + val totalMessages = messages.flatMap { it.sms ?: emptyList() }.size + messages.flatMap { it.mms ?: emptyList() }.size + if (totalMessages <= 0) { + callback.invoke(IMPORT_NOTHING_NEW) + return@ensureBackgroundThread + } onProgress.invoke(totalMessages, messagesImported) for (message in messages) { if (config.importSms) { - message.sms.forEach { backup -> + message.sms?.forEach { backup -> messageWriter.writeSmsMessage(backup) messagesImported++ onProgress.invoke(totalMessages, messagesImported) } } if (config.importMms) { - message.mms.forEach { backup -> + message.mms?.forEach { backup -> messageWriter.writeMmsMessage(backup) messagesImported++ onProgress.invoke(totalMessages, messagesImported) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesReader.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesReader.kt index 4c62c8c1..175ad735 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesReader.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesReader.kt @@ -105,7 +105,7 @@ class MessagesReader(private val context: Context) { val transactionId = cursor.getStringValueOrNull(Mms.TRANSACTION_ID) val parts = getParts(mmsId) - val addresses = getMMSAddresses(mmsId) + val addresses = getMmsAddresses(mmsId) block( MmsBackup( creator, @@ -197,7 +197,7 @@ class MessagesReader(private val context: Context) { } @SuppressLint("NewApi") - private fun getMMSAddresses(messageId: Long): List { + private fun getMmsAddresses(messageId: Long): List { val addresses = mutableListOf() val uri = if (isRPlus()) Mms.Addr.getAddrUriForMessage(messageId.toString()) else Uri.parse("content://mms/$messageId/addr") val projection = arrayOf(Mms.Addr.ADDRESS, Mms.Addr.TYPE, Mms.Addr.CHARSET) @@ -213,7 +213,15 @@ class MessagesReader(private val context: Context) { } fun getMessagesCount(): Int { - return countRows(Sms.CONTENT_URI) + countRows(Mms.CONTENT_URI) + return getSmsCount() + getMmsCount() + } + + fun getMmsCount(): Int { + return countRows(Mms.CONTENT_URI) + } + + fun getSmsCount(): Int { + return countRows(Sms.CONTENT_URI) } private fun countRows(uri: Uri): Int { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ExportedMessage.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ExportedMessage.kt index 35a5fddc..06253d48 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ExportedMessage.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ExportedMessage.kt @@ -4,7 +4,7 @@ import com.google.gson.annotations.SerializedName data class ExportedMessage( @SerializedName("sms") - val sms: List, + val sms: List?, @SerializedName("mms") - val mms: List, + val mms: List?, )