Workaround attachment filename issue

This commit is contained in:
Naveen 2022-11-05 00:21:59 +05:30
parent 61a57aa2b5
commit cd2c94ccee
4 changed files with 23 additions and 23 deletions

View File

@ -66,7 +66,7 @@ dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:63ee2676f5'
implementation 'org.greenrobot:eventbus:3.3.1'
implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61'
implementation 'com.github.tibbi:android-smsmms:875a46a9c4'
implementation 'com.github.tibbi:android-smsmms:3581774c39'
implementation "me.leolin:ShortcutBadger:1.1.22"
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'com.googlecode.ez-vcard:ez-vcard:0.11.3'

View File

@ -156,7 +156,7 @@ class ThreadActivity : SimpleActivity() {
override fun onPause() {
super.onPause()
if (thread_type_message.value != "" && getAttachments().isEmpty()) {
if (thread_type_message.value != "" && getAttachmentSelections().isEmpty()) {
saveSmsDraft(thread_type_message.value, threadId)
} else {
deleteSmsDraft(threadId)
@ -905,10 +905,10 @@ class ThreadActivity : SimpleActivity() {
return adapter as? AttachmentsAdapter
}
private fun getAttachments() = getAttachmentsAdapter()?.attachments ?: emptyList()
private fun getAttachmentSelections() = getAttachmentsAdapter()?.attachments ?: emptyList()
private fun addAttachment(uri: Uri) {
if (getAttachments().any { it.uri.toString() == uri.toString() }) {
if (getAttachmentSelections().any { it.uri.toString() == uri.toString() }) {
toast(R.string.duplicate_item_warning)
return
}
@ -961,7 +961,7 @@ class ThreadActivity : SimpleActivity() {
}
private fun checkSendMessageAvailability() {
if (thread_type_message.text!!.isNotEmpty() || (getAttachments().isNotEmpty() && !getAttachments().any { it.isPending })) {
if (thread_type_message.text!!.isNotEmpty() || (getAttachmentSelections().isNotEmpty() && !getAttachmentSelections().any { it.isPending })) {
thread_send_message.isEnabled = true
thread_send_message.isClickable = true
thread_send_message.alpha = 0.9f
@ -975,7 +975,7 @@ class ThreadActivity : SimpleActivity() {
private fun sendMessage() {
var text = thread_type_message.value
if (text.isEmpty() && getAttachments().isEmpty()) {
if (text.isEmpty() && getAttachmentSelections().isEmpty()) {
showErrorToast(getString(R.string.unknown_error_occurred))
return
}
@ -1033,7 +1033,7 @@ class ThreadActivity : SimpleActivity() {
private fun sendNormalMessage(text: String, subscriptionId: Int) {
val addresses = participants.getAddresses()
val attachments = getAttachments().map { it.uri }
val attachments = buildMessageAttachments()
try {
refreshedSinceSent = false
@ -1227,7 +1227,7 @@ class ThreadActivity : SimpleActivity() {
private fun isMmsMessage(text: String): Boolean {
val isGroupMms = participants.size > 1 && config.sendGroupMessageMMS
val isLongMmsMessage = isLongMmsMessage(text) && config.sendLongMessageMMS
return getAttachments().isNotEmpty() || isGroupMms || isLongMmsMessage
return getAttachmentSelections().isNotEmpty() || isGroupMms || isLongMmsMessage
}
private fun updateMessageType() {
@ -1363,7 +1363,7 @@ class ThreadActivity : SimpleActivity() {
read = false,
threadId = threadId,
isMMS = isMmsMessage(text),
attachment = buildMessageAttachment(text, messageId),
attachment = MessageAttachment(messageId, text, buildMessageAttachments(messageId)),
senderName = "",
senderPhotoUri = "",
subscriptionId = subscriptionId,
@ -1371,13 +1371,9 @@ class ThreadActivity : SimpleActivity() {
)
}
private fun buildMessageAttachment(text: String, messageId: Long): MessageAttachment {
val attachments = getAttachments()
.map { Attachment(null, messageId, it.uri.toString(), contentResolver.getType(it.uri) ?: "*/*", 0, 0, "") }
.toArrayList()
return MessageAttachment(messageId, text, attachments)
}
private fun buildMessageAttachments(messageId: Long = -1L) = getAttachmentSelections()
.map { Attachment(null, messageId, it.uri.toString(), it.mimetype, 0, 0, it.filename) }
.toArrayList()
private fun setupAttachmentPickerView() {
val colors = arrayOf(

View File

@ -4,7 +4,6 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.os.Looper
import androidx.core.app.AlarmManagerCompat
@ -15,6 +14,7 @@ import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.isMarshmallowPlus
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.models.Attachment
import com.simplemobiletools.smsmessenger.models.Message
import com.simplemobiletools.smsmessenger.receivers.ScheduledMessageReceiver
import com.simplemobiletools.smsmessenger.receivers.SmsStatusDeliveredReceiver
@ -34,7 +34,7 @@ fun Context.getSendMessageSettings(): Settings {
return settings
}
fun Context.sendMessage(text: String, addresses: List<String>, subscriptionId: Int?, attachments: List<Uri>) {
fun Context.sendMessage(text: String, addresses: List<String>, subscriptionId: Int?, attachments: List<Attachment>) {
val settings = getSendMessageSettings()
if (subscriptionId != null) {
settings.subscriptionId = subscriptionId
@ -44,11 +44,15 @@ fun Context.sendMessage(text: String, addresses: List<String>, subscriptionId: I
val message = com.klinker.android.send_message.Message(text, addresses.toTypedArray())
if (attachments.isNotEmpty()) {
for (uri in attachments) {
for (attachment in attachments) {
try {
val byteArray = contentResolver.openInputStream(uri)?.readBytes() ?: continue
val mimeType = contentResolver.getType(uri) ?: continue
message.addMedia(byteArray, mimeType)
val uri = attachment.getUri()
contentResolver.openInputStream(uri)?.use {
val bytes = it.readBytes()
val mimeType = contentResolver.getType(uri) ?: return@use
val name = attachment.filename
message.addMedia(bytes, mimeType, name, name)
}
} catch (e: Exception) {
showErrorToast(e)
} catch (e: Error) {

View File

@ -40,7 +40,7 @@ class ScheduledMessageReceiver : BroadcastReceiver() {
}
val addresses = message.participants.getAddresses()
val attachments = message.attachment?.attachments?.mapNotNull { it.getUri() } ?: emptyList()
val attachments = message.attachment?.attachments ?: emptyList()
try {
context.sendMessage(message.body, addresses, message.subscriptionId, attachments)