From ec6bf55025e0f0772d23bccac5cecd647be0ea00 Mon Sep 17 00:00:00 2001 From: merkost Date: Thu, 20 Jul 2023 13:37:54 +1000 Subject: [PATCH] Added and refactored xml import support --- .../smsmessenger/activities/SettingsActivity.kt | 17 ++++------------- .../smsmessenger/helpers/MessagesImporter.kt | 15 ++++++++++----- .../smsmessenger/models/MessagesBackup.kt | 3 ++- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/SettingsActivity.kt index 47aac9c5..094f5e56 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/SettingsActivity.kt @@ -13,13 +13,10 @@ import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.smsmessenger.R import com.simplemobiletools.smsmessenger.dialogs.ExportMessagesDialog -import com.simplemobiletools.smsmessenger.dialogs.ImportMessagesDialog import com.simplemobiletools.smsmessenger.extensions.config import com.simplemobiletools.smsmessenger.helpers.* import com.simplemobiletools.smsmessenger.models.* import kotlinx.android.synthetic.main.activity_settings.* -import kotlinx.serialization.SerializationException -import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import java.util.* @@ -28,7 +25,7 @@ import kotlin.system.exitProcess class SettingsActivity : SimpleActivity() { private var blockedNumbersAtPause = -1 private val messagesFileType = "application/json" - private val messageImportFileType = "application/json, application/xml, text/plain" + private val messageImportFileTypes = listOf("application/json", "application/xml", "text/xml") override fun onCreate(savedInstanceState: Bundle?) { isMaterialActivity = true @@ -81,16 +78,10 @@ class SettingsActivity : SimpleActivity() { } } - private val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri -> + private val getContent = registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri -> if (uri != null) { toast(R.string.importing) - MessagesImporter(this).importMessages(uri) { deserializedList -> - if (deserializedList.isEmpty()) { - toast(R.string.no_entries_for_importing) - } else { - ImportMessagesDialog(this, deserializedList) - } - } + MessagesImporter(this).importMessages(uri) } } @@ -111,7 +102,7 @@ class SettingsActivity : SimpleActivity() { private fun setupMessagesImport() { settings_import_messages_holder.setOnClickListener { - getContent.launch(messageImportFileType) + getContent.launch(messageImportFileTypes.toTypedArray()) } } 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 8d04ec47..66eaff0c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt @@ -27,15 +27,12 @@ class MessagesImporter(private val activity: SimpleActivity) { fun importMessages(uri: Uri) { try { val fileType = activity.contentResolver.getType(uri).orEmpty() - val isXml = isXmlMimeType(fileType) || (uri.path?.endsWith("txt") == true && isFileXml(uri)) - if (isXml) { getInputStreamFromUri(uri)!!.importXml() } else { importJson(uri) } - } catch (e: Exception) { activity.showErrorToast(e) } @@ -133,7 +130,11 @@ class MessagesImporter(private val activity: SimpleActivity) { } refreshMessages() } - // TODO: Add result to xml import + when { + messagesFailed > 0 && messagesImported > 0 -> activity.toast(R.string.importing_some_entries_failed) + messagesFailed > 0 -> activity.toast(R.string.importing_failed) + else -> activity.toast(R.string.importing_successful) + } } private fun XmlPullParser.readSms(): SmsBackup { @@ -183,6 +184,10 @@ class MessagesImporter(private val activity: SimpleActivity) { } private fun isXmlMimeType(mimeType: String): Boolean { - return mimeType.equals("application/xml", ignoreCase = true) + return mimeType.equals("application/xml", ignoreCase = true) || mimeType.equals("text/xml", ignoreCase = true) + } + + private fun isJsonMimeType(mimeType: String): Boolean { + return mimeType.equals("application/json", ignoreCase = true) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt index 9f97aac2..a01137c9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.smsmessenger.models import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.SerializationException import kotlinx.serialization.json.* @Serializable(with = BackupSerializer::class) @@ -17,7 +18,7 @@ object BackupSerializer : return when (element.jsonObject["backupType"]?.jsonPrimitive?.content) { "sms" -> SmsBackup.serializer() "mms" -> MmsBackup.serializer() - else -> throw Exception("ERROR: No Serializer found. Serialization failed.") + else -> throw SerializationException("ERROR: No Serializer found. Serialization failed.") } } }