diff --git a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt index 29e5ff67f..580debe92 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt @@ -57,6 +57,26 @@ interface Musikr { locations: List, onProgress: suspend (IndexingProgress) -> Unit = {} ): 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): 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): MusicLocation? companion object { /** @@ -116,6 +136,14 @@ private class MusikrImpl( private val extractStep: ExtractStep, private val evaluateStep: EvaluateStep ) : Musikr { + override fun updateLocationExclusions(context: Context, uri: android.net.Uri, excludedSubdirs: Set): MusicLocation? { + return MusicLocation.existing(context, uri, excludedSubdirs) + } + + override fun newLocationWithExclusions(context: Context, uri: android.net.Uri, excludedSubdirs: Set): MusicLocation? { + return MusicLocation.new(context, uri, excludedSubdirs) + } + override suspend fun run( locations: List, onProgress: suspend (IndexingProgress) -> Unit diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/MusicLocation.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/MusicLocation.kt index 636d16c61..37baa94b9 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/MusicLocation.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/MusicLocation.kt @@ -26,7 +26,7 @@ import org.oxycblt.musikr.fs.device.contentResolverSafe import org.oxycblt.musikr.fs.path.DocumentPathFactory 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 = emptySet()) { override fun equals(other: Any?) = other is MusicLocation && uri == other.uri 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() companion object { - fun new(context: Context, uri: Uri): MusicLocation? { + fun new(context: Context, uri: Uri, excludedSubdirs: Set = emptySet()): MusicLocation? { if (!DocumentsContract.isTreeUri(uri)) return null val documentPathFactory = DocumentPathFactory.from(context) val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null @@ -47,10 +47,10 @@ class MusicLocation private constructor(val uri: Uri, val path: Path) { uri, 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 = emptySet()): MusicLocation? { val documentPathFactory = DocumentPathFactory.from(context) if (!DocumentsContract.isTreeUri(uri)) return null val notPersisted = @@ -59,14 +59,28 @@ class MusicLocation private constructor(val uri: Uri, val path: Path) { } if (notPersisted) return null val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null - return MusicLocation(uri, path) + return MusicLocation(uri, path, excludedSubdirs) } - fun toString(list: List) = - list.joinToString(";") { it.uri.toString().replace(";", "\\;") } + fun toString(list: List): String { + 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 { - 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) + } } } } diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt index c0c27f5ae..346d02132 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt @@ -22,7 +22,6 @@ import android.content.ContentResolver import android.content.Context import android.net.Uri import android.provider.DocumentsContract -import android.util.Log import androidx.core.net.toUri import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -40,6 +39,7 @@ import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.merge import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.takeWhile +import kotlinx.coroutines.flow.transform import org.oxycblt.musikr.fs.MusicLocation import org.oxycblt.musikr.fs.Path @@ -76,7 +76,7 @@ private class DeviceFSImpl( if (modifiedMs == 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( @@ -85,13 +85,14 @@ private class DeviceFSImpl( path: Path, modifiedMs: Long, parent: DeviceDirectory?, - fileTree: FileTree + fileTree: FileTree, + excludedSubdirs: Set = emptySet() ): DeviceDirectory = coroutineScope { val uri = DocumentsContract.buildDocumentUriUsingTree(rootUri, treeDocumentId) val cached = fileTree.queryDirectory(uri) if (cached != null && cached.modifiedMs == modifiedMs) { return@coroutineScope hydrateCached( - cached = cached, parentDir = parent, path = path, fileTree = fileTree) + cached = cached, parentDir = parent, path = path, fileTree = fileTree, excludedSubdirs = excludedSubdirs) } val dir = DeviceDirectoryImpl( @@ -102,7 +103,6 @@ private class DeviceFSImpl( children = emptyFlow()) dir.children = flow { - Log.d("DeviceFS", "Querying $uri") contentResolver.useQuery( DocumentsContract.buildChildDocumentsUriUsingTree( rootUri, @@ -129,8 +129,6 @@ private class DeviceFSImpl( val childSubdirUris = mutableListOf() val childFileUris = mutableListOf() - val subqueries = mutableListOf() - while (cursor.moveToNext()) { val childId = cursor.getString(childUriIndex) val displayName = cursor.getString(displayNameIndex) @@ -145,6 +143,11 @@ private class DeviceFSImpl( val lastModified = cursor.getLong(lastModifiedIndex) if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) { + // Skip this directory if it's in the excluded list + if (displayName in excludedSubdirs) { + continue + } + val subdir = query( rootUri = rootUri, @@ -152,11 +155,11 @@ private class DeviceFSImpl( path = newPath, modifiedMs = modifiedMs, parent = dir, - fileTree = fileTree) + fileTree = fileTree, + excludedSubdirs = excludedSubdirs) childSubdirUris.add(subdir.uri.toString()) emit(StreamedFile.More(subdir)) } else { - Log.d("DeviceFS", "Querying file $childId") val size = cursor.getLong(sizeIndex) val childUri = DocumentsContract.buildDocumentUriUsingTree( @@ -202,7 +205,8 @@ private class DeviceFSImpl( cached: CachedDirectory, parentDir: DeviceDirectory?, path: Path, - fileTree: FileTree + fileTree: FileTree, + excludedSubdirs: Set = emptySet() ): DeviceDirectory = coroutineScope { val dir = DeviceDirectoryImpl( @@ -215,17 +219,23 @@ private class DeviceFSImpl( flow { emitAll( merge( - cached.subdirUris.asFlow().map { subdirUriString -> + cached.subdirUris.asFlow().transform { subdirUriString -> val subdirUri = subdirUriString.toUri() val cachedSubdir = requireNotNull(fileTree.queryDirectory(subdirUri)) { "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, dir, dir.path.file(cachedSubdir.name), - fileTree) + fileTree, + excludedSubdirs)) }, cached.fileUris.asFlow().map { val cachedFile = requireNotNull(fileTree.queryFile(it.toUri()))