This commit is contained in:
Alexander Capehart
2025-05-14 11:07:52 -06:00
parent 84cf0bf29f
commit b3f51ea4b7
4 changed files with 69 additions and 5 deletions
@@ -39,20 +39,21 @@ import org.oxycblt.musikr.fs.MusicLocation
import org.oxycblt.musikr.fs.Path
internal interface DeviceFS {
fun explore(locations: Flow<MusicLocation>): Flow<DeviceDirectory>
suspend fun explore(locations: Flow<MusicLocation>): Flow<DeviceDirectory>
companion object {
fun from(context: Context, withHidden: Boolean): DeviceFS =
DeviceFSImpl(context.contentResolverSafe, withHidden)
DeviceFSImpl(context.contentResolverSafe, JsonFileTreeCache(context), withHidden)
}
}
@OptIn(ExperimentalCoroutinesApi::class)
private class DeviceFSImpl(
private val contentResolver: ContentResolver,
private val cache: FileTreeCache,
private val withHidden: Boolean
) : DeviceFS {
override fun explore(locations: Flow<MusicLocation>): Flow<DeviceDirectory> =
override fun explore(locations: Flow<MusicLocation>): Flow<DeviceDirectory> {
locations.mapNotNull { location ->
val treeDocumentId =
DocumentsContract.getTreeDocumentId(location.uri)
@@ -73,10 +74,11 @@ private class DeviceFSImpl(
location.uri,
treeDocumentId,
location.path,
0,
modifiedMs,
null
)
}
}
private suspend fun query(
rootUri: Uri,
@@ -93,7 +95,6 @@ private class DeviceFSImpl(
children = emptyFlow()
)
dir.children = flow {
Log.d("DeviceFS", "Finished querying $path")
contentResolver.useQuery(
DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri,
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 Auxio Project
* FileTreeCache.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.musikr.fs.device
import android.content.Context
import android.net.Uri
import android.util.Log
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import java.io.File
interface FileTreeCache {
suspend fun queryRootDirectory(uri: Uri): CachedRoot
suspend fun updateRootDirectory(uri: Uri, cachedRoot: CachedRoot)
suspend fun queryDirectory(uri: Uri): CachedDirectory
suspend fun updateDirectory(uri: Uri, directory: CachedDirectory)
suspend fun queryFile(uri: Uri): CachedFile?
suspend fun updateFile(uri: Uri, file: CachedFile)
}
data class CachedRoot(
val uri: String,
val path: String,
val modifiedMs: Long,
val childUris: List<String>
)
data class CachedDirectory(
val uri: String,
val name: String,
val modifiedMs: Long,
val childUris: List<String>
)
data class CachedFile(
val uri: String,
val name: String,
val modifiedMs: Long,
val mimeType: String,
val size: Long
)