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
+1
View File
@@ -30,6 +30,7 @@ plugins {
//noinspection GradlePluginVersion
id 'com.android.library' version "$agp_version" apply false
id "org.jetbrains.kotlin.android" version "$kotlin_version" apply false
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" apply false
id "com.google.devtools.ksp" version '2.0.21-1.0.25' apply false
// We use spotless in the root build.gradle to apply to all modules.
id "com.diffplug.spotless" version "6.25.0" apply true
+3
View File
@@ -4,6 +4,7 @@ plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id "com.google.devtools.ksp"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.diffplug.spotless"
id "kotlin-parcelize"
id "org.jetbrains.dokka"
@@ -62,6 +63,8 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0'
// AndroidX
implementation "androidx.core:core-ktx:$core_version"
@@ -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
)