Skip to content

Commit

Permalink
Fix avatar size in pill (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
yostyle committed Jul 30, 2024
1 parent 142f130 commit 986f817
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/1082.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correction du crash lorsque l'image de l'avatar d'un membre est trop grande.
2 changes: 1 addition & 1 deletion library/ui-strings/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2013,7 +2013,7 @@
<string name="create_spaces_make_sure_access">Assurez-vous que les bonnes personnes ont accès à %s.</string>
<string name="create_spaces_who_are_you_working_with">Avec qui travaillez-vous \?</string>
<string name="create_spaces_join_info_help">Pour rejoindre un espace existant, il vous faut une invitation.</string>
<string name="error_file_too_big_simple">Le fichier est trop volumineux pour être envoyé.</string>
<string name="error_file_too_big_simple">Le fichier est trop volumineux ou l’image est trop grande pour être envoyée.</string> <!-- Tchap: Use custom string -->
<string name="send_file_step_compressing_video">Compression de la vidéo %d %%</string>
<string name="send_file_step_compressing_image">Compression de l’image…</string>
<string name="use_as_default_and_do_not_ask_again">Utiliser par défaut et ne plus demander</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.matrix.android.sdk.internal.session.content

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import com.squareup.moshi.Moshi
import kotlinx.coroutines.Dispatchers
Expand All @@ -37,12 +39,14 @@ import org.matrix.android.sdk.api.failure.MatrixError
import org.matrix.android.sdk.api.session.content.ContentUrlResolver
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
import org.matrix.android.sdk.api.util.MimeTypes.isMimeTypeImage
import org.matrix.android.sdk.internal.di.Authenticated
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.ProgressRequestBody
import org.matrix.android.sdk.internal.network.awaitResponse
import org.matrix.android.sdk.internal.network.toFailure
import org.matrix.android.sdk.internal.util.TemporaryFileCreator
import timber.log.Timber
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
Expand Down Expand Up @@ -70,6 +74,7 @@ internal class FileUploader @Inject constructor(
): ContentUploadResponse {
// Check size limit
val maxUploadFileSize = homeServerCapabilitiesService.getHomeServerCapabilities().maxUploadFileSize
val maxImageSize = 32_000_000

if (maxUploadFileSize != HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN &&
file.length() > maxUploadFileSize) {
Expand All @@ -83,6 +88,24 @@ internal class FileUploader @Inject constructor(
)
}

// TCHAP Check image size limit
if (mimeType.isMimeTypeImage()) {
BitmapFactory.Options().run {
inJustDecodeBounds = true
decodeBitmap(file, this)
if (outHeight * outWidth > maxImageSize) {
// Known limitation and image size too big for the server, save the pain to upload it
throw Failure.ServerError(
error = MatrixError(
code = MatrixError.M_TOO_LARGE,
message = "Cannot upload images larger than ${maxImageSize / 1_000_000} Megapixels"
),
httpCode = 413
)
}
}
}

val uploadBody = object : RequestBody() {
override fun contentLength() = file.length()

Expand Down Expand Up @@ -135,6 +158,17 @@ internal class FileUploader @Inject constructor(
}
}

private fun decodeBitmap(file: File, options: BitmapFactory.Options = BitmapFactory.Options()): Bitmap? {
return try {
file.inputStream().use { inputStream ->
BitmapFactory.decodeStream(inputStream, null, options)
}
} catch (e: Exception) {
Timber.e(e, "Cannot decode Bitmap")
null
}
}

private suspend fun upload(
uploadBody: RequestBody,
filename: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ class AvatarRenderer @Inject constructor(
fun render(
glideRequests: GlideRequests,
matrixItem: MatrixItem,
target: Target<Drawable>
target: Target<Drawable>,
maxPxSize: Int = 0
) {
val placeholder = getPlaceholderDrawable(matrixItem)
glideRequests.loadResolvedUrl(matrixItem.avatarUrl)
Expand All @@ -172,7 +173,8 @@ class AvatarRenderer @Inject constructor(
it.transform(MultiTransformation(CenterCrop(), RoundedCorners(dimensionConverter.dpToPx(8))))
}
else -> {
it.apply(RequestOptions.circleCropTransform())
// TCHAP Fix avatar sizing
it.apply(RequestOptions.circleCropTransform().override(maxPxSize, maxPxSize))
}
}
}
Expand Down Expand Up @@ -264,10 +266,10 @@ class AvatarRenderer @Inject constructor(
}

@AnyThread
fun getCachedDrawable(glideRequests: GlideRequests, matrixItem: MatrixItem): Drawable {
fun getCachedDrawable(glideRequests: GlideRequests, matrixItem: MatrixItem, maxPxSize: Int = 0): Drawable {
return glideRequests.loadResolvedUrl(matrixItem.avatarUrl)
.onlyRetrieveFromCache(true)
.apply(RequestOptions.circleCropTransform())
.apply(RequestOptions.circleCropTransform().override(maxPxSize, maxPxSize)) // TCHAP Fix avatar sizing
.submit()
.get()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ class PillImageSpan(
override val matrixItem: MatrixItem
) : ReplacementSpan(), MatrixItemSpan {

private val maxPxSize = context.resources.getDimensionPixelSize(im.vector.lib.ui.styles.R.dimen.pill_avatar_size)
private val pillDrawable = createChipDrawable()
private val target = PillImageSpanTarget(this)
private var tv: WeakReference<TextView>? = null

@UiThread
fun bind(textView: TextView) {
tv = WeakReference(textView)
avatarRenderer.render(glideRequests, matrixItem, target)
avatarRenderer.render(glideRequests, matrixItem, target, maxPxSize) // TCHAP Fix avatar sizing
}

// ReplacementSpan *****************************************************************************
Expand Down Expand Up @@ -144,7 +145,7 @@ class PillImageSpan(
}
else -> {
try {
avatarRenderer.getCachedDrawable(glideRequests, matrixItem)
avatarRenderer.getCachedDrawable(glideRequests, matrixItem, maxPxSize) // TCHAP Fix avatar sizing
} catch (exception: Exception) {
avatarRenderer.getPlaceholderDrawable(matrixItem)
}
Expand Down

0 comments on commit 986f817

Please sign in to comment.