@@ -50,79 +50,69 @@ private class DeviceFSImpl(
5050) : DeviceFS {
5151 override fun explore (locations : Flow <MusicLocation >): Flow <DeviceFile > =
5252 locations.flatMapMerge { location ->
53- val (_, query) =
54- exploreDirectoryImpl(
55- location.uri,
56- DocumentsContract .getTreeDocumentId(location.uri),
57- location.path,
58- null )
59- query
53+ exploreDirectoryImpl(
54+ location.uri,
55+ DocumentsContract .getTreeDocumentId(location.uri),
56+ location.path,
57+ null )
6058 }
6159
6260 private fun exploreDirectoryImpl (
6361 rootUri : Uri ,
6462 treeDocumentId : String ,
6563 relativePath : Path ,
6664 parent : Deferred <DeviceDirectory >?
67- ): Pair < DeviceDirectory , Flow <DeviceFile >> {
65+ ): Flow <DeviceFile > = flow {
6866 // Make a kotlin future
6967 val uri = DocumentsContract .buildChildDocumentsUriUsingTree(rootUri, treeDocumentId)
7068 val directoryDeferred = CompletableDeferred <DeviceDirectory >()
71- val childrenDeferred = CompletableDeferred <List <DeviceFSEntry >>()
72- val dir = DeviceDirectory (uri, relativePath, parent, childrenDeferred)
73- val query = flow {
74- val recursive = mutableListOf<Flow <DeviceFile >>()
75- val children = mutableListOf<DeviceFSEntry >()
76- contentResolver.useQuery(uri, PROJECTION ) { cursor ->
77- val childUriIndex =
78- cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_DOCUMENT_ID )
79- val displayNameIndex =
80- cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_DISPLAY_NAME )
81- val mimeTypeIndex =
82- cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_MIME_TYPE )
83- val sizeIndex = cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_SIZE )
84- val lastModifiedIndex =
85- cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_LAST_MODIFIED )
69+ val recursive = mutableListOf<Flow <DeviceFile >>()
70+ val children = mutableListOf<DeviceFSEntry >()
71+ contentResolver.useQuery(uri, PROJECTION ) { cursor ->
72+ val childUriIndex =
73+ cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_DOCUMENT_ID )
74+ val displayNameIndex =
75+ cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_DISPLAY_NAME )
76+ val mimeTypeIndex =
77+ cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_MIME_TYPE )
78+ val sizeIndex = cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_SIZE )
79+ val lastModifiedIndex =
80+ cursor.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_LAST_MODIFIED )
8681
87- while (cursor.moveToNext()) {
88- val childId = cursor.getString(childUriIndex)
89- val displayName = cursor.getString(displayNameIndex)
82+ while (cursor.moveToNext()) {
83+ val childId = cursor.getString(childUriIndex)
84+ val displayName = cursor.getString(displayNameIndex)
9085
91- // Skip hidden files/directories if ignoreHidden is true
92- if (! withHidden && displayName.startsWith(" ." )) {
93- continue
94- }
86+ // Skip hidden files/directories if ignoreHidden is true
87+ if (! withHidden && displayName.startsWith(" ." )) {
88+ continue
89+ }
9590
96- val newPath = relativePath.file(displayName)
97- val mimeType = cursor.getString(mimeTypeIndex)
98- val lastModified = cursor.getLong(lastModifiedIndex)
91+ val newPath = relativePath.file(displayName)
92+ val mimeType = cursor.getString(mimeTypeIndex)
93+ val lastModified = cursor.getLong(lastModifiedIndex)
9994
100- if (mimeType == DocumentsContract .Document .MIME_TYPE_DIR ) {
101- val (dir, query) =
102- exploreDirectoryImpl(rootUri, childId, newPath, directoryDeferred)
103- children.add(dir)
104- recursive.add(query)
105- } else {
106- val size = cursor.getLong(sizeIndex)
107- val childUri = DocumentsContract .buildDocumentUriUsingTree(rootUri, childId)
108- val file =
109- DeviceFile (
110- uri = childUri,
111- mimeType = mimeType,
112- path = newPath,
113- size = size,
114- modifiedMs = lastModified,
115- parent = directoryDeferred)
116- children.add(file)
117- emit(file)
118- }
95+ if (mimeType == DocumentsContract .Document .MIME_TYPE_DIR ) {
96+ recursive.add(
97+ exploreDirectoryImpl(rootUri, childId, newPath, directoryDeferred))
98+ } else {
99+ val size = cursor.getLong(sizeIndex)
100+ val childUri = DocumentsContract .buildDocumentUriUsingTree(rootUri, childId)
101+ val file =
102+ DeviceFile (
103+ uri = childUri,
104+ mimeType = mimeType,
105+ path = newPath,
106+ size = size,
107+ modifiedMs = lastModified,
108+ parent = directoryDeferred)
109+ children.add(file)
110+ emit(file)
119111 }
120- childrenDeferred.complete(children)
121- directoryDeferred.complete(dir)
122- emitAll(recursive.asFlow().flattenMerge())
123112 }
113+ directoryDeferred.complete(DeviceDirectory (uri, relativePath, parent, children))
114+ emitAll(recursive.asFlow().flattenMerge())
124115 }
125- return dir to query
126116 }
127117
128118 private companion object {
0 commit comments