From b3f51ea4b79c1998b3c5600c74a4bc57f4ab62e3 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Wed, 14 May 2025 11:07:52 -0600 Subject: [PATCH] checkpt --- build.gradle | 1 + musikr/build.gradle | 3 + .../org/oxycblt/musikr/fs/device/DeviceFS.kt | 11 ++-- .../oxycblt/musikr/fs/device/FileTreeCache.kt | 59 +++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 musikr/src/main/java/org/oxycblt/musikr/fs/device/FileTreeCache.kt diff --git a/build.gradle b/build.gradle index 7bc5b84a9..c62c033d1 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/musikr/build.gradle b/musikr/build.gradle index 9e819dcff..31d256899 100644 --- a/musikr/build.gradle +++ b/musikr/build.gradle @@ -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" diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt index 6b67bbe42..eae70db4a 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt @@ -39,20 +39,21 @@ import org.oxycblt.musikr.fs.MusicLocation import org.oxycblt.musikr.fs.Path internal interface DeviceFS { - fun explore(locations: Flow): Flow + suspend fun explore(locations: Flow): Flow 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): Flow = + override fun explore(locations: Flow): Flow { 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, diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/device/FileTreeCache.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/device/FileTreeCache.kt new file mode 100644 index 000000000..daf112dee --- /dev/null +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/device/FileTreeCache.kt @@ -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 . + */ + +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 +) + +data class CachedDirectory( + val uri: String, + val name: String, + val modifiedMs: Long, + val childUris: List +) + +data class CachedFile( + val uri: String, + val name: String, + val modifiedMs: Long, + val mimeType: String, + val size: Long +)