Merge pull request #616 from kcotugno/stream-json-decode-on-import

Stream json during import fixes #329
This commit is contained in:
Tibor Kaputa 2023-03-25 09:02:56 +01:00 committed by GitHub
commit d6f8d58867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 53 additions and 26 deletions

View File

@ -1,14 +1,14 @@
package com.simplemobiletools.smsmessenger.helpers package com.simplemobiletools.smsmessenger.helpers
import android.content.Context import android.content.Context
import android.provider.Telephony.*
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.extensions.* import com.simplemobiletools.smsmessenger.extensions.*
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.* import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.*
import com.simplemobiletools.smsmessenger.models.ExportedMessage import com.simplemobiletools.smsmessenger.models.MmsBackup
import com.simplemobiletools.smsmessenger.models.SmsBackup
import java.io.File import java.io.File
class MessagesImporter(private val context: Context) { class MessagesImporter(private val context: Context) {
@ -32,33 +32,59 @@ class MessagesImporter(private val context: Context) {
} }
inputStream.bufferedReader().use { reader -> inputStream.bufferedReader().use { reader ->
val json = reader.readText() val jsonReader = gson.newJsonReader(reader)
val type = object : TypeToken<List<ExportedMessage>>() {}.type val smsMessageType = object : TypeToken<SmsBackup>() {}.type
val messages = gson.fromJson<List<ExportedMessage>>(json, type) val mmsMessageType = object : TypeToken<MmsBackup>() {}.type
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) jsonReader.beginArray()
for (message in messages) { while (jsonReader.hasNext()) {
if (config.importSms) { jsonReader.beginObject()
message.sms?.forEach { backup -> while (jsonReader.hasNext()) {
messageWriter.writeSmsMessage(backup) if (jsonReader.nextName().equals("sms")) {
messagesImported++ if (config.importSms) {
onProgress.invoke(totalMessages, messagesImported) jsonReader.beginArray()
} while (jsonReader.hasNext()) {
} try {
if (config.importMms) { val message = gson.fromJson<SmsBackup>(jsonReader, smsMessageType)
message.mms?.forEach { backup -> messageWriter.writeSmsMessage(message)
messageWriter.writeMmsMessage(backup) messagesImported++
messagesImported++ } catch (e: Exception) {
onProgress.invoke(totalMessages, messagesImported) context.showErrorToast(e)
messagesFailed++
}
}
jsonReader.endArray()
} else {
jsonReader.skipValue()
}
}
if (jsonReader.nextName().equals("mms")) {
if (config.importMms) {
jsonReader.beginArray()
while (jsonReader.hasNext()) {
try {
val message = gson.fromJson<MmsBackup>(jsonReader, mmsMessageType)
messageWriter.writeMmsMessage(message)
messagesImported++
} catch (e: Exception) {
context.showErrorToast(e)
messagesFailed++
}
}
jsonReader.endArray()
} else {
jsonReader.skipValue()
}
} }
} }
jsonReader.endObject()
refreshMessages() refreshMessages()
} }
jsonReader.endArray()
} }
} catch (e: Exception) { } catch (e: Exception) {
context.showErrorToast(e) context.showErrorToast(e)
@ -67,8 +93,9 @@ class MessagesImporter(private val context: Context) {
callback.invoke( callback.invoke(
when { when {
messagesImported == 0 -> IMPORT_FAIL messagesImported == 0 && messagesFailed == 0 -> IMPORT_NOTHING_NEW
messagesFailed > 0 -> IMPORT_PARTIAL messagesFailed > 0 && messagesImported > 0 -> IMPORT_PARTIAL
messagesFailed > 0 -> IMPORT_FAIL
else -> IMPORT_OK else -> IMPORT_OK
} }
) )