mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-06-05 21:49:22 +02:00
show an avatar at the incoming messages
This commit is contained in:
@@ -19,11 +19,13 @@ import com.simplemobiletools.commons.views.MyRecyclerView
|
|||||||
import com.simplemobiletools.smsmessenger.R
|
import com.simplemobiletools.smsmessenger.R
|
||||||
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
||||||
import com.simplemobiletools.smsmessenger.extensions.deleteMessage
|
import com.simplemobiletools.smsmessenger.extensions.deleteMessage
|
||||||
|
import com.simplemobiletools.smsmessenger.extensions.loadImage
|
||||||
import com.simplemobiletools.smsmessenger.helpers.*
|
import com.simplemobiletools.smsmessenger.helpers.*
|
||||||
import com.simplemobiletools.smsmessenger.models.Message
|
import com.simplemobiletools.smsmessenger.models.Message
|
||||||
import com.simplemobiletools.smsmessenger.models.ThreadDateTime
|
import com.simplemobiletools.smsmessenger.models.ThreadDateTime
|
||||||
import com.simplemobiletools.smsmessenger.models.ThreadError
|
import com.simplemobiletools.smsmessenger.models.ThreadError
|
||||||
import com.simplemobiletools.smsmessenger.models.ThreadItem
|
import com.simplemobiletools.smsmessenger.models.ThreadItem
|
||||||
|
import kotlinx.android.synthetic.main.item_contact_with_number.view.*
|
||||||
import kotlinx.android.synthetic.main.item_received_message.view.*
|
import kotlinx.android.synthetic.main.item_received_message.view.*
|
||||||
import kotlinx.android.synthetic.main.item_thread_date_time.view.*
|
import kotlinx.android.synthetic.main.item_thread_date_time.view.*
|
||||||
|
|
||||||
@@ -159,13 +161,23 @@ class ThreadAdapter(
|
|||||||
|
|
||||||
private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime
|
private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime
|
||||||
|
|
||||||
|
override fun onViewRecycled(holder: ViewHolder) {
|
||||||
|
super.onViewRecycled(holder)
|
||||||
|
if (!activity.isDestroyed && !activity.isFinishing && holder.itemView.thread_message_photo != null) {
|
||||||
|
Glide.with(activity).clear(holder.itemView.thread_message_photo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun setupView(view: View, message: Message) {
|
private fun setupView(view: View, message: Message) {
|
||||||
view.apply {
|
view.apply {
|
||||||
thread_message_body.text = message.body
|
thread_message_body.text = message.body
|
||||||
|
|
||||||
if (message.isReceivedMessage()) {
|
if (message.isReceivedMessage()) {
|
||||||
|
thread_message_photo.beVisible()
|
||||||
thread_message_body.setTextColor(textColor)
|
thread_message_body.setTextColor(textColor)
|
||||||
|
context.loadImage(message.senderPhotoUri, thread_message_photo, message.senderName)
|
||||||
} else {
|
} else {
|
||||||
|
thread_message_photo?.beGone()
|
||||||
val background = context.getAdjustedPrimaryColor()
|
val background = context.getAdjustedPrimaryColor()
|
||||||
thread_message_body.background.applyColorFilter(background.adjustAlpha(0.8f))
|
thread_message_body.background.applyColorFilter(background.adjustAlpha(0.8f))
|
||||||
thread_message_body.setTextColor(background.getContrastColor())
|
thread_message_body.setTextColor(background.getContrastColor())
|
||||||
|
@@ -77,7 +77,7 @@ fun Context.getMessages(threadId: Int): ArrayList<Message> {
|
|||||||
val thread = cursor.getIntValue(Sms.THREAD_ID)
|
val thread = cursor.getIntValue(Sms.THREAD_ID)
|
||||||
val participant = Contact(0, senderName, photoUri, senderNumber)
|
val participant = Contact(0, senderName, photoUri, senderNumber)
|
||||||
val isMMS = false
|
val isMMS = false
|
||||||
val message = Message(id, body, type, arrayListOf(participant), date, read, thread, isMMS, null)
|
val message = Message(id, body, type, arrayListOf(participant), date, read, thread, isMMS, null, senderName, photoUri)
|
||||||
messages.add(message)
|
messages.add(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +122,20 @@ fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<
|
|||||||
val participants = getThreadParticipants(threadId, contactsMap)
|
val participants = getThreadParticipants(threadId, contactsMap)
|
||||||
val isMMS = true
|
val isMMS = true
|
||||||
val attachment = getMmsAttachment(mmsId)
|
val attachment = getMmsAttachment(mmsId)
|
||||||
val message = Message(mmsId, attachment?.text ?: "", type, participants, date, read, threadId, isMMS, attachment)
|
val body = attachment?.text ?: ""
|
||||||
|
var senderName = ""
|
||||||
|
var senderPhotoUri = ""
|
||||||
|
|
||||||
|
if (type != Mms.MESSAGE_BOX_SENT && type != Mms.MESSAGE_BOX_FAILED) {
|
||||||
|
val number = getMMSSender(mmsId)
|
||||||
|
val namePhoto = getNameAndPhotoFromPhoneNumber(number)
|
||||||
|
if (namePhoto != null) {
|
||||||
|
senderName = namePhoto.name
|
||||||
|
senderPhotoUri = namePhoto.photoUri ?: ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val message = Message(mmsId, body, type, participants, date, read, threadId, isMMS, attachment, senderName, senderPhotoUri)
|
||||||
messages.add(message)
|
messages.add(message)
|
||||||
|
|
||||||
participants.forEach {
|
participants.forEach {
|
||||||
@@ -132,6 +145,24 @@ fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<
|
|||||||
return messages
|
return messages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun Context.getMMSSender(msgId: Int): String {
|
||||||
|
val uri = Uri.parse("${Mms.CONTENT_URI}/$msgId/addr")
|
||||||
|
val projection = arrayOf(
|
||||||
|
Mms.Addr.ADDRESS
|
||||||
|
)
|
||||||
|
|
||||||
|
try {
|
||||||
|
val cursor = contentResolver.query(uri, projection, null, null, null)
|
||||||
|
cursor?.use {
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
return cursor.getStringValue(Mms.Addr.ADDRESS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (ignored: Exception) {
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
fun Context.getConversations(): ArrayList<Conversation> {
|
fun Context.getConversations(): ArrayList<Conversation> {
|
||||||
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
|
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
@@ -620,11 +651,7 @@ fun Context.getNotificationLetterIcon(name: String): Bitmap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun Context.loadImage(path: String, imageView: ImageView, placeholderName: String, placeholderImage: Drawable? = null) {
|
fun Context.loadImage(path: String, imageView: ImageView, placeholderName: String, placeholderImage: Drawable? = null) {
|
||||||
val placeholder = if (placeholderImage == null) {
|
val placeholder = placeholderImage ?: BitmapDrawable(resources, getNotificationLetterIcon(placeholderName))
|
||||||
BitmapDrawable(resources, getNotificationLetterIcon(placeholderName))
|
|
||||||
} else {
|
|
||||||
placeholderImage
|
|
||||||
}
|
|
||||||
|
|
||||||
val options = RequestOptions()
|
val options = RequestOptions()
|
||||||
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
|
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
|
||||||
|
@@ -4,7 +4,7 @@ import android.provider.Telephony
|
|||||||
|
|
||||||
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 isMMS: Boolean, val attachment: MessageAttachment?
|
val isMMS: Boolean, val attachment: MessageAttachment?, val senderName: String, val senderPhotoUri: String
|
||||||
) : ThreadItem() {
|
) : ThreadItem() {
|
||||||
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX
|
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user