Merge pull request #1135 from OxygenCobalt/hotfix

v4.0.6
This commit is contained in:
Alexander Capehart
2025-06-22 20:20:55 -06:00
committed by GitHub
13 changed files with 237 additions and 15 deletions
+10
View File
@@ -1,5 +1,15 @@
# Changelog
## 4.0.6
#### What's Improved
- Added option to enable/disable the file tree cache (disabled by default)
to prevent the library from not being updated on Samsung devices
#### What's Fixed
- Fixed loading failing/crashing the app on some devices
- Fixed file tree cache being stored in cache storage rather than app storage
## 4.0.5
#### What's Improved
+2 -2
View File
@@ -18,8 +18,8 @@ android {
defaultConfig {
applicationId namespace
versionName "4.0.5"
versionCode 64
versionName "4.0.6"
versionCode 65
minSdk min_sdk
targetSdk target_sdk
@@ -29,6 +29,7 @@ import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import org.oxycblt.auxio.image.covers.SettingCovers
import org.oxycblt.auxio.music.MusicRepository.IndexingWorker
import org.oxycblt.auxio.music.shim.NoOpFileTreeCache
import org.oxycblt.auxio.music.shim.WriteOnlyFileTreeCache
import org.oxycblt.auxio.music.shim.WriteOnlyMutableCache
import org.oxycblt.musikr.IndexingProgress
@@ -396,8 +397,12 @@ constructor(
val cache = if (withCache) cache else WriteOnlyMutableCache(cache)
val covers = settingCovers.mutate(context, newRevision)
val fileTreeCache =
if (withCache) FileTreeCache.from(context)
else WriteOnlyFileTreeCache(FileTreeCache.from(context))
if (musicSettings.useFileTreeCache) {
if (withCache) FileTreeCache.from(context)
else WriteOnlyFileTreeCache(FileTreeCache.from(context))
} else {
NoOpFileTreeCache()
}
val query = Query(source = locations, exclude = excludedLocations)
val storage = Storage(cache, covers, storedPlaylists, fileTreeCache)
val interpretation = Interpretation(nameFactory, separators, withHidden)
@@ -51,6 +51,8 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
var separators: String
/** Whether to enable more advanced sorting by articles and numbers. */
val intelligentSorting: Boolean
/** Whether to use the file-system cache for improved loading times. */
val useFileTreeCache: Boolean
interface Listener {
/** Called when the current music locations changed. */
@@ -133,6 +135,9 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
override val intelligentSorting: Boolean
get() = sharedPreferences.getBoolean(getString(R.string.set_key_auto_sort_names), true)
override val useFileTreeCache: Boolean
get() = sharedPreferences.getBoolean(getString(R.string.set_key_fs_cache), false)
override fun onSettingChanged(key: String, listener: MusicSettings.Listener) {
// TODO: Differentiate "hard reloads" (Need the cache) and "Soft reloads"
// (just need to manipulate data)
@@ -145,7 +150,8 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
getString(R.string.set_key_separators),
getString(R.string.set_key_auto_sort_names),
getString(R.string.set_key_with_hidden),
getString(R.string.set_key_exclude_non_music) -> {
getString(R.string.set_key_exclude_non_music),
getString(R.string.set_key_fs_cache) -> {
L.d("Dispatching indexing setting change for $key")
listener.onIndexingSettingChanged()
}
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2025 Auxio Project
* NoOpFileTreeCache.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.music.shim
import android.net.Uri
import org.oxycblt.musikr.fs.device.CachedDirectory
import org.oxycblt.musikr.fs.device.CachedFile
import org.oxycblt.musikr.fs.device.FileTree
import org.oxycblt.musikr.fs.device.FileTreeCache
/**
* A no-op implementation of [FileTreeCache] that doesn't cache anything. This implementation always
* returns cache misses and discards all updates.
*/
class NoOpFileTreeCache : FileTreeCache {
override fun read(): FileTree = NoOpFileTree()
}
/**
* A no-op implementation of [FileTree] that doesn't cache anything. All queries return null (cache
* miss) and all updates are discarded.
*/
class NoOpFileTree : FileTree {
override suspend fun queryDirectory(uri: Uri): CachedDirectory? {
// Always return null to simulate a cache miss
return null
}
override suspend fun updateDirectory(uri: Uri, directory: CachedDirectory) {
// Do nothing - discard the update
}
override suspend fun queryFile(uri: Uri): CachedFile? {
// Always return null to simulate a cache miss
return null
}
override suspend fun updateFile(uri: Uri, file: CachedFile) {
// Do nothing - discard the update
}
override suspend fun write() {
// Do nothing - no cache to write
}
}
+1
View File
@@ -15,6 +15,7 @@
<string name="set_key_observing" translatable="false">auxio_observing</string>
<string name="set_key_music_dirs" translatable="false">auxio_music_dirs</string>
<string name="set_key_excluded_dirs" translatable="false">auxio_excluded_dirs</string>
<string name="set_key_fs_cache" translatable="false">auxio_fs_cache</string>
<string name="set_key_cover_mode" translatable="false">auxio_cover_mode2</string>
<string name="set_key_square_covers" translatable="false">auxio_square_covers</string>
<string name="set_key_exclude_non_music" translatable="false">auxio_exclude_non_music</string>
+2
View File
@@ -315,6 +315,8 @@
<string name="set_locations">Music folders</string>
<string name="set_locations_desc">Manage where music should be loaded from</string>
<string name="set_locations_new">New folder</string>
<string name="set_fs_cache">Use file-system cache</string>
<string name="set_fs_cache_desc">Improves loading times, but can prevent new music from being added</string>
<string name="set_excluded_locations">Excluded folders</string>
<string name="set_excluded_locations_desc">Manage folders to exclude from music loading</string>
<string name="set_reindex">Refresh music</string>
@@ -38,6 +38,13 @@
app:summary="@string/set_excluded_locations_desc"
app:title="@string/set_excluded_locations" />
<SwitchPreferenceCompat
app:key="@string/set_key_fs_cache"
app:summary="@string/set_fs_cache_desc"
app:title="@string/set_fs_cache"
app:defaultValue="false" />
<Preference
app:key="@string/set_key_reindex"
app:summary="@string/set_reindex_desc"
@@ -0,0 +1,4 @@
Auxio 4.0.0 completely overhauls the user experience, with a refreshed design based on the latest Material Design specs
and a brand new music loader with signifigant improvements to device and tag support.
This patch fixes music loading failing/crashing on some devices and makes a caching system that prevented the music library from updating on some devices.
For more information, see https://github.com/OxygenCobalt/Auxio/releases/tag/v4.0.6.
@@ -18,7 +18,6 @@
package org.oxycblt.musikr.fs.device
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.provider.DocumentsContract
@@ -52,12 +51,12 @@ internal interface DeviceFS {
companion object {
fun from(context: Context, withHidden: Boolean): DeviceFS =
DeviceFSImpl(context.contentResolverSafe, withHidden)
DeviceFSImpl(context.rateLimitedContentResolver, withHidden)
}
}
private class DeviceFSImpl(
private val contentResolver: ContentResolver,
private val contentResolver: RateLimitedContentResolver,
private val withHidden: Boolean
) : DeviceFS {
private val explorationScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
@@ -25,9 +25,9 @@ import java.io.File
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
interface FileTreeCache {
fun read(): FileTree
@@ -94,8 +94,7 @@ private class FileTreeCacheImpl(private val context: Context) : FileTreeCache {
return if (cacheFile.exists()) {
try {
val jsonContent = cacheFile.readText()
val fileTreeCache = json.decodeFromString<FileTreeData>(jsonContent)
val fileTreeCache = json.decodeFromStream<FileTreeData>(cacheFile.inputStream())
FileTreeImpl(
context,
fileTreeCache.directories.toMutableMap(),
@@ -153,9 +152,7 @@ private class FileTreeImpl(
val fileTreeCache =
FileTreeData(
directories = mutableDirectories.toMap(), files = mutableFiles.toMap())
val jsonContent = json.encodeToString(fileTreeCache)
cacheFile.writeText(jsonContent)
json.encodeToStream(fileTreeCache, cacheFile.outputStream())
Log.d(TAG, "Successfully wrote cache to disk")
} catch (e: Exception) {
Log.e(TAG, "Failed to write cache file", e)
@@ -30,6 +30,15 @@ import android.net.Uri
internal val Context.contentResolverSafe: ContentResolver
get() = applicationContext.contentResolver
internal val Context.rateLimitedContentResolver: RateLimitedContentResolver
get() =
RateLimitedContentResolver(
contentResolver = contentResolverSafe,
maxTokens = 100, // Maximum burst capacity
refillRate = 1000.0, // 1000 queries per second (100 queries per 100ms)
maxConcurrency = 10 // Maximum concurrent queries
)
/**
* A shortcut for querying the [ContentResolver] database.
*
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2024 Auxio Project
* RateLimitedContentResolver.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.musikr.fs.device
import android.content.ContentResolver
import android.database.Cursor
import android.net.Uri
import kotlin.math.min
import kotlinx.coroutines.delay
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.sync.withPermit
/**
* A rate-limited wrapper around ContentResolver that uses a token bucket algorithm to limit the
* rate of queries.
*
* @param contentResolver The underlying ContentResolver to wrap
* @param maxTokens The maximum number of tokens in the bucket (burst capacity)
* @param refillRate The rate at which tokens are refilled per second
* @param maxConcurrency The maximum number of concurrent queries allowed
*/
internal class RateLimitedContentResolver(
private val contentResolver: ContentResolver,
private val maxTokens: Int = 1000,
private val refillRate: Double = 1000.0, // tokens per second
private val maxConcurrency: Int = 10
) {
private val tokenBucket = TokenBucket(maxTokens, refillRate)
private val concurrencySemaphore = Semaphore(maxConcurrency)
/**
* A rate-limited version of ContentResolver.query This is a suspend function that should be
* called from a coroutine
*/
suspend fun query(
uri: Uri,
projection: Array<out String>?,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): Cursor? =
concurrencySemaphore.withPermit {
tokenBucket.acquire()
contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
}
/**
* A shortcut for querying the database with automatic resource management. Rate-limited version
* of the useQuery extension function. This is a suspend function that should be called from a
* coroutine
*/
suspend inline fun <reified R> useQuery(
uri: Uri,
projection: Array<out String>,
selector: String? = null,
args: Array<String>? = null,
sortOrder: String? = null,
crossinline block: suspend (Cursor) -> R
): R =
concurrencySemaphore.withPermit {
tokenBucket.acquire()
contentResolver.useQuery(uri, projection, selector, args, sortOrder) { cursor ->
block(cursor)
}
}
/** Token bucket implementation for rate limiting */
internal class TokenBucket(
private val maxTokens: Int,
private val refillRate: Double // tokens per second
) {
private var tokens: Double = maxTokens.toDouble()
private var lastRefillTime: Long = System.currentTimeMillis()
private val mutex = Mutex()
private val tokenWaitTime = (1000.0 / refillRate).toLong() // milliseconds per token
suspend fun acquire() {
mutex.withLock {
refill()
// Wait until a token is available
while (tokens < 1.0) {
val waitTime = ((1.0 - tokens) * tokenWaitTime).toLong()
delay(waitTime)
refill()
}
tokens -= 1.0
}
}
private fun refill() {
val now = System.currentTimeMillis()
val timePassed = now - lastRefillTime
val tokensToAdd = (timePassed / 1000.0) * refillRate
if (tokensToAdd > 0) {
tokens = min(tokens + tokensToAdd, maxTokens.toDouble())
lastRefillTime = now
}
}
}
}