Revert "musikr: add subfolder exclusion prototype"

This reverts commit f2b1ba71f2.
This commit is contained in:
Alexander Capehart
2025-05-29 20:58:41 -06:00
parent f2b1ba71f2
commit d49f719de7
3 changed files with 21 additions and 73 deletions
@@ -58,26 +58,6 @@ 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.
@@ -136,14 +116,6 @@ 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, val excludedSubdirs: Set<String> = emptySet()) { class MusicLocation private constructor(val uri: Uri, val path: Path) {
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, val exclud
override fun toString(): String = uri.toString() override fun toString(): String = uri.toString()
companion object { companion object {
fun new(context: Context, uri: Uri, excludedSubdirs: Set<String> = emptySet()): MusicLocation? { fun new(context: Context, uri: Uri): 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, val exclud
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, excludedSubdirs) return MusicLocation(uri, path)
} }
fun existing(context: Context, uri: Uri, excludedSubdirs: Set<String> = emptySet()): MusicLocation? { fun existing(context: Context, uri: Uri): 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,28 +59,14 @@ class MusicLocation private constructor(val uri: Uri, val path: Path, val exclud
} }
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, excludedSubdirs) return MusicLocation(uri, path)
} }
fun toString(list: List<MusicLocation>): String { fun toString(list: List<MusicLocation>) =
return list.joinToString(";") { location -> list.joinToString(";") { it.uri.toString().replace(";", "\\;") }
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 { entry -> return string.splitEscaped { it == ';' }.mapNotNull { existing(context, Uri.parse(it)) }
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,6 +22,7 @@ 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
@@ -39,7 +40,6 @@ 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, location.excludedSubdirs) return query(location.uri, treeDocumentId, location.path, modifiedMs, null, fileTree)
} }
private suspend fun query( private suspend fun query(
@@ -85,14 +85,13 @@ 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, excludedSubdirs = excludedSubdirs) cached = cached, parentDir = parent, path = path, fileTree = fileTree)
} }
val dir = val dir =
DeviceDirectoryImpl( DeviceDirectoryImpl(
@@ -103,6 +102,7 @@ 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,6 +129,8 @@ 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)
@@ -143,11 +145,6 @@ 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,
@@ -155,11 +152,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(
@@ -205,8 +202,7 @@ 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(
@@ -219,23 +215,17 @@ private class DeviceFSImpl(
flow { flow {
emitAll( emitAll(
merge( merge(
cached.subdirUris.asFlow().transform { subdirUriString -> cached.subdirUris.asFlow().map { 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."
} }
// Skip this directory if it's in the excluded list hydrateCached(
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()))