Merge pull request #589 from Naveen3Singh/improve_compressor

Improve image compression
This commit is contained in:
Tibor Kaputa 2023-03-04 20:34:26 +01:00 committed by GitHub
commit 758d538e64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 102 additions and 92 deletions

View file

@ -33,6 +33,7 @@ import android.view.inputmethod.EditorInfo
import android.widget.LinearLayout
import android.widget.LinearLayout.LayoutParams
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.res.ResourcesCompat
@ -1100,6 +1101,23 @@ class ThreadActivity : SimpleActivity() {
return
}
val mimeType = contentResolver.getType(uri)
if (mimeType == null) {
toast(R.string.unknown_error_occurred)
return
}
val isImage = mimeType.isImageMimeType()
val isGif = mimeType.isGifMimeType()
if (isGif || !isImage) {
// is it assumed that images will always be compressed below the max MMS size limit
val fileSize = getFileSizeFromUri(uri)
val mmsFileSizeLimit = config.mmsFileSizeLimit
if (mmsFileSizeLimit != FILE_SIZE_NONE && fileSize > mmsFileSizeLimit) {
toast(R.string.attachment_sized_exceeds_max_limit, length = Toast.LENGTH_LONG)
return
}
}
var adapter = getAttachmentsAdapter()
if (adapter == null) {
adapter = AttachmentsAdapter(
@ -1115,17 +1133,12 @@ class ThreadActivity : SimpleActivity() {
}
thread_attachments_recyclerview.beVisible()
val mimeType = contentResolver.getType(uri)
if (mimeType == null) {
toast(R.string.unknown_error_occurred)
return
}
val attachment = AttachmentSelection(
id = id,
uri = uri,
mimetype = mimeType,
filename = getFilenameFromUri(uri),
isPending = mimeType.isImageMimeType() && !mimeType.isGifMimeType()
isPending = isImage && !isGif
)
adapter.addAttachment(attachment)
checkSendMessageAvailability()
@ -1228,8 +1241,9 @@ class ThreadActivity : SimpleActivity() {
sendMessageCompat(text, addresses, subscriptionId, attachments)
ensureBackgroundThread {
val messageIds = messages.map { it.id }
val message = getMessages(threadId, getImageResolutions = true, limit = 1).firstOrNull { it.id !in messageIds }
if (message != null) {
val messages = getMessages(threadId, getImageResolutions = true, limit = maxOf(1, attachments.size))
.filter { it.id !in messageIds }
for (message in messages) {
insertOrUpdateMessage(message)
}
}