|
1 | 1 | package com.simplemobiletools.smsmessenger.helpers |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import android.util.JsonToken |
4 | 5 | import com.google.gson.Gson |
5 | 6 | import com.google.gson.reflect.TypeToken |
6 | 7 | import com.simplemobiletools.commons.extensions.showErrorToast |
7 | 8 | import com.simplemobiletools.commons.helpers.ensureBackgroundThread |
8 | | -import com.simplemobiletools.smsmessenger.extensions.* |
9 | | -import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.* |
| 9 | +import com.simplemobiletools.smsmessenger.extensions.config |
| 10 | +import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_FAIL |
| 11 | +import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_NOTHING_NEW |
| 12 | +import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_OK |
| 13 | +import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_PARTIAL |
10 | 14 | import com.simplemobiletools.smsmessenger.models.MmsBackup |
11 | 15 | import com.simplemobiletools.smsmessenger.models.SmsBackup |
12 | 16 | import java.io.File |
@@ -40,43 +44,39 @@ class MessagesImporter(private val context: Context) { |
40 | 44 | while (jsonReader.hasNext()) { |
41 | 45 | jsonReader.beginObject() |
42 | 46 | 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 { |
| 47 | + val nextToken = jsonReader.peek() |
| 48 | + if (nextToken.ordinal == JsonToken.NAME.ordinal) { |
| 49 | + val msgType = jsonReader.nextName() |
| 50 | + |
| 51 | + if ((!msgType.equals("sms") && !msgType.equals("mms")) || |
| 52 | + (msgType.equals("sms") && !config.importSms) || |
| 53 | + (msgType.equals("mms") && !config.importMms) |
| 54 | + ) { |
58 | 55 | jsonReader.skipValue() |
| 56 | + continue |
59 | 57 | } |
60 | | - } |
61 | 58 |
|
62 | | - if (jsonReader.nextName().equals("mms")) { |
63 | | - if (config.importMms) { |
64 | | - jsonReader.beginArray() |
65 | | - |
66 | | - while (jsonReader.hasNext()) { |
67 | | - try { |
| 59 | + jsonReader.beginArray() |
| 60 | + while (jsonReader.hasNext()) { |
| 61 | + try { |
| 62 | + if (msgType.equals("sms")) { |
| 63 | + val message = gson.fromJson<SmsBackup>(jsonReader, smsMessageType) |
| 64 | + messageWriter.writeSmsMessage(message) |
| 65 | + } else { |
68 | 66 | val message = gson.fromJson<MmsBackup>(jsonReader, mmsMessageType) |
69 | 67 | messageWriter.writeMmsMessage(message) |
70 | | - messagesImported++ |
71 | | - } catch (e: Exception) { |
72 | | - context.showErrorToast(e) |
73 | | - messagesFailed++ |
74 | 68 | } |
| 69 | + |
| 70 | + messagesImported++ |
| 71 | + } catch (e: Exception) { |
| 72 | + context.showErrorToast(e) |
| 73 | + messagesFailed++ |
| 74 | + |
75 | 75 | } |
76 | | - jsonReader.endArray() |
77 | | - } else { |
78 | | - jsonReader.skipValue() |
79 | 76 | } |
| 77 | + jsonReader.endArray() |
| 78 | + } else { |
| 79 | + jsonReader.skipValue() |
80 | 80 | } |
81 | 81 | } |
82 | 82 |
|
|
0 commit comments