diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index 17cc4b90..4de5434b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -35,6 +35,7 @@ import com.simplemobiletools.smsmessenger.R import com.simplemobiletools.smsmessenger.activities.ThreadActivity import com.simplemobiletools.smsmessenger.databases.MessagesDatabase import com.simplemobiletools.smsmessenger.helpers.* +import com.simplemobiletools.smsmessenger.helpers.AttachmentUtils.parseAttachmentNames import com.simplemobiletools.smsmessenger.interfaces.AttachmentsDao import com.simplemobiletools.smsmessenger.interfaces.ConversationsDao import com.simplemobiletools.smsmessenger.interfaces.MessageAttachmentsDao @@ -307,7 +308,8 @@ fun Context.getMmsAttachment(id: Long, getImageResolutions: Boolean): MessageAtt val selectionArgs = arrayOf(id.toString()) val messageAttachment = MessageAttachment(id, "", arrayListOf()) - var attachmentName = "" + var attachmentNames: List? = null + var attachmentCount = 0 queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor -> val partId = cursor.getLongValue(Mms._ID) val mimetype = cursor.getStringValue(Mms.Part.CONTENT_TYPE) @@ -332,15 +334,14 @@ fun Context.getMmsAttachment(id: Long, getImageResolutions: Boolean): MessageAtt val attachment = Attachment(partId, id, fileUri.toString(), mimetype, width, height, "") messageAttachment.attachments.add(attachment) } else if (mimetype != "application/smil") { - val attachment = Attachment(partId, id, Uri.withAppendedPath(uri, partId.toString()).toString(), mimetype, 0, 0, attachmentName) + val attachment = Attachment( + partId, id, Uri.withAppendedPath(uri, partId.toString()).toString(), mimetype, 0, 0, attachmentNames?.getOrNull(attachmentCount) ?: "" + ) messageAttachment.attachments.add(attachment) + attachmentCount++ } else { - // todo: fix filename parsing, xml is shown some sometimes val text = cursor.getStringValue(Mms.Part.TEXT) - val cutName = text.substringAfter("ref src=\"").substringBefore("\"") - if (cutName.isNotEmpty()) { - attachmentName = cutName - } + attachmentNames = parseAttachmentNames(text) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/AttachmentUtils.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/AttachmentUtils.kt new file mode 100644 index 00000000..2fd6239f --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/AttachmentUtils.kt @@ -0,0 +1,72 @@ +package com.simplemobiletools.smsmessenger.helpers + +import android.util.Xml +import org.xmlpull.v1.XmlPullParser + +object AttachmentUtils { + + fun parseAttachmentNames(text: String): List { + val parser = Xml.newPullParser() + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false) + parser.setInput(text.reader()) + parser.nextTag() + + return readSmil(parser) + } + + private fun readSmil(parser: XmlPullParser): List { + parser.require(XmlPullParser.START_TAG, null, "smil") + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.eventType != XmlPullParser.START_TAG) { + continue + } + if (parser.name == "body") { + return readBody(parser) + } else { + skip(parser) + } + } + + return emptyList() + } + + private fun readBody(parser: XmlPullParser): List { + val names = mutableListOf() + parser.require(XmlPullParser.START_TAG, null, "body") + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.eventType != XmlPullParser.START_TAG) { + continue + } + if (parser.name == "par") { + parser.require(XmlPullParser.START_TAG, null, "par") + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.eventType != XmlPullParser.START_TAG) { + continue + } + if (parser.name == "ref") { + val value = parser.getAttributeValue(null, "src") + names.add(value) + parser.nextTag() + } + } + } else { + skip(parser) + } + } + return names + } + + private fun skip(parser: XmlPullParser) { + if (parser.eventType != XmlPullParser.START_TAG) { + throw IllegalStateException() + } + var depth = 1 + while (depth != 0) { + when (parser.next()) { + XmlPullParser.END_TAG -> depth-- + XmlPullParser.START_TAG -> depth++ + } + } + } + +}