diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/BackupType.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/BackupType.kt new file mode 100644 index 00000000..c6132aee --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/BackupType.kt @@ -0,0 +1,13 @@ +package com.simplemobiletools.smsmessenger.models + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +enum class BackupType { + @SerialName("sms") + SMS, + + @SerialName("mms") + MMS, +} diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt new file mode 100644 index 00000000..9f97aac2 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt @@ -0,0 +1,23 @@ +package com.simplemobiletools.smsmessenger.models + +import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.* + +@Serializable(with = BackupSerializer::class) +sealed class MessagesBackup() { + @SerialName("backupType") + abstract val backupType: BackupType +} + +object BackupSerializer : + JsonContentPolymorphicSerializer(MessagesBackup::class) { + override fun selectDeserializer(element: JsonElement): DeserializationStrategy { + return when (element.jsonObject["backupType"]?.jsonPrimitive?.content) { + "sms" -> SmsBackup.serializer() + "mms" -> MmsBackup.serializer() + else -> throw Exception("ERROR: No Serializer found. Serialization failed.") + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MmsBackup.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MmsBackup.kt index c2c2c93c..3d0c9d25 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MmsBackup.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MmsBackup.kt @@ -46,7 +46,9 @@ data class MmsBackup( val addresses: List, @SerializedName("parts") val parts: List, -) { + + override val backupType: BackupType = BackupType.MMS, +): MessagesBackup() { fun toContentValues(): ContentValues { return contentValuesOf( diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/SmsBackup.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/SmsBackup.kt index 41b128e1..ff3c9a39 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/SmsBackup.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/SmsBackup.kt @@ -30,8 +30,10 @@ data class SmsBackup( @SerializedName("type") val type: Int, @SerializedName("service_center") - val serviceCenter: String? -) { + val serviceCenter: String?, + + override val backupType: BackupType = BackupType.SMS, + ): MessagesBackup() { fun toContentValues(): ContentValues { return contentValuesOf(