mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
Load MMS parts lazily, display single image if available
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package data.mapper
|
||||
|
||||
import android.content.ContentUris
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony
|
||||
import data.model.MmsPart
|
||||
import timber.log.Timber
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import javax.inject.Inject
|
||||
|
||||
class CursorToPart @Inject constructor(private val context: Context) : Mapper<Cursor, MmsPart> {
|
||||
|
||||
companion object {
|
||||
val CONTENT_URI: Uri = Uri.parse("content://mms/part")
|
||||
}
|
||||
|
||||
override fun map(from: Cursor) = MmsPart().apply {
|
||||
id = from.getLong(from.getColumnIndexOrThrow(Telephony.Mms.Part._ID))
|
||||
messageId = from.getLong(from.getColumnIndexOrThrow(Telephony.Mms.Part.MSG_ID))
|
||||
type = from.getString(from.getColumnIndexOrThrow(Telephony.Mms.Part.CONTENT_TYPE))
|
||||
data = from.getString(from.getColumnIndexOrThrow(Telephony.Mms.Part._DATA))
|
||||
|
||||
when {
|
||||
isImage() -> image = getMmsImage(id)
|
||||
isText() -> text = getMmsText(id)
|
||||
else -> Timber.v("Unhandled type: $type")
|
||||
}
|
||||
}
|
||||
|
||||
private fun getMmsText(id: Long): String {
|
||||
val uri = ContentUris.withAppendedId(CONTENT_URI, id)
|
||||
val sb = StringBuilder()
|
||||
context.contentResolver.openInputStream(uri)?.use { inputStream ->
|
||||
val isr = InputStreamReader(inputStream, "UTF-8")
|
||||
val reader = BufferedReader(isr)
|
||||
var temp = reader.readLine()
|
||||
while (temp != null) {
|
||||
sb.append(temp)
|
||||
temp = reader.readLine()
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
private fun getMmsImage(id: Long): String? {
|
||||
return ContentUris.withAppendedId(CONTENT_URI, id).toString()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,7 @@ package data.model
|
||||
import android.content.ContentUris
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony.*
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmObject
|
||||
import io.realm.annotations.PrimaryKey
|
||||
|
||||
@@ -73,6 +74,7 @@ open class Message : RealmObject() {
|
||||
var mmsStatus: Int = 0
|
||||
var subject: String = ""
|
||||
var textContentType: String = ""
|
||||
var parts: RealmList<MmsPart> = RealmList()
|
||||
|
||||
fun getUri(): Uri {
|
||||
val baseUri = if (isMms()) Mms.CONTENT_URI else Sms.CONTENT_URI
|
||||
|
||||
@@ -18,11 +18,26 @@
|
||||
*/
|
||||
package data.model
|
||||
|
||||
import android.net.Uri
|
||||
import io.realm.RealmObject
|
||||
import io.realm.annotations.PrimaryKey
|
||||
|
||||
open class MmsPart : RealmObject() {
|
||||
|
||||
@PrimaryKey var id: Long = 0
|
||||
var messageId: Long = 0
|
||||
var type: String = ""
|
||||
var data: String? = null
|
||||
|
||||
var text: String? = null
|
||||
var image: String? = null
|
||||
|
||||
fun isImage()
|
||||
= "image/jpeg" == type
|
||||
|| "image/bmp" == type
|
||||
|| "image/gif" == type
|
||||
|| "image/jpg" == type
|
||||
|| "image/png" == type
|
||||
|
||||
fun isText() = "text/plain" == type
|
||||
|
||||
class MmsPart {
|
||||
var name = ""
|
||||
var mimeType = ""
|
||||
var data: ByteArray? = null
|
||||
var path: Uri? = null
|
||||
}
|
||||
@@ -27,15 +27,14 @@ import android.provider.Telephony.Sms
|
||||
import android.provider.Telephony.TextBasedSmsColumns
|
||||
import common.util.Keys
|
||||
import common.util.MessageUtils
|
||||
import common.util.extensions.asFlowable
|
||||
import common.util.extensions.asMaybe
|
||||
import common.util.extensions.insertOrUpdate
|
||||
import common.util.extensions.mapNotNull
|
||||
import common.util.extensions.*
|
||||
import data.mapper.CursorToConversation
|
||||
import data.mapper.CursorToPart
|
||||
import data.mapper.CursorToRecipient
|
||||
import data.model.Conversation
|
||||
import data.model.InboxItem
|
||||
import data.model.Message
|
||||
import data.model.MmsPart
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Maybe
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
@@ -43,6 +42,7 @@ import io.reactivex.rxkotlin.Flowables
|
||||
import io.reactivex.rxkotlin.toFlowable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmResults
|
||||
import io.realm.Sort
|
||||
import javax.inject.Inject
|
||||
@@ -53,6 +53,7 @@ class MessageRepository @Inject constructor(
|
||||
private val context: Context,
|
||||
private val messageIds: Keys,
|
||||
private val cursorToConversation: CursorToConversation,
|
||||
private val cursorToPart: CursorToPart,
|
||||
private val cursorToRecipient: CursorToRecipient) {
|
||||
|
||||
fun getConversations(archived: Boolean = false): Flowable<List<InboxItem>> {
|
||||
@@ -139,6 +140,20 @@ class MessageRepository @Inject constructor(
|
||||
.findAllSorted("date")
|
||||
}
|
||||
|
||||
fun loadParts(id: Long) {
|
||||
Realm.getDefaultInstance()?.use { realm ->
|
||||
realm.where(Message::class.java).equalTo("id", id).findFirst()?.let { message ->
|
||||
realm.executeTransaction {
|
||||
val parts = realm.copyToRealmOrUpdate(context.contentResolver.query(CursorToPart.CONTENT_URI, null,
|
||||
"${Telephony.Mms.Part.MSG_ID} = ?", arrayOf(message.contentId.toString()), null)
|
||||
.map { cursorToPart.map(it) })
|
||||
|
||||
message.parts = RealmList<MmsPart>().apply { addAll(parts) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the list of messages which should be shown in the notification
|
||||
* for a given conversation
|
||||
|
||||
Reference in New Issue
Block a user