fix crash when glide fails to load avatar in ShareShortcutHelper

This commit is contained in:
Conny Duck 2024-05-04 16:19:11 +02:00
parent e96ca01dec
commit b7d2f37a67
No known key found for this signature in database
2 changed files with 22 additions and 9 deletions

View File

@ -8,7 +8,6 @@ import com.bumptech.glide.request.target.Target
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlinx.coroutines.suspendCancellableCoroutine
import okio.IOException
/**
* Allows waiting for a Glide request to complete without blocking a background thread.
@ -26,7 +25,7 @@ suspend fun <R> RequestBuilder<R>.submitAsync(
target: Target<R>,
isFirstResource: Boolean
): Boolean {
continuation.resumeWithException(e ?: IOException("Image loading failed"))
continuation.resumeWithException(e ?: GlideException("Image loading failed"))
return false
}

View File

@ -21,11 +21,15 @@ import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.util.Log
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.app.Person
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.core.graphics.drawable.toBitmap
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.GlideException
import com.keylesspalace.tusky.MainActivity
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.db.AccountEntity
@ -49,14 +53,20 @@ class ShareShortcutHelper @Inject constructor(
val maxNumberOfShortcuts = ShortcutManagerCompat.getMaxShortcutCountPerActivity(context)
val shortcuts = accountManager.accounts.take(maxNumberOfShortcuts).map { account ->
val shortcuts = accountManager.accounts.take(maxNumberOfShortcuts).mapNotNull { account ->
val bmp = Glide.with(context)
.asBitmap()
.load(account.profilePictureUrl)
.placeholder(R.drawable.avatar_default)
.error(R.drawable.avatar_default)
.submitAsync(innerSize, innerSize)
val bmp = try {
Glide.with(context)
.asBitmap()
.load(account.profilePictureUrl)
.placeholder(R.drawable.avatar_default)
.error(R.drawable.avatar_default)
.submitAsync(innerSize, innerSize)
} catch (e: GlideException) {
// https://github.com/bumptech/glide/issues/4672 :/
Log.w(TAG, "failed to load avatar ${account.profilePictureUrl}", e)
AppCompatResources.getDrawable(context, R.drawable.avatar_default)?.toBitmap(innerSize, innerSize) ?: return@mapNotNull null
}
// 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)
@ -101,4 +111,8 @@ class ShareShortcutHelper @Inject constructor(
fun removeShortcut(account: AccountEntity) {
ShortcutManagerCompat.removeDynamicShortcuts(context, listOf(account.id.toString()))
}
companion object {
private const val TAG = "ShareShortcutHelper"
}
}