From 29efd6a100e3f80b2bad972e4bd87dc25350d7fa Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Fri, 4 Jul 2025 13:51:16 -0600 Subject: [PATCH] music: fixup mediastore query setup --- .../oxycblt/auxio/music/MusicRepository.kt | 13 ++- .../org/oxycblt/auxio/music/MusicSettings.kt | 28 ++++--- .../auxio/music/locations/LocationConfig.kt | 80 ------------------- .../musikr/fs/mediastore/MediaStore.kt | 36 +++++---- 4 files changed, 47 insertions(+), 110 deletions(-) delete mode 100644 app/src/main/java/org/oxycblt/auxio/music/locations/LocationConfig.kt diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt index 4093c95c0..d670adc34 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt @@ -387,18 +387,17 @@ constructor( } else { Naming.simple() } - val locations = musicSettings.musicLocations - val excludedLocations = musicSettings.excludedLocations - val withHidden = musicSettings.withHidden val currentRevision = musicSettings.revision val newRevision = currentRevision?.takeIf { withCache } ?: UUID.randomUUID() val cache = if (withCache) cache else WriteOnlyMutableCache(cache) val covers = settingCovers.mutate(context, newRevision) - val fs = when (musicSettings.locationMode) { - MusicLocationMode.SAF -> SAF.from(context, musicSettings.safQuery ?: return) - MusicLocationMode.SYSTEM -> MediaStoreFS.from(context, musicSettings.mediaStoreQuery ?: return) - } + val fs = + when (musicSettings.locationMode) { + MusicLocationMode.SAF -> SAF.from(context, musicSettings.safQuery ?: return) + MusicLocationMode.SYSTEM -> + MediaStoreFS.from(context, musicSettings.mediaStoreQuery ?: return) + } val storage = Storage(cache, covers, storedPlaylists) val interpretation = Interpretation(nameFactory, separators) val config = Config(fs, storage, interpretation) diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt index cb44c997a..e93439b50 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt @@ -31,9 +31,7 @@ import org.oxycblt.musikr.fs.mediastore.MediaStoreFS import org.oxycblt.musikr.fs.saf.SAF import timber.log.Timber as L -/** - * Represents the mode for loading music locations. - */ +/** Represents the mode for loading music locations. */ enum class MusicLocationMode { /** Use Storage Access Framework (file picker) to select specific folders */ SAF, @@ -65,6 +63,12 @@ interface MusicSettings : Settings { val useFileTreeCache: Boolean /** The mode for loading music locations (SAF or System database). */ var locationMode: MusicLocationMode + /** The list of music locations to include when using SAF mode. */ + var musicLocations: List + /** The list of locations to exclude. */ + var excludedLocations: List + /** Whether to exclude non-music files when using MediaStore. */ + val excludeNonMusic: Boolean interface Listener { /** Called when the current music locations changed. */ @@ -141,13 +145,19 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont // MediaStore query is used when in system database mode if (locationMode != MusicLocationMode.SYSTEM) return null + // Determine filter mode based on whether we have excluded locations + val filterMode = + if (excludedLocations.isEmpty()) { + MediaStoreFS.FilterMode.INCLUDE + } else { + MediaStoreFS.FilterMode.EXCLUDE + } + return MediaStoreFS.Query( - include = emptyList(), // No include filter in system database mode - exclude = excludedLocations, - excludeNonMusic = excludeNonMusic) + mode = filterMode, filtered = excludedLocations, excludeNonMusic = excludeNonMusic) } - private var musicLocations: List + override var musicLocations: List get() { val locations = sharedPreferences.getString(getString(R.string.set_key_music_locations), null) @@ -164,7 +174,7 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont } } - private var excludedLocations: List + override var excludedLocations: List get() { val locations = sharedPreferences.getString(getString(R.string.set_key_excluded_locations), null) @@ -180,7 +190,7 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont } } - private val excludeNonMusic: Boolean + override val excludeNonMusic: Boolean get() = sharedPreferences.getBoolean(getString(R.string.set_key_exclude_non_music), true) override fun onSettingChanged(key: String, listener: MusicSettings.Listener) { diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/LocationConfig.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationConfig.kt deleted file mode 100644 index d2032e094..000000000 --- a/app/src/main/java/org/oxycblt/auxio/music/locations/LocationConfig.kt +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2025 Auxio Project - * LocationConfig.kt is part of Auxio. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.oxycblt.auxio.music.locations - -import android.content.Context -import androidx.core.net.toUri -import org.oxycblt.auxio.util.splitEscaped -import org.oxycblt.musikr.fs.Location -import org.oxycblt.musikr.fs.mediastore.MediaStoreFS -import org.oxycblt.musikr.fs.saf.SAF - -sealed interface LocationConfig { - data class Filesystem(private val query: SAF.Query) : LocationConfig - - data class SystemDatabase(private val query: MediaStoreFS.Query) : LocationConfig - - companion object { - fun from(context: Context, string: String): LocationConfig? { - val typeSplit = string.splitEscaped('!') - if (typeSplit.size != 2) return null - val type = typeSplit[0] - val args = typeSplit[1].splitEscaped('|') - return when (type) { - "saf" -> { - if (args.size < 3) return null - val source = - args[0] - .splitEscaped(';') - .map { it.toUri() } - .mapNotNull { Location.Unopened.from(context, it)?.open(context) } - val exclude = - args[1] - .splitEscaped(';') - .map { it.toUri() } - .mapNotNull { Location.Unopened.from(context, it) } - val withHidden = args[2].toBoolean() - val query = - SAF.Query(source = source, exclude = exclude, withHidden = withHidden) - Filesystem(query = query) - } - "media" -> { - if (args.size < 3) return null - val include = - args[0] - .splitEscaped(';') - .map { it.toUri() } - .mapNotNull { Location.Unopened.from(context, it) } - val exclude = - args[1] - .splitEscaped(';') - .map { it.toUri() } - .mapNotNull { Location.Unopened.from(context, it) } - val excludeNonMusic = args[1].toBoolean() - SystemDatabase( - MediaStoreFS.Query( - include = include, - exclude = exclude, - excludeNonMusic = excludeNonMusic)) - } - else -> null - } - } - } -} diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/mediastore/MediaStore.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/mediastore/MediaStore.kt index 1ac3cdef7..c0db46ff2 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/mediastore/MediaStore.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/mediastore/MediaStore.kt @@ -65,19 +65,22 @@ private constructor( } // Handle include/exclude directories - if (query.include.isNotEmpty()) { - val pathSelector = pathInterpreterFactory.createSelector(query.include.map { it.path }) - if (pathSelector != null) { - selector += " AND (${pathSelector.template})" - args.addAll(pathSelector.args) + when (query.mode) { + FilterMode.INCLUDE -> { + val pathSelector = + pathInterpreterFactory.createSelector(query.filtered.map { it.path }) + if (pathSelector != null) { + selector += " AND (${pathSelector.template})" + args.addAll(pathSelector.args) + } } - } - - if (query.exclude.isNotEmpty()) { - val pathSelector = pathInterpreterFactory.createSelector(query.exclude.map { it.path }) - if (pathSelector != null) { - selector += " AND NOT (${pathSelector.template})" - args.addAll(pathSelector.args) + FilterMode.EXCLUDE -> { + val pathSelector = + pathInterpreterFactory.createSelector(query.filtered.map { it.path }) + if (pathSelector != null) { + selector += " AND NOT (${pathSelector.template})" + args.addAll(pathSelector.args) + } } } @@ -215,11 +218,16 @@ private constructor( } data class Query( - val include: List, - val exclude: List, + val mode: FilterMode, + val filtered: List, val excludeNonMusic: Boolean ) + enum class FilterMode { + INCLUDE, + EXCLUDE + } + companion object { fun from(context: Context, query: Query) = MediaStoreFS(