Skip to content

Commit

Permalink
Fix crash when album art bitmap is not initialized (race condition) (…
Browse files Browse the repository at this point in the history
…should replace with proper concurrency in the future) (closes #655)
  • Loading branch information
25huizengek1 committed Jan 6, 2025
1 parent bf8ec7b commit a837ee9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/src/main/kotlin/app/vitune/android/service/BitmapProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ class BitmapProvider(
private val getBitmapSize: () -> Int,
private val getColor: (isDark: Boolean) -> Int
) {
@set:Synchronized
var lastUri: Uri? = null
private set

@set:Synchronized
private var lastBitmap: Bitmap? = null
get() = field?.takeUnless { it.isRecycled }
set(value) {
field = value
listener?.invoke(value)
Expand All @@ -41,7 +44,11 @@ class BitmapProvider(
val isSystemInDarkMode = resources.configuration.uiMode and
Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES

if (::defaultBitmap.isInitialized && isSystemInDarkMode == lastIsSystemInDarkMode) return false
var oldBitmap: Bitmap? = null
if (::defaultBitmap.isInitialized) {
if (isSystemInDarkMode == lastIsSystemInDarkMode) return false
oldBitmap = defaultBitmap
}

lastIsSystemInDarkMode = isSystemInDarkMode

Expand All @@ -53,6 +60,7 @@ class BitmapProvider(
).applyCanvas {
drawColor(getColor(isSystemInDarkMode))
}
oldBitmap?.recycle()

return lastBitmap == null
}
Expand All @@ -66,7 +74,6 @@ class BitmapProvider(
return
}

currentTask?.dispose()
lastUri = uri

if (uri == null) {
Expand All @@ -75,6 +82,7 @@ class BitmapProvider(
return
}

val oldTask = currentTask
currentTask = applicationContext.imageLoader.enqueue(
ImageRequest.Builder(applicationContext)
.data(uri.thumbnail(getBitmapSize()))
Expand All @@ -91,6 +99,7 @@ class BitmapProvider(
)
.build()
)
oldTask?.dispose()
}

fun setListener(callback: ((Bitmap?) -> Unit)?) {
Expand Down

0 comments on commit a837ee9

Please sign in to comment.