musikr: add subfolder exclusion prototype

This commit is contained in:
Alexander Capehart
2025-05-29 20:55:52 -06:00
parent 7f672d63da
commit f2b1ba71f2
3 changed files with 73 additions and 21 deletions
@@ -58,6 +58,26 @@ interface Musikr {
onProgress: suspend (IndexingProgress) -> Unit = {} onProgress: suspend (IndexingProgress) -> Unit = {}
): LibraryResult ): LibraryResult
/**
* Update a music location with a set of subdirectories to exclude from scanning.
*
* @param context The context to use for loading resources.
* @param uri The URI of the music location to update.
* @param excludedSubdirs The set of subdirectory names to exclude when scanning this location.
* @return The updated [MusicLocation] or null if the location doesn't exist.
*/
fun updateLocationExclusions(context: Context, uri: android.net.Uri, excludedSubdirs: Set<String>): MusicLocation?
/**
* Create a new music location with a set of subdirectories to exclude from scanning.
*
* @param context The context to use for loading resources.
* @param uri The URI of the music location to create.
* @param excludedSubdirs The set of subdirectory names to exclude when scanning this location.
* @return The new [MusicLocation] or null if the location couldn't be created.
*/
fun newLocationWithExclusions(context: Context, uri: android.net.Uri, excludedSubdirs: Set<String>): MusicLocation?
companion object { companion object {
/** /**
* Create a new instance from the given configuration. * Create a new instance from the given configuration.
@@ -116,6 +136,14 @@ private class MusikrImpl(
private val extractStep: ExtractStep, private val extractStep: ExtractStep,
private val evaluateStep: EvaluateStep private val evaluateStep: EvaluateStep
) : Musikr { ) : Musikr {
override fun updateLocationExclusions(context: Context, uri: android.net.Uri, excludedSubdirs: Set<String>): MusicLocation? {
return MusicLocation.existing(context, uri, excludedSubdirs)
}
override fun newLocationWithExclusions(context: Context, uri: android.net.Uri, excludedSubdirs: Set<String>): MusicLocation? {
return MusicLocation.new(context, uri, excludedSubdirs)
}
override suspend fun run( override suspend fun run(
locations: List<MusicLocation>, locations: List<MusicLocation>,
onProgress: suspend (IndexingProgress) -> Unit onProgress: suspend (IndexingProgress) -> Unit
@@ -26,7 +26,7 @@ import org.oxycblt.musikr.fs.device.contentResolverSafe
import org.oxycblt.musikr.fs.path.DocumentPathFactory import org.oxycblt.musikr.fs.path.DocumentPathFactory
import org.oxycblt.musikr.util.splitEscaped import org.oxycblt.musikr.util.splitEscaped
class MusicLocation private constructor(val uri: Uri, val path: Path) { class MusicLocation private constructor(val uri: Uri, val path: Path, val excludedSubdirs: Set<String> = emptySet()) {
override fun equals(other: Any?) = other is MusicLocation && uri == other.uri override fun equals(other: Any?) = other is MusicLocation && uri == other.uri
override fun hashCode() = 31 * uri.hashCode() override fun hashCode() = 31 * uri.hashCode()
@@ -34,7 +34,7 @@ class MusicLocation private constructor(val uri: Uri, val path: Path) {
override fun toString(): String = uri.toString() override fun toString(): String = uri.toString()
companion object { companion object {
fun new(context: Context, uri: Uri): MusicLocation? { fun new(context: Context, uri: Uri, excludedSubdirs: Set<String> = emptySet()): MusicLocation? {
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 val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
@@ -47,10 +47,10 @@ class MusicLocation private constructor(val uri: Uri, val path: Path) {
uri, uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION) Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
} }
return MusicLocation(uri, path) return MusicLocation(uri, path, excludedSubdirs)
} }
fun existing(context: Context, uri: Uri): MusicLocation? { fun existing(context: Context, uri: Uri, excludedSubdirs: Set<String> = emptySet()): MusicLocation? {
val documentPathFactory = DocumentPathFactory.from(context) val documentPathFactory = DocumentPathFactory.from(context)
if (!DocumentsContract.isTreeUri(uri)) return null if (!DocumentsContract.isTreeUri(uri)) return null
val notPersisted = val notPersisted =
@@ -59,14 +59,28 @@ class MusicLocation private constructor(val uri: Uri, val path: Path) {
} }
if (notPersisted) return null if (notPersisted) return null
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
return MusicLocation(uri, path) return MusicLocation(uri, path, excludedSubdirs)
} }
fun toString(list: List<MusicLocation>) = fun toString(list: List<MusicLocation>): String {
list.joinToString(";") { it.uri.toString().replace(";", "\\;") } return list.joinToString(";") { location ->
val uriStr = location.uri.toString().replace(";", "\\;")
val excludedSubdirsStr = location.excludedSubdirs.joinToString(",") { it.replace(",", "\\,").replace(";", "\\;") }
if (excludedSubdirsStr.isEmpty()) uriStr else "$uriStr|$excludedSubdirsStr"
}
}
fun existing(context: Context, string: String): List<MusicLocation> { fun existing(context: Context, string: String): List<MusicLocation> {
return string.splitEscaped { it == ';' }.mapNotNull { existing(context, Uri.parse(it)) } return string.splitEscaped { it == ';' }.mapNotNull { entry ->
val parts = entry.split("|", limit = 2)
val uriStr = parts[0]
val excludedSubdirs = if (parts.size > 1) {
parts[1].splitEscaped { it == ',' }.toSet()
} else {
emptySet()
}
existing(context, Uri.parse(uriStr), excludedSubdirs)
}
} }
} }
} }
@@ -22,7 +22,6 @@ import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.provider.DocumentsContract import android.provider.DocumentsContract
import android.util.Log
import androidx.core.net.toUri import androidx.core.net.toUri
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -40,6 +39,7 @@ import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.merge import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.flow.transform
import org.oxycblt.musikr.fs.MusicLocation import org.oxycblt.musikr.fs.MusicLocation
import org.oxycblt.musikr.fs.Path import org.oxycblt.musikr.fs.Path
@@ -76,7 +76,7 @@ private class DeviceFSImpl(
if (modifiedMs == null) { if (modifiedMs == null) {
return null return null
} }
return query(location.uri, treeDocumentId, location.path, modifiedMs, null, fileTree) return query(location.uri, treeDocumentId, location.path, modifiedMs, null, fileTree, location.excludedSubdirs)
} }
private suspend fun query( private suspend fun query(
@@ -85,13 +85,14 @@ private class DeviceFSImpl(
path: Path, path: Path,
modifiedMs: Long, modifiedMs: Long,
parent: DeviceDirectory?, parent: DeviceDirectory?,
fileTree: FileTree fileTree: FileTree,
excludedSubdirs: Set<String> = emptySet()
): DeviceDirectory = coroutineScope { ): DeviceDirectory = coroutineScope {
val uri = DocumentsContract.buildDocumentUriUsingTree(rootUri, treeDocumentId) val uri = DocumentsContract.buildDocumentUriUsingTree(rootUri, treeDocumentId)
val cached = fileTree.queryDirectory(uri) val cached = fileTree.queryDirectory(uri)
if (cached != null && cached.modifiedMs == modifiedMs) { if (cached != null && cached.modifiedMs == modifiedMs) {
return@coroutineScope hydrateCached( return@coroutineScope hydrateCached(
cached = cached, parentDir = parent, path = path, fileTree = fileTree) cached = cached, parentDir = parent, path = path, fileTree = fileTree, excludedSubdirs = excludedSubdirs)
} }
val dir = val dir =
DeviceDirectoryImpl( DeviceDirectoryImpl(
@@ -102,7 +103,6 @@ private class DeviceFSImpl(
children = emptyFlow()) children = emptyFlow())
dir.children = dir.children =
flow { flow {
Log.d("DeviceFS", "Querying $uri")
contentResolver.useQuery( contentResolver.useQuery(
DocumentsContract.buildChildDocumentsUriUsingTree( DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri, rootUri,
@@ -129,8 +129,6 @@ private class DeviceFSImpl(
val childSubdirUris = mutableListOf<String>() val childSubdirUris = mutableListOf<String>()
val childFileUris = mutableListOf<String>() val childFileUris = mutableListOf<String>()
val subqueries = mutableListOf<DeviceDirectory>()
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
val childId = cursor.getString(childUriIndex) val childId = cursor.getString(childUriIndex)
val displayName = cursor.getString(displayNameIndex) val displayName = cursor.getString(displayNameIndex)
@@ -145,6 +143,11 @@ private class DeviceFSImpl(
val lastModified = cursor.getLong(lastModifiedIndex) val lastModified = cursor.getLong(lastModifiedIndex)
if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) { if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) {
// Skip this directory if it's in the excluded list
if (displayName in excludedSubdirs) {
continue
}
val subdir = val subdir =
query( query(
rootUri = rootUri, rootUri = rootUri,
@@ -152,11 +155,11 @@ private class DeviceFSImpl(
path = newPath, path = newPath,
modifiedMs = modifiedMs, modifiedMs = modifiedMs,
parent = dir, parent = dir,
fileTree = fileTree) fileTree = fileTree,
excludedSubdirs = excludedSubdirs)
childSubdirUris.add(subdir.uri.toString()) childSubdirUris.add(subdir.uri.toString())
emit(StreamedFile.More(subdir)) emit(StreamedFile.More(subdir))
} else { } else {
Log.d("DeviceFS", "Querying file $childId")
val size = cursor.getLong(sizeIndex) val size = cursor.getLong(sizeIndex)
val childUri = val childUri =
DocumentsContract.buildDocumentUriUsingTree( DocumentsContract.buildDocumentUriUsingTree(
@@ -202,7 +205,8 @@ private class DeviceFSImpl(
cached: CachedDirectory, cached: CachedDirectory,
parentDir: DeviceDirectory?, parentDir: DeviceDirectory?,
path: Path, path: Path,
fileTree: FileTree fileTree: FileTree,
excludedSubdirs: Set<String> = emptySet()
): DeviceDirectory = coroutineScope { ): DeviceDirectory = coroutineScope {
val dir = val dir =
DeviceDirectoryImpl( DeviceDirectoryImpl(
@@ -215,17 +219,23 @@ private class DeviceFSImpl(
flow { flow {
emitAll( emitAll(
merge( merge(
cached.subdirUris.asFlow().map { subdirUriString -> cached.subdirUris.asFlow().transform { subdirUriString ->
val subdirUri = subdirUriString.toUri() val subdirUri = subdirUriString.toUri()
val cachedSubdir = val cachedSubdir =
requireNotNull(fileTree.queryDirectory(subdirUri)) { requireNotNull(fileTree.queryDirectory(subdirUri)) {
"No cached subdir for $subdirUri, malformed cache! Rescan needed." "No cached subdir for $subdirUri, malformed cache! Rescan needed."
} }
hydrateCached( // Skip this directory if it's in the excluded list
if (cachedSubdir.name in excludedSubdirs) {
return@transform
}
emit(hydrateCached(
cachedSubdir, cachedSubdir,
dir, dir,
dir.path.file(cachedSubdir.name), dir.path.file(cachedSubdir.name),
fileTree) fileTree,
excludedSubdirs))
}, },
cached.fileUris.asFlow().map { cached.fileUris.asFlow().map {
val cachedFile = requireNotNull(fileTree.queryFile(it.toUri())) val cachedFile = requireNotNull(fileTree.queryFile(it.toUri()))