mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-06-05 21:49:22 +02:00
add a generic Attachment view for handling others than images and videos
This commit is contained in:
@@ -2,6 +2,7 @@ package com.simplemobiletools.smsmessenger.adapters
|
|||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.drawable.Drawable
|
import android.graphics.drawable.Drawable
|
||||||
|
import android.net.Uri
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
@@ -33,6 +34,8 @@ import com.simplemobiletools.smsmessenger.models.ThreadError
|
|||||||
import com.simplemobiletools.smsmessenger.models.ThreadItem
|
import com.simplemobiletools.smsmessenger.models.ThreadItem
|
||||||
import kotlinx.android.synthetic.main.item_attachment_image.view.*
|
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_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.*
|
import kotlinx.android.synthetic.main.item_thread_date_time.view.*
|
||||||
|
|
||||||
class ThreadAdapter(
|
class ThreadAdapter(
|
||||||
@@ -177,6 +180,7 @@ class ThreadAdapter(
|
|||||||
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
|
||||||
|
thread_message_body.beVisibleIf(message.body.isNotEmpty())
|
||||||
|
|
||||||
if (message.isReceivedMessage()) {
|
if (message.isReceivedMessage()) {
|
||||||
thread_message_sender_photo.beVisible()
|
thread_message_sender_photo.beVisible()
|
||||||
@@ -191,14 +195,13 @@ class ThreadAdapter(
|
|||||||
|
|
||||||
thread_mesage_attachments_holder.removeAllViews()
|
thread_mesage_attachments_holder.removeAllViews()
|
||||||
if (message.attachment?.attachments?.isNotEmpty() == true) {
|
if (message.attachment?.attachments?.isNotEmpty() == true) {
|
||||||
message.attachment.attachments.forEach {
|
for (attachment in message.attachment.attachments) {
|
||||||
val attachment = it
|
|
||||||
val imageView = layoutInflater.inflate(R.layout.item_attachment_image, null)
|
|
||||||
thread_mesage_attachments_holder.addView(imageView)
|
|
||||||
|
|
||||||
val type = attachment.type
|
val type = attachment.type
|
||||||
|
val uri = attachment.uri
|
||||||
if (type.startsWith("image/") || type.startsWith("video/")) {
|
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 isTallImage = attachment.height > attachment.width
|
||||||
val transformation = if (isTallImage) CenterCrop() else FitCenter()
|
val transformation = if (isTallImage) CenterCrop() else FitCenter()
|
||||||
val options = RequestOptions()
|
val options = RequestOptions()
|
||||||
@@ -225,13 +228,26 @@ class ThreadAdapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
builder.into(imageView.attachment_image)
|
builder.into(imageView.attachment_image)
|
||||||
imageView.attachment_image.setOnClickListener {
|
imageView.attachment_image.setOnClickListener { launchViewIntent(uri, type) }
|
||||||
Intent().apply {
|
} else {
|
||||||
action = Intent.ACTION_VIEW
|
if (message.isReceivedMessage()) {
|
||||||
setDataAndType(uri, type)
|
val attachmentView = layoutInflater.inflate(R.layout.item_received_unknown_attachment, null).apply {
|
||||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
thread_received_attachment_label.apply {
|
||||||
activity.startActivity(this)
|
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) {
|
private fun setupDateTime(view: View, dateTime: ThreadDateTime) {
|
||||||
view.apply {
|
view.apply {
|
||||||
thread_date_time.text = dateTime.date.formatDateOrTime(context, false)
|
thread_date_time.text = dateTime.date.formatDateOrTime(context, false)
|
||||||
|
@@ -230,6 +230,9 @@ fun Context.getMmsAttachment(id: Int): MessageAttachment? {
|
|||||||
} else if (type.startsWith("image/") || type.startsWith("video/")) {
|
} else if (type.startsWith("image/") || type.startsWith("video/")) {
|
||||||
val attachment = Attachment(Uri.withAppendedPath(uri, partId), type, 0, 0)
|
val attachment = Attachment(Uri.withAppendedPath(uri, partId), type, 0, 0)
|
||||||
messageAttachment.attachments.add(attachment)
|
messageAttachment.attachments.add(attachment)
|
||||||
|
} else if (type != "application/smil") {
|
||||||
|
val attachment = Attachment(Uri.withAppendedPath(uri, partId), type, 0, 0)
|
||||||
|
messageAttachment.attachments.add(attachment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
25
app/src/main/res/layout/item_received_unknown_attachment.xml
Normal file
25
app/src/main/res/layout/item_received_unknown_attachment.xml
Normal 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>
|
25
app/src/main/res/layout/item_sent_unknown_attachment.xml
Normal file
25
app/src/main/res/layout/item_sent_unknown_attachment.xml
Normal 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>
|
@@ -4,6 +4,7 @@
|
|||||||
<string name="type_a_message">Type a message…</string>
|
<string name="type_a_message">Type a message…</string>
|
||||||
<string name="message_not_sent">Message has not been sent.</string>
|
<string name="message_not_sent">Message has not been sent.</string>
|
||||||
<string name="add_person">Add Person</string>
|
<string name="add_person">Add Person</string>
|
||||||
|
<string name="attachment">Attachment</string>
|
||||||
|
|
||||||
<!-- New message -->
|
<!-- New message -->
|
||||||
<string name="create_new_message">Create new message</string>
|
<string name="create_new_message">Create new message</string>
|
||||||
|
Reference in New Issue
Block a user