music: share code between music/excluded locations

Co-authored by Claude.
This commit is contained in:
Alexander Capehart
2025-05-31 14:45:13 -06:00
parent 15c1c23b5c
commit 1ad9df3163
18 changed files with 253 additions and 349 deletions
@@ -22,13 +22,13 @@ 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.OpenedLocation
import org.oxycblt.musikr.fs.Location.Opened
import org.oxycblt.musikr.fs.device.FileTreeCache
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<OpenedLocation>, val exclude: List<Location>)
data class Query(val source: List<Location.Opened>, val exclude: List<Location>)
/** Side-effect laden [Storage] for use during music loading and [MutableLibrary] operation. */
data class Storage(
@@ -25,52 +25,52 @@ import android.provider.DocumentsContract
import org.oxycblt.musikr.fs.device.contentResolverSafe
import org.oxycblt.musikr.fs.path.DocumentPathFactory
class Location private constructor(val uri: Uri, val path: Path) {
sealed class Location(val uri: Uri, val path: Path) {
override fun equals(other: Any?) = other is Location && uri == other.uri
override fun hashCode() = 31 * uri.hashCode()
override fun toString(): String = uri.toString()
fun open(context: Context): OpenedLocation? {
if (isUnopened(context, uri)) {
try {
context.contentResolverSafe.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
} catch (e: Exception) {
// Not fully sure what happens if I'm disallowed to take the permission,
// check for both circumstances (error or no-op)
return null
}
class Unopened private constructor(uri: Uri, path: Path) : Location(uri, path) {
fun open(context: Context): Opened? {
if (isUnopened(context, uri)) {
return null
try {
context.contentResolverSafe.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
} catch (e: Exception) {
// Not fully sure what happens if I'm disallowed to take the permission,
// check for both circumstances (error or no-op)
return null
}
if (isUnopened(context, uri)) {
return null
}
}
return Opened(uri, path)
}
private fun isUnopened(context: Context, uri: Uri) =
context.contentResolverSafe.persistedUriPermissions.none {
it.uri == uri && it.isReadPermission && it.isWritePermission
}
companion object {
fun from(context: Context, uri: Uri): Unopened? {
if (!DocumentsContract.isTreeUri(uri)) return null
val documentPathFactory = DocumentPathFactory.from(context)
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
return Unopened(uri, path)
}
}
return OpenedLocation(uri, path)
}
private fun isUnopened(context: Context, uri: Uri) =
context.contentResolverSafe.persistedUriPermissions.none {
it.uri == uri && it.isReadPermission && it.isWritePermission
}
class Opened internal constructor(uri: Uri, path: Path) : Location(uri, path) {
override fun equals(other: Any?) =
other is Opened && uri == other.uri && path == other.path
companion object {
fun from(context: Context, uri: Uri): Location? {
if (!DocumentsContract.isTreeUri(uri)) return null
val documentPathFactory = DocumentPathFactory.from(context)
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
return Location(uri, path)
}
override fun hashCode() = 31 * uri.hashCode() + path.hashCode()
}
}
class OpenedLocation internal constructor(val uri: Uri, val path: Path) {
override fun equals(other: Any?) =
other is OpenedLocation && uri == other.uri && path == other.path
override fun hashCode() = 31 * uri.hashCode() + path.hashCode()
override fun toString(): String = uri.toString()
}
@@ -8,7 +8,7 @@ The filesystem package provides abstraction layers for accessing music files on
### Core Types
**OpenedLocation** (`OpenedLocation.kt`)
**Location.Opened** (`Location.Opened.kt`)
- Represents a location to search for music
- Can be a device storage location or content URI
@@ -50,7 +50,7 @@ The filesystem package provides abstraction layers for accessing music files on
## Usage Flow
1. User provides `OpenedLocation`s to scan
1. User provides `Location.Opened`s to scan
2. `DeviceFS` traverses these locations
3. `FileTreeCache` optimizes subsequent scans
4. Discovered files are passed to the pipeline for processing
@@ -44,7 +44,7 @@ import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.flow.transform
import org.oxycblt.musikr.Query
import org.oxycblt.musikr.fs.Location
import org.oxycblt.musikr.fs.OpenedLocation
import org.oxycblt.musikr.fs.Location.Opened
import org.oxycblt.musikr.fs.Path
internal interface DeviceFS {
@@ -68,7 +68,7 @@ private class DeviceFSImpl(
}
private suspend fun queryRoot(
location: OpenedLocation,
location: Location.Opened,
fileTree: FileTree,
exclude: List<Location>
): DeviceDirectory? {
@@ -49,7 +49,7 @@ The `PipelineItem.kt` defines the data types that flow through the pipeline:
## Pipeline Flow
```
OpenedLocation → ExploreStep → ExtractStep → EvaluateStep → Library
Location.Opened → ExploreStep → ExtractStep → EvaluateStep → Library
```
Data flows through the pipeline using Kotlin's Flow API, with extensive use of parallel processing and buffering to optimize performance.
@@ -22,11 +22,11 @@ import android.content.Context
import android.database.ContentObserver
import android.os.Handler
import android.os.Looper
import org.oxycblt.musikr.fs.OpenedLocation
import org.oxycblt.musikr.fs.Location
internal class LocationObserver(
private val context: Context,
private val location: OpenedLocation,
private val location: Location.Opened,
private val listener: UpdateTracker.Callback
) : ContentObserver(Handler(Looper.getMainLooper())), Runnable {
private val handler = Handler(Looper.getMainLooper())
@@ -19,15 +19,15 @@
package org.oxycblt.musikr.track
import android.content.Context
import org.oxycblt.musikr.fs.OpenedLocation
import org.oxycblt.musikr.fs.Location
interface UpdateTracker {
fun track(locations: List<OpenedLocation>)
fun track(locations: List<Location.Opened>)
fun release()
interface Callback {
fun onUpdate(location: OpenedLocation)
fun onUpdate(location: Location.Opened)
}
companion object {
@@ -42,7 +42,7 @@ private class UpdateTrackerImpl(
) : UpdateTracker {
private val observers = mutableListOf<LocationObserver>()
override fun track(locations: List<OpenedLocation>) {
override fun track(locations: List<Location.Opened>) {
release()
observers.addAll(locations.map { LocationObserver(context, it, callback) })
}