forked from upstream-mirrors/quik
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.content.ContentUris
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.Telephony.*
|
import android.provider.Telephony.*
|
||||||
|
import io.realm.RealmList
|
||||||
import io.realm.RealmObject
|
import io.realm.RealmObject
|
||||||
import io.realm.annotations.PrimaryKey
|
import io.realm.annotations.PrimaryKey
|
||||||
|
|
||||||
@@ -73,6 +74,7 @@ open class Message : RealmObject() {
|
|||||||
var mmsStatus: Int = 0
|
var mmsStatus: Int = 0
|
||||||
var subject: String = ""
|
var subject: String = ""
|
||||||
var textContentType: String = ""
|
var textContentType: String = ""
|
||||||
|
var parts: RealmList<MmsPart> = RealmList()
|
||||||
|
|
||||||
fun getUri(): Uri {
|
fun getUri(): Uri {
|
||||||
val baseUri = if (isMms()) Mms.CONTENT_URI else Sms.CONTENT_URI
|
val baseUri = if (isMms()) Mms.CONTENT_URI else Sms.CONTENT_URI
|
||||||
|
|||||||
@@ -18,11 +18,26 @@
|
|||||||
*/
|
*/
|
||||||
package data.model
|
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 android.provider.Telephony.TextBasedSmsColumns
|
||||||
import common.util.Keys
|
import common.util.Keys
|
||||||
import common.util.MessageUtils
|
import common.util.MessageUtils
|
||||||
import common.util.extensions.asFlowable
|
import common.util.extensions.*
|
||||||
import common.util.extensions.asMaybe
|
|
||||||
import common.util.extensions.insertOrUpdate
|
|
||||||
import common.util.extensions.mapNotNull
|
|
||||||
import data.mapper.CursorToConversation
|
import data.mapper.CursorToConversation
|
||||||
|
import data.mapper.CursorToPart
|
||||||
import data.mapper.CursorToRecipient
|
import data.mapper.CursorToRecipient
|
||||||
import data.model.Conversation
|
import data.model.Conversation
|
||||||
import data.model.InboxItem
|
import data.model.InboxItem
|
||||||
import data.model.Message
|
import data.model.Message
|
||||||
|
import data.model.MmsPart
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import io.reactivex.Maybe
|
import io.reactivex.Maybe
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
@@ -43,6 +42,7 @@ import io.reactivex.rxkotlin.Flowables
|
|||||||
import io.reactivex.rxkotlin.toFlowable
|
import io.reactivex.rxkotlin.toFlowable
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
|
import io.realm.RealmList
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -53,6 +53,7 @@ class MessageRepository @Inject constructor(
|
|||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val messageIds: Keys,
|
private val messageIds: Keys,
|
||||||
private val cursorToConversation: CursorToConversation,
|
private val cursorToConversation: CursorToConversation,
|
||||||
|
private val cursorToPart: CursorToPart,
|
||||||
private val cursorToRecipient: CursorToRecipient) {
|
private val cursorToRecipient: CursorToRecipient) {
|
||||||
|
|
||||||
fun getConversations(archived: Boolean = false): Flowable<List<InboxItem>> {
|
fun getConversations(archived: Boolean = false): Flowable<List<InboxItem>> {
|
||||||
@@ -139,6 +140,20 @@ class MessageRepository @Inject constructor(
|
|||||||
.findAllSorted("date")
|
.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
|
* Retrieves the list of messages which should be shown in the notification
|
||||||
* for a given conversation
|
* for a given conversation
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package interactor
|
||||||
|
|
||||||
|
import data.repository.MessageRepository
|
||||||
|
import io.reactivex.Flowable
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class LoadMmsParts @Inject constructor(private val messageRepo: MessageRepository): Interactor<Long, Long>() {
|
||||||
|
|
||||||
|
override fun buildObservable(params: Long): Flowable<Long> {
|
||||||
|
return Flowable.just(params)
|
||||||
|
.doOnNext { messageId -> messageRepo.loadParts(messageId) }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ import com.jakewharton.rxbinding2.view.RxView
|
|||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import common.util.Colors
|
import common.util.Colors
|
||||||
import common.util.DateFormatter
|
import common.util.DateFormatter
|
||||||
|
import common.util.GlideApp
|
||||||
import common.util.extensions.dpToPx
|
import common.util.extensions.dpToPx
|
||||||
import common.util.extensions.setBackgroundTint
|
import common.util.extensions.setBackgroundTint
|
||||||
import common.util.extensions.setPadding
|
import common.util.extensions.setPadding
|
||||||
@@ -34,13 +35,14 @@ import common.util.extensions.setVisible
|
|||||||
import data.model.Contact
|
import data.model.Contact
|
||||||
import data.model.Message
|
import data.model.Message
|
||||||
import data.model.PhoneNumber
|
import data.model.PhoneNumber
|
||||||
import presentation.common.base.QkViewHolder
|
import interactor.LoadMmsParts
|
||||||
import io.reactivex.disposables.CompositeDisposable
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.PublishSubject
|
||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
import io.realm.RealmRecyclerViewAdapter
|
import io.realm.RealmRecyclerViewAdapter
|
||||||
import kotlinx.android.synthetic.main.message_list_item_in.view.*
|
import kotlinx.android.synthetic.main.message_list_item_in.view.*
|
||||||
|
import presentation.common.base.QkViewHolder
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -48,7 +50,8 @@ import javax.inject.Inject
|
|||||||
class MessagesAdapter @Inject constructor(
|
class MessagesAdapter @Inject constructor(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val colors: Colors,
|
private val colors: Colors,
|
||||||
private val dateFormatter: DateFormatter)
|
private val dateFormatter: DateFormatter,
|
||||||
|
private val loadMmsParts: LoadMmsParts)
|
||||||
: RealmRecyclerViewAdapter<Message, QkViewHolder>(null, true) {
|
: RealmRecyclerViewAdapter<Message, QkViewHolder>(null, true) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -88,7 +91,12 @@ class MessagesAdapter @Inject constructor(
|
|||||||
return QkViewHolder(view)
|
return QkViewHolder(view)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView?) {
|
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
||||||
|
super.onAttachedToRecyclerView(recyclerView)
|
||||||
|
disposables += loadMmsParts
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
|
||||||
super.onDetachedFromRecyclerView(recyclerView)
|
super.onDetachedFromRecyclerView(recyclerView)
|
||||||
disposables.clear()
|
disposables.clear()
|
||||||
}
|
}
|
||||||
@@ -100,6 +108,17 @@ class MessagesAdapter @Inject constructor(
|
|||||||
RxView.clicks(view).subscribe { Timber.v(message.toString()) }
|
RxView.clicks(view).subscribe { Timber.v(message.toString()) }
|
||||||
RxView.longClicks(view).subscribe { longClicks.onNext(message) }
|
RxView.longClicks(view).subscribe { longClicks.onNext(message) }
|
||||||
|
|
||||||
|
// If it's an MMS and the parts haven't been loaded, kick off a load
|
||||||
|
if (message.isMms() && message.parts.isEmpty()) {
|
||||||
|
loadMmsParts.execute(message.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
view.image.setVisible(false)
|
||||||
|
message.parts.firstOrNull { it.image != null }?.let { part ->
|
||||||
|
view.image.setVisible(true)
|
||||||
|
GlideApp.with(context).load(part.image).into(view.image)
|
||||||
|
}
|
||||||
|
|
||||||
bindGrouping(view, position)
|
bindGrouping(view, position)
|
||||||
|
|
||||||
val age = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - message.date)
|
val age = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - message.date)
|
||||||
|
|||||||
@@ -64,6 +64,11 @@
|
|||||||
android:paddingTop="8dp"
|
android:paddingTop="8dp"
|
||||||
tools:backgroundTint="@color/tools_theme">
|
tools:backgroundTint="@color/tools_theme">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/image"
|
||||||
|
android:layout_width="128dp"
|
||||||
|
android:layout_height="128dp" />
|
||||||
|
|
||||||
<presentation.common.widget.TightTextView
|
<presentation.common.widget.TightTextView
|
||||||
android:id="@+id/body"
|
android:id="@+id/body"
|
||||||
style="@style/TextPrimary"
|
style="@style/TextPrimary"
|
||||||
|
|||||||
@@ -58,6 +58,11 @@
|
|||||||
android:paddingTop="8dp"
|
android:paddingTop="8dp"
|
||||||
tools:backgroundTint="@color/white">
|
tools:backgroundTint="@color/white">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/image"
|
||||||
|
android:layout_width="128dp"
|
||||||
|
android:layout_height="128dp" />
|
||||||
|
|
||||||
<presentation.common.widget.TightTextView
|
<presentation.common.widget.TightTextView
|
||||||
android:id="@+id/body"
|
android:id="@+id/body"
|
||||||
style="@style/TextPrimary"
|
style="@style/TextPrimary"
|
||||||
|
|||||||
Reference in New Issue
Block a user