music: revamp paths

Revamp paths with an entirely new abstraction that should improve
testability and integration with M3U playlists.
This commit is contained in:
Alexander Capehart
2023-12-19 22:14:59 -07:00
parent 08ca71b7b0
commit 364675b252
50 changed files with 422 additions and 277 deletions
@@ -102,10 +102,7 @@ class SongDetailDialog : ViewBindingMaterialDialogFragment<DialogSongDetailBindi
}
add(SongProperty(R.string.lbl_disc, zipped))
}
add(SongProperty(R.string.lbl_file_name, song.path.name))
add(
SongProperty(
R.string.lbl_relative_path, song.path.parent.resolveName(context)))
add(SongProperty(R.string.lbl_path, song.path.resolve(context)))
info.resolvedMimeType.resolveName(context)?.let {
add(SongProperty(R.string.lbl_format, it))
}
@@ -24,8 +24,8 @@ import androidx.core.content.edit
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.fs.Directory
import org.oxycblt.auxio.music.fs.MusicDirectories
import org.oxycblt.auxio.music.dirs.DocumentTreePathFactory
import org.oxycblt.auxio.music.dirs.MusicDirectories
import org.oxycblt.auxio.settings.Settings
import org.oxycblt.auxio.util.getSystemServiceCompat
import org.oxycblt.auxio.util.logD
@@ -55,8 +55,12 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
}
}
class MusicSettingsImpl @Inject constructor(@ApplicationContext context: Context) :
Settings.Impl<MusicSettings.Listener>(context), MusicSettings {
class MusicSettingsImpl
@Inject
constructor(
@ApplicationContext context: Context,
val documentTreePathFactory: DocumentTreePathFactory
) : Settings.Impl<MusicSettings.Listener>(context), MusicSettings {
private val storageManager = context.getSystemServiceCompat(StorageManager::class)
override var musicDirs: MusicDirectories
@@ -64,7 +68,7 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext context: Context
val dirs =
(sharedPreferences.getStringSet(getString(R.string.set_key_music_dirs), null)
?: emptySet())
.mapNotNull { Directory.fromDocumentTreeUri(storageManager, it) }
.mapNotNull(documentTreePathFactory::deserializeDocumentTreePath)
return MusicDirectories(
dirs,
sharedPreferences.getBoolean(getString(R.string.set_key_music_dirs_include), false))
@@ -73,7 +77,7 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext context: Context
sharedPreferences.edit {
putStringSet(
getString(R.string.set_key_music_dirs),
value.dirs.map(Directory::toDocumentTreeUri).toSet())
value.dirs.map(documentTreePathFactory::serializeDocumentTreePath).toSet())
putBoolean(getString(R.string.set_key_music_dirs_include), value.shouldInclude)
apply()
}
@@ -28,7 +28,6 @@ import org.oxycblt.auxio.music.Music
import org.oxycblt.auxio.music.MusicType
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.music.fs.MimeType
import org.oxycblt.auxio.music.fs.Path
import org.oxycblt.auxio.music.fs.toAudioUri
import org.oxycblt.auxio.music.fs.toCoverUri
import org.oxycblt.auxio.music.info.Date
@@ -85,14 +84,10 @@ class SongImpl(
requireNotNull(rawSong.mediaStoreId) { "Invalid raw ${rawSong.fileName}: No id" }
.toAudioUri()
override val path =
Path(
name =
requireNotNull(rawSong.directory) { "Invalid raw ${rawSong.fileName}: No parent directory" }
.file(
requireNotNull(rawSong.fileName) {
"Invalid raw ${rawSong.fileName}: No display name"
},
parent =
requireNotNull(rawSong.directory) {
"Invalid raw ${rawSong.fileName}: No parent directory"
})
override val mimeType =
MimeType(
@@ -247,11 +242,11 @@ class SongImpl(
* @return This instance upcasted to [Song].
*/
fun finalize(): Song {
checkNotNull(_album) { "Malformed song ${path.name}: No album" }
checkNotNull(_album) { "Malformed song ${path}: No album" }
check(_artists.isNotEmpty()) { "Malformed song ${path.name}: No artists" }
check(_artists.isNotEmpty()) { "Malformed song ${path}: No artists" }
check(_artists.size == rawArtists.size) {
"Malformed song ${path.name}: Artist grouping mismatch"
"Malformed song ${path}: Artist grouping mismatch"
}
for (i in _artists.indices) {
// Non-destructively reorder the linked artists so that they align with
@@ -262,10 +257,8 @@ class SongImpl(
_artists[i] = other
}
check(_genres.isNotEmpty()) { "Malformed song ${path.name}: No genres" }
check(_genres.size == rawGenres.size) {
"Malformed song ${path.name}: Genre grouping mismatch"
}
check(_genres.isNotEmpty()) { "Malformed song ${path}: No genres" }
check(_genres.size == rawGenres.size) { "Malformed song ${path}: Genre grouping mismatch" }
for (i in _genres.indices) {
// Non-destructively reorder the linked genres so that they align with
// the genre ordering within the song metadata.
@@ -519,6 +512,7 @@ class ArtistImpl(
return this
}
}
/**
* Library-backed implementation of [Genre].
*
@@ -22,7 +22,7 @@ import java.util.UUID
import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.music.Music
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.music.fs.Directory
import org.oxycblt.auxio.music.fs.Path
import org.oxycblt.auxio.music.info.Date
import org.oxycblt.auxio.music.info.ReleaseType
@@ -44,7 +44,7 @@ data class RawSong(
/** @see Song.path */
var fileName: String? = null,
/** @see Song.path */
var directory: Directory? = null,
var directory: Path? = null,
/** @see Song.size */
var size: Long? = null,
/** @see Song.durationMs */
@@ -16,13 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.music.fs
package org.oxycblt.auxio.music.dirs
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.databinding.ItemMusicDirBinding
import org.oxycblt.auxio.list.recycler.DialogRecyclerView
import org.oxycblt.auxio.music.fs.Path
import org.oxycblt.auxio.util.context
import org.oxycblt.auxio.util.inflater
import org.oxycblt.auxio.util.logD
@@ -35,11 +36,11 @@ import org.oxycblt.auxio.util.logD
*/
class DirectoryAdapter(private val listener: Listener) :
RecyclerView.Adapter<MusicDirViewHolder>() {
private val _dirs = mutableListOf<Directory>()
private val _dirs = mutableListOf<Path>()
/**
* The current list of [Directory]s, may not line up with [MusicDirectories] due to removals.
* The current list of [SystemPath]s, may not line up with [MusicDirectories] due to removals.
*/
val dirs: List<Directory> = _dirs
val dirs: List<Path> = _dirs
override fun getItemCount() = dirs.size
@@ -50,37 +51,37 @@ class DirectoryAdapter(private val listener: Listener) :
holder.bind(dirs[position], listener)
/**
* Add a [Directory] to the end of the list.
* Add a [Path] to the end of the list.
*
* @param dir The [Directory] to add.
* @param path The [Path] to add.
*/
fun add(dir: Directory) {
if (_dirs.contains(dir)) return
logD("Adding $dir")
_dirs.add(dir)
fun add(path: Path) {
if (_dirs.contains(path)) return
logD("Adding $path")
_dirs.add(path)
notifyItemInserted(_dirs.lastIndex)
}
/**
* Add a list of [Directory] instances to the end of the list.
* Add a list of [Path] instances to the end of the list.
*
* @param dirs The [Directory] instances to add.
* @param path The [Path] instances to add.
*/
fun addAll(dirs: List<Directory>) {
logD("Adding ${dirs.size} directories")
val oldLastIndex = dirs.lastIndex
_dirs.addAll(dirs)
notifyItemRangeInserted(oldLastIndex, dirs.size)
fun addAll(path: List<Path>) {
logD("Adding ${path.size} directories")
val oldLastIndex = path.lastIndex
_dirs.addAll(path)
notifyItemRangeInserted(oldLastIndex, path.size)
}
/**
* Remove a [Directory] from the list.
* Remove a [Path] from the list.
*
* @param dir The [Directory] to remove. Must exist in the list.
* @param path The [Path] to remove. Must exist in the list.
*/
fun remove(dir: Directory) {
logD("Removing $dir")
val idx = _dirs.indexOf(dir)
fun remove(path: Path) {
logD("Removing $path")
val idx = _dirs.indexOf(path)
_dirs.removeAt(idx)
notifyItemRemoved(idx)
}
@@ -88,7 +89,7 @@ class DirectoryAdapter(private val listener: Listener) :
/** A Listener for [DirectoryAdapter] interactions. */
interface Listener {
/** Called when the delete button on a directory item is clicked. */
fun onRemoveDirectory(dir: Directory)
fun onRemoveDirectory(dir: Path)
}
}
@@ -102,12 +103,12 @@ class MusicDirViewHolder private constructor(private val binding: ItemMusicDirBi
/**
* Bind new data to this instance.
*
* @param dir The new [Directory] to bind.
* @param path The new [Path] to bind.
* @param listener A [DirectoryAdapter.Listener] to bind interactions to.
*/
fun bind(dir: Directory, listener: DirectoryAdapter.Listener) {
binding.dirPath.text = dir.resolveName(binding.context)
binding.dirDelete.setOnClickListener { listener.onRemoveDirectory(dir) }
fun bind(path: Path, listener: DirectoryAdapter.Listener) {
binding.dirPath.text = path.resolve(binding.context)
binding.dirDelete.setOnClickListener { listener.onRemoveDirectory(path) }
}
companion object {
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Auxio Project
* DirectoryModule.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.auxio.music.dirs
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
@Module
@InstallIn(SingletonComponent::class)
interface DirectoryModule {
@Binds
fun documentTreePathFactory(factory: DocumentTreePathFactoryImpl): DocumentTreePathFactory
}
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2023 Auxio Project
* DocumentTreePathFactory.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.auxio.music.dirs
import android.net.Uri
import android.provider.DocumentsContract
import java.io.File
import javax.inject.Inject
import org.oxycblt.auxio.music.fs.Components
import org.oxycblt.auxio.music.fs.Path
import org.oxycblt.auxio.music.fs.Volume
import org.oxycblt.auxio.music.fs.VolumeManager
/**
* A factory for parsing the reverse-engineered format of the URIs obtained from the document tree
* (i.e directory) folder.
*
* @author Alexander Capehart (OxygenCobalt)
*/
interface DocumentTreePathFactory {
/**
* Unpacks a document tree URI into a [Path] instance, using [deserializeDocumentTreePath].
*
* @param uri The document tree URI to unpack.
* @return The [Path] instance, or null if the URI could not be unpacked.
*/
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 serializeDocumentTreePath(path: Path): String
/**
* Deserializes a document tree URI format path into a [Path] instance.
*
* @param path The path to deserialize.
* @return The [Path] instance, or null if the path could not be deserialized.
*/
fun deserializeDocumentTreePath(path: String): Path?
}
class DocumentTreePathFactoryImpl @Inject constructor(private val volumeManager: VolumeManager) :
DocumentTreePathFactory {
override fun unpackDocumentTreeUri(uri: Uri): Path? {
// Convert the document tree URI into it's relative path form, which can then be
// parsed into a Directory instance.
val docUri =
DocumentsContract.buildDocumentUriUsingTree(
uri, DocumentsContract.getTreeDocumentId(uri))
val treeUri = DocumentsContract.getTreeDocumentId(docUri)
return deserializeDocumentTreePath(treeUri)
}
override fun serializeDocumentTreePath(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 deserializeDocumentTreePath(path: String): Path? {
// Document tree URIs consist of a prefixed volume name followed by a relative path,
// delimited with a colon.
val split = path.split(File.pathSeparator, limit = 2)
val volume =
when (split[0]) {
// The primary storage has a volume prefix of "primary", regardless
// of if it's internal or not.
DOCUMENT_URI_PRIMARY_NAME -> volumeManager.getInternalVolume()
// Removable storage has a volume prefix of it's UUID, try to find it
// within StorageManager's volume list.
else ->
volumeManager.getVolumes().find { it is Volume.External && it.id == split[0] }
}
val relativePath = split.getOrNull(1) ?: return null
return Path(volume ?: return null, Components.parse(relativePath))
}
private companion object {
const val DOCUMENT_URI_PRIMARY_NAME = "primary"
}
}
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Auxio Project
* MusicDirectories.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.auxio.music.dirs
import org.oxycblt.auxio.music.fs.Path
/**
* Represents the configuration for specific directories to filter to/from when loading music.
*
* @param dirs A list of [Directory] instances. How these are interpreted depends on [shouldInclude]
* @param shouldInclude True if the library should only load from the [Directory] instances, false
* if the library should not load from the [Directory] instances.
* @author Alexander Capehart (OxygenCobalt)
*/
data class MusicDirectories(val dirs: List<Path>, val shouldInclude: Boolean)
@@ -16,13 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.music.fs
package org.oxycblt.auxio.music.dirs
import android.content.ActivityNotFoundException
import android.net.Uri
import android.os.Bundle
import android.os.storage.StorageManager
import android.provider.DocumentsContract
import android.view.LayoutInflater
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
@@ -35,8 +33,8 @@ import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.DialogMusicDirsBinding
import org.oxycblt.auxio.music.MusicSettings
import org.oxycblt.auxio.music.fs.Path
import org.oxycblt.auxio.ui.ViewBindingMaterialDialogFragment
import org.oxycblt.auxio.util.getSystemServiceCompat
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.showToast
@@ -50,7 +48,7 @@ class MusicDirsDialog :
ViewBindingMaterialDialogFragment<DialogMusicDirsBinding>(), DirectoryAdapter.Listener {
private val dirAdapter = DirectoryAdapter(this)
private var openDocumentTreeLauncher: ActivityResultLauncher<Uri?>? = null
private var storageManager: StorageManager? = null
@Inject lateinit var documentTreePathFactory: DocumentTreePathFactory
@Inject lateinit var musicSettings: MusicSettings
override fun onCreateBinding(inflater: LayoutInflater) =
@@ -70,10 +68,6 @@ class MusicDirsDialog :
}
override fun onBindingCreated(binding: DialogMusicDirsBinding, savedInstanceState: Bundle?) {
val context = requireContext()
val storageManager =
context.getSystemServiceCompat(StorageManager::class).also { storageManager = it }
openDocumentTreeLauncher =
registerForActivityResult(
ActivityResultContracts.OpenDocumentTree(), ::addDocumentTreeUriToDirs)
@@ -107,9 +101,8 @@ class MusicDirsDialog :
if (pendingDirs != null) {
dirs =
MusicDirectories(
pendingDirs.mapNotNull {
Directory.fromDocumentTreeUri(storageManager, it)
},
pendingDirs.mapNotNull(
documentTreePathFactory::deserializeDocumentTreePath),
savedInstanceState.getBoolean(KEY_PENDING_MODE))
}
}
@@ -133,18 +126,18 @@ class MusicDirsDialog :
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putStringArrayList(
KEY_PENDING_DIRS, ArrayList(dirAdapter.dirs.map { it.toString() }))
KEY_PENDING_DIRS,
ArrayList(dirAdapter.dirs.map(documentTreePathFactory::serializeDocumentTreePath)))
outState.putBoolean(KEY_PENDING_MODE, isUiModeInclude(requireBinding()))
}
override fun onDestroyBinding(binding: DialogMusicDirsBinding) {
super.onDestroyBinding(binding)
storageManager = null
openDocumentTreeLauncher = null
binding.dirsRecycler.adapter = null
}
override fun onRemoveDirectory(dir: Directory) {
override fun onRemoveDirectory(dir: Path) {
dirAdapter.remove(dir)
requireBinding().dirsEmpty.isVisible = dirAdapter.dirs.isEmpty()
}
@@ -162,15 +155,7 @@ class MusicDirsDialog :
return
}
// Convert the document tree URI into it's relative path form, which can then be
// parsed into a Directory instance.
val docUri =
DocumentsContract.buildDocumentUriUsingTree(
uri, DocumentsContract.getTreeDocumentId(uri))
val treeUri = DocumentsContract.getTreeDocumentId(docUri)
val dir =
Directory.fromDocumentTreeUri(
requireNotNull(storageManager) { "StorageManager was not available" }, treeUri)
val dir = documentTreePathFactory.unpackDocumentTreeUri(uri)
if (dir != null) {
dirAdapter.add(dir)
@@ -24,119 +24,174 @@ import android.os.storage.StorageManager
import android.os.storage.StorageVolume
import android.webkit.MimeTypeMap
import java.io.File
import javax.inject.Inject
import org.oxycblt.auxio.R
import org.oxycblt.auxio.util.logD
/**
* A full absolute path to a file. Only intended for display purposes. For accessing files, URIs are
* preferred in all cases due to scoped storage limitations.
* An abstraction of an android file system path, including the volume and relative path.
*
* @param name The name of the file.
* @param parent The parent [Directory] of the file.
* @author Alexander Capehart (OxygenCobalt)
* @param volume The volume that the path is on.
* @param components The components of the path of the file, relative to the root of the volume.
*/
data class Path(val name: String, val parent: Directory)
data class Path(
val volume: Volume,
val components: Components,
) {
/** The name of the file/directory. */
val name: String?
get() = components.name
/**
* A volume-aware relative path to a directory.
*
* @param volume The [StorageVolume] that the [Directory] is contained in.
* @param relativePath The relative path from within the [StorageVolume] to the [Directory].
* @author Alexander Capehart (OxygenCobalt)
*/
class Directory private constructor(val volume: StorageVolume, val relativePath: String) {
/**
* Resolve the [Directory] instance into a human-readable path name.
*
* @param context [Context] required to obtain volume descriptions.
* @return A human-readable path.
* @see StorageVolume.getDescription
*/
fun resolveName(context: Context) =
context.getString(R.string.fmt_path, volume.getDescriptionCompat(context), relativePath)
/** The parent directory of the path, or itself if it's the root path. */
val directory: Path
get() = Path(volume, components.parent())
override fun toString() = "Path(storageVolume=$volume, components=$components)"
/**
* Converts this [Directory] instance into an opaque document tree path. This is a huge
* violation of the document tree URI contract, but it's also the only one can sensibly work
* with these uris in the UI, and it doesn't exactly matter since we never write or read to
* directory.
* Transforms this [Path] into a "file" of the given name that's within the "directory"
* represented by the current path. Ex. "/storage/emulated/0/Music" ->
* "/storage/emulated/0/Music/file.mp3"
*
* @return A URI [String] abiding by the document tree specification, or null if the [Directory]
* is not valid.
* @param fileName The name of the file to append to the path.
* @return The new [Path] instance.
*/
fun toDocumentTreeUri() =
// Document tree URIs consist of a prefixed volume name followed by a relative path.
if (volume.isInternalCompat) {
// The primary storage has a volume prefix of "primary", regardless
// of if it's internal or not.
"$DOCUMENT_URI_PRIMARY_NAME:$relativePath"
} else {
// Removable storage has a volume prefix of it's UUID.
volume.uuidCompat?.let { uuid -> "$uuid:$relativePath" }
}
fun file(fileName: String) = Path(volume, components.child(fileName))
override fun hashCode(): Int {
var result = volume.hashCode()
result = 31 * result + relativePath.hashCode()
return result
}
/**
* Resolves the [Path] in a human-readable format.
*
* @param context [Context] required to obtain human-readable strings.
*/
fun resolve(context: Context) = "${volume.resolveName(context)}/$components"
}
override fun equals(other: Any?) =
other is Directory && other.volume == volume && other.relativePath == relativePath
sealed interface Volume {
/** The name of the volume as it appears in MediaStore. */
val mediaStoreName: String?
companion object {
/** The name given to the internal volume when in a document tree URI. */
private const val DOCUMENT_URI_PRIMARY_NAME = "primary"
/**
* The components of the path to the volume, relative from the system root. Should not be used
* except for compatibility purposes.
*/
val components: Components?
/**
* Create a new directory instance from the given components.
*
* @param volume The [StorageVolume] that the [Directory] is contained in.
* @param relativePath The relative path from within the [StorageVolume] to the [Directory].
* Will be stripped of any trailing separators for a consistent internal representation.
* @return A new [Directory] created from the components.
*/
fun from(volume: StorageVolume, relativePath: String) =
Directory(
volume, relativePath.removePrefix(File.separator).removeSuffix(File.separator))
/** Resolves the name of the volume in a human-readable format. */
fun resolveName(context: Context): String
/**
* Create a new directory from a document tree URI. This is a huge violation of the document
* tree URI contract, but it's also the only one can sensibly work with these uris in the
* UI, and it doesn't exactly matter since we never write or read directory.
*
* @param storageManager [StorageManager] in order to obtain the [StorageVolume] specified
* in the given URI.
* @param uri The URI string to parse into a [Directory].
* @return A new [Directory] parsed from the URI, or null if the URI is not valid.
*/
fun fromDocumentTreeUri(storageManager: StorageManager, uri: String): Directory? {
// Document tree URIs consist of a prefixed volume name followed by a relative path,
// delimited with a colon.
val split = uri.split(File.pathSeparator, limit = 2)
val volume =
when (split[0]) {
// The primary storage has a volume prefix of "primary", regardless
// of if it's internal or not.
DOCUMENT_URI_PRIMARY_NAME -> storageManager.primaryStorageVolumeCompat
// Removable storage has a volume prefix of it's UUID, try to find it
// within StorageManager's volume list.
else -> storageManager.storageVolumesCompat.find { it.uuidCompat == split[0] }
}
val relativePath = split.getOrNull(1)
return from(volume ?: return null, relativePath ?: return null)
}
/** A volume representing the device's internal storage. */
interface Internal : Volume
/** A volume representing an external storage device, identified by a UUID. */
interface External : Volume {
/** The UUID of the volume. */
val id: String?
}
}
/**
* Represents the configuration for specific directories to filter to/from when loading music.
* The components of a path. This allows the path to be manipulated without having tp handle
* separator parsing.
*
* @param dirs A list of [Directory] instances. How these are interpreted depends on [shouldInclude]
* @param shouldInclude True if the library should only load from the [Directory] instances, false
* if the library should not load from the [Directory] instances.
* @author Alexander Capehart (OxygenCobalt)
* @param components The components of the path.
*/
data class MusicDirectories(val dirs: List<Directory>, val shouldInclude: Boolean)
@JvmInline
value class Components private constructor(val components: List<String>) {
/** The name of the file/directory. */
val name: String?
get() = components.lastOrNull()
override fun toString() = components.joinToString(File.separator)
/**
* Returns a new [Components] instance with the last element of the path removed as a "parent"
* element of the original instance.
*
* @return The new [Components] instance, or the original instance if it's the root path.
*/
fun parent() = Components(components.dropLast(1))
/**
* Returns a new [Components] instance with the given name appended to the end of the path as a
* "child" element of the original instance.
*
* @param name The name of the file/directory to append to the path.
*/
fun child(name: String) =
if (name.isNotEmpty()) {
Components(components + name.trimSlashes()).also { logD(it.components) }
} else {
this
}
companion object {
/**
* Parses a path string into a [Components] instance by the system path separator.
*
* @param path The path string to parse.
* @return The [Components] instance.
*/
fun parse(path: String) =
Components(path.trimSlashes().split(File.separatorChar).filter { it.isNotEmpty() })
private fun String.trimSlashes() = trimStart(File.separatorChar).trimEnd(File.separatorChar)
}
}
/** A wrapper around [StorageManager] that provides instances of the [Volume] interface. */
interface VolumeManager {
/**
* The internal storage volume of the device.
*
* @see StorageManager.getPrimaryStorageVolume
*/
fun getInternalVolume(): Volume.Internal
/**
* The list of [Volume]s currently recognized by [StorageManager].
*
* @see StorageManager.getStorageVolumes
*/
fun getVolumes(): List<Volume>
}
class VolumeManagerImpl @Inject constructor(private val storageManager: StorageManager) :
VolumeManager {
override fun getInternalVolume(): Volume.Internal =
InternalVolumeImpl(storageManager.primaryStorageVolume)
override fun getVolumes() =
storageManager.storageVolumesCompat.map {
if (it.isInternalCompat) {
InternalVolumeImpl(it)
} else {
ExternalVolumeImpl(it)
}
}
private class InternalVolumeImpl(val storageVolume: StorageVolume) : Volume.Internal {
override val mediaStoreName
get() = storageVolume.mediaStoreVolumeNameCompat
override val components
get() = storageVolume.directoryCompat?.let(Components::parse)
override fun resolveName(context: Context) = storageVolume.getDescriptionCompat(context)
}
private class ExternalVolumeImpl(val storageVolume: StorageVolume) : Volume.External {
override val id
get() = storageVolume.uuidCompat
override val mediaStoreName
get() = storageVolume.mediaStoreVolumeNameCompat
override val components
get() = storageVolume.directoryCompat?.let(Components::parse)
override fun resolveName(context: Context) = storageVolume.getDescriptionCompat(context)
}
}
/**
* A mime type of a file. Only intended for display.
@@ -19,16 +19,22 @@
package org.oxycblt.auxio.music.fs
import android.content.Context
import android.os.storage.StorageManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import org.oxycblt.auxio.util.getSystemServiceCompat
@Module
@InstallIn(SingletonComponent::class)
class FsModule {
@Provides
fun mediaStoreExtractor(@ApplicationContext context: Context) =
MediaStoreExtractor.from(context)
fun volumeManager(@ApplicationContext context: Context): VolumeManager =
VolumeManagerImpl(context.getSystemServiceCompat(StorageManager::class))
@Provides
fun mediaStoreExtractor(@ApplicationContext context: Context, volumeManager: VolumeManager) =
MediaStoreExtractor.from(context, volumeManager)
}
@@ -21,7 +21,6 @@ package org.oxycblt.auxio.music.fs
import android.content.Context
import android.database.Cursor
import android.os.Build
import android.os.storage.StorageManager
import android.provider.MediaStore
import androidx.annotation.RequiresApi
import androidx.core.database.getIntOrNull
@@ -31,10 +30,10 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.yield
import org.oxycblt.auxio.music.cache.Cache
import org.oxycblt.auxio.music.device.RawSong
import org.oxycblt.auxio.music.dirs.MusicDirectories
import org.oxycblt.auxio.music.info.Date
import org.oxycblt.auxio.music.metadata.parseId3v2PositionField
import org.oxycblt.auxio.music.metadata.transformPositionField
import org.oxycblt.auxio.util.getSystemServiceCompat
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.sendWithTimeout
@@ -93,13 +92,16 @@ interface MediaStoreExtractor {
* Create a framework-backed instance.
*
* @param context [Context] required.
* @param volumeManager [VolumeManager] required.
* @return A new [MediaStoreExtractor] that will work best on the device's API level.
*/
fun from(context: Context): MediaStoreExtractor =
fun from(context: Context, volumeManager: VolumeManager): MediaStoreExtractor =
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> Api30MediaStoreExtractor(context)
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> Api29MediaStoreExtractor(context)
else -> Api21MediaStoreExtractor(context)
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ->
Api30MediaStoreExtractor(context, volumeManager)
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ->
Api29MediaStoreExtractor(context, volumeManager)
else -> Api21MediaStoreExtractor(context, volumeManager)
}
}
}
@@ -249,15 +251,15 @@ private abstract class BaseMediaStoreExtractor(protected val context: Context) :
protected abstract val dirSelectorTemplate: String
/**
* Add a [Directory] to the given list of projection selector arguments.
* Add a [SystemPath] to the given list of projection selector arguments.
*
* @param dir The [Directory] to add.
* @param path The [SystemPath] to add.
* @param args The destination list to append selector arguments to that are analogous to the
* given [Directory].
* @return true if the [Directory] was added, false otherwise.
* given [SystemPath].
* @return true if the [SystemPath] was added, false otherwise.
* @see dirSelectorTemplate
*/
protected abstract fun addDirToSelector(dir: Directory, args: MutableList<String>): Boolean
protected abstract fun addDirToSelector(path: Path, args: MutableList<String>): Boolean
protected abstract fun wrapQuery(
cursor: Cursor,
@@ -362,7 +364,8 @@ private abstract class BaseMediaStoreExtractor(protected val context: Context) :
// Note: The separation between version-specific backends may not be the cleanest. To preserve
// speed, we only want to add redundancy on known issues, not with possible issues.
private class Api21MediaStoreExtractor(context: Context) : BaseMediaStoreExtractor(context) {
private class Api21MediaStoreExtractor(context: Context, private val volumeManager: VolumeManager) :
BaseMediaStoreExtractor(context) {
override val projection: Array<String>
get() =
super.projection +
@@ -378,28 +381,27 @@ private class Api21MediaStoreExtractor(context: Context) : BaseMediaStoreExtract
override val dirSelectorTemplate: String
get() = "${MediaStore.Audio.Media.DATA} LIKE ?"
override fun addDirToSelector(dir: Directory, args: MutableList<String>): Boolean {
override fun addDirToSelector(path: Path, args: MutableList<String>): Boolean {
// "%" signifies to accept any DATA value that begins with the Directory's path,
// thus recursively filtering all files in the directory.
args.add("${dir.volume.directoryCompat ?: return false}/${dir.relativePath}%")
args.add("${path.volume.components ?: return false}${path.components}%")
return true
}
override fun wrapQuery(
cursor: Cursor,
genreNamesMap: Map<Long, String>,
): MediaStoreExtractor.Query =
Query(cursor, genreNamesMap, context.getSystemServiceCompat(StorageManager::class))
): MediaStoreExtractor.Query = Query(cursor, genreNamesMap, volumeManager)
private class Query(
cursor: Cursor,
genreNamesMap: Map<Long, String>,
storageManager: StorageManager
volumeManager: VolumeManager
) : BaseMediaStoreExtractor.Query(cursor, genreNamesMap) {
// Set up cursor indices for later use.
private val trackIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.TRACK)
private val dataIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DATA)
private val volumes = storageManager.storageVolumesCompat
private val volumes = volumeManager.getVolumes()
override fun populateFileInfo(rawSong: RawSong) {
super.populateFileInfo(rawSong)
@@ -417,10 +419,10 @@ private class Api21MediaStoreExtractor(context: Context) : BaseMediaStoreExtract
// the Directory we will use.
val rawPath = data.substringBeforeLast(File.separatorChar)
for (volume in volumes) {
val volumePath = volume.directoryCompat ?: continue
val volumePath = (volume.components ?: continue).toString()
val strippedPath = rawPath.removePrefix(volumePath)
if (strippedPath != rawPath) {
rawSong.directory = Directory.from(volume, strippedPath)
rawSong.directory = Path(volume, Components.parse(strippedPath))
break
}
}
@@ -466,26 +468,26 @@ private abstract class BaseApi29MediaStoreExtractor(context: Context) :
"(${MediaStore.Audio.AudioColumns.VOLUME_NAME} LIKE ? " +
"AND ${MediaStore.Audio.AudioColumns.RELATIVE_PATH} LIKE ?)"
override fun addDirToSelector(dir: Directory, args: MutableList<String>): Boolean {
override fun addDirToSelector(path: Path, args: MutableList<String>): Boolean {
// MediaStore uses a different naming scheme for it's volume column convert this
// directory's volume to it.
args.add(dir.volume.mediaStoreVolumeNameCompat ?: return false)
args.add(path.volume.mediaStoreName ?: return false)
// "%" signifies to accept any DATA value that begins with the Directory's path,
// thus recursively filtering all files in the directory.
args.add("${dir.relativePath}%")
args.add("${path.components}%")
return true
}
abstract class Query(
cursor: Cursor,
genreNamesMap: Map<Long, String>,
storageManager: StorageManager
private val volumeManager: VolumeManager
) : BaseMediaStoreExtractor.Query(cursor, genreNamesMap) {
private val volumeIndex =
cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.VOLUME_NAME)
private val relativePathIndex =
cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.RELATIVE_PATH)
private val volumes = storageManager.storageVolumesCompat
private val volumes = volumeManager.getVolumes()
final override fun populateFileInfo(rawSong: RawSong) {
super.populateFileInfo(rawSong)
@@ -493,9 +495,9 @@ private abstract class BaseApi29MediaStoreExtractor(context: Context) :
// This is combined with the plain relative path column to create the directory.
val volumeName = cursor.getString(volumeIndex)
val relativePath = cursor.getString(relativePathIndex)
val volume = volumes.find { it.mediaStoreVolumeNameCompat == volumeName }
val volume = volumes.find { it.mediaStoreName == volumeName }
if (volume != null) {
rawSong.directory = Directory.from(volume, relativePath)
rawSong.directory = Path(volume, Components.parse(relativePath))
}
}
}
@@ -509,7 +511,8 @@ private abstract class BaseApi29MediaStoreExtractor(context: Context) :
* @author Alexander Capehart (OxygenCobalt)
*/
@RequiresApi(Build.VERSION_CODES.Q)
private class Api29MediaStoreExtractor(context: Context) : BaseApi29MediaStoreExtractor(context) {
private class Api29MediaStoreExtractor(context: Context, private val volumeManager: VolumeManager) :
BaseApi29MediaStoreExtractor(context) {
override val projection: Array<String>
get() = super.projection + arrayOf(MediaStore.Audio.AudioColumns.TRACK)
@@ -517,14 +520,13 @@ private class Api29MediaStoreExtractor(context: Context) : BaseApi29MediaStoreEx
override fun wrapQuery(
cursor: Cursor,
genreNamesMap: Map<Long, String>
): MediaStoreExtractor.Query =
Query(cursor, genreNamesMap, context.getSystemServiceCompat(StorageManager::class))
): MediaStoreExtractor.Query = Query(cursor, genreNamesMap, volumeManager)
private class Query(
cursor: Cursor,
genreNamesMap: Map<Long, String>,
storageManager: StorageManager
) : BaseApi29MediaStoreExtractor.Query(cursor, genreNamesMap, storageManager) {
volumeManager: VolumeManager
) : BaseApi29MediaStoreExtractor.Query(cursor, genreNamesMap, volumeManager) {
private val trackIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.TRACK)
override fun populateTags(rawSong: RawSong) {
@@ -549,7 +551,8 @@ private class Api29MediaStoreExtractor(context: Context) : BaseApi29MediaStoreEx
* @author Alexander Capehart (OxygenCobalt)
*/
@RequiresApi(Build.VERSION_CODES.R)
private class Api30MediaStoreExtractor(context: Context) : BaseApi29MediaStoreExtractor(context) {
private class Api30MediaStoreExtractor(context: Context, private val volumeManager: VolumeManager) :
BaseApi29MediaStoreExtractor(context) {
override val projection: Array<String>
get() =
super.projection +
@@ -562,14 +565,13 @@ private class Api30MediaStoreExtractor(context: Context) : BaseApi29MediaStoreEx
override fun wrapQuery(
cursor: Cursor,
genreNamesMap: Map<Long, String>
): MediaStoreExtractor.Query =
Query(cursor, genreNamesMap, context.getSystemServiceCompat(StorageManager::class))
): MediaStoreExtractor.Query = Query(cursor, genreNamesMap, volumeManager)
private class Query(
cursor: Cursor,
genreNamesMap: Map<Long, String>,
storageManager: StorageManager
) : BaseApi29MediaStoreExtractor.Query(cursor, genreNamesMap, storageManager) {
volumeManager: VolumeManager
) : BaseApi29MediaStoreExtractor.Query(cursor, genreNamesMap, volumeManager) {
private val trackIndex =
cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.CD_TRACK_NUMBER)
private val discIndex =
@@ -71,7 +71,7 @@ class SearchEngineImpl @Inject constructor(@ApplicationContext private val conte
return SearchEngine.Items(
songs =
items.songs?.searchListImpl(query) { q, song ->
song.path.name.contains(q, ignoreCase = true)
song.path.name?.contains(q, ignoreCase = true) == true
},
albums = items.albums?.searchListImpl(query),
artists = items.artists?.searchListImpl(query),