From 014ac2a4cff3a1329f62479e2e45c1f0ef957ba0 Mon Sep 17 00:00:00 2001 From: merkost Date: Mon, 24 Jul 2023 13:40:38 +1000 Subject: [PATCH] Added invalid_file_format toast on XML importing --- .../smsmessenger/helpers/MessagesImporter.kt | 70 ++++++++++--------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt index e9a70bf8..af95f391 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt @@ -94,48 +94,52 @@ class MessagesImporter(private val activity: SimpleActivity) { } private fun InputStream.importXml() { - bufferedReader().use { reader -> - val xmlParser = Xml.newPullParser().apply { - setInput(reader) - } - - xmlParser.nextTag() - xmlParser.require(XmlPullParser.START_TAG, null, "smses") - - var depth = 1 - while (depth != 0) { - when (xmlParser.next()) { - XmlPullParser.END_TAG -> depth-- - XmlPullParser.START_TAG -> depth++ + try { + bufferedReader().use { reader -> + val xmlParser = Xml.newPullParser().apply { + setInput(reader) } - if (xmlParser.eventType != XmlPullParser.START_TAG) { - continue - } + xmlParser.nextTag() + xmlParser.require(XmlPullParser.START_TAG, null, "smses") - try { - if (xmlParser.name == "sms") { - if (config.importSms) { - val message = xmlParser.readSms() - messageWriter.writeSmsMessage(message) - messagesImported++ + var depth = 1 + while (depth != 0) { + when (xmlParser.next()) { + XmlPullParser.END_TAG -> depth-- + XmlPullParser.START_TAG -> depth++ + } + + if (xmlParser.eventType != XmlPullParser.START_TAG) { + continue + } + + try { + if (xmlParser.name == "sms") { + if (config.importSms) { + val message = xmlParser.readSms() + messageWriter.writeSmsMessage(message) + messagesImported++ + } else { + xmlParser.skip() + } } else { xmlParser.skip() } - } else { - xmlParser.skip() + } catch (e: Exception) { + activity.showErrorToast(e) + messagesFailed++ } - } catch (e: Exception) { - activity.showErrorToast(e) - messagesFailed++ } + refreshMessages() } - refreshMessages() - } - when { - messagesFailed > 0 && messagesImported > 0 -> activity.toast(R.string.importing_some_entries_failed) - messagesFailed > 0 -> activity.toast(R.string.importing_failed) - else -> activity.toast(R.string.importing_successful) + when { + messagesFailed > 0 && messagesImported > 0 -> activity.toast(R.string.importing_some_entries_failed) + messagesFailed > 0 -> activity.toast(R.string.importing_failed) + else -> activity.toast(R.string.importing_successful) + } + } catch (_: Exception) { + activity.toast(R.string.invalid_file_format) } }