@@ -22,7 +22,6 @@ import android.content.ContentResolver
2222import android.content.Context
2323import android.net.Uri
2424import android.provider.DocumentsContract
25- import android.util.Log
2625import androidx.core.net.toUri
2726import kotlinx.coroutines.CoroutineScope
2827import kotlinx.coroutines.Dispatchers
@@ -40,6 +39,7 @@ import kotlinx.coroutines.flow.mapNotNull
4039import kotlinx.coroutines.flow.merge
4140import kotlinx.coroutines.flow.shareIn
4241import kotlinx.coroutines.flow.takeWhile
42+ import kotlinx.coroutines.flow.transform
4343import org.oxycblt.musikr.fs.MusicLocation
4444import org.oxycblt.musikr.fs.Path
4545
@@ -76,7 +76,7 @@ private class DeviceFSImpl(
7676 if (modifiedMs == null ) {
7777 return null
7878 }
79- return query(location.uri, treeDocumentId, location.path, modifiedMs, null , fileTree)
79+ return query(location.uri, treeDocumentId, location.path, modifiedMs, null , fileTree, location.excludedSubdirs )
8080 }
8181
8282 private suspend fun query (
@@ -85,13 +85,14 @@ private class DeviceFSImpl(
8585 path : Path ,
8686 modifiedMs : Long ,
8787 parent : DeviceDirectory ? ,
88- fileTree : FileTree
88+ fileTree : FileTree ,
89+ excludedSubdirs : Set <String > = emptySet()
8990 ): DeviceDirectory = coroutineScope {
9091 val uri = DocumentsContract .buildDocumentUriUsingTree(rootUri, treeDocumentId)
9192 val cached = fileTree.queryDirectory(uri)
9293 if (cached != null && cached.modifiedMs == modifiedMs) {
9394 return @coroutineScope hydrateCached(
94- cached = cached, parentDir = parent, path = path, fileTree = fileTree)
95+ cached = cached, parentDir = parent, path = path, fileTree = fileTree, excludedSubdirs = excludedSubdirs )
9596 }
9697 val dir =
9798 DeviceDirectoryImpl (
@@ -102,7 +103,6 @@ private class DeviceFSImpl(
102103 children = emptyFlow())
103104 dir.children =
104105 flow {
105- Log .d(" DeviceFS" , " Querying $uri " )
106106 contentResolver.useQuery(
107107 DocumentsContract .buildChildDocumentsUriUsingTree(
108108 rootUri,
@@ -129,8 +129,6 @@ private class DeviceFSImpl(
129129 val childSubdirUris = mutableListOf<String >()
130130 val childFileUris = mutableListOf<String >()
131131
132- val subqueries = mutableListOf<DeviceDirectory >()
133-
134132 while (cursor.moveToNext()) {
135133 val childId = cursor.getString(childUriIndex)
136134 val displayName = cursor.getString(displayNameIndex)
@@ -145,18 +143,23 @@ private class DeviceFSImpl(
145143 val lastModified = cursor.getLong(lastModifiedIndex)
146144
147145 if (mimeType == DocumentsContract .Document .MIME_TYPE_DIR ) {
146+ // Skip this directory if it's in the excluded list
147+ if (displayName in excludedSubdirs) {
148+ continue
149+ }
150+
148151 val subdir =
149152 query(
150153 rootUri = rootUri,
151154 treeDocumentId = childId,
152155 path = newPath,
153156 modifiedMs = modifiedMs,
154157 parent = dir,
155- fileTree = fileTree)
158+ fileTree = fileTree,
159+ excludedSubdirs = excludedSubdirs)
156160 childSubdirUris.add(subdir.uri.toString())
157161 emit(StreamedFile .More (subdir))
158162 } else {
159- Log .d(" DeviceFS" , " Querying file $childId " )
160163 val size = cursor.getLong(sizeIndex)
161164 val childUri =
162165 DocumentsContract .buildDocumentUriUsingTree(
@@ -202,7 +205,8 @@ private class DeviceFSImpl(
202205 cached : CachedDirectory ,
203206 parentDir : DeviceDirectory ? ,
204207 path : Path ,
205- fileTree : FileTree
208+ fileTree : FileTree ,
209+ excludedSubdirs : Set <String > = emptySet()
206210 ): DeviceDirectory = coroutineScope {
207211 val dir =
208212 DeviceDirectoryImpl (
@@ -215,17 +219,23 @@ private class DeviceFSImpl(
215219 flow {
216220 emitAll(
217221 merge(
218- cached.subdirUris.asFlow().map { subdirUriString ->
222+ cached.subdirUris.asFlow().transform { subdirUriString ->
219223 val subdirUri = subdirUriString.toUri()
220224 val cachedSubdir =
221225 requireNotNull(fileTree.queryDirectory(subdirUri)) {
222226 " No cached subdir for $subdirUri , malformed cache! Rescan needed."
223227 }
224- hydrateCached(
228+ // Skip this directory if it's in the excluded list
229+ if (cachedSubdir.name in excludedSubdirs) {
230+ return @transform
231+ }
232+
233+ emit(hydrateCached(
225234 cachedSubdir,
226235 dir,
227236 dir.path.file(cachedSubdir.name),
228- fileTree)
237+ fileTree,
238+ excludedSubdirs))
229239 },
230240 cached.fileUris.asFlow().map {
231241 val cachedFile = requireNotNull(fileTree.queryFile(it.toUri()))
0 commit comments