Handle cases where sms or mms does not exist in import or export

This commit is contained in:
darthpaul 2021-09-21 15:19:09 +01:00
parent 27180c6053
commit 27a9cf4369
4 changed files with 23 additions and 11 deletions

View File

@ -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) {

View File

@ -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<List<ExportedMessage>>() {}.type
val messages = gson.fromJson<List<ExportedMessage>>(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)

View File

@ -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<MmsAddress> {
private fun getMmsAddresses(messageId: Long): List<MmsAddress> {
val addresses = mutableListOf<MmsAddress>()
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 {

View File

@ -4,7 +4,7 @@ import com.google.gson.annotations.SerializedName
data class ExportedMessage(
@SerializedName("sms")
val sms: List<SmsBackup>,
val sms: List<SmsBackup>?,
@SerializedName("mms")
val mms: List<MmsBackup>,
val mms: List<MmsBackup>?,
)