From 0bb269a37df0311703081405e87d303b44bce545 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Sun, 24 Mar 2024 19:32:28 +0100 Subject: [PATCH] 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. --- .../app/pachli/util/ShareShortcutHelper.kt | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/app/pachli/util/ShareShortcutHelper.kt b/app/src/main/java/app/pachli/util/ShareShortcutHelper.kt index 0392e34b1..a4ca7eac3 100644 --- a/app/src/main/java/app/pachli/util/ShareShortcutHelper.kt +++ b/app/src/main/java/app/pachli/util/ShareShortcutHelper.kt @@ -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,19 +37,30 @@ 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) + .submit(innerSize, innerSize) + .get() + } else { + Glide.with(context) + .asBitmap() + .load(account.profilePictureUrl) + .error(DR.drawable.avatar_default) + .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() - } else { - Glide.with(context) - .asBitmap() - .load(account.profilePictureUrl) - .error(DR.drawable.avatar_default) - .submit(innerSize, innerSize) - .get() } // inset the loaded bitmap inside a 108dp transparent canvas so it looks good as adaptive icon