musikr.fs: prototype arbitrary saf locations
This commit is contained in:
@@ -247,10 +247,10 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
|
||||
|
||||
private fun String.toOpenedLocations(): List<Location.Opened> =
|
||||
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> =
|
||||
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> {
|
||||
val split = mutableListOf<String>()
|
||||
|
||||
@@ -303,11 +303,10 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
||||
val context = requireContext()
|
||||
val location = Location.Unopened.from(context, uri)
|
||||
|
||||
if (location != null) {
|
||||
pendingLocationCallback?.invoke(location)
|
||||
} else {
|
||||
requireContext().showToast(R.string.err_bad_location)
|
||||
if (location.path.volume == null) {
|
||||
requireContext().showToast(R.string.err_weird_location)
|
||||
}
|
||||
pendingLocationCallback?.invoke(location)
|
||||
pendingLocationCallback = null
|
||||
}
|
||||
|
||||
|
||||
@@ -347,6 +347,7 @@
|
||||
<string name="err_no_app">No app found that can handle this task</string>
|
||||
<!-- No folders in the "Music Folders" setting -->
|
||||
<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>
|
||||
|
||||
<!-- Description Namespace | Accessibility Strings -->
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String>) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 ? " +
|
||||
|
||||
Reference in New Issue
Block a user