fix: Don't crash on invalid avatars (#566)

Workaround a Glide bug where the error() handler is not always called,
in this case when the URL does not resolve to an image; for example, a
misconfigured server that redirects requests for the image to an HTML
page.

Catch the exception and use the default avatar image in these cases.
This commit is contained in:
Nik Clayton 2024-03-24 19:32:28 +01:00 committed by GitHub
parent d3c7c7c89a
commit 0bb269a37d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 8 deletions

View File

@ -29,6 +29,7 @@ import app.pachli.core.database.model.AccountEntity
import app.pachli.core.designsystem.R as DR
import app.pachli.core.navigation.MainActivityIntent
import com.bumptech.glide.Glide
import java.util.concurrent.ExecutionException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@ -36,7 +37,8 @@ suspend fun updateShortcut(context: Context, account: AccountEntity) = withConte
val innerSize = context.resources.getDimensionPixelSize(DR.dimen.adaptive_bitmap_inner_size)
val outerSize = context.resources.getDimensionPixelSize(DR.dimen.adaptive_bitmap_outer_size)
val bmp = if (TextUtils.isEmpty(account.profilePictureUrl)) {
val bmp = try {
if (TextUtils.isEmpty(account.profilePictureUrl)) {
Glide.with(context)
.asBitmap()
.load(DR.drawable.avatar_default)
@ -50,6 +52,16 @@ suspend fun updateShortcut(context: Context, account: AccountEntity) = withConte
.submit(innerSize, innerSize)
.get()
}
} catch (e: ExecutionException) {
// The `.error` handler isn't always used. For example, Glide throws
// ExecutionException if the URL does not point at an image. Fallback to
// the default avatar (https://github.com/bumptech/glide/issues/4672).
Glide.with(context)
.asBitmap()
.load(DR.drawable.avatar_default)
.submit(innerSize, innerSize)
.get()
}
// inset the loaded bitmap inside a 108dp transparent canvas so it looks good as adaptive icon
val outBmp = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARGB_8888)