musikr: refactor fs api
Need to make it extendable to MediaStore now.
This commit is contained in:
@@ -22,7 +22,7 @@ import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.covers.stored.CoverStorage
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
class NullCovers(private val storage: CoverStorage) : MutableCovers<NullCover> {
|
||||
|
||||
@@ -30,6 +30,7 @@ import kotlinx.coroutines.yield
|
||||
import org.oxycblt.auxio.image.covers.SettingCovers
|
||||
import org.oxycblt.auxio.music.MusicRepository.IndexingWorker
|
||||
import org.oxycblt.auxio.music.shim.WriteOnlyMutableCache
|
||||
import org.oxycblt.musikr.Config
|
||||
import org.oxycblt.musikr.IndexingProgress
|
||||
import org.oxycblt.musikr.Interpretation
|
||||
import org.oxycblt.musikr.Library
|
||||
@@ -37,10 +38,10 @@ import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.Musikr
|
||||
import org.oxycblt.musikr.MutableLibrary
|
||||
import org.oxycblt.musikr.Playlist
|
||||
import org.oxycblt.musikr.Query
|
||||
import org.oxycblt.musikr.Song
|
||||
import org.oxycblt.musikr.Storage
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.fs.saf.SAF
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.tag.interpret.Naming
|
||||
import org.oxycblt.musikr.tag.interpret.Separators
|
||||
@@ -393,10 +394,11 @@ constructor(
|
||||
val newRevision = currentRevision?.takeIf { withCache } ?: UUID.randomUUID()
|
||||
val cache = if (withCache) cache else WriteOnlyMutableCache(cache)
|
||||
val covers = settingCovers.mutate(context, newRevision)
|
||||
val query = Query(source = locations, exclude = excludedLocations)
|
||||
val fs = SAF.from(context, SAF.Query(locations, excludedLocations, withHidden))
|
||||
val storage = Storage(cache, covers, storedPlaylists)
|
||||
val interpretation = Interpretation(nameFactory, separators, withHidden)
|
||||
val result = Musikr.new(context, storage, interpretation).run(query, ::emitIndexingProgress)
|
||||
val interpretation = Interpretation(nameFactory, separators)
|
||||
val config = Config(fs, storage, interpretation)
|
||||
val result = Musikr.new(context, config).run(::emitIndexingProgress)
|
||||
// Music loading completed, update the revision right now so we re-use this work
|
||||
// later.
|
||||
musicSettings.revision = newRevision
|
||||
|
||||
@@ -21,7 +21,7 @@ package org.oxycblt.auxio.music.shim
|
||||
import org.oxycblt.musikr.cache.CacheResult
|
||||
import org.oxycblt.musikr.cache.CachedSong
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
|
||||
class WriteOnlyMutableCache(private val inner: MutableCache) : MutableCache {
|
||||
override suspend fun read(file: DeviceFile): CacheResult {
|
||||
|
||||
@@ -21,15 +21,14 @@ package org.oxycblt.musikr
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
import org.oxycblt.musikr.fs.Location.Opened
|
||||
import org.oxycblt.musikr.fs.FS
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.tag.interpret.Naming
|
||||
import org.oxycblt.musikr.tag.interpret.Separators
|
||||
|
||||
data class Query(val source: List<Location.Opened>, val exclude: List<Location>)
|
||||
data class Config(val fs: FS, val storage: Storage, val interpretation: Interpretation)
|
||||
|
||||
/** Side-effect laden [Storage] for use during music loading and [MutableLibrary] operation. */
|
||||
/** Side-effect laden [Config] for use during music loading and [MutableLibrary] operation. */
|
||||
data class Storage(
|
||||
/**
|
||||
* A repository of cached metadata to read and write from over the course of music loading. This
|
||||
@@ -49,17 +48,13 @@ data class Storage(
|
||||
* be used during music loading and mutated when creating, renaming, or deleting playlists in
|
||||
* the library.
|
||||
*/
|
||||
val storedPlaylists: StoredPlaylists
|
||||
val storedPlaylists: StoredPlaylists,
|
||||
)
|
||||
|
||||
/** Configuration for how to interpret and extrapolate certain audio tags. */
|
||||
data class Interpretation(
|
||||
/** How to construct names from audio tags. */
|
||||
val naming: Naming,
|
||||
|
||||
/** What separators delimit multi-value audio tags. */
|
||||
val separators: Separators,
|
||||
|
||||
/** Whether to include hidden files and directories (those starting with a dot). */
|
||||
val withHidden: Boolean
|
||||
)
|
||||
|
||||
@@ -99,7 +99,7 @@ interface Library {
|
||||
/**
|
||||
* A mutable extension of [Library].
|
||||
*
|
||||
* Operations here will cause side-effects within the [Storage] used when this library was loaded.
|
||||
* Operations here will cause side-effects within the [Config] used when this library was loaded.
|
||||
* However, it won't actually mutate the [Library] itself, rather return a cloned instance with the
|
||||
* changes applied. It is up to the client to update their reference to the library within their
|
||||
* state handling.
|
||||
@@ -108,7 +108,7 @@ interface MutableLibrary : Library {
|
||||
/**
|
||||
* Create a new [Playlist] with the given name and songs.
|
||||
*
|
||||
* This will commit the new playlist to the stored playlists in the [Storage] used to load the
|
||||
* This will commit the new playlist to the stored playlists in the [Config] used to load the
|
||||
* library.
|
||||
*
|
||||
* @param name the name of the playlist
|
||||
|
||||
@@ -44,36 +44,32 @@ import org.oxycblt.musikr.pipeline.ExtractStep
|
||||
*/
|
||||
interface Musikr {
|
||||
/**
|
||||
* Start loading music from the given [locations] and the configuration provided earlier.
|
||||
* Start loading music using the given config and the configuration provided earlier.
|
||||
*
|
||||
* @param query The [Query] to load music from.
|
||||
* @param onProgress Optional callback to receive progress on the current status of the music
|
||||
* pipeline. Warning: These events will be rapid-fire.
|
||||
* @return A handle to the newly created library alongside further cleanup.
|
||||
*/
|
||||
suspend fun run(
|
||||
query: Query,
|
||||
onProgress: suspend (IndexingProgress) -> Unit = {}
|
||||
): LibraryResult
|
||||
suspend fun run(onProgress: suspend (IndexingProgress) -> Unit = {}): LibraryResult
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Create a new instance from the given configuration.
|
||||
*
|
||||
* @param context The context to use for loading resources.
|
||||
* @param storage Side-effect laden storage for use within the music loader **and** when
|
||||
* @param config Side-effect laden storage for use within the music loader **and** when
|
||||
* mutating [MutableLibrary]. You should take responsibility for managing their long-term
|
||||
* state.
|
||||
* @param interpretation The configuration to use for interpreting certain vague tags. This
|
||||
* should be configured by the user, if possible.
|
||||
*/
|
||||
fun new(context: Context, storage: Storage, interpretation: Interpretation): Musikr =
|
||||
fun new(context: Context, config: Config): Musikr =
|
||||
MusikrImpl(
|
||||
context,
|
||||
storage,
|
||||
ExploreStep.from(context, storage, interpretation),
|
||||
ExtractStep.from(context, storage),
|
||||
EvaluateStep.new(storage, interpretation))
|
||||
config,
|
||||
ExploreStep.from(context, config),
|
||||
ExtractStep.from(context, config),
|
||||
EvaluateStep.new(config, config.interpretation))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,37 +107,34 @@ sealed interface IndexingProgress {
|
||||
|
||||
private class MusikrImpl(
|
||||
private val context: Context,
|
||||
private val storage: Storage,
|
||||
private val config: Config,
|
||||
private val exploreStep: ExploreStep,
|
||||
private val extractStep: ExtractStep,
|
||||
private val evaluateStep: EvaluateStep
|
||||
) : Musikr {
|
||||
override suspend fun run(query: Query, onProgress: suspend (IndexingProgress) -> Unit) =
|
||||
coroutineScope {
|
||||
var exploredCount = 0
|
||||
var extractedCount = 0
|
||||
val explored =
|
||||
exploreStep
|
||||
.explore(query)
|
||||
.buffer(Channel.UNLIMITED)
|
||||
.onStart { onProgress(IndexingProgress.Songs(0, 0)) }
|
||||
.onEach { onProgress(IndexingProgress.Songs(extractedCount, ++exploredCount)) }
|
||||
val extracted =
|
||||
extractStep
|
||||
.extract(explored)
|
||||
.buffer(Channel.UNLIMITED)
|
||||
.onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) }
|
||||
.onCompletion { onProgress(IndexingProgress.Indeterminate) }
|
||||
val library = evaluateStep.evaluate(extracted)
|
||||
LibraryResultImpl(storage, library)
|
||||
}
|
||||
override suspend fun run(onProgress: suspend (IndexingProgress) -> Unit) = coroutineScope {
|
||||
var exploredCount = 0
|
||||
var extractedCount = 0
|
||||
val explored =
|
||||
exploreStep
|
||||
.explore()
|
||||
.buffer(Channel.UNLIMITED)
|
||||
.onStart { onProgress(IndexingProgress.Songs(0, 0)) }
|
||||
.onEach { onProgress(IndexingProgress.Songs(extractedCount, ++exploredCount)) }
|
||||
val extracted =
|
||||
extractStep
|
||||
.extract(explored)
|
||||
.buffer(Channel.UNLIMITED)
|
||||
.onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) }
|
||||
.onCompletion { onProgress(IndexingProgress.Indeterminate) }
|
||||
val library = evaluateStep.evaluate(extracted)
|
||||
LibraryResultImpl(config, library)
|
||||
}
|
||||
}
|
||||
|
||||
private class LibraryResultImpl(
|
||||
private val storage: Storage,
|
||||
override val library: MutableLibrary
|
||||
) : LibraryResult {
|
||||
private class LibraryResultImpl(private val config: Config, override val library: MutableLibrary) :
|
||||
LibraryResult {
|
||||
override suspend fun cleanup() {
|
||||
storage.covers.cleanup(library.songs.mapNotNull { it.cover })
|
||||
config.storage.covers.cleanup(library.songs.mapNotNull { it.cover })
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@
|
||||
|
||||
package org.oxycblt.musikr.cache
|
||||
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Properties
|
||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.oxycblt.musikr.cache.Cache
|
||||
import org.oxycblt.musikr.cache.CacheResult
|
||||
import org.oxycblt.musikr.cache.CachedSong
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Properties
|
||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ package org.oxycblt.musikr.covers
|
||||
|
||||
import android.os.ParcelFileDescriptor
|
||||
import java.io.InputStream
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.MemoryCover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.covers.FDCover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
private const val PREFIX = "mcf:"
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.covers.FDCover
|
||||
import org.oxycblt.musikr.covers.MemoryCover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
private const val PREFIX = "mcs:"
|
||||
|
||||
+7
-3
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Auxio Project
|
||||
* DeviceFSEntries.kt is part of Auxio.
|
||||
* FS.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
|
||||
@@ -16,11 +16,15 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.fs.device
|
||||
package org.oxycblt.musikr.fs
|
||||
|
||||
import android.net.Uri
|
||||
import kotlinx.coroutines.Deferred
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface FS {
|
||||
fun explore(): Flow<DeviceFile>
|
||||
}
|
||||
|
||||
sealed interface DeviceFSEntry {
|
||||
val uri: Uri
|
||||
@@ -22,8 +22,8 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.DocumentsContract
|
||||
import org.oxycblt.musikr.fs.device.contentResolverSafe
|
||||
import org.oxycblt.musikr.fs.path.DocumentPathFactory
|
||||
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
||||
|
||||
sealed class Location(val uri: Uri, val path: Path) {
|
||||
override fun equals(other: Any?) = other is Location && uri == other.uri
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,8 @@ import java.io.File
|
||||
import org.oxycblt.musikr.fs.Components
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import org.oxycblt.musikr.fs.Volume
|
||||
import org.oxycblt.musikr.fs.device.contentResolverSafe
|
||||
import org.oxycblt.musikr.fs.device.useQuery
|
||||
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
||||
import org.oxycblt.musikr.fs.saf.useQuery
|
||||
|
||||
/**
|
||||
* A factory for parsing the reverse-engineered format of the URIs obtained from document picker.
|
||||
|
||||
+1
-10
@@ -16,7 +16,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.fs.device
|
||||
package org.oxycblt.musikr.fs.saf
|
||||
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
@@ -30,15 +30,6 @@ 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.
|
||||
*
|
||||
+24
-22
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Auxio Project
|
||||
* DeviceFS.kt is part of Auxio.
|
||||
* Copyright (c) 2025 Auxio Project
|
||||
* SAF.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
|
||||
@@ -16,8 +16,9 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.fs.device
|
||||
package org.oxycblt.musikr.fs.saf
|
||||
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.provider.DocumentsContract
|
||||
@@ -30,24 +31,17 @@ import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.flow.flatMapMerge
|
||||
import kotlinx.coroutines.flow.flattenMerge
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import org.oxycblt.musikr.Query
|
||||
import org.oxycblt.musikr.fs.DeviceDirectory
|
||||
import org.oxycblt.musikr.fs.DeviceFSEntry
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.FS
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
|
||||
internal interface DeviceFS {
|
||||
fun explore(query: Query): Flow<DeviceFile>
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, withHidden: Boolean): DeviceFS =
|
||||
DeviceFSImpl(context.rateLimitedContentResolver, withHidden)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
private class DeviceFSImpl(
|
||||
private val contentResolver: RateLimitedContentResolver,
|
||||
private val withHidden: Boolean
|
||||
) : DeviceFS {
|
||||
override fun explore(query: Query): Flow<DeviceFile> =
|
||||
class SAF
|
||||
private constructor(private val contentResolver: ContentResolver, private val query: Query) : FS {
|
||||
override fun explore(): Flow<DeviceFile> =
|
||||
query.source.asFlow().flatMapMerge { location ->
|
||||
exploreDirectoryImpl(
|
||||
location.uri,
|
||||
@@ -57,7 +51,7 @@ private class DeviceFSImpl(
|
||||
query.exclude.mapTo(mutableSetOf()) { it.uri })
|
||||
}
|
||||
|
||||
private suspend fun exploreDirectoryImpl(
|
||||
private fun exploreDirectoryImpl(
|
||||
rootUri: Uri,
|
||||
treeDocumentId: String,
|
||||
relativePath: Path,
|
||||
@@ -85,7 +79,7 @@ private class DeviceFSImpl(
|
||||
val displayName = cursor.getString(displayNameIndex)
|
||||
|
||||
// Skip hidden files/directories if ignoreHidden is true
|
||||
if (!withHidden && displayName.startsWith(".")) {
|
||||
if (!query.withHidden && displayName.startsWith(".")) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -122,8 +116,16 @@ private class DeviceFSImpl(
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val PROJECTION =
|
||||
data class Query(
|
||||
val source: List<Location.Opened>,
|
||||
val exclude: List<Location.Unopened>,
|
||||
val withHidden: Boolean
|
||||
)
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, query: Query) = SAF(context.contentResolverSafe, query)
|
||||
|
||||
private val PROJECTION =
|
||||
arrayOf(
|
||||
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
|
||||
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
|
||||
@@ -23,7 +23,7 @@ import android.content.Context
|
||||
import java.io.FileInputStream
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
|
||||
internal interface MetadataExtractor {
|
||||
suspend fun extract(deviceFile: DeviceFile): Metadata?
|
||||
|
||||
@@ -21,7 +21,7 @@ package org.oxycblt.musikr.metadata
|
||||
import android.util.Log
|
||||
import java.io.FileInputStream
|
||||
import java.nio.ByteBuffer
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
|
||||
internal class NativeInputStream(private val deviceFile: DeviceFile, fis: FileInputStream) {
|
||||
private val channel = fis.channel
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
package org.oxycblt.musikr.metadata
|
||||
|
||||
import java.io.FileInputStream
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
|
||||
internal object TagLibJNI {
|
||||
init {
|
||||
|
||||
@@ -20,9 +20,9 @@ package org.oxycblt.musikr.pipeline
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.filterIsInstance
|
||||
import org.oxycblt.musikr.Config
|
||||
import org.oxycblt.musikr.Interpretation
|
||||
import org.oxycblt.musikr.MutableLibrary
|
||||
import org.oxycblt.musikr.Storage
|
||||
import org.oxycblt.musikr.graph.MusicGraph
|
||||
import org.oxycblt.musikr.model.LibraryFactory
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
@@ -33,11 +33,11 @@ internal interface EvaluateStep {
|
||||
suspend fun evaluate(extractedMusic: Flow<Extracted>): MutableLibrary
|
||||
|
||||
companion object {
|
||||
fun new(storage: Storage, interpretation: Interpretation): EvaluateStep =
|
||||
fun new(config: Config, interpretation: Interpretation): EvaluateStep =
|
||||
EvaluateStepImpl(
|
||||
TagInterpreter.new(interpretation),
|
||||
PlaylistInterpreter.new(interpretation),
|
||||
storage.storedPlaylists,
|
||||
config.storage.storedPlaylists,
|
||||
LibraryFactory.new())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,46 +30,32 @@ import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.merge
|
||||
import org.oxycblt.musikr.Interpretation
|
||||
import org.oxycblt.musikr.Query
|
||||
import org.oxycblt.musikr.Config
|
||||
import org.oxycblt.musikr.Storage
|
||||
import org.oxycblt.musikr.cache.Cache
|
||||
import org.oxycblt.musikr.cache.CacheResult
|
||||
import org.oxycblt.musikr.cache.CachedSong
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.fs.device.DeviceFS
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.fs.FS
|
||||
import org.oxycblt.musikr.playlist.m3u.M3U
|
||||
|
||||
internal interface ExploreStep {
|
||||
fun explore(query: Query): Flow<Explored>
|
||||
fun explore(): Flow<Explored>
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, storage: Storage, interpretation: Interpretation): ExploreStep =
|
||||
ExploreStepImpl(
|
||||
DeviceFS.from(context = context, withHidden = interpretation.withHidden),
|
||||
storage.cache,
|
||||
storage.covers,
|
||||
storage.storedPlaylists)
|
||||
fun from(context: Context, config: Config): ExploreStep =
|
||||
ExploreStepImpl(config.fs, config.storage)
|
||||
}
|
||||
}
|
||||
|
||||
private class ExploreStepImpl(
|
||||
private val deviceFS: DeviceFS,
|
||||
private val cache: Cache,
|
||||
private val covers: Covers<out Cover>,
|
||||
private val storedPlaylists: StoredPlaylists
|
||||
) : ExploreStep {
|
||||
override fun explore(query: Query): Flow<Explored> {
|
||||
private class ExploreStepImpl(private val fs: FS, private val storage: Storage) : ExploreStep {
|
||||
override fun explore(): Flow<Explored> {
|
||||
val addingMs = System.currentTimeMillis()
|
||||
return merge(
|
||||
deviceFS
|
||||
.explore(query)
|
||||
fs.explore()
|
||||
.filter { it.mimeType.startsWith("audio/") || it.mimeType == M3U.MIME_TYPE }
|
||||
.distributedMap(n = 8, on = Dispatchers.IO, buffer = Channel.UNLIMITED) { file ->
|
||||
when (val cacheResult = cache.read(file)) {
|
||||
when (val cacheResult = storage.cache.read(file)) {
|
||||
is CacheResult.Hit -> NeedsCover(cacheResult.song)
|
||||
is CacheResult.Stale ->
|
||||
Finalized(NewSong(cacheResult.file, cacheResult.addedMs))
|
||||
@@ -83,7 +69,7 @@ private class ExploreStepImpl(
|
||||
is Finalized -> it
|
||||
is NeedsCover -> {
|
||||
when (val coverResult =
|
||||
it.cachedSong.coverId?.let { id -> covers.obtain(id) }) {
|
||||
it.cachedSong.coverId?.let { id -> storage.covers.obtain(id) }) {
|
||||
is CoverResult.Hit ->
|
||||
Finalized(it.cachedSong.toRawSong(coverResult.cover))
|
||||
null -> Finalized(it.cachedSong.toRawSong(null))
|
||||
@@ -96,7 +82,7 @@ private class ExploreStepImpl(
|
||||
.map { it.explored }
|
||||
.flowOn(Dispatchers.IO)
|
||||
.buffer(Channel.UNLIMITED),
|
||||
flow { emitAll(storedPlaylists.read().asFlow()) }
|
||||
flow { emitAll(storage.storedPlaylists.read().asFlow()) }
|
||||
.map { RawPlaylist(it) }
|
||||
.flowOn(Dispatchers.IO)
|
||||
.buffer(Channel.UNLIMITED))
|
||||
|
||||
@@ -26,7 +26,7 @@ import kotlinx.coroutines.flow.buffer
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onCompletion
|
||||
import org.oxycblt.musikr.Storage
|
||||
import org.oxycblt.musikr.Config
|
||||
import org.oxycblt.musikr.cache.CachedSong
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
@@ -40,9 +40,12 @@ internal interface ExtractStep {
|
||||
fun extract(nodes: Flow<Explored>): Flow<Extracted>
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, storage: Storage): ExtractStep =
|
||||
fun from(context: Context, config: Config): ExtractStep =
|
||||
ExtractStepImpl(
|
||||
MetadataExtractor.from(context), TagParser.new(), storage.cache, storage.covers)
|
||||
MetadataExtractor.from(context),
|
||||
TagParser.new(),
|
||||
config.storage.cache,
|
||||
config.storage.covers)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
package org.oxycblt.musikr.pipeline
|
||||
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.metadata.Properties
|
||||
import org.oxycblt.musikr.playlist.PlaylistFile
|
||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||
|
||||
@@ -23,8 +23,8 @@ import android.net.Uri
|
||||
import org.oxycblt.musikr.Playlist
|
||||
import org.oxycblt.musikr.fs.Components
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import org.oxycblt.musikr.fs.device.contentResolverSafe
|
||||
import org.oxycblt.musikr.fs.path.DocumentPathFactory
|
||||
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
||||
import org.oxycblt.musikr.playlist.m3u.M3U
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,8 +20,8 @@ package org.oxycblt.musikr.tag.interpret
|
||||
|
||||
import org.oxycblt.musikr.Interpretation
|
||||
import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.Format
|
||||
import org.oxycblt.musikr.fs.device.DeviceFile
|
||||
import org.oxycblt.musikr.pipeline.RawSong
|
||||
import org.oxycblt.musikr.tag.Disc
|
||||
import org.oxycblt.musikr.tag.Name
|
||||
|
||||
Reference in New Issue
Block a user