image: fix provider caching issues

- Covers would hypothetically not be updated in android auto
if the setting changed to off
- Cover fetching might fail in weird ways due to the current
error throwing
This commit is contained in:
Alexander Capehart 2025-01-06 11:32:03 -07:00
parent bbc4db156e
commit 298a30da6d
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 12 additions and 11 deletions

View file

@ -35,12 +35,13 @@ class CoverProvider : ContentProvider() {
override fun onCreate(): Boolean = true
override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? {
check(mode == "r") { "Unsupported mode: $mode" }
check(uriMatcher.match(uri) == 1) { "Unknown URI: $uri" }
val id = requireNotNull(uri.lastPathSegment) { "No ID in URI: $uri" }
val coverId = requireNotNull(SiloedCoverId.parse(id)) { "Invalid ID: $id" }
if (mode != "r" || uriMatcher.match(uri) != 1) {
return null
}
val id = uri.lastPathSegment ?: return null
val coverId = SiloedCoverId.parse(id) ?: return null
return runBlocking {
val siloedCovers = SiloedCovers.from(requireContext(), coverId.silo)
val siloedCovers = SiloedCovers.from(requireNotNull(context), coverId.silo)
when (val res = siloedCovers.obtain(id)) {
is ObtainResult.Hit -> res.cover.fd()
is ObtainResult.Miss -> null

View file

@ -20,21 +20,21 @@ package org.oxycblt.auxio.image.covers
import android.content.Context
import org.oxycblt.musikr.cover.Cover
import org.oxycblt.musikr.cover.CoverIdentifier
import org.oxycblt.musikr.cover.MutableCovers
import org.oxycblt.musikr.cover.ObtainResult
class NullCovers(private val context: Context, private val identifier: CoverIdentifier) :
class NullCovers(private val context: Context) :
MutableCovers {
override suspend fun obtain(id: String) = ObtainResult.Hit(NullCover(id))
override suspend fun obtain(id: String) = ObtainResult.Hit(NullCover)
override suspend fun write(data: ByteArray): Cover = NullCover(identifier.identify(data))
override suspend fun write(data: ByteArray): Cover = NullCover
override suspend fun cleanup(excluding: Collection<Cover>) {
context.coversDir().listFiles()?.forEach { it.deleteRecursively() }
}
}
class NullCover(override val id: String) : Cover {
data object NullCover : Cover {
override val id = "null"
override suspend fun open() = null
}

View file

@ -37,7 +37,7 @@ constructor(private val imageSettings: ImageSettings, private val identifier: Co
SettingCovers {
override suspend fun create(context: Context, revision: UUID): MutableCovers =
when (imageSettings.coverMode) {
CoverMode.OFF -> NullCovers(context, identifier)
CoverMode.OFF -> NullCovers(context)
CoverMode.SAVE_SPACE -> siloedCovers(context, revision, CoverParams.of(500, 70))
CoverMode.BALANCED -> siloedCovers(context, revision, CoverParams.of(750, 85))
CoverMode.HIGH_QUALITY -> siloedCovers(context, revision, CoverParams.of(1000, 100))