show an avatar at the incoming messages

This commit is contained in:
tibbi 2020-04-11 22:44:02 +02:00
parent 6ef4a3b23a
commit 7f441218d2
3 changed files with 47 additions and 8 deletions

View File

@ -19,11 +19,13 @@ import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.extensions.deleteMessage
import com.simplemobiletools.smsmessenger.extensions.loadImage
import com.simplemobiletools.smsmessenger.helpers.*
import com.simplemobiletools.smsmessenger.models.Message
import com.simplemobiletools.smsmessenger.models.ThreadDateTime
import com.simplemobiletools.smsmessenger.models.ThreadError
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_thread_date_time.view.*
@ -159,13 +161,23 @@ class ThreadAdapter(
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) {
view.apply {
thread_message_body.text = message.body
if (message.isReceivedMessage()) {
thread_message_photo.beVisible()
thread_message_body.setTextColor(textColor)
context.loadImage(message.senderPhotoUri, thread_message_photo, message.senderName)
} else {
thread_message_photo?.beGone()
val background = context.getAdjustedPrimaryColor()
thread_message_body.background.applyColorFilter(background.adjustAlpha(0.8f))
thread_message_body.setTextColor(background.getContrastColor())

View File

@ -77,7 +77,7 @@ fun Context.getMessages(threadId: Int): ArrayList<Message> {
val thread = cursor.getIntValue(Sms.THREAD_ID)
val participant = Contact(0, senderName, photoUri, senderNumber)
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)
}
@ -122,7 +122,20 @@ fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<
val participants = getThreadParticipants(threadId, contactsMap)
val isMMS = true
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)
participants.forEach {
@ -132,6 +145,24 @@ fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<
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> {
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
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) {
val placeholder = if (placeholderImage == null) {
BitmapDrawable(resources, getNotificationLetterIcon(placeholderName))
} else {
placeholderImage
}
val placeholder = placeholderImage ?: BitmapDrawable(resources, getNotificationLetterIcon(placeholderName))
val options = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)

View File

@ -4,7 +4,7 @@ import android.provider.Telephony
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 isMMS: Boolean, val attachment: MessageAttachment?
val isMMS: Boolean, val attachment: MessageAttachment?, val senderName: String, val senderPhotoUri: String
) : ThreadItem() {
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX
}