musikr: refactor shared tracking system
This commit is contained in:
+9
-9
@@ -18,7 +18,7 @@
|
||||
|
||||
package org.oxycblt.musikr.cache
|
||||
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.metadata.Properties
|
||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||
|
||||
@@ -37,10 +37,10 @@ interface Cache {
|
||||
* Read a [CachedSong] corresponding to the given [file] from the cache. This can result in
|
||||
* several outcomes represented by [CacheResult].
|
||||
*
|
||||
* @param file the [DeviceFile] to read from the cache
|
||||
* @param file the [File] to read from the cache
|
||||
* @return a [CacheResult] representing the result of the operation.
|
||||
*/
|
||||
suspend fun read(file: DeviceFile): CacheResult
|
||||
suspend fun read(file: File): CacheResult
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +56,7 @@ interface MutableCache : Cache {
|
||||
* Write a [CachedSong] to the cache.
|
||||
*
|
||||
* This should commit the metadata to the repository in such a way that it can be retrieved
|
||||
* later by [read] using only the [DeviceFile].
|
||||
* later by [read] using only the [File].
|
||||
*
|
||||
* @param cachedSong the [CachedSong] to write to the cache
|
||||
*/
|
||||
@@ -78,7 +78,7 @@ interface MutableCache : Cache {
|
||||
/** A cached song entry containing the data needed by the rest of the loader. */
|
||||
data class CachedSong(
|
||||
/** The file this song corresponds to. */
|
||||
val file: DeviceFile,
|
||||
val file: File,
|
||||
/** The properties of the song. */
|
||||
val properties: Properties,
|
||||
/** The parsed tags of the song. */
|
||||
@@ -107,15 +107,15 @@ sealed interface CacheResult {
|
||||
/**
|
||||
* A cache entry was not found.
|
||||
*
|
||||
* @param file the [DeviceFile] that could not be found in the cache.
|
||||
* @param file the [File] that could not be found in the cache.
|
||||
*/
|
||||
data class Miss(val file: DeviceFile) : CacheResult
|
||||
data class Miss(val file: File) : CacheResult
|
||||
|
||||
/**
|
||||
* A cache entry was found, but it's out of date compared to the [file] given.
|
||||
*
|
||||
* @param file the [DeviceFile] that was found in the cache.
|
||||
* @param file the [File] that was found in the cache.
|
||||
* @param addedMs the time the song was added to the cache.
|
||||
*/
|
||||
data class Stale(val file: DeviceFile, val addedMs: Long) : CacheResult
|
||||
data class Stale(val file: File, val addedMs: Long) : CacheResult
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.oxycblt.musikr.cache.Cache
|
||||
import org.oxycblt.musikr.cache.CacheResult
|
||||
import org.oxycblt.musikr.cache.CachedSong
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.metadata.Properties
|
||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||
* Create an instance with [from].
|
||||
*/
|
||||
class DBCache private constructor(private val readDao: CacheReadDao) : Cache {
|
||||
override suspend fun read(file: DeviceFile): CacheResult {
|
||||
override suspend fun read(file: File): CacheResult {
|
||||
val dbSong = readDao.selectSong(file.uri.toString()) ?: return CacheResult.Miss(file)
|
||||
if (dbSong.modifiedMs != file.modifiedMs) {
|
||||
return CacheResult.Stale(file, dbSong.addedMs)
|
||||
@@ -94,7 +94,7 @@ class DBCache private constructor(private val readDao: CacheReadDao) : Cache {
|
||||
class MutableDBCache
|
||||
private constructor(private val inner: DBCache, private val writeDao: CacheWriteDao) :
|
||||
MutableCache {
|
||||
override suspend fun read(file: DeviceFile) = inner.read(file)
|
||||
override suspend fun read(file: File) = inner.read(file)
|
||||
|
||||
override suspend fun write(cachedSong: CachedSong) {
|
||||
val dbSong =
|
||||
|
||||
@@ -20,7 +20,7 @@ package org.oxycblt.musikr.covers
|
||||
|
||||
import android.os.ParcelFileDescriptor
|
||||
import java.io.InputStream
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
/**
|
||||
@@ -54,11 +54,11 @@ interface MutableCovers<T : Cover> : Covers<T> {
|
||||
* This could result in side-effect-laden storage, or be a simple translation into a lazily
|
||||
* loaded [Cover] instance.
|
||||
*
|
||||
* @param file The [DeviceFile] to of the file to create a cover for.
|
||||
* @param file The [File] to of the file to create a cover for.
|
||||
* @param metadata The [Metadata] to use to create the cover.
|
||||
* @return a [CoverResult] indicating whether the cover was created or not
|
||||
*/
|
||||
suspend fun create(file: DeviceFile, metadata: Metadata): CoverResult<T>
|
||||
suspend fun create(file: File, metadata: Metadata): CoverResult<T>
|
||||
|
||||
/**
|
||||
* Cleanup the cover repository by removing any covers that are not in the [excluding]
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
/**
|
||||
@@ -72,7 +72,7 @@ class MutableChainedCovers<R : Cover, T : R>(vararg many: MutableCovers<out T>)
|
||||
|
||||
override suspend fun obtain(id: String): CoverResult<R> = inner.obtain(id)
|
||||
|
||||
override suspend fun create(file: DeviceFile, metadata: Metadata): CoverResult<R> {
|
||||
override suspend fun create(file: File, metadata: Metadata): CoverResult<R> {
|
||||
for (cover in _many) {
|
||||
val result = cover.create(file, metadata)
|
||||
if (result is CoverResult.Hit) {
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.MemoryCover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
/**
|
||||
@@ -44,7 +44,7 @@ import org.oxycblt.musikr.metadata.Metadata
|
||||
class EmbeddedCovers(private val coverIdentifier: CoverIdentifier) : MutableCovers<MemoryCover> {
|
||||
override suspend fun obtain(id: String): CoverResult<MemoryCover> = CoverResult.Miss()
|
||||
|
||||
override suspend fun create(file: DeviceFile, metadata: Metadata): CoverResult<MemoryCover> {
|
||||
override suspend fun create(file: File, metadata: Metadata): CoverResult<MemoryCover> {
|
||||
val data = metadata.cover ?: return CoverResult.Miss()
|
||||
val id = coverIdentifier.identify(data)
|
||||
return CoverResult.Hit(EmbeddedCover(id, data))
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.covers.FDCover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
private const val PREFIX = "mcf:"
|
||||
@@ -93,13 +93,13 @@ class MutableFSCovers(private val context: Context) : MutableCovers<FDCover> {
|
||||
|
||||
override suspend fun obtain(id: String): CoverResult<FDCover> = inner.obtain(id)
|
||||
|
||||
override suspend fun create(file: DeviceFile, metadata: Metadata): CoverResult<FDCover> {
|
||||
override suspend fun create(file: File, metadata: Metadata): CoverResult<FDCover> {
|
||||
// Since DeviceFiles is a streaming API, we have to wait for the current recursive
|
||||
// query to finally finish to be able to have a complete list of siblings to search for.
|
||||
val parent = file.parent.await()
|
||||
val bestCover =
|
||||
parent.children
|
||||
.filterIsInstance<DeviceFile>()
|
||||
.filterIsInstance<File>()
|
||||
.map { it to coverArtScore(it) }
|
||||
.maxBy { it.second }
|
||||
if (bestCover.second > 0) {
|
||||
@@ -116,7 +116,7 @@ class MutableFSCovers(private val context: Context) : MutableCovers<FDCover> {
|
||||
// that should not be managed by the app
|
||||
}
|
||||
|
||||
private suspend fun coverArtScore(file: DeviceFile): Int {
|
||||
private suspend fun coverArtScore(file: File): Int {
|
||||
if (!file.mimeType.startsWith("image/", ignoreCase = true)) {
|
||||
// Not an image file. You lose!
|
||||
return 0
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.covers.FDCover
|
||||
import org.oxycblt.musikr.covers.MemoryCover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.metadata.Metadata
|
||||
|
||||
private const val PREFIX = "mcs:"
|
||||
@@ -75,7 +75,7 @@ class MutableStoredCovers(
|
||||
|
||||
override suspend fun obtain(id: String): CoverResult<FDCover> = base.obtain(id)
|
||||
|
||||
override suspend fun create(file: DeviceFile, metadata: Metadata): CoverResult<FDCover> {
|
||||
override suspend fun create(file: File, metadata: Metadata): CoverResult<FDCover> {
|
||||
val memoryCover =
|
||||
when (val cover = src.create(file, metadata)) {
|
||||
is CoverResult.Hit -> cover.cover
|
||||
|
||||
@@ -23,26 +23,32 @@ import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface FS {
|
||||
fun explore(): Flow<DeviceFile>
|
||||
fun explore(): Flow<File>
|
||||
|
||||
fun track(): Flow<FSUpdate>
|
||||
}
|
||||
|
||||
sealed interface DeviceFSEntry {
|
||||
sealed interface FSEntry {
|
||||
val uri: Uri?
|
||||
val path: Path
|
||||
}
|
||||
|
||||
data class DeviceDirectory(
|
||||
data class Directory(
|
||||
override val uri: Uri?,
|
||||
override val path: Path,
|
||||
val parent: Deferred<DeviceDirectory>?,
|
||||
var children: List<DeviceFile>
|
||||
) : DeviceFSEntry
|
||||
val parent: Deferred<Directory>?,
|
||||
var children: List<File>
|
||||
) : FSEntry
|
||||
|
||||
data class DeviceFile(
|
||||
data class File(
|
||||
override val uri: Uri,
|
||||
override val path: Path,
|
||||
val modifiedMs: Long,
|
||||
val mimeType: String,
|
||||
val size: Long,
|
||||
val parent: Deferred<DeviceDirectory>
|
||||
) : DeviceFSEntry
|
||||
val parent: Deferred<Directory>
|
||||
) : FSEntry
|
||||
|
||||
sealed interface FSUpdate {
|
||||
data class LocationChanged(val location: Location.Opened?) : FSUpdate
|
||||
}
|
||||
|
||||
@@ -24,21 +24,25 @@ import android.provider.MediaStore
|
||||
import androidx.core.database.getStringOrNull
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.yield
|
||||
import org.oxycblt.musikr.fs.DeviceDirectory
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.Directory
|
||||
import org.oxycblt.musikr.fs.FS
|
||||
import org.oxycblt.musikr.fs.FSUpdate
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import org.oxycblt.musikr.fs.path.MediaStorePathInterpreter
|
||||
import org.oxycblt.musikr.fs.path.VolumeManager
|
||||
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
||||
import org.oxycblt.musikr.fs.saf.useQuery
|
||||
import org.oxycblt.musikr.fs.track.LocationObserver
|
||||
|
||||
/**
|
||||
* MediaStore implementation of [FS] that queries the Android MediaStore database for audio files
|
||||
* and yields them as [DeviceFile] instances.
|
||||
* and yields them as [File] instances.
|
||||
*/
|
||||
class MediaStoreFS
|
||||
private constructor(
|
||||
@@ -48,84 +52,8 @@ private constructor(
|
||||
) : FS {
|
||||
private val pathInterpreterFactory = MediaStorePathInterpreter.Factory.from(volumeManager)
|
||||
|
||||
private fun buildDirectoryHierarchy(files: List<DeviceFile>) {
|
||||
// Map to store directories by their path
|
||||
val directoryMap = mutableMapOf<Path, DeviceDirectory>()
|
||||
|
||||
// Group files by their parent directory path
|
||||
val filesByParent = files.groupBy { it.path.directory }
|
||||
|
||||
// First pass: Create all directories and complete file parent deferreds
|
||||
for ((dirPath, dirFiles) in filesByParent) {
|
||||
val directory =
|
||||
directoryMap.getOrPut(dirPath) {
|
||||
// Create parent deferred for this directory
|
||||
val parentDeferred =
|
||||
if (dirPath.components.components.isEmpty()) {
|
||||
// Root directory has no parent
|
||||
null
|
||||
} else {
|
||||
CompletableDeferred<DeviceDirectory>()
|
||||
}
|
||||
|
||||
DeviceDirectory(
|
||||
uri = null, // Directories don't have MediaStore URIs
|
||||
path = dirPath,
|
||||
parent = parentDeferred,
|
||||
children = dirFiles)
|
||||
}
|
||||
|
||||
// Update children if directory already existed
|
||||
if (directory.children.isEmpty()) {
|
||||
directory.children = dirFiles
|
||||
}
|
||||
|
||||
// Complete parent deferreds for all files in this directory
|
||||
dirFiles.forEach { file ->
|
||||
(file.parent as CompletableDeferred<DeviceDirectory>).complete(directory)
|
||||
}
|
||||
}
|
||||
|
||||
// Second pass: Create any missing parent directories
|
||||
val directories = directoryMap.values.toList()
|
||||
for (directory in directories) {
|
||||
var currentPath = directory.path
|
||||
while (currentPath.components.components.isNotEmpty()) {
|
||||
val parentPath = currentPath.directory
|
||||
if (!directoryMap.containsKey(parentPath)) {
|
||||
val grandParentDeferred =
|
||||
if (parentPath.components.components.isEmpty()) {
|
||||
null
|
||||
} else {
|
||||
CompletableDeferred<DeviceDirectory>()
|
||||
}
|
||||
|
||||
directoryMap[parentPath] =
|
||||
DeviceDirectory(
|
||||
uri = null,
|
||||
path = parentPath,
|
||||
parent = grandParentDeferred,
|
||||
children = emptyList())
|
||||
}
|
||||
currentPath = parentPath
|
||||
}
|
||||
}
|
||||
|
||||
// Third pass: Complete all directory parent deferreds
|
||||
for (directory in directoryMap.values) {
|
||||
val parentDeferred = directory.parent as? CompletableDeferred<DeviceDirectory>
|
||||
if (parentDeferred != null && !parentDeferred.isCompleted) {
|
||||
val parentPath = directory.path.directory
|
||||
val parentDirectory = directoryMap[parentPath]
|
||||
if (parentDirectory != null) {
|
||||
parentDeferred.complete(parentDirectory)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override fun explore(): Flow<DeviceFile> = flow {
|
||||
override fun explore(): Flow<File> = flow {
|
||||
val projection = BASE_PROJECTION + pathInterpreterFactory.projection
|
||||
var selector = BASE_SELECTOR
|
||||
val args = mutableListOf<String>()
|
||||
@@ -136,7 +64,7 @@ private constructor(
|
||||
}
|
||||
|
||||
// Collect all files and track unique directories
|
||||
val allFiles = mutableListOf<DeviceFile>()
|
||||
val allFiles = mutableListOf<File>()
|
||||
|
||||
context.contentResolverSafe.useQuery(
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
@@ -164,9 +92,9 @@ private constructor(
|
||||
cursor.getLong(dateModifiedIndex) * 1000 // Convert to milliseconds
|
||||
|
||||
// Create file with empty deferred parent
|
||||
val parentDeferred = CompletableDeferred<DeviceDirectory>()
|
||||
val parentDeferred = CompletableDeferred<Directory>()
|
||||
val deviceFile =
|
||||
DeviceFile(
|
||||
File(
|
||||
uri = uri,
|
||||
path = path,
|
||||
modifiedMs = dateModified,
|
||||
@@ -184,6 +112,90 @@ private constructor(
|
||||
buildDirectoryHierarchy(allFiles)
|
||||
}
|
||||
|
||||
override fun track(): Flow<FSUpdate> = callbackFlow {
|
||||
val observer =
|
||||
LocationObserver(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI) {
|
||||
trySend(FSUpdate.LocationChanged(null))
|
||||
}
|
||||
awaitClose { observer.release() }
|
||||
}
|
||||
|
||||
private fun buildDirectoryHierarchy(files: List<File>) {
|
||||
// Map to store directories by their path
|
||||
val directoryMap = mutableMapOf<Path, Directory>()
|
||||
|
||||
// Group files by their parent directory path
|
||||
val filesByParent = files.groupBy { it.path.directory }
|
||||
|
||||
// First pass: Create all directories and complete file parent deferreds
|
||||
for ((dirPath, dirFiles) in filesByParent) {
|
||||
val directory =
|
||||
directoryMap.getOrPut(dirPath) {
|
||||
// Create parent deferred for this directory
|
||||
val parentDeferred =
|
||||
if (dirPath.components.components.isEmpty()) {
|
||||
// Root directory has no parent
|
||||
null
|
||||
} else {
|
||||
CompletableDeferred<Directory>()
|
||||
}
|
||||
|
||||
Directory(
|
||||
uri = null, // Directories don't have MediaStore URIs
|
||||
path = dirPath,
|
||||
parent = parentDeferred,
|
||||
children = dirFiles)
|
||||
}
|
||||
|
||||
// Update children if directory already existed
|
||||
if (directory.children.isEmpty()) {
|
||||
directory.children = dirFiles
|
||||
}
|
||||
|
||||
// Complete parent deferreds for all files in this directory
|
||||
dirFiles.forEach { file ->
|
||||
(file.parent as CompletableDeferred<Directory>).complete(directory)
|
||||
}
|
||||
}
|
||||
|
||||
// Second pass: Create any missing parent directories
|
||||
val directories = directoryMap.values.toList()
|
||||
for (directory in directories) {
|
||||
var currentPath = directory.path
|
||||
while (currentPath.components.components.isNotEmpty()) {
|
||||
val parentPath = currentPath.directory
|
||||
if (!directoryMap.containsKey(parentPath)) {
|
||||
val grandParentDeferred =
|
||||
if (parentPath.components.components.isEmpty()) {
|
||||
null
|
||||
} else {
|
||||
CompletableDeferred<Directory>()
|
||||
}
|
||||
|
||||
directoryMap[parentPath] =
|
||||
Directory(
|
||||
uri = null,
|
||||
path = parentPath,
|
||||
parent = grandParentDeferred,
|
||||
children = emptyList())
|
||||
}
|
||||
currentPath = parentPath
|
||||
}
|
||||
}
|
||||
|
||||
// Third pass: Complete all directory parent deferreds
|
||||
for (directory in directoryMap.values) {
|
||||
val parentDeferred = directory.parent as? CompletableDeferred<Directory>
|
||||
if (parentDeferred != null && !parentDeferred.isCompleted) {
|
||||
val parentPath = directory.path.directory
|
||||
val parentDirectory = directoryMap[parentPath]
|
||||
if (parentDirectory != null) {
|
||||
parentDeferred.complete(parentDirectory)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun from(context: Context) =
|
||||
MediaStoreFS(
|
||||
|
||||
@@ -25,22 +25,30 @@ import android.provider.DocumentsContract
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.flow.flatMapMerge
|
||||
import kotlinx.coroutines.flow.flattenMerge
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import org.oxycblt.musikr.fs.DeviceDirectory
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.Directory
|
||||
import org.oxycblt.musikr.fs.FS
|
||||
import org.oxycblt.musikr.fs.FSUpdate
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import org.oxycblt.musikr.fs.track.LocationObserver
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class SAF
|
||||
private constructor(private val contentResolver: ContentResolver, private val query: Query) : FS {
|
||||
override fun explore(): Flow<DeviceFile> =
|
||||
private constructor(
|
||||
private val context: Context,
|
||||
private val contentResolver: ContentResolver,
|
||||
private val query: Query
|
||||
) : FS {
|
||||
override fun explore(): Flow<File> =
|
||||
query.source.asFlow().flatMapMerge { location ->
|
||||
exploreDirectoryImpl(
|
||||
location.uri,
|
||||
@@ -50,18 +58,32 @@ private constructor(private val contentResolver: ContentResolver, private val qu
|
||||
query.exclude.mapTo(mutableSetOf()) { it.uri })
|
||||
}
|
||||
|
||||
override fun track(): Flow<FSUpdate> = callbackFlow {
|
||||
val observers = mutableListOf<LocationObserver>()
|
||||
|
||||
query.source.forEach { location ->
|
||||
val observer =
|
||||
LocationObserver(context, location.uri) {
|
||||
trySend(FSUpdate.LocationChanged(location))
|
||||
}
|
||||
observers.add(observer)
|
||||
}
|
||||
|
||||
awaitClose { observers.forEach { observer -> observer.release() } }
|
||||
}
|
||||
|
||||
private fun exploreDirectoryImpl(
|
||||
rootUri: Uri,
|
||||
treeDocumentId: String,
|
||||
relativePath: Path,
|
||||
parent: Deferred<DeviceDirectory>?,
|
||||
parent: Deferred<Directory>?,
|
||||
exclude: Set<Uri>
|
||||
): Flow<DeviceFile> = flow {
|
||||
): Flow<File> = flow {
|
||||
// Make a kotlin future
|
||||
val uri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, treeDocumentId)
|
||||
val directoryDeferred = CompletableDeferred<DeviceDirectory>()
|
||||
val recursive = mutableListOf<Flow<DeviceFile>>()
|
||||
val children = mutableListOf<DeviceFile>()
|
||||
val directoryDeferred = CompletableDeferred<Directory>()
|
||||
val recursive = mutableListOf<Flow<File>>()
|
||||
val children = mutableListOf<File>()
|
||||
contentResolver.useQuery(uri, PROJECTION) { cursor ->
|
||||
val childUriIndex =
|
||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
||||
@@ -99,7 +121,7 @@ private constructor(private val contentResolver: ContentResolver, private val qu
|
||||
} else {
|
||||
val size = cursor.getLong(sizeIndex)
|
||||
val file =
|
||||
DeviceFile(
|
||||
File(
|
||||
uri = childUri,
|
||||
mimeType = mimeType,
|
||||
path = newPath,
|
||||
@@ -110,7 +132,7 @@ private constructor(private val contentResolver: ContentResolver, private val qu
|
||||
emit(file)
|
||||
}
|
||||
}
|
||||
directoryDeferred.complete(DeviceDirectory(uri, relativePath, parent, children))
|
||||
directoryDeferred.complete(Directory(uri, relativePath, parent, children))
|
||||
emitAll(recursive.asFlow().flattenMerge())
|
||||
}
|
||||
}
|
||||
@@ -122,7 +144,7 @@ private constructor(private val contentResolver: ContentResolver, private val qu
|
||||
)
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, query: Query) = SAF(context.contentResolverSafe, query)
|
||||
fun from(context: Context, query: Query) = SAF(context, context.contentResolverSafe, query)
|
||||
|
||||
private val PROJECTION =
|
||||
arrayOf(
|
||||
|
||||
+6
-6
@@ -16,23 +16,23 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.track
|
||||
package org.oxycblt.musikr.fs.track
|
||||
|
||||
import android.content.Context
|
||||
import android.database.ContentObserver
|
||||
import android.net.Uri
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
|
||||
internal class LocationObserver(
|
||||
private val context: Context,
|
||||
private val location: Location.Opened,
|
||||
private val listener: UpdateTracker.Callback
|
||||
private val uri: Uri,
|
||||
private val onUpdate: () -> Unit
|
||||
) : ContentObserver(Handler(Looper.getMainLooper())), Runnable {
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
init {
|
||||
context.applicationContext.contentResolver.registerContentObserver(location.uri, true, this)
|
||||
context.applicationContext.contentResolver.registerContentObserver(uri, true, this)
|
||||
}
|
||||
|
||||
fun release() {
|
||||
@@ -47,7 +47,7 @@ internal class LocationObserver(
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
listener.onUpdate(location)
|
||||
onUpdate()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
@@ -23,10 +23,10 @@ import android.content.Context
|
||||
import java.io.FileInputStream
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
|
||||
internal interface MetadataExtractor {
|
||||
suspend fun extract(deviceFile: DeviceFile): Metadata?
|
||||
suspend fun extract(deviceFile: File): Metadata?
|
||||
|
||||
companion object {
|
||||
fun from(context: Context): MetadataExtractor =
|
||||
@@ -36,7 +36,7 @@ internal interface MetadataExtractor {
|
||||
|
||||
private class MetadataExtractorImpl(private val contentResolver: ContentResolver) :
|
||||
MetadataExtractor {
|
||||
override suspend fun extract(deviceFile: DeviceFile): Metadata? =
|
||||
override suspend fun extract(deviceFile: File): Metadata? =
|
||||
withContext(Dispatchers.IO) {
|
||||
contentResolver.openFileDescriptor(deviceFile.uri, "r")?.use { fd ->
|
||||
val fis = FileInputStream(fd.fileDescriptor)
|
||||
|
||||
@@ -21,9 +21,9 @@ package org.oxycblt.musikr.metadata
|
||||
import android.util.Log
|
||||
import java.io.FileInputStream
|
||||
import java.nio.ByteBuffer
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
|
||||
internal class NativeInputStream(private val deviceFile: DeviceFile, fis: FileInputStream) {
|
||||
internal class NativeInputStream(private val deviceFile: File, fis: FileInputStream) {
|
||||
private val channel = fis.channel
|
||||
|
||||
fun name() = requireNotNull(deviceFile.path.name)
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
package org.oxycblt.musikr.metadata
|
||||
|
||||
import java.io.FileInputStream
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
|
||||
internal object TagLibJNI {
|
||||
init {
|
||||
@@ -31,7 +31,7 @@ internal object TagLibJNI {
|
||||
*
|
||||
* Note: This method is blocking and should be handled as such if calling from a coroutine.
|
||||
*/
|
||||
fun open(deviceFile: DeviceFile, fis: FileInputStream): Metadata? {
|
||||
fun open(deviceFile: File, fis: FileInputStream): Metadata? {
|
||||
val inputStream = NativeInputStream(deviceFile, fis)
|
||||
val tag = openNative(inputStream)
|
||||
inputStream.close()
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
package org.oxycblt.musikr.pipeline
|
||||
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.metadata.Properties
|
||||
import org.oxycblt.musikr.playlist.PlaylistFile
|
||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||
@@ -36,7 +36,7 @@ internal sealed interface Explored : PipelineItem {
|
||||
sealed interface Known : Explored, Complete
|
||||
}
|
||||
|
||||
internal data class NewSong(val file: DeviceFile, val addedMs: Long) : Explored.New
|
||||
internal data class NewSong(val file: File, val addedMs: Long) : Explored.New
|
||||
|
||||
internal sealed interface Extracted : PipelineItem {
|
||||
sealed interface Valid : Complete, Extracted
|
||||
@@ -49,7 +49,7 @@ internal data object InvalidSong : Extracted.Invalid
|
||||
internal data class RawPlaylist(val file: PlaylistFile) : Explored.Known, Extracted.Valid
|
||||
|
||||
internal data class RawSong(
|
||||
val file: DeviceFile,
|
||||
val file: File,
|
||||
val properties: Properties,
|
||||
val tags: ParsedTags,
|
||||
val cover: Cover?,
|
||||
|
||||
@@ -20,7 +20,7 @@ package org.oxycblt.musikr.tag.interpret
|
||||
|
||||
import org.oxycblt.musikr.Interpretation
|
||||
import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.fs.DeviceFile
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.fs.Format
|
||||
import org.oxycblt.musikr.pipeline.RawSong
|
||||
import org.oxycblt.musikr.tag.Disc
|
||||
@@ -151,7 +151,7 @@ private class TagInterpreterImpl(private val interpretation: Interpretation) : T
|
||||
|
||||
private fun makePreAlbum(
|
||||
parsedTags: ParsedTags,
|
||||
deviceFile: DeviceFile,
|
||||
deviceFile: File,
|
||||
individualPreArtists: List<PreArtist>,
|
||||
albumPreArtists: List<PreArtist>,
|
||||
interpretation: Interpretation
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Auxio Project
|
||||
* UpdateTracker.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.track
|
||||
|
||||
import android.content.Context
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
|
||||
interface UpdateTracker {
|
||||
fun track(locations: List<Location.Opened>)
|
||||
|
||||
fun release()
|
||||
|
||||
interface Callback {
|
||||
fun onUpdate(location: Location.Opened)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, callback: Callback): UpdateTracker =
|
||||
UpdateTrackerImpl(context, callback)
|
||||
}
|
||||
}
|
||||
|
||||
private class UpdateTrackerImpl(
|
||||
private val context: Context,
|
||||
private val callback: UpdateTracker.Callback
|
||||
) : UpdateTracker {
|
||||
private val observers = mutableListOf<LocationObserver>()
|
||||
|
||||
override fun track(locations: List<Location.Opened>) {
|
||||
release()
|
||||
observers.addAll(locations.map { LocationObserver(context, it, callback) })
|
||||
}
|
||||
|
||||
override fun release() {
|
||||
observers.forEach { it.release() }
|
||||
observers.clear()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user