From 7f441218d2381474c8fe2d02e56a844f10970352 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 11 Apr 2020 22:44:02 +0200 Subject: [PATCH] show an avatar at the incoming messages --- .../smsmessenger/adapters/ThreadAdapter.kt | 12 ++++++ .../smsmessenger/extensions/Context.kt | 41 +++++++++++++++---- .../smsmessenger/models/Message.kt | 2 +- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt index fb622769..6970bc5b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt @@ -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()) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index 045055fb..e794f435 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -77,7 +77,7 @@ fun Context.getMessages(threadId: Int): ArrayList { 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 { 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) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Message.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Message.kt index ead43bf5..0b9f797f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Message.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Message.kt @@ -4,7 +4,7 @@ import android.provider.Telephony data class Message( val id: Int, val body: String, val type: Int, val participants: ArrayList, 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 }