mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-03-24 23:09:02 +03:00
Compress images before sending (for devices below Android 10).
Fixes #1333
This commit is contained in:
parent
7b3fa501c6
commit
96d6a72b97
3 changed files with 75 additions and 16 deletions
CHANGES.md
matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content
vector/src/main/java/im/vector/riotx/features/attachments/preview
|
@ -13,6 +13,7 @@ Bugfix 🐛:
|
||||||
- Incomplete predicate in RealmCryptoStore#getOutgoingRoomKeyRequest (#1519)
|
- Incomplete predicate in RealmCryptoStore#getOutgoingRoomKeyRequest (#1519)
|
||||||
- User could not redact message that they have sent (#1543)
|
- User could not redact message that they have sent (#1543)
|
||||||
- Use vendor prefix for non merged MSC (#1537)
|
- Use vendor prefix for non merged MSC (#1537)
|
||||||
|
- Compress images before sending (for devices below Android 10) (#1333)
|
||||||
|
|
||||||
Translations 🗣:
|
Translations 🗣:
|
||||||
-
|
-
|
||||||
|
|
|
@ -17,9 +17,13 @@
|
||||||
package im.vector.matrix.android.internal.session.content
|
package im.vector.matrix.android.internal.session.content
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
|
import android.os.Build
|
||||||
import androidx.work.CoroutineWorker
|
import androidx.work.CoroutineWorker
|
||||||
import androidx.work.WorkerParameters
|
import androidx.work.WorkerParameters
|
||||||
import com.squareup.moshi.JsonClass
|
import com.squareup.moshi.JsonClass
|
||||||
|
import id.zelory.compressor.Compressor
|
||||||
|
import id.zelory.compressor.constraint.default
|
||||||
import im.vector.matrix.android.api.session.content.ContentAttachmentData
|
import im.vector.matrix.android.api.session.content.ContentAttachmentData
|
||||||
import im.vector.matrix.android.api.session.events.model.Event
|
import im.vector.matrix.android.api.session.events.model.Event
|
||||||
import im.vector.matrix.android.api.session.events.model.toContent
|
import im.vector.matrix.android.api.session.events.model.toContent
|
||||||
|
@ -38,6 +42,9 @@ import im.vector.matrix.android.internal.worker.WorkerParamsFactory
|
||||||
import im.vector.matrix.android.internal.worker.getSessionComponent
|
import im.vector.matrix.android.internal.worker.getSessionComponent
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.util.UUID
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
private data class NewImageAttributes(
|
private data class NewImageAttributes(
|
||||||
|
@ -154,26 +161,70 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
||||||
var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null
|
var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
val contentUploadResponse = if (params.isEncrypted) {
|
// Temporary disable compressing for Android 10 and above.
|
||||||
Timber.v("Encrypt file")
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
|
val contentUploadResponse = if (params.isEncrypted) {
|
||||||
|
Timber.v("Encrypt file")
|
||||||
|
notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
|
||||||
|
|
||||||
val encryptionResult = MXEncryptedAttachments.encryptAttachment(inputStream, attachment.getSafeMimeType())
|
val encryptionResult = MXEncryptedAttachments.encryptAttachment(inputStream, attachment.getSafeMimeType())
|
||||||
uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
|
uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
|
||||||
|
|
||||||
fileUploader
|
fileUploader
|
||||||
.uploadByteArray(encryptionResult.encryptedByteArray, attachment.name, "application/octet-stream", progressListener)
|
.uploadByteArray(encryptionResult.encryptedByteArray, attachment.name, "application/octet-stream", progressListener)
|
||||||
|
} else {
|
||||||
|
fileUploader
|
||||||
|
.uploadByteArray(inputStream.readBytes(), attachment.name, attachment.getSafeMimeType(), progressListener)
|
||||||
|
}
|
||||||
|
handleSuccess(params,
|
||||||
|
contentUploadResponse.contentUri,
|
||||||
|
uploadedFileEncryptedFileInfo,
|
||||||
|
uploadedThumbnailUrl,
|
||||||
|
uploadedThumbnailEncryptedFileInfo,
|
||||||
|
newImageAttributes)
|
||||||
} else {
|
} else {
|
||||||
fileUploader
|
val cacheFile = File.createTempFile(attachment.name ?: UUID.randomUUID().toString(), ".jpg", context.cacheDir)
|
||||||
.uploadByteArray(inputStream.readBytes(), attachment.name, attachment.getSafeMimeType(), progressListener)
|
cacheFile.parentFile?.mkdirs()
|
||||||
}
|
if (cacheFile.exists()) {
|
||||||
|
cacheFile.delete()
|
||||||
|
}
|
||||||
|
cacheFile.createNewFile()
|
||||||
|
cacheFile.deleteOnExit()
|
||||||
|
|
||||||
handleSuccess(params,
|
val outputStream = FileOutputStream(cacheFile)
|
||||||
contentUploadResponse.contentUri,
|
outputStream.use {
|
||||||
uploadedFileEncryptedFileInfo,
|
inputStream.copyTo(outputStream)
|
||||||
uploadedThumbnailUrl,
|
}
|
||||||
uploadedThumbnailEncryptedFileInfo,
|
|
||||||
newImageAttributes)
|
val contentUploadResponse = if (attachment.type == ContentAttachmentData.Type.IMAGE && params.compressBeforeSending) {
|
||||||
|
Compressor.compress(context, cacheFile) {
|
||||||
|
default(
|
||||||
|
width = MAX_IMAGE_SIZE,
|
||||||
|
height = MAX_IMAGE_SIZE
|
||||||
|
)
|
||||||
|
}.also { compressedFile ->
|
||||||
|
val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||||
|
BitmapFactory.decodeFile(compressedFile.absolutePath, options)
|
||||||
|
val fileSize = compressedFile.length().toInt()
|
||||||
|
newImageAttributes = NewImageAttributes(
|
||||||
|
options.outWidth,
|
||||||
|
options.outHeight,
|
||||||
|
fileSize
|
||||||
|
)
|
||||||
|
}.let { compressedFile ->
|
||||||
|
fileUploader.uploadFile(compressedFile, attachment.name, attachment.getSafeMimeType(), progressListener)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fileUploader.uploadFile(cacheFile, attachment.name, attachment.getSafeMimeType(), progressListener)
|
||||||
|
}
|
||||||
|
|
||||||
|
handleSuccess(params,
|
||||||
|
contentUploadResponse.contentUri,
|
||||||
|
uploadedFileEncryptedFileInfo,
|
||||||
|
uploadedThumbnailUrl,
|
||||||
|
uploadedThumbnailEncryptedFileInfo,
|
||||||
|
newImageAttributes)
|
||||||
|
}
|
||||||
} catch (t: Throwable) {
|
} catch (t: Throwable) {
|
||||||
Timber.e(t)
|
Timber.e(t)
|
||||||
handleFailure(params, t)
|
handleFailure(params, t)
|
||||||
|
|
|
@ -21,6 +21,7 @@ import android.app.Activity.RESULT_CANCELED
|
||||||
import android.app.Activity.RESULT_OK
|
import android.app.Activity.RESULT_OK
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
|
@ -143,6 +144,12 @@ class AttachmentsPreviewFragment @Inject constructor(
|
||||||
attachmentPreviewerBigList.scrollToPosition(state.currentAttachmentIndex)
|
attachmentPreviewerBigList.scrollToPosition(state.currentAttachmentIndex)
|
||||||
attachmentPreviewerMiniatureList.scrollToPosition(state.currentAttachmentIndex)
|
attachmentPreviewerMiniatureList.scrollToPosition(state.currentAttachmentIndex)
|
||||||
attachmentPreviewerSendImageOriginalSize.text = resources.getQuantityString(R.plurals.send_images_with_original_size, state.attachments.size)
|
attachmentPreviewerSendImageOriginalSize.text = resources.getQuantityString(R.plurals.send_images_with_original_size, state.attachments.size)
|
||||||
|
|
||||||
|
// Temporary disable compressing for Android 10 and above.
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
|
attachmentPreviewerSendImageOriginalSize.isChecked = true
|
||||||
|
attachmentPreviewerSendImageOriginalSize.isEnabled = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue