musikr: refactor shared tracking system
This commit is contained in:
@@ -22,13 +22,13 @@ import org.oxycblt.musikr.covers.Cover
|
|||||||
import org.oxycblt.musikr.covers.CoverResult
|
import org.oxycblt.musikr.covers.CoverResult
|
||||||
import org.oxycblt.musikr.covers.MutableCovers
|
import org.oxycblt.musikr.covers.MutableCovers
|
||||||
import org.oxycblt.musikr.covers.stored.CoverStorage
|
import org.oxycblt.musikr.covers.stored.CoverStorage
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.metadata.Metadata
|
import org.oxycblt.musikr.metadata.Metadata
|
||||||
|
|
||||||
class NullCovers(private val storage: CoverStorage) : MutableCovers<NullCover> {
|
class NullCovers(private val storage: CoverStorage) : MutableCovers<NullCover> {
|
||||||
override suspend fun obtain(id: String) = CoverResult.Hit(NullCover)
|
override suspend fun obtain(id: String) = CoverResult.Hit(NullCover)
|
||||||
|
|
||||||
override suspend fun create(file: DeviceFile, metadata: Metadata) = CoverResult.Hit(NullCover)
|
override suspend fun create(file: File, metadata: Metadata) = CoverResult.Hit(NullCover)
|
||||||
|
|
||||||
override suspend fun cleanup(excluding: Collection<Cover>) {
|
override suspend fun cleanup(excluding: Collection<Cover>) {
|
||||||
storage.ls(setOf()).map { storage.rm(it) }
|
storage.ls(setOf()).map { storage.rm(it) }
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ import org.oxycblt.musikr.Playlist
|
|||||||
import org.oxycblt.musikr.Song
|
import org.oxycblt.musikr.Song
|
||||||
import org.oxycblt.musikr.Storage
|
import org.oxycblt.musikr.Storage
|
||||||
import org.oxycblt.musikr.cache.MutableCache
|
import org.oxycblt.musikr.cache.MutableCache
|
||||||
import org.oxycblt.musikr.fs.mediastore.MediaStoreFS
|
|
||||||
import org.oxycblt.musikr.fs.saf.SAF
|
import org.oxycblt.musikr.fs.saf.SAF
|
||||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||||
import org.oxycblt.musikr.tag.interpret.Naming
|
import org.oxycblt.musikr.tag.interpret.Naming
|
||||||
|
|||||||
@@ -150,8 +150,7 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
|
|||||||
getString(R.string.set_key_separators),
|
getString(R.string.set_key_separators),
|
||||||
getString(R.string.set_key_auto_sort_names),
|
getString(R.string.set_key_auto_sort_names),
|
||||||
getString(R.string.set_key_with_hidden),
|
getString(R.string.set_key_with_hidden),
|
||||||
getString(R.string.set_key_exclude_non_music),
|
getString(R.string.set_key_exclude_non_music) -> {
|
||||||
getString(R.string.set_key_fs_cache) -> {
|
|
||||||
L.d("Dispatching indexing setting change for $key")
|
L.d("Dispatching indexing setting change for $key")
|
||||||
listener.onIndexingSettingChanged()
|
listener.onIndexingSettingChanged()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,47 +32,36 @@ import org.oxycblt.auxio.ForegroundServiceNotification
|
|||||||
import org.oxycblt.auxio.music.IndexingState
|
import org.oxycblt.auxio.music.IndexingState
|
||||||
import org.oxycblt.auxio.music.MusicRepository
|
import org.oxycblt.auxio.music.MusicRepository
|
||||||
import org.oxycblt.auxio.music.MusicSettings
|
import org.oxycblt.auxio.music.MusicSettings
|
||||||
import org.oxycblt.auxio.music.shim.UpdateTrackerFactory
|
|
||||||
import org.oxycblt.auxio.playback.state.PlaybackStateManager
|
import org.oxycblt.auxio.playback.state.PlaybackStateManager
|
||||||
import org.oxycblt.auxio.util.getSystemServiceCompat
|
import org.oxycblt.auxio.util.getSystemServiceCompat
|
||||||
import org.oxycblt.musikr.MusicParent
|
import org.oxycblt.musikr.MusicParent
|
||||||
import org.oxycblt.musikr.fs.Location
|
import org.oxycblt.musikr.fs.saf.SAF
|
||||||
import org.oxycblt.musikr.track.UpdateTracker
|
|
||||||
import timber.log.Timber as L
|
import timber.log.Timber as L
|
||||||
|
|
||||||
class IndexingHolder
|
class IndexingHolder
|
||||||
private constructor(
|
private constructor(
|
||||||
workerContext: Context,
|
private val workerContext: Context,
|
||||||
private val foregroundListener: ForegroundListener,
|
private val foregroundListener: ForegroundListener,
|
||||||
private val playbackManager: PlaybackStateManager,
|
private val playbackManager: PlaybackStateManager,
|
||||||
private val musicRepository: MusicRepository,
|
private val musicRepository: MusicRepository,
|
||||||
private val musicSettings: MusicSettings,
|
private val musicSettings: MusicSettings,
|
||||||
private val imageLoader: ImageLoader,
|
private val imageLoader: ImageLoader
|
||||||
updateTrackerFactory: UpdateTrackerFactory
|
|
||||||
) :
|
) :
|
||||||
MusicRepository.IndexingWorker,
|
MusicRepository.IndexingWorker,
|
||||||
MusicRepository.IndexingListener,
|
MusicRepository.IndexingListener,
|
||||||
MusicRepository.UpdateListener,
|
MusicRepository.UpdateListener,
|
||||||
MusicSettings.Listener,
|
MusicSettings.Listener {
|
||||||
UpdateTracker.Callback {
|
|
||||||
class Factory
|
class Factory
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
private val playbackManager: PlaybackStateManager,
|
private val playbackManager: PlaybackStateManager,
|
||||||
private val musicRepository: MusicRepository,
|
private val musicRepository: MusicRepository,
|
||||||
private val musicSettings: MusicSettings,
|
private val musicSettings: MusicSettings,
|
||||||
private val imageLoader: ImageLoader,
|
private val imageLoader: ImageLoader
|
||||||
private val updateTrackerFactory: UpdateTrackerFactory
|
|
||||||
) {
|
) {
|
||||||
fun create(context: Context, listener: ForegroundListener) =
|
fun create(context: Context, listener: ForegroundListener) =
|
||||||
IndexingHolder(
|
IndexingHolder(
|
||||||
context,
|
context, listener, playbackManager, musicRepository, musicSettings, imageLoader)
|
||||||
listener,
|
|
||||||
playbackManager,
|
|
||||||
musicRepository,
|
|
||||||
musicSettings,
|
|
||||||
imageLoader,
|
|
||||||
updateTrackerFactory)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val indexJob = Job()
|
private val indexJob = Job()
|
||||||
@@ -85,18 +74,18 @@ private constructor(
|
|||||||
.getSystemServiceCompat(PowerManager::class)
|
.getSystemServiceCompat(PowerManager::class)
|
||||||
.newWakeLock(
|
.newWakeLock(
|
||||||
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":IndexingComponent")
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":IndexingComponent")
|
||||||
private val updateTracker = updateTrackerFactory.create(this)
|
private var trackingJob: Job? = null
|
||||||
|
|
||||||
fun attach() {
|
fun attach() {
|
||||||
musicSettings.registerListener(this)
|
musicSettings.registerListener(this)
|
||||||
musicRepository.addUpdateListener(this)
|
musicRepository.addUpdateListener(this)
|
||||||
musicRepository.addIndexingListener(this)
|
musicRepository.addIndexingListener(this)
|
||||||
musicRepository.registerWorker(this)
|
musicRepository.registerWorker(this)
|
||||||
updateTracker.track(musicSettings.musicLocations)
|
startTracking()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun release() {
|
fun release() {
|
||||||
updateTracker.release()
|
stopTracking()
|
||||||
musicRepository.unregisterWorker(this)
|
musicRepository.unregisterWorker(this)
|
||||||
musicRepository.removeIndexingListener(this)
|
musicRepository.removeIndexingListener(this)
|
||||||
musicRepository.removeUpdateListener(this)
|
musicRepository.removeUpdateListener(this)
|
||||||
@@ -168,15 +157,30 @@ private constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onUpdate(location: Location.Opened) {
|
private fun startTracking() {
|
||||||
if (musicSettings.shouldBeObserving) {
|
stopTracking()
|
||||||
musicRepository.requestIndex(true)
|
val locations = musicSettings.musicLocations
|
||||||
}
|
if (locations.isEmpty()) return
|
||||||
|
|
||||||
|
trackingJob =
|
||||||
|
indexScope.launch {
|
||||||
|
val query = SAF.Query(source = locations, exclude = emptyList(), withHidden = false)
|
||||||
|
SAF.from(workerContext, query).track().collect {
|
||||||
|
if (musicSettings.shouldBeObserving) {
|
||||||
|
musicRepository.requestIndex(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopTracking() {
|
||||||
|
trackingJob?.cancel()
|
||||||
|
trackingJob = null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onMusicLocationsChanged() {
|
override fun onMusicLocationsChanged() {
|
||||||
super.onMusicLocationsChanged()
|
super.onMusicLocationsChanged()
|
||||||
updateTracker.track(musicSettings.musicLocations)
|
startTracking()
|
||||||
musicRepository.requestIndex(true)
|
musicRepository.requestIndex(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,8 +39,4 @@ class MusikrShimModule {
|
|||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun storedPlaylists(@ApplicationContext context: Context) = StoredPlaylists.from(context)
|
fun storedPlaylists(@ApplicationContext context: Context) = StoredPlaylists.from(context)
|
||||||
|
|
||||||
@Provides
|
|
||||||
fun updateTrackerFactory(@ApplicationContext context: Context): UpdateTrackerFactory =
|
|
||||||
UpdateTrackerFactoryImpl(context)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2025 Auxio Project
|
|
||||||
* UpdateTrackerFactory.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.auxio.music.shim
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import org.oxycblt.musikr.track.UpdateTracker
|
|
||||||
|
|
||||||
interface UpdateTrackerFactory {
|
|
||||||
fun create(callback: UpdateTracker.Callback): UpdateTracker
|
|
||||||
}
|
|
||||||
|
|
||||||
class UpdateTrackerFactoryImpl(private val context: Context) : UpdateTrackerFactory {
|
|
||||||
override fun create(callback: UpdateTracker.Callback) = UpdateTracker.from(context, callback)
|
|
||||||
}
|
|
||||||
@@ -21,10 +21,10 @@ package org.oxycblt.auxio.music.shim
|
|||||||
import org.oxycblt.musikr.cache.CacheResult
|
import org.oxycblt.musikr.cache.CacheResult
|
||||||
import org.oxycblt.musikr.cache.CachedSong
|
import org.oxycblt.musikr.cache.CachedSong
|
||||||
import org.oxycblt.musikr.cache.MutableCache
|
import org.oxycblt.musikr.cache.MutableCache
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.File
|
||||||
|
|
||||||
class WriteOnlyMutableCache(private val inner: MutableCache) : MutableCache {
|
class WriteOnlyMutableCache(private val inner: MutableCache) : MutableCache {
|
||||||
override suspend fun read(file: DeviceFile): CacheResult {
|
override suspend fun read(file: File): CacheResult {
|
||||||
return when (val result = inner.read(file)) {
|
return when (val result = inner.read(file)) {
|
||||||
is CacheResult.Hit -> CacheResult.Stale(file, result.song.addedMs)
|
is CacheResult.Hit -> CacheResult.Stale(file, result.song.addedMs)
|
||||||
else -> result
|
else -> result
|
||||||
|
|||||||
@@ -38,13 +38,6 @@
|
|||||||
app:summary="@string/set_excluded_locations_desc"
|
app:summary="@string/set_excluded_locations_desc"
|
||||||
app:title="@string/set_excluded_locations" />
|
app:title="@string/set_excluded_locations" />
|
||||||
|
|
||||||
|
|
||||||
<SwitchPreferenceCompat
|
|
||||||
app:key="@string/set_key_fs_cache"
|
|
||||||
app:summary="@string/set_fs_cache_desc"
|
|
||||||
app:title="@string/set_fs_cache"
|
|
||||||
app:defaultValue="false" />
|
|
||||||
|
|
||||||
<Preference
|
<Preference
|
||||||
app:key="@string/set_key_reindex"
|
app:key="@string/set_key_reindex"
|
||||||
app:summary="@string/set_reindex_desc"
|
app:summary="@string/set_reindex_desc"
|
||||||
|
|||||||
+9
-9
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
package org.oxycblt.musikr.cache
|
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.metadata.Properties
|
||||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
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
|
* Read a [CachedSong] corresponding to the given [file] from the cache. This can result in
|
||||||
* several outcomes represented by [CacheResult].
|
* 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.
|
* @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.
|
* Write a [CachedSong] to the cache.
|
||||||
*
|
*
|
||||||
* This should commit the metadata to the repository in such a way that it can be retrieved
|
* 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
|
* @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. */
|
/** A cached song entry containing the data needed by the rest of the loader. */
|
||||||
data class CachedSong(
|
data class CachedSong(
|
||||||
/** The file this song corresponds to. */
|
/** The file this song corresponds to. */
|
||||||
val file: DeviceFile,
|
val file: File,
|
||||||
/** The properties of the song. */
|
/** The properties of the song. */
|
||||||
val properties: Properties,
|
val properties: Properties,
|
||||||
/** The parsed tags of the song. */
|
/** The parsed tags of the song. */
|
||||||
@@ -107,15 +107,15 @@ sealed interface CacheResult {
|
|||||||
/**
|
/**
|
||||||
* A cache entry was not found.
|
* 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.
|
* 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.
|
* @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.CacheResult
|
||||||
import org.oxycblt.musikr.cache.CachedSong
|
import org.oxycblt.musikr.cache.CachedSong
|
||||||
import org.oxycblt.musikr.cache.MutableCache
|
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.metadata.Properties
|
||||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ import org.oxycblt.musikr.tag.parse.ParsedTags
|
|||||||
* Create an instance with [from].
|
* Create an instance with [from].
|
||||||
*/
|
*/
|
||||||
class DBCache private constructor(private val readDao: CacheReadDao) : Cache {
|
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)
|
val dbSong = readDao.selectSong(file.uri.toString()) ?: return CacheResult.Miss(file)
|
||||||
if (dbSong.modifiedMs != file.modifiedMs) {
|
if (dbSong.modifiedMs != file.modifiedMs) {
|
||||||
return CacheResult.Stale(file, dbSong.addedMs)
|
return CacheResult.Stale(file, dbSong.addedMs)
|
||||||
@@ -94,7 +94,7 @@ class DBCache private constructor(private val readDao: CacheReadDao) : Cache {
|
|||||||
class MutableDBCache
|
class MutableDBCache
|
||||||
private constructor(private val inner: DBCache, private val writeDao: CacheWriteDao) :
|
private constructor(private val inner: DBCache, private val writeDao: CacheWriteDao) :
|
||||||
MutableCache {
|
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) {
|
override suspend fun write(cachedSong: CachedSong) {
|
||||||
val dbSong =
|
val dbSong =
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ package org.oxycblt.musikr.covers
|
|||||||
|
|
||||||
import android.os.ParcelFileDescriptor
|
import android.os.ParcelFileDescriptor
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.metadata.Metadata
|
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
|
* This could result in side-effect-laden storage, or be a simple translation into a lazily
|
||||||
* loaded [Cover] instance.
|
* 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.
|
* @param metadata The [Metadata] to use to create the cover.
|
||||||
* @return a [CoverResult] indicating whether the cover was created or not
|
* @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]
|
* 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.CoverResult
|
||||||
import org.oxycblt.musikr.covers.Covers
|
import org.oxycblt.musikr.covers.Covers
|
||||||
import org.oxycblt.musikr.covers.MutableCovers
|
import org.oxycblt.musikr.covers.MutableCovers
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.metadata.Metadata
|
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 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) {
|
for (cover in _many) {
|
||||||
val result = cover.create(file, metadata)
|
val result = cover.create(file, metadata)
|
||||||
if (result is CoverResult.Hit) {
|
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.CoverResult
|
||||||
import org.oxycblt.musikr.covers.MemoryCover
|
import org.oxycblt.musikr.covers.MemoryCover
|
||||||
import org.oxycblt.musikr.covers.MutableCovers
|
import org.oxycblt.musikr.covers.MutableCovers
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.metadata.Metadata
|
import org.oxycblt.musikr.metadata.Metadata
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,7 +44,7 @@ import org.oxycblt.musikr.metadata.Metadata
|
|||||||
class EmbeddedCovers(private val coverIdentifier: CoverIdentifier) : MutableCovers<MemoryCover> {
|
class EmbeddedCovers(private val coverIdentifier: CoverIdentifier) : MutableCovers<MemoryCover> {
|
||||||
override suspend fun obtain(id: String): CoverResult<MemoryCover> = CoverResult.Miss()
|
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 data = metadata.cover ?: return CoverResult.Miss()
|
||||||
val id = coverIdentifier.identify(data)
|
val id = coverIdentifier.identify(data)
|
||||||
return CoverResult.Hit(EmbeddedCover(id, 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.Covers
|
||||||
import org.oxycblt.musikr.covers.FDCover
|
import org.oxycblt.musikr.covers.FDCover
|
||||||
import org.oxycblt.musikr.covers.MutableCovers
|
import org.oxycblt.musikr.covers.MutableCovers
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.metadata.Metadata
|
import org.oxycblt.musikr.metadata.Metadata
|
||||||
|
|
||||||
private const val PREFIX = "mcf:"
|
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 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
|
// 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.
|
// query to finally finish to be able to have a complete list of siblings to search for.
|
||||||
val parent = file.parent.await()
|
val parent = file.parent.await()
|
||||||
val bestCover =
|
val bestCover =
|
||||||
parent.children
|
parent.children
|
||||||
.filterIsInstance<DeviceFile>()
|
.filterIsInstance<File>()
|
||||||
.map { it to coverArtScore(it) }
|
.map { it to coverArtScore(it) }
|
||||||
.maxBy { it.second }
|
.maxBy { it.second }
|
||||||
if (bestCover.second > 0) {
|
if (bestCover.second > 0) {
|
||||||
@@ -116,7 +116,7 @@ class MutableFSCovers(private val context: Context) : MutableCovers<FDCover> {
|
|||||||
// that should not be managed by the app
|
// 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)) {
|
if (!file.mimeType.startsWith("image/", ignoreCase = true)) {
|
||||||
// Not an image file. You lose!
|
// Not an image file. You lose!
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import org.oxycblt.musikr.covers.Covers
|
|||||||
import org.oxycblt.musikr.covers.FDCover
|
import org.oxycblt.musikr.covers.FDCover
|
||||||
import org.oxycblt.musikr.covers.MemoryCover
|
import org.oxycblt.musikr.covers.MemoryCover
|
||||||
import org.oxycblt.musikr.covers.MutableCovers
|
import org.oxycblt.musikr.covers.MutableCovers
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.metadata.Metadata
|
import org.oxycblt.musikr.metadata.Metadata
|
||||||
|
|
||||||
private const val PREFIX = "mcs:"
|
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 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 =
|
val memoryCover =
|
||||||
when (val cover = src.create(file, metadata)) {
|
when (val cover = src.create(file, metadata)) {
|
||||||
is CoverResult.Hit -> cover.cover
|
is CoverResult.Hit -> cover.cover
|
||||||
|
|||||||
@@ -23,26 +23,32 @@ import kotlinx.coroutines.Deferred
|
|||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
interface FS {
|
interface FS {
|
||||||
fun explore(): Flow<DeviceFile>
|
fun explore(): Flow<File>
|
||||||
|
|
||||||
|
fun track(): Flow<FSUpdate>
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed interface DeviceFSEntry {
|
sealed interface FSEntry {
|
||||||
val uri: Uri?
|
val uri: Uri?
|
||||||
val path: Path
|
val path: Path
|
||||||
}
|
}
|
||||||
|
|
||||||
data class DeviceDirectory(
|
data class Directory(
|
||||||
override val uri: Uri?,
|
override val uri: Uri?,
|
||||||
override val path: Path,
|
override val path: Path,
|
||||||
val parent: Deferred<DeviceDirectory>?,
|
val parent: Deferred<Directory>?,
|
||||||
var children: List<DeviceFile>
|
var children: List<File>
|
||||||
) : DeviceFSEntry
|
) : FSEntry
|
||||||
|
|
||||||
data class DeviceFile(
|
data class File(
|
||||||
override val uri: Uri,
|
override val uri: Uri,
|
||||||
override val path: Path,
|
override val path: Path,
|
||||||
val modifiedMs: Long,
|
val modifiedMs: Long,
|
||||||
val mimeType: String,
|
val mimeType: String,
|
||||||
val size: Long,
|
val size: Long,
|
||||||
val parent: Deferred<DeviceDirectory>
|
val parent: Deferred<Directory>
|
||||||
) : DeviceFSEntry
|
) : 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 androidx.core.database.getStringOrNull
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.yield
|
import kotlinx.coroutines.yield
|
||||||
import org.oxycblt.musikr.fs.DeviceDirectory
|
import org.oxycblt.musikr.fs.Directory
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
|
||||||
import org.oxycblt.musikr.fs.FS
|
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
|
||||||
import org.oxycblt.musikr.fs.path.MediaStorePathInterpreter
|
import org.oxycblt.musikr.fs.path.MediaStorePathInterpreter
|
||||||
import org.oxycblt.musikr.fs.path.VolumeManager
|
import org.oxycblt.musikr.fs.path.VolumeManager
|
||||||
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
||||||
import org.oxycblt.musikr.fs.saf.useQuery
|
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
|
* 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
|
class MediaStoreFS
|
||||||
private constructor(
|
private constructor(
|
||||||
@@ -48,84 +52,8 @@ private constructor(
|
|||||||
) : FS {
|
) : FS {
|
||||||
private val pathInterpreterFactory = MediaStorePathInterpreter.Factory.from(volumeManager)
|
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)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
override fun explore(): Flow<DeviceFile> = flow {
|
override fun explore(): Flow<File> = flow {
|
||||||
val projection = BASE_PROJECTION + pathInterpreterFactory.projection
|
val projection = BASE_PROJECTION + pathInterpreterFactory.projection
|
||||||
var selector = BASE_SELECTOR
|
var selector = BASE_SELECTOR
|
||||||
val args = mutableListOf<String>()
|
val args = mutableListOf<String>()
|
||||||
@@ -136,7 +64,7 @@ private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Collect all files and track unique directories
|
// Collect all files and track unique directories
|
||||||
val allFiles = mutableListOf<DeviceFile>()
|
val allFiles = mutableListOf<File>()
|
||||||
|
|
||||||
context.contentResolverSafe.useQuery(
|
context.contentResolverSafe.useQuery(
|
||||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||||
@@ -164,9 +92,9 @@ private constructor(
|
|||||||
cursor.getLong(dateModifiedIndex) * 1000 // Convert to milliseconds
|
cursor.getLong(dateModifiedIndex) * 1000 // Convert to milliseconds
|
||||||
|
|
||||||
// Create file with empty deferred parent
|
// Create file with empty deferred parent
|
||||||
val parentDeferred = CompletableDeferred<DeviceDirectory>()
|
val parentDeferred = CompletableDeferred<Directory>()
|
||||||
val deviceFile =
|
val deviceFile =
|
||||||
DeviceFile(
|
File(
|
||||||
uri = uri,
|
uri = uri,
|
||||||
path = path,
|
path = path,
|
||||||
modifiedMs = dateModified,
|
modifiedMs = dateModified,
|
||||||
@@ -184,6 +112,90 @@ private constructor(
|
|||||||
buildDirectoryHierarchy(allFiles)
|
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 {
|
companion object {
|
||||||
fun from(context: Context) =
|
fun from(context: Context) =
|
||||||
MediaStoreFS(
|
MediaStoreFS(
|
||||||
|
|||||||
@@ -25,22 +25,30 @@ import android.provider.DocumentsContract
|
|||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import kotlinx.coroutines.Deferred
|
import kotlinx.coroutines.Deferred
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.asFlow
|
import kotlinx.coroutines.flow.asFlow
|
||||||
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
import kotlinx.coroutines.flow.emitAll
|
import kotlinx.coroutines.flow.emitAll
|
||||||
import kotlinx.coroutines.flow.flatMapMerge
|
import kotlinx.coroutines.flow.flatMapMerge
|
||||||
import kotlinx.coroutines.flow.flattenMerge
|
import kotlinx.coroutines.flow.flattenMerge
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import org.oxycblt.musikr.fs.DeviceDirectory
|
import org.oxycblt.musikr.fs.Directory
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
|
||||||
import org.oxycblt.musikr.fs.FS
|
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.Location
|
||||||
import org.oxycblt.musikr.fs.Path
|
import org.oxycblt.musikr.fs.Path
|
||||||
|
import org.oxycblt.musikr.fs.track.LocationObserver
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
class SAF
|
class SAF
|
||||||
private constructor(private val contentResolver: ContentResolver, private val query: Query) : FS {
|
private constructor(
|
||||||
override fun explore(): Flow<DeviceFile> =
|
private val context: Context,
|
||||||
|
private val contentResolver: ContentResolver,
|
||||||
|
private val query: Query
|
||||||
|
) : FS {
|
||||||
|
override fun explore(): Flow<File> =
|
||||||
query.source.asFlow().flatMapMerge { location ->
|
query.source.asFlow().flatMapMerge { location ->
|
||||||
exploreDirectoryImpl(
|
exploreDirectoryImpl(
|
||||||
location.uri,
|
location.uri,
|
||||||
@@ -50,18 +58,32 @@ private constructor(private val contentResolver: ContentResolver, private val qu
|
|||||||
query.exclude.mapTo(mutableSetOf()) { it.uri })
|
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(
|
private fun exploreDirectoryImpl(
|
||||||
rootUri: Uri,
|
rootUri: Uri,
|
||||||
treeDocumentId: String,
|
treeDocumentId: String,
|
||||||
relativePath: Path,
|
relativePath: Path,
|
||||||
parent: Deferred<DeviceDirectory>?,
|
parent: Deferred<Directory>?,
|
||||||
exclude: Set<Uri>
|
exclude: Set<Uri>
|
||||||
): Flow<DeviceFile> = flow {
|
): Flow<File> = flow {
|
||||||
// Make a kotlin future
|
// Make a kotlin future
|
||||||
val uri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, treeDocumentId)
|
val uri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, treeDocumentId)
|
||||||
val directoryDeferred = CompletableDeferred<DeviceDirectory>()
|
val directoryDeferred = CompletableDeferred<Directory>()
|
||||||
val recursive = mutableListOf<Flow<DeviceFile>>()
|
val recursive = mutableListOf<Flow<File>>()
|
||||||
val children = mutableListOf<DeviceFile>()
|
val children = mutableListOf<File>()
|
||||||
contentResolver.useQuery(uri, PROJECTION) { cursor ->
|
contentResolver.useQuery(uri, PROJECTION) { cursor ->
|
||||||
val childUriIndex =
|
val childUriIndex =
|
||||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
||||||
@@ -99,7 +121,7 @@ private constructor(private val contentResolver: ContentResolver, private val qu
|
|||||||
} else {
|
} else {
|
||||||
val size = cursor.getLong(sizeIndex)
|
val size = cursor.getLong(sizeIndex)
|
||||||
val file =
|
val file =
|
||||||
DeviceFile(
|
File(
|
||||||
uri = childUri,
|
uri = childUri,
|
||||||
mimeType = mimeType,
|
mimeType = mimeType,
|
||||||
path = newPath,
|
path = newPath,
|
||||||
@@ -110,7 +132,7 @@ private constructor(private val contentResolver: ContentResolver, private val qu
|
|||||||
emit(file)
|
emit(file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
directoryDeferred.complete(DeviceDirectory(uri, relativePath, parent, children))
|
directoryDeferred.complete(Directory(uri, relativePath, parent, children))
|
||||||
emitAll(recursive.asFlow().flattenMerge())
|
emitAll(recursive.asFlow().flattenMerge())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,7 +144,7 @@ private constructor(private val contentResolver: ContentResolver, private val qu
|
|||||||
)
|
)
|
||||||
|
|
||||||
companion object {
|
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 =
|
private val PROJECTION =
|
||||||
arrayOf(
|
arrayOf(
|
||||||
|
|||||||
+6
-6
@@ -16,23 +16,23 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* 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.content.Context
|
||||||
import android.database.ContentObserver
|
import android.database.ContentObserver
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import org.oxycblt.musikr.fs.Location
|
|
||||||
|
|
||||||
internal class LocationObserver(
|
internal class LocationObserver(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val location: Location.Opened,
|
private val uri: Uri,
|
||||||
private val listener: UpdateTracker.Callback
|
private val onUpdate: () -> Unit
|
||||||
) : ContentObserver(Handler(Looper.getMainLooper())), Runnable {
|
) : ContentObserver(Handler(Looper.getMainLooper())), Runnable {
|
||||||
private val handler = Handler(Looper.getMainLooper())
|
private val handler = Handler(Looper.getMainLooper())
|
||||||
|
|
||||||
init {
|
init {
|
||||||
context.applicationContext.contentResolver.registerContentObserver(location.uri, true, this)
|
context.applicationContext.contentResolver.registerContentObserver(uri, true, this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun release() {
|
fun release() {
|
||||||
@@ -47,7 +47,7 @@ internal class LocationObserver(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
listener.onUpdate(location)
|
onUpdate()
|
||||||
}
|
}
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
@@ -23,10 +23,10 @@ import android.content.Context
|
|||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.File
|
||||||
|
|
||||||
internal interface MetadataExtractor {
|
internal interface MetadataExtractor {
|
||||||
suspend fun extract(deviceFile: DeviceFile): Metadata?
|
suspend fun extract(deviceFile: File): Metadata?
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun from(context: Context): MetadataExtractor =
|
fun from(context: Context): MetadataExtractor =
|
||||||
@@ -36,7 +36,7 @@ internal interface MetadataExtractor {
|
|||||||
|
|
||||||
private class MetadataExtractorImpl(private val contentResolver: ContentResolver) :
|
private class MetadataExtractorImpl(private val contentResolver: ContentResolver) :
|
||||||
MetadataExtractor {
|
MetadataExtractor {
|
||||||
override suspend fun extract(deviceFile: DeviceFile): Metadata? =
|
override suspend fun extract(deviceFile: File): Metadata? =
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
contentResolver.openFileDescriptor(deviceFile.uri, "r")?.use { fd ->
|
contentResolver.openFileDescriptor(deviceFile.uri, "r")?.use { fd ->
|
||||||
val fis = FileInputStream(fd.fileDescriptor)
|
val fis = FileInputStream(fd.fileDescriptor)
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ package org.oxycblt.musikr.metadata
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.nio.ByteBuffer
|
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
|
private val channel = fis.channel
|
||||||
|
|
||||||
fun name() = requireNotNull(deviceFile.path.name)
|
fun name() = requireNotNull(deviceFile.path.name)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
package org.oxycblt.musikr.metadata
|
package org.oxycblt.musikr.metadata
|
||||||
|
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.File
|
||||||
|
|
||||||
internal object TagLibJNI {
|
internal object TagLibJNI {
|
||||||
init {
|
init {
|
||||||
@@ -31,7 +31,7 @@ internal object TagLibJNI {
|
|||||||
*
|
*
|
||||||
* Note: This method is blocking and should be handled as such if calling from a coroutine.
|
* 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 inputStream = NativeInputStream(deviceFile, fis)
|
||||||
val tag = openNative(inputStream)
|
val tag = openNative(inputStream)
|
||||||
inputStream.close()
|
inputStream.close()
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
package org.oxycblt.musikr.pipeline
|
package org.oxycblt.musikr.pipeline
|
||||||
|
|
||||||
import org.oxycblt.musikr.covers.Cover
|
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.metadata.Properties
|
||||||
import org.oxycblt.musikr.playlist.PlaylistFile
|
import org.oxycblt.musikr.playlist.PlaylistFile
|
||||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||||
@@ -36,7 +36,7 @@ internal sealed interface Explored : PipelineItem {
|
|||||||
sealed interface Known : Explored, Complete
|
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 {
|
internal sealed interface Extracted : PipelineItem {
|
||||||
sealed interface Valid : Complete, Extracted
|
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 RawPlaylist(val file: PlaylistFile) : Explored.Known, Extracted.Valid
|
||||||
|
|
||||||
internal data class RawSong(
|
internal data class RawSong(
|
||||||
val file: DeviceFile,
|
val file: File,
|
||||||
val properties: Properties,
|
val properties: Properties,
|
||||||
val tags: ParsedTags,
|
val tags: ParsedTags,
|
||||||
val cover: Cover?,
|
val cover: Cover?,
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ package org.oxycblt.musikr.tag.interpret
|
|||||||
|
|
||||||
import org.oxycblt.musikr.Interpretation
|
import org.oxycblt.musikr.Interpretation
|
||||||
import org.oxycblt.musikr.Music
|
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.fs.Format
|
||||||
import org.oxycblt.musikr.pipeline.RawSong
|
import org.oxycblt.musikr.pipeline.RawSong
|
||||||
import org.oxycblt.musikr.tag.Disc
|
import org.oxycblt.musikr.tag.Disc
|
||||||
@@ -151,7 +151,7 @@ private class TagInterpreterImpl(private val interpretation: Interpretation) : T
|
|||||||
|
|
||||||
private fun makePreAlbum(
|
private fun makePreAlbum(
|
||||||
parsedTags: ParsedTags,
|
parsedTags: ParsedTags,
|
||||||
deviceFile: DeviceFile,
|
deviceFile: File,
|
||||||
individualPreArtists: List<PreArtist>,
|
individualPreArtists: List<PreArtist>,
|
||||||
albumPreArtists: List<PreArtist>,
|
albumPreArtists: List<PreArtist>,
|
||||||
interpretation: Interpretation
|
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