Skip to content

Commit

Permalink
fix: Don't crash on invalid avatars (#566)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikclayton committed Mar 24, 2024
1 parent d3c7c7c commit 0bb269a
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions app/src/main/java/app/pachli/util/ShareShortcutHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,38 @@ 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

suspend fun updateShortcut(context: Context, account: AccountEntity) = withContext(Dispatchers.IO) {
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
Expand Down

0 comments on commit 0bb269a

Please sign in to comment.