musikr.fs: prototype arbitrary saf locations

This commit is contained in:
Alexander Capehart
2026-01-11 21:07:19 -07:00
parent 53b9b464d8
commit c730091637
7 changed files with 29 additions and 29 deletions
@@ -247,10 +247,10 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
private fun String.toOpenedLocations(): List<Location.Opened> = private fun String.toOpenedLocations(): List<Location.Opened> =
splitEscaped { it == ';' } 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<Location.Unopened> = private fun String.toUnopenedLocations(): List<Location.Unopened> =
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<String> { private inline fun String.splitEscaped(selector: (Char) -> Boolean): List<String> {
val split = mutableListOf<String>() val split = mutableListOf<String>()
@@ -303,11 +303,10 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
val context = requireContext() val context = requireContext()
val location = Location.Unopened.from(context, uri) val location = Location.Unopened.from(context, uri)
if (location != null) { if (location.path.volume == null) {
pendingLocationCallback?.invoke(location) requireContext().showToast(R.string.err_weird_location)
} else {
requireContext().showToast(R.string.err_bad_location)
} }
pendingLocationCallback?.invoke(location)
pendingLocationCallback = null pendingLocationCallback = null
} }
+1
View File
@@ -347,6 +347,7 @@
<string name="err_no_app">No app found that can handle this task</string> <string name="err_no_app">No app found that can handle this task</string>
<!-- No folders in the "Music Folders" setting --> <!-- No folders in the "Music Folders" setting -->
<string name="err_bad_location">This folder is not supported</string> <string name="err_bad_location">This folder is not supported</string>
<string name="err_weird_location">Warning: This folder is managed by a 3rd-party</string>
<string name="lbl_no_folders">No folders</string> <string name="lbl_no_folders">No folders</string>
<!-- Description Namespace | Accessibility Strings --> <!-- Description Namespace | Accessibility Strings -->
@@ -60,11 +60,15 @@ sealed class Location(val uri: Uri, val path: Path) {
} }
companion object { 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 if (!DocumentsContract.isTreeUri(uri)) return null
val documentPathFactory = DocumentPathFactory.from(context) val documentPathFactory = DocumentPathFactory.from(context)
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null return documentPathFactory.unpackDocumentTreeUri(uri)
return Unopened(uri, path)
} }
} }
} }
@@ -24,10 +24,11 @@ import java.io.File
/** /**
* An abstraction of an android file system path, including the volume and relative path. * 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. * @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. */ /** The name of the file/directory. */
val name: String? val name: String?
get() = components.name 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. * @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 { sealed interface Volume {
@@ -170,6 +172,8 @@ value class Components private constructor(val components: List<String>) {
fun parseWindows(path: String) = fun parseWindows(path: String) =
Components(path.trimSlashes().split('\\').filter { it.isNotEmpty() }) Components(path.trimSlashes().split('\\').filter { it.isNotEmpty() })
fun root() = Components(emptyList())
private fun String.trimSlashes() = trimStart(File.separatorChar).trimEnd(File.separatorChar) private fun String.trimSlashes() = trimStart(File.separatorChar).trimEnd(File.separatorChar)
} }
} }
@@ -51,14 +51,6 @@ internal interface DocumentPathFactory {
*/ */
fun unpackDocumentTreeUri(uri: Uri): Path? 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. * Deserializes a document tree URI format path into a [Path] instance.
* *
@@ -122,15 +114,6 @@ private class DocumentPathFactoryImpl(
return fromDocumentId(treeUri) 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? { override fun fromDocumentId(path: String): Path? {
// Document tree URIs consist of a prefixed volume name followed by a relative path, // Document tree URIs consist of a prefixed volume name followed by a relative path,
// delimited with a colon. // delimited with a colon.
@@ -136,6 +136,10 @@ private constructor(private val cursor: Cursor, volumeManager: VolumeManager) :
var template = "" var template = ""
for (i in paths.indices) { for (i in paths.indices) {
val path = paths[i] 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 val volume = path.volume.components ?: continue
template += template +=
if (args.isEmpty()) { if (args.isEmpty()) {
@@ -216,6 +220,11 @@ private constructor(private val cursor: Cursor, volumeManager: VolumeManager) :
var template = "" var template = ""
for (i in paths.indices) { for (i in paths.indices) {
val path = paths[i] val path = paths[i]
if (path.volume == null) {
// FIXME: This means we are silently ignoring third-party storage volumes
// in MediaStore.
continue
}
template += template +=
if (args.isEmpty()) { if (args.isEmpty()) {
"(${MediaStore.Audio.AudioColumns.VOLUME_NAME} LIKE ? " + "(${MediaStore.Audio.AudioColumns.VOLUME_NAME} LIKE ? " +