Skip to content

Commit da117ec

Browse files
committed
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
1 parent abedc2a commit da117ec

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.simplemobiletools.smsmessenger.helpers
22

33
import android.content.Context
4+
import android.util.JsonToken
45
import com.google.gson.Gson
56
import com.google.gson.reflect.TypeToken
67
import com.simplemobiletools.commons.extensions.showErrorToast
@@ -40,43 +41,38 @@ class MessagesImporter(private val context: Context) {
4041
while (jsonReader.hasNext()) {
4142
jsonReader.beginObject()
4243
while (jsonReader.hasNext()) {
43-
if (jsonReader.nextName().equals("sms")) {
44-
if (config.importSms) {
45-
jsonReader.beginArray()
46-
while (jsonReader.hasNext()) {
47-
try {
48-
val message = gson.fromJson<SmsBackup>(jsonReader, smsMessageType)
49-
messageWriter.writeSmsMessage(message)
50-
messagesImported++
51-
} catch (e: Exception) {
52-
context.showErrorToast(e)
53-
messagesFailed++
54-
}
55-
}
56-
jsonReader.endArray()
57-
} else {
44+
val nextToken = jsonReader.peek()
45+
if (nextToken.ordinal == JsonToken.NAME.ordinal) {
46+
val msgType = jsonReader.nextName()
47+
48+
if ((!msgType.equals("sms") && !msgType.equals("mms")) ||
49+
(msgType.equals("sms") && !config.importSms) ||
50+
(msgType.equals("mms") && !config.importMms)) {
5851
jsonReader.skipValue()
52+
continue
5953
}
60-
}
6154

62-
if (jsonReader.nextName().equals("mms")) {
63-
if (config.importMms) {
64-
jsonReader.beginArray()
65-
66-
while (jsonReader.hasNext()) {
67-
try {
55+
jsonReader.beginArray()
56+
while (jsonReader.hasNext()) {
57+
try {
58+
if (msgType.equals("sms")) {
59+
val message = gson.fromJson<SmsBackup>(jsonReader, smsMessageType)
60+
messageWriter.writeSmsMessage(message)
61+
} else {
6862
val message = gson.fromJson<MmsBackup>(jsonReader, mmsMessageType)
6963
messageWriter.writeMmsMessage(message)
70-
messagesImported++
71-
} catch (e: Exception) {
72-
context.showErrorToast(e)
73-
messagesFailed++
7464
}
65+
66+
messagesImported++
67+
} catch (e: Exception) {
68+
context.showErrorToast(e)
69+
messagesFailed++
70+
7571
}
76-
jsonReader.endArray()
77-
} else {
78-
jsonReader.skipValue()
7972
}
73+
jsonReader.endArray()
74+
} else {
75+
jsonReader.skipValue()
8076
}
8177
}
8278

0 commit comments

Comments
 (0)