From c730091637a4afe77b0651f7215faa66a7f54b75 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Sun, 11 Jan 2026 21:07:19 -0700 Subject: [PATCH] musikr.fs: prototype arbitrary saf locations --- .../org/oxycblt/auxio/music/MusicSettings.kt | 4 ++-- .../auxio/music/locations/LocationsDialog.kt | 7 +++---- app/src/main/res/values/strings.xml | 1 + .../main/java/org/oxycblt/musikr/fs/Location.kt | 10 +++++++--- .../src/main/java/org/oxycblt/musikr/fs/Path.kt | 10 +++++++--- .../musikr/fs/path/DocumentPathFactory.kt | 17 ----------------- .../musikr/fs/path/MediaStorePathInterpreter.kt | 9 +++++++++ 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt index 372630dcb..c5fd7f21a 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt @@ -247,10 +247,10 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont private fun String.toOpenedLocations(): List = splitEscaped { it == ';' } - .mapNotNull { Location.Unopened.from(context, it.toUri())?.open(context) } + .mapNotNull { Location.Unopened.from(context, it.toUri()).open(context) } private fun String.toUnopenedLocations(): List = - splitEscaped { it == ';' }.mapNotNull { Location.Unopened.from(context, it.toUri()) } + splitEscaped { it == ';' }.map { Location.Unopened.from(context, it.toUri()) } private inline fun String.splitEscaped(selector: (Char) -> Boolean): List { val split = mutableListOf() diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt index 775c300c5..2161d7088 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt @@ -303,11 +303,10 @@ class LocationsDialog : ViewBindingMaterialDialogFragmentNo app found that can handle this task This folder is not supported + Warning: This folder is managed by a 3rd-party No folders diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/Location.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/Location.kt index 083290436..b37cd51d9 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/Location.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/Location.kt @@ -60,11 +60,15 @@ sealed class Location(val uri: Uri, val path: Path) { } companion object { - fun from(context: Context, uri: Uri): Unopened? { + fun from(context: Context, uri: Uri): Unopened { + val path = getDocumentTreePath(context, uri) ?: Path(null, Components.root()) + return Unopened(uri, path) + } + + private fun getDocumentTreePath(context: Context, uri: Uri): Path? { if (!DocumentsContract.isTreeUri(uri)) return null val documentPathFactory = DocumentPathFactory.from(context) - val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null - return Unopened(uri, path) + return documentPathFactory.unpackDocumentTreeUri(uri) } } } diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/Path.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/Path.kt index c5cba4ca5..9568ba647 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/Path.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/Path.kt @@ -24,10 +24,11 @@ import java.io.File /** * An abstraction of an android file system path, including the volume and relative path. * - * @param volume The volume that the path is on. + * @param volume The volume that the path is on. Might be null if the Path is not associated with a + * standard SAF document URI. * @param components The components of the path of the file, relative to the root of the volume. */ -data class Path(val volume: Volume, val components: Components) { +data class Path(val volume: Volume?, val components: Components) { /** The name of the file/directory. */ val name: String? get() = components.name @@ -51,7 +52,8 @@ data class Path(val volume: Volume, val components: Components) { * * @param context [Context] required to obtain human-readable strings. */ - fun resolve(context: Context) = "${volume.resolveName(context)}/$components" + fun resolve(context: Context) = + volume?.let { "${volume.resolveName(context)}/$components" } ?: components.toString() } sealed interface Volume { @@ -170,6 +172,8 @@ value class Components private constructor(val components: List) { fun parseWindows(path: String) = Components(path.trimSlashes().split('\\').filter { it.isNotEmpty() }) + fun root() = Components(emptyList()) + private fun String.trimSlashes() = trimStart(File.separatorChar).trimEnd(File.separatorChar) } } diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/path/DocumentPathFactory.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/path/DocumentPathFactory.kt index aeeb5bb81..1c04ba7c3 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/path/DocumentPathFactory.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/path/DocumentPathFactory.kt @@ -51,14 +51,6 @@ internal interface DocumentPathFactory { */ fun unpackDocumentTreeUri(uri: Uri): Path? - /** - * Serializes a [Path] instance into a document tree URI format path. - * - * @param path The [Path] instance to serialize. - * @return The serialized path. - */ - fun toDocumentId(path: Path): String - /** * Deserializes a document tree URI format path into a [Path] instance. * @@ -122,15 +114,6 @@ private class DocumentPathFactoryImpl( return fromDocumentId(treeUri) } - override fun toDocumentId(path: Path): String = - when (val volume = path.volume) { - // The primary storage has a volume prefix of "primary", regardless - // of if it's internal or not. - is Volume.Internal -> "$DOCUMENT_URI_PRIMARY_NAME:${path.components}" - // Document tree URIs consist of a prefixed volume name followed by a relative path. - is Volume.External -> "${volume.id}:${path.components}" - } - override fun fromDocumentId(path: String): Path? { // Document tree URIs consist of a prefixed volume name followed by a relative path, // delimited with a colon. diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/path/MediaStorePathInterpreter.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/path/MediaStorePathInterpreter.kt index 6ec3cd42d..ebe145da5 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/path/MediaStorePathInterpreter.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/path/MediaStorePathInterpreter.kt @@ -136,6 +136,10 @@ private constructor(private val cursor: Cursor, volumeManager: VolumeManager) : var template = "" for (i in paths.indices) { val path = paths[i] + if (path.volume == null) { + // FIXME: This means we are silently ignoring third-party storage volumes + continue + } val volume = path.volume.components ?: continue template += if (args.isEmpty()) { @@ -216,6 +220,11 @@ private constructor(private val cursor: Cursor, volumeManager: VolumeManager) : var template = "" for (i in paths.indices) { val path = paths[i] + if (path.volume == null) { + // FIXME: This means we are silently ignoring third-party storage volumes + // in MediaStore. + continue + } template += if (args.isEmpty()) { "(${MediaStore.Audio.AudioColumns.VOLUME_NAME} LIKE ? " +