Create generic file binder to allow viewing any unhandled mms part type

This commit is contained in:
moezbhatti
2019-10-17 01:31:44 -04:00
parent d46193abba
commit 629e7a2d72
13 changed files with 229 additions and 60 deletions
@@ -27,6 +27,6 @@ fun MmsPart.isImage() = ContentType.isImageType(type)
fun MmsPart.isVideo() = ContentType.isVideoType(type)
fun MmsPart.isText() = ContentType.isTextType(type)
fun MmsPart.isText() = ContentType.TEXT_PLAIN == type
fun MmsPart.isVCard() = ContentType.TEXT_VCARD == type
fun MmsPart.isVCard() = ContentType.TEXT_VCARD == type
@@ -18,20 +18,13 @@
*/
package com.moez.QKSMS.mapper
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.Telephony
import com.moez.QKSMS.extensions.isImage
import com.moez.QKSMS.extensions.isSmil
import com.moez.QKSMS.extensions.isText
import com.moez.QKSMS.extensions.isVideo
import androidx.core.database.getIntOrNull
import androidx.core.database.getStringOrNull
import com.moez.QKSMS.model.MmsPart
import com.moez.QKSMS.util.tryOrNull
import timber.log.Timber
import java.io.BufferedReader
import java.io.InputStreamReader
import javax.inject.Inject
class CursorToPartImpl @Inject constructor(private val context: Context) : CursorToPart {
@@ -42,41 +35,12 @@ class CursorToPartImpl @Inject constructor(private val context: Context) : Curso
override fun map(from: Cursor) = MmsPart().apply {
id = from.getLong(from.getColumnIndexOrThrow(Telephony.Mms.Part._ID))
// Type will sometimes return null, resulting in a crash if we don't default to an empty string
type = from.getString(from.getColumnIndexOrThrow(Telephony.Mms.Part.CONTENT_TYPE)) ?: ""
val data = from.getString(from.getColumnIndexOrThrow(Telephony.Mms.Part._DATA))
when {
isSmil() || isImage() || isVideo() -> {
// Do nothing special
}
isText() -> {
text = if (data == null) {
from.getString(from.getColumnIndexOrThrow("text"))
} else {
val uri = ContentUris.withAppendedId(CONTENT_URI, id)
val sb = StringBuilder()
val inputStream = tryOrNull(false) { context.contentResolver.openInputStream(uri) }
inputStream?.use {
val isr = InputStreamReader(inputStream, "UTF-8")
val reader = BufferedReader(isr)
var temp = reader.readLine()
while (temp != null) {
sb.append(temp)
temp = reader.readLine()
}
}
sb.toString()
}
}
else -> Timber.v("Unhandled type: $type")
}
type = from.getStringOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.CONTENT_TYPE)) ?: "*/*"
seq = from.getIntOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.SEQ)) ?: -1
name = from.getStringOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.NAME))
?: from.getStringOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.CONTENT_LOCATION))
?.split("/")?.last()
text = from.getStringOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.TEXT))
}
override fun getPartsCursor(messageId: Long): Cursor? {
@@ -85,6 +85,10 @@ class QkRealmMigration : RealmMigration {
?.addField("blockingClient", Integer::class.java)
?.addField("blockReason", String::class.java)
realm.schema.get("MmsPart")
?.addField("seq", Integer::class.java, FieldAttribute.REQUIRED)
?.addField("name", String::class.java)
version++
}