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.MutableCovers
|
||||
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
|
||||
|
||||
class NullCovers(private val storage: CoverStorage) : MutableCovers<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>) {
|
||||
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.Storage
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.fs.mediastore.MediaStoreFS
|
||||
import org.oxycblt.musikr.fs.saf.SAF
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
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_auto_sort_names),
|
||||
getString(R.string.set_key_with_hidden),
|
||||
getString(R.string.set_key_exclude_non_music),
|
||||
getString(R.string.set_key_fs_cache) -> {
|
||||
getString(R.string.set_key_exclude_non_music) -> {
|
||||
L.d("Dispatching indexing setting change for $key")
|
||||
listener.onIndexingSettingChanged()
|
||||
}
|
||||
|
||||
@@ -32,47 +32,36 @@ import org.oxycblt.auxio.ForegroundServiceNotification
|
||||
import org.oxycblt.auxio.music.IndexingState
|
||||
import org.oxycblt.auxio.music.MusicRepository
|
||||
import org.oxycblt.auxio.music.MusicSettings
|
||||
import org.oxycblt.auxio.music.shim.UpdateTrackerFactory
|
||||
import org.oxycblt.auxio.playback.state.PlaybackStateManager
|
||||
import org.oxycblt.auxio.util.getSystemServiceCompat
|
||||
import org.oxycblt.musikr.MusicParent
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
import org.oxycblt.musikr.track.UpdateTracker
|
||||
import org.oxycblt.musikr.fs.saf.SAF
|
||||
import timber.log.Timber as L
|
||||
|
||||
class IndexingHolder
|
||||
private constructor(
|
||||
workerContext: Context,
|
||||
private val workerContext: Context,
|
||||
private val foregroundListener: ForegroundListener,
|
||||
private val playbackManager: PlaybackStateManager,
|
||||
private val musicRepository: MusicRepository,
|
||||
private val musicSettings: MusicSettings,
|
||||
private val imageLoader: ImageLoader,
|
||||
updateTrackerFactory: UpdateTrackerFactory
|
||||
private val imageLoader: ImageLoader
|
||||
) :
|
||||
MusicRepository.IndexingWorker,
|
||||
MusicRepository.IndexingListener,
|
||||
MusicRepository.UpdateListener,
|
||||
MusicSettings.Listener,
|
||||
UpdateTracker.Callback {
|
||||
MusicSettings.Listener {
|
||||
class Factory
|
||||
@Inject
|
||||
constructor(
|
||||
private val playbackManager: PlaybackStateManager,
|
||||
private val musicRepository: MusicRepository,
|
||||
private val musicSettings: MusicSettings,
|
||||
private val imageLoader: ImageLoader,
|
||||
private val updateTrackerFactory: UpdateTrackerFactory
|
||||
private val imageLoader: ImageLoader
|
||||
) {
|
||||
fun create(context: Context, listener: ForegroundListener) =
|
||||
IndexingHolder(
|
||||
context,
|
||||
listener,
|
||||
playbackManager,
|
||||
musicRepository,
|
||||
musicSettings,
|
||||
imageLoader,
|
||||
updateTrackerFactory)
|
||||
context, listener, playbackManager, musicRepository, musicSettings, imageLoader)
|
||||
}
|
||||
|
||||
private val indexJob = Job()
|
||||
@@ -85,18 +74,18 @@ private constructor(
|
||||
.getSystemServiceCompat(PowerManager::class)
|
||||
.newWakeLock(
|
||||
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":IndexingComponent")
|
||||
private val updateTracker = updateTrackerFactory.create(this)
|
||||
private var trackingJob: Job? = null
|
||||
|
||||
fun attach() {
|
||||
musicSettings.registerListener(this)
|
||||
musicRepository.addUpdateListener(this)
|
||||
musicRepository.addIndexingListener(this)
|
||||
musicRepository.registerWorker(this)
|
||||
updateTracker.track(musicSettings.musicLocations)
|
||||
startTracking()
|
||||
}
|
||||
|
||||
fun release() {
|
||||
updateTracker.release()
|
||||
stopTracking()
|
||||
musicRepository.unregisterWorker(this)
|
||||
musicRepository.removeIndexingListener(this)
|
||||
musicRepository.removeUpdateListener(this)
|
||||
@@ -168,15 +157,30 @@ private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUpdate(location: Location.Opened) {
|
||||
if (musicSettings.shouldBeObserving) {
|
||||
musicRepository.requestIndex(true)
|
||||
}
|
||||
private fun startTracking() {
|
||||
stopTracking()
|
||||
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() {
|
||||
super.onMusicLocationsChanged()
|
||||
updateTracker.track(musicSettings.musicLocations)
|
||||
startTracking()
|
||||
musicRepository.requestIndex(true)
|
||||
}
|
||||
|
||||
|
||||
@@ -39,8 +39,4 @@ class MusikrShimModule {
|
||||
@Singleton
|
||||
@Provides
|
||||
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.CachedSong
|
||||
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 {
|
||||
override suspend fun read(file: DeviceFile): CacheResult {
|
||||
override suspend fun read(file: File): CacheResult {
|
||||
return when (val result = inner.read(file)) {
|
||||
is CacheResult.Hit -> CacheResult.Stale(file, result.song.addedMs)
|
||||
else -> result
|
||||
|
||||
Reference in New Issue
Block a user