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:
parent
d3c7c7c89a
commit
0bb269a37d
@ -29,6 +29,7 @@ import app.pachli.core.database.model.AccountEntity
|
|||||||
import app.pachli.core.designsystem.R as DR
|
import app.pachli.core.designsystem.R as DR
|
||||||
import app.pachli.core.navigation.MainActivityIntent
|
import app.pachli.core.navigation.MainActivityIntent
|
||||||
import com.bumptech.glide.Glide
|
import com.bumptech.glide.Glide
|
||||||
|
import java.util.concurrent.ExecutionException
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
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 innerSize = context.resources.getDimensionPixelSize(DR.dimen.adaptive_bitmap_inner_size)
|
||||||
val outerSize = context.resources.getDimensionPixelSize(DR.dimen.adaptive_bitmap_outer_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)
|
Glide.with(context)
|
||||||
.asBitmap()
|
.asBitmap()
|
||||||
.load(DR.drawable.avatar_default)
|
.load(DR.drawable.avatar_default)
|
||||||
.submit(innerSize, innerSize)
|
.submit(innerSize, innerSize)
|
||||||
.get()
|
.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
|
// inset the loaded bitmap inside a 108dp transparent canvas so it looks good as adaptive icon
|
||||||
|
Loading…
x
Reference in New Issue
Block a user