Support compressing images on Android 10 and above.
This commit is contained in:
parent
96d6a72b97
commit
4349331ee7
|
@ -13,7 +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)
|
- Compress images before sending (#1333)
|
||||||
|
|
||||||
Translations 🗣:
|
Translations 🗣:
|
||||||
-
|
-
|
||||||
|
|
|
@ -18,7 +18,6 @@ package im.vector.matrix.android.internal.session.content
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.BitmapFactory
|
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
|
||||||
|
@ -43,6 +42,7 @@ 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.File
|
||||||
|
import java.io.FileInputStream
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
@ -161,29 +161,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
||||||
var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null
|
var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
// Temporary disable compressing for Android 10 and above.
|
var cacheFile = File.createTempFile(attachment.name ?: UUID.randomUUID().toString(), ".jpg", context.cacheDir)
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
val contentUploadResponse = if (params.isEncrypted) {
|
|
||||||
Timber.v("Encrypt file")
|
|
||||||
notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
|
|
||||||
|
|
||||||
val encryptionResult = MXEncryptedAttachments.encryptAttachment(inputStream, attachment.getSafeMimeType())
|
|
||||||
uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
|
|
||||||
|
|
||||||
fileUploader
|
|
||||||
.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 {
|
|
||||||
val cacheFile = File.createTempFile(attachment.name ?: UUID.randomUUID().toString(), ".jpg", context.cacheDir)
|
|
||||||
cacheFile.parentFile?.mkdirs()
|
cacheFile.parentFile?.mkdirs()
|
||||||
if (cacheFile.exists()) {
|
if (cacheFile.exists()) {
|
||||||
cacheFile.delete()
|
cacheFile.delete()
|
||||||
|
@ -196,8 +174,8 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
||||||
inputStream.copyTo(outputStream)
|
inputStream.copyTo(outputStream)
|
||||||
}
|
}
|
||||||
|
|
||||||
val contentUploadResponse = if (attachment.type == ContentAttachmentData.Type.IMAGE && params.compressBeforeSending) {
|
if (attachment.type == ContentAttachmentData.Type.IMAGE && params.compressBeforeSending) {
|
||||||
Compressor.compress(context, cacheFile) {
|
cacheFile = Compressor.compress(context, cacheFile) {
|
||||||
default(
|
default(
|
||||||
width = MAX_IMAGE_SIZE,
|
width = MAX_IMAGE_SIZE,
|
||||||
height = MAX_IMAGE_SIZE
|
height = MAX_IMAGE_SIZE
|
||||||
|
@ -211,11 +189,21 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
||||||
options.outHeight,
|
options.outHeight,
|
||||||
fileSize
|
fileSize
|
||||||
)
|
)
|
||||||
}.let { compressedFile ->
|
|
||||||
fileUploader.uploadFile(compressedFile, attachment.name, attachment.getSafeMimeType(), progressListener)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val contentUploadResponse = if (params.isEncrypted) {
|
||||||
|
Timber.v("Encrypt file")
|
||||||
|
notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
|
||||||
|
|
||||||
|
val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(cacheFile), attachment.getSafeMimeType())
|
||||||
|
uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
|
||||||
|
|
||||||
|
fileUploader
|
||||||
|
.uploadByteArray(encryptionResult.encryptedByteArray, attachment.name, "application/octet-stream", progressListener)
|
||||||
} else {
|
} else {
|
||||||
fileUploader.uploadFile(cacheFile, attachment.name, attachment.getSafeMimeType(), progressListener)
|
fileUploader
|
||||||
|
.uploadFile(cacheFile, attachment.name, attachment.getSafeMimeType(), progressListener)
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSuccess(params,
|
handleSuccess(params,
|
||||||
|
@ -224,7 +212,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
||||||
uploadedThumbnailUrl,
|
uploadedThumbnailUrl,
|
||||||
uploadedThumbnailEncryptedFileInfo,
|
uploadedThumbnailEncryptedFileInfo,
|
||||||
newImageAttributes)
|
newImageAttributes)
|
||||||
}
|
|
||||||
} catch (t: Throwable) {
|
} catch (t: Throwable) {
|
||||||
Timber.e(t)
|
Timber.e(t)
|
||||||
handleFailure(params, t)
|
handleFailure(params, t)
|
||||||
|
|
|
@ -21,7 +21,6 @@ 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
|
||||||
|
@ -144,12 +143,6 @@ 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…
Reference in New Issue