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 'com.github.SimpleMobileTools:Simple-Commons:63ee2676f5'
implementation 'org.greenrobot:eventbus:3.3.1' implementation 'org.greenrobot:eventbus:3.3.1'
implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61' 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 "me.leolin:ShortcutBadger:1.1.22"
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'com.googlecode.ez-vcard:ez-vcard:0.11.3' implementation 'com.googlecode.ez-vcard:ez-vcard:0.11.3'

View File

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

View File

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

View File

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