adding MMS attachments at the Message class
This commit is contained in:
parent
44a1d85436
commit
500f2bc8fe
|
@ -3,8 +3,6 @@ package com.simplemobiletools.smsmessenger.extensions
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Bitmap
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.ContactsContract
|
import android.provider.ContactsContract
|
||||||
import android.provider.ContactsContract.CommonDataKinds
|
import android.provider.ContactsContract.CommonDataKinds
|
||||||
|
@ -20,7 +18,7 @@ import com.simplemobiletools.commons.helpers.isMarshmallowPlus
|
||||||
import com.simplemobiletools.commons.helpers.isQPlus
|
import com.simplemobiletools.commons.helpers.isQPlus
|
||||||
import com.simplemobiletools.smsmessenger.helpers.Config
|
import com.simplemobiletools.smsmessenger.helpers.Config
|
||||||
import com.simplemobiletools.smsmessenger.models.Contact
|
import com.simplemobiletools.smsmessenger.models.Contact
|
||||||
import com.simplemobiletools.smsmessenger.models.MMS
|
import com.simplemobiletools.smsmessenger.models.MessageAttachment
|
||||||
import com.simplemobiletools.smsmessenger.models.Message
|
import com.simplemobiletools.smsmessenger.models.Message
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.ArrayList
|
import kotlin.collections.ArrayList
|
||||||
|
@ -62,7 +60,7 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
|
||||||
val read = cursor.getIntValue(Sms.READ) == 1
|
val read = cursor.getIntValue(Sms.READ) == 1
|
||||||
val thread = cursor.getIntValue(Sms.THREAD_ID)
|
val thread = cursor.getIntValue(Sms.THREAD_ID)
|
||||||
val participant = Contact(0, senderName, "", senderNumber, false)
|
val participant = Contact(0, senderName, "", senderNumber, false)
|
||||||
val message = Message(id, body, type, arrayListOf(participant), date, read, thread)
|
val message = Message(id, body, type, arrayListOf(participant), date, read, thread, null)
|
||||||
messages.add(message)
|
messages.add(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,8 +105,8 @@ fun Context.getMMS(threadId: Int? = null): ArrayList<Message> {
|
||||||
val read = cursor.getIntValue(Mms.READ) == 1
|
val read = cursor.getIntValue(Mms.READ) == 1
|
||||||
val thread = cursor.getIntValue(Mms.THREAD_ID)
|
val thread = cursor.getIntValue(Mms.THREAD_ID)
|
||||||
val participants = getThreadParticipants(thread)
|
val participants = getThreadParticipants(thread)
|
||||||
val mms = getMmsContent(id)
|
val attachment = getMmsAttachment(id)
|
||||||
val message = Message(id, mms?.text ?: "", type, participants, date, read, thread)
|
val message = Message(id, attachment?.text ?: "", type, participants, date, read, thread, attachment)
|
||||||
messages.add(message)
|
messages.add(message)
|
||||||
}
|
}
|
||||||
return messages
|
return messages
|
||||||
|
@ -116,7 +114,7 @@ fun Context.getMMS(threadId: Int? = null): ArrayList<Message> {
|
||||||
|
|
||||||
// based on https://stackoverflow.com/a/6446831/1967672
|
// based on https://stackoverflow.com/a/6446831/1967672
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
fun Context.getMmsContent(id: Int): MMS? {
|
fun Context.getMmsAttachment(id: Int): MessageAttachment? {
|
||||||
val uri = if (isQPlus()) {
|
val uri = if (isQPlus()) {
|
||||||
Mms.Part.CONTENT_URI
|
Mms.Part.CONTENT_URI
|
||||||
} else {
|
} else {
|
||||||
|
@ -130,19 +128,20 @@ fun Context.getMmsContent(id: Int): MMS? {
|
||||||
)
|
)
|
||||||
val selection = "${Mms.Part.MSG_ID} = ?"
|
val selection = "${Mms.Part.MSG_ID} = ?"
|
||||||
val selectionArgs = arrayOf(id.toString())
|
val selectionArgs = arrayOf(id.toString())
|
||||||
val mms = MMS(id, "", null)
|
val attachment = MessageAttachment(id, "", null, "")
|
||||||
|
|
||||||
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
|
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
|
||||||
val partId = cursor.getStringValue(Mms._ID)
|
val partId = cursor.getStringValue(Mms._ID)
|
||||||
val type = cursor.getStringValue(Mms.Part.CONTENT_TYPE)
|
val type = cursor.getStringValue(Mms.Part.CONTENT_TYPE)
|
||||||
if (type == "text/plain") {
|
if (type == "text/plain") {
|
||||||
mms.text = cursor.getStringValue(Mms.Part.TEXT) ?: ""
|
attachment.text = cursor.getStringValue(Mms.Part.TEXT) ?: ""
|
||||||
} else if (type.startsWith("image/")) {
|
} else if (type.startsWith("image/")) {
|
||||||
mms.image = getMmsImage(uri, partId)
|
attachment.uri = Uri.withAppendedPath(uri, partId)
|
||||||
|
attachment.type = type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mms
|
return attachment
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getThreadParticipants(threadId: Int): ArrayList<Contact> {
|
fun Context.getThreadParticipants(threadId: Int): ArrayList<Contact> {
|
||||||
|
@ -192,18 +191,6 @@ fun Context.getPhoneNumberFromAddressId(canonicalAddressId: Int): String {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Context.getMmsImage(uri: Uri, id: String): Bitmap? {
|
|
||||||
val partURI = Uri.withAppendedPath(uri, id)
|
|
||||||
var bitmap: Bitmap? = null
|
|
||||||
try {
|
|
||||||
contentResolver.openInputStream(partURI).use {
|
|
||||||
bitmap = BitmapFactory.decodeStream(it)
|
|
||||||
}
|
|
||||||
} catch (ignored: Exception) {
|
|
||||||
}
|
|
||||||
return bitmap
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Context.getAvailableContacts(callback: (ArrayList<Contact>) -> Unit) {
|
fun Context.getAvailableContacts(callback: (ArrayList<Contact>) -> Unit) {
|
||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
val names = getContactNames()
|
val names = getContactNames()
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package com.simplemobiletools.smsmessenger.models
|
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
|
|
||||||
data class MMS(val id: Int, var text: String, var image: Bitmap?)
|
|
|
@ -4,7 +4,8 @@ import android.provider.Telephony
|
||||||
import com.simplemobiletools.smsmessenger.extensions.getThreadTitle
|
import com.simplemobiletools.smsmessenger.extensions.getThreadTitle
|
||||||
|
|
||||||
data class Message(
|
data class Message(
|
||||||
val id: Int, val body: String, val type: Int, val participants: ArrayList<Contact>, val date: Int, val read: Boolean, val thread: Int
|
val id: Int, val body: String, val type: Int, val participants: ArrayList<Contact>, val date: Int, val read: Boolean, val thread: Int,
|
||||||
|
val attachment: MessageAttachment?
|
||||||
) : ThreadItem() {
|
) : ThreadItem() {
|
||||||
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX
|
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.simplemobiletools.smsmessenger.models
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
|
||||||
|
data class MessageAttachment(val id: Int, var text: String, var uri: Uri?, var type: String)
|
Loading…
Reference in New Issue