add a generic Attachment view for handling others than images and videos

This commit is contained in:
tibbi 2020-04-12 23:11:42 +02:00
parent 9f475ddfad
commit 5c79ca361d
6 changed files with 96 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.smsmessenger.adapters
import android.content.Intent
import android.graphics.drawable.Drawable
import android.net.Uri
import android.view.Menu
import android.view.View
import android.view.ViewGroup
@ -33,6 +34,8 @@ import com.simplemobiletools.smsmessenger.models.ThreadError
import com.simplemobiletools.smsmessenger.models.ThreadItem
import kotlinx.android.synthetic.main.item_attachment_image.view.*
import kotlinx.android.synthetic.main.item_received_message.view.*
import kotlinx.android.synthetic.main.item_received_unknown_attachment.view.*
import kotlinx.android.synthetic.main.item_sent_unknown_attachment.view.*
import kotlinx.android.synthetic.main.item_thread_date_time.view.*
class ThreadAdapter(
@ -177,6 +180,7 @@ class ThreadAdapter(
private fun setupView(view: View, message: Message) {
view.apply {
thread_message_body.text = message.body
thread_message_body.beVisibleIf(message.body.isNotEmpty())
if (message.isReceivedMessage()) {
thread_message_sender_photo.beVisible()
@ -191,14 +195,13 @@ class ThreadAdapter(
thread_mesage_attachments_holder.removeAllViews()
if (message.attachment?.attachments?.isNotEmpty() == true) {
message.attachment.attachments.forEach {
val attachment = it
val imageView = layoutInflater.inflate(R.layout.item_attachment_image, null)
thread_mesage_attachments_holder.addView(imageView)
for (attachment in message.attachment.attachments) {
val type = attachment.type
val uri = attachment.uri
if (type.startsWith("image/") || type.startsWith("video/")) {
val uri = attachment.uri
val imageView = layoutInflater.inflate(R.layout.item_attachment_image, null)
thread_mesage_attachments_holder.addView(imageView)
val isTallImage = attachment.height > attachment.width
val transformation = if (isTallImage) CenterCrop() else FitCenter()
val options = RequestOptions()
@ -225,13 +228,26 @@ class ThreadAdapter(
}
builder.into(imageView.attachment_image)
imageView.attachment_image.setOnClickListener {
Intent().apply {
action = Intent.ACTION_VIEW
setDataAndType(uri, type)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
activity.startActivity(this)
imageView.attachment_image.setOnClickListener { launchViewIntent(uri, type) }
} else {
if (message.isReceivedMessage()) {
val attachmentView = layoutInflater.inflate(R.layout.item_received_unknown_attachment, null).apply {
thread_received_attachment_label.apply {
setTextColor(textColor)
setOnClickListener { launchViewIntent(uri, type) }
}
}
thread_mesage_attachments_holder.addView(attachmentView)
} else {
val background = context.getAdjustedPrimaryColor()
val attachmentView = layoutInflater.inflate(R.layout.item_sent_unknown_attachment, null).apply {
thread_sent_attachment_label.apply {
this.background.applyColorFilter(background.adjustAlpha(0.8f))
setTextColor(background.getContrastColor())
setOnClickListener { launchViewIntent(uri, type) }
}
}
thread_mesage_attachments_holder.addView(attachmentView)
}
}
@ -241,6 +257,20 @@ class ThreadAdapter(
}
}
private fun launchViewIntent(uri: Uri, type: String) {
Intent().apply {
action = Intent.ACTION_VIEW
setDataAndType(uri, type)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
if (resolveActivity(activity.packageManager) != null) {
activity.startActivity(this)
} else {
activity.toast(R.string.no_app_found)
}
}
}
private fun setupDateTime(view: View, dateTime: ThreadDateTime) {
view.apply {
thread_date_time.text = dateTime.date.formatDateOrTime(context, false)

View File

@ -230,6 +230,9 @@ fun Context.getMmsAttachment(id: Int): MessageAttachment? {
} else if (type.startsWith("image/") || type.startsWith("video/")) {
val attachment = Attachment(Uri.withAppendedPath(uri, partId), type, 0, 0)
messageAttachment.attachments.add(attachment)
} else if (type != "application/smil") {
val attachment = Attachment(Uri.withAppendedPath(uri, partId), type, 0, 0)
messageAttachment.attachments.add(attachment)
}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/thread_received_attachment_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin">
<TextView
android:id="@+id/thread_received_attachment_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:background="@drawable/item_received_background"
android:drawableStart="@drawable/ic_attach_file_vector"
android:drawablePadding="@dimen/small_margin"
android:gravity="center_vertical"
android:paddingStart="@dimen/small_margin"
android:paddingTop="@dimen/normal_margin"
android:paddingEnd="@dimen/normal_margin"
android:paddingBottom="@dimen/normal_margin"
android:text="@string/attachment"
android:textSize="@dimen/normal_text_size" />
</RelativeLayout>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/thread_sent_attachment_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin">
<TextView
android:id="@+id/thread_sent_attachment_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@drawable/item_sent_background"
android:drawableEnd="@drawable/ic_attach_file_vector"
android:drawablePadding="@dimen/small_margin"
android:gravity="center_vertical"
android:paddingStart="@dimen/normal_margin"
android:paddingTop="@dimen/normal_margin"
android:paddingEnd="@dimen/small_margin"
android:paddingBottom="@dimen/normal_margin"
android:text="@string/attachment"
android:textSize="@dimen/normal_text_size" />
</RelativeLayout>

View File

@ -4,6 +4,7 @@
<string name="type_a_message">Type a message…</string>
<string name="message_not_sent">Message has not been sent.</string>
<string name="add_person">Add Person</string>
<string name="attachment">Attachment</string>
<!-- New message -->
<string name="create_new_message">Create new message</string>