Added and refactored xml import support
This commit is contained in:
parent
05ced83909
commit
ec6bf55025
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue