Skip to content

Commit

Permalink
[v25.1] fix crash when glide fails to load avatar in ShareShortcutHel…
Browse files Browse the repository at this point in the history
…per (tuskyapp#4417)

🙄 
fixes tuskyapp#4416
  • Loading branch information
connyduck authored May 4, 2024
1 parent 056aaa7 commit c10f82f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -49,14 +53,20 @@ class ShareShortcutHelper @Inject constructor(

val maxNumberOfShortcuts = ShortcutManagerCompat.getMaxShortcutCountPerActivity(context)

val shortcuts = accountManager.accounts.take(maxNumberOfShortcuts).map { account ->

val bmp = Glide.with(context)
.asBitmap()
.load(account.profilePictureUrl)
.placeholder(R.drawable.avatar_default)
.error(R.drawable.avatar_default)
.submitAsync(innerSize, innerSize)
val shortcuts = accountManager.accounts.take(maxNumberOfShortcuts).mapNotNull { account ->

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)
Expand Down Expand Up @@ -100,4 +110,8 @@ class ShareShortcutHelper @Inject constructor(
fun removeShortcut(account: AccountEntity) {
ShortcutManagerCompat.removeDynamicShortcuts(context, listOf(account.id.toString()))
}

companion object {
private const val TAG = "ShareShortcutHelper"
}
}

0 comments on commit c10f82f

Please sign in to comment.