Fix import for partial exports

The previous implementation assumed that each JSON object would have an
"SMS" key and a "MMS" key, and in that order. This caused an exception
when it tried to read the next NAME token because the next token is
actually the closing bracket of the object. This commit fixes this issue
by checking that the next token is actually a NAME. If it is, we consume
the token and handle it according to its value, which may be either
"sms" or "mms". If it's neither of those, we skip it.

Fixes #646
This commit is contained in:
Kevin Cotugno 2023-05-19 06:54:39 -07:00
parent abedc2a18a
commit da117ecca4
1 changed files with 26 additions and 30 deletions

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.smsmessenger.helpers
import android.content.Context
import android.util.JsonToken
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.showErrorToast
@ -40,43 +41,38 @@ class MessagesImporter(private val context: Context) {
while (jsonReader.hasNext()) {
jsonReader.beginObject()
while (jsonReader.hasNext()) {
if (jsonReader.nextName().equals("sms")) {
if (config.importSms) {
jsonReader.beginArray()
while (jsonReader.hasNext()) {
try {
val nextToken = jsonReader.peek()
if (nextToken.ordinal == JsonToken.NAME.ordinal) {
val msgType = jsonReader.nextName()
if ((!msgType.equals("sms") && !msgType.equals("mms")) ||
(msgType.equals("sms") && !config.importSms) ||
(msgType.equals("mms") && !config.importMms)) {
jsonReader.skipValue()
continue
}
jsonReader.beginArray()
while (jsonReader.hasNext()) {
try {
if (msgType.equals("sms")) {
val message = gson.fromJson<SmsBackup>(jsonReader, smsMessageType)
messageWriter.writeSmsMessage(message)
messagesImported++
} catch (e: Exception) {
context.showErrorToast(e)
messagesFailed++
}
}
jsonReader.endArray()
} else {
jsonReader.skipValue()
}
}
if (jsonReader.nextName().equals("mms")) {
if (config.importMms) {
jsonReader.beginArray()
while (jsonReader.hasNext()) {
try {
} else {
val message = gson.fromJson<MmsBackup>(jsonReader, mmsMessageType)
messageWriter.writeMmsMessage(message)
messagesImported++
} catch (e: Exception) {
context.showErrorToast(e)
messagesFailed++
}
messagesImported++
} catch (e: Exception) {
context.showErrorToast(e)
messagesFailed++
}
jsonReader.endArray()
} else {
jsonReader.skipValue()
}
jsonReader.endArray()
} else {
jsonReader.skipValue()
}
}