Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 629e7a2d authored by moezbhatti's avatar moezbhatti
Browse files

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

parent d46193ab
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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
+8 −44
Original line number Diff line number Diff line
@@ -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? {
+4 −0
Original line number Diff line number Diff line
@@ -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++
        }

+1 −0
Original line number Diff line number Diff line
@@ -105,6 +105,7 @@ open class Message : RealmObject() {
            isSms() -> body

            else -> parts
                    .filter { it.type == "text/plain" }
                    .mapNotNull { it.text }
                    .joinToString("\n") { text -> text }
        }
+2 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ open class MmsPart : RealmObject() {

    @PrimaryKey var id: Long = 0
    var type: String = ""
    var seq: Int = -1
    var name: String? = null
    var text: String? = null

    @LinkingObjects("parts")
Loading