music: improve dialog/fix bugs

This commit is contained in:
Alexander Capehart
2025-07-04 15:17:21 -06:00
parent 29efd6a100
commit e8e6c2ee4d
10 changed files with 183 additions and 146 deletions
@@ -145,4 +145,12 @@ object IntegerTable {
const val COVER_MODE_SAVE_SPACE = 0xA125
/** CoverMode.AsIs */
const val COVER_MODE_AS_IS = 0xA126
/** LocationMode.SAF */
const val LOCATION_MODE_SAF = 0xA127
/** LocationMode.MediaStore */
const val LOCATION_MODE_MEDIA_STORE = 0xA128
/** LocationMode.FileSystem */
const val FILTER_MODE_INCLUDE = 0xA129
/** LocationMode.FileSystem */
const val FILTER_MODE_EXCLUDE = 0xA12A
}
@@ -29,6 +29,7 @@ import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import org.oxycblt.auxio.image.covers.SettingCovers
import org.oxycblt.auxio.music.MusicRepository.IndexingWorker
import org.oxycblt.auxio.music.locations.LocationMode
import org.oxycblt.auxio.music.shim.WriteOnlyMutableCache
import org.oxycblt.musikr.Config
import org.oxycblt.musikr.IndexingProgress
@@ -394,9 +395,9 @@ constructor(
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)
LocationMode.SAF -> SAF.from(context, musicSettings.safQuery)
LocationMode.MEDIA_STORE ->
MediaStoreFS.from(context, musicSettings.mediaStoreQuery)
}
val storage = Storage(cache, covers, storedPlaylists)
val interpretation = Interpretation(nameFactory, separators)
@@ -24,21 +24,16 @@ import androidx.core.net.toUri
import dagger.hilt.android.qualifiers.ApplicationContext
import java.util.UUID
import javax.inject.Inject
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.locations.LocationMode
import org.oxycblt.auxio.settings.Settings
import org.oxycblt.auxio.util.unlikelyToBeNull
import org.oxycblt.musikr.fs.Location
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. */
enum class MusicLocationMode {
/** Use Storage Access Framework (file picker) to select specific folders */
SAF,
/** Use system MediaStore database to load all music */
SYSTEM
}
/**
* User configuration specific to music system.
*
@@ -47,12 +42,12 @@ enum class MusicLocationMode {
interface MusicSettings : Settings<MusicSettings.Listener> {
/** The current library revision. */
var revision: UUID?
/** The mode for loading music locations (SAF or System database). */
var locationMode: LocationMode
/** The currently configured SAF query (if any) * */
val safQuery: SAF.Query?
var safQuery: SAF.Query
/** The currently configured MediaStore query (if any) * */
val mediaStoreQuery: MediaStoreFS.Query?
/** Whether to ignore hidden files and directories during music loading. */
val withHidden: Boolean
var mediaStoreQuery: MediaStoreFS.Query
/** Whether to be actively watching for changes in the music library. */
val shouldBeObserving: Boolean
/** A [String] of characters representing the desired characters to denote multi-value tags. */
@@ -61,14 +56,6 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
val intelligentSorting: Boolean
/** Whether to use the file-system cache for improved loading times. */
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<Location.Opened>
/** The list of locations to exclude. */
var excludedLocations: List<Location.Unopened>
/** Whether to exclude non-music files when using MediaStore. */
val excludeNonMusic: Boolean
interface Listener {
/** Called when the current music locations changed. */
@@ -95,9 +82,6 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
}
}
override val withHidden: Boolean
get() = sharedPreferences.getBoolean(getString(R.string.set_key_with_hidden), false)
override val shouldBeObserving: Boolean
get() = sharedPreferences.getBoolean(getString(R.string.set_key_observing), false)
@@ -118,81 +102,82 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
override val useFileTreeCache: Boolean
get() = sharedPreferences.getBoolean(getString(R.string.set_key_fs_cache), false)
override var locationMode: MusicLocationMode
override var locationMode: LocationMode
get() {
val ordinal = sharedPreferences.getInt(getString(R.string.set_key_saf_mode), 0)
return MusicLocationMode.values().getOrNull(ordinal) ?: MusicLocationMode.SAF
val mode =
sharedPreferences.getInt(
getString(R.string.set_key_saf_mode), IntegerTable.LOCATION_MODE_SAF)
return LocationMode.fromInt(mode) ?: LocationMode.SAF
}
set(value) {
sharedPreferences.edit {
putInt(getString(R.string.set_key_saf_mode), value.ordinal)
putInt(getString(R.string.set_key_saf_mode), value.intCode)
apply()
}
}
override val safQuery: SAF.Query?
override var safQuery: SAF.Query
get() {
// SAF query is used when in SAF mode and we have music locations configured
if (locationMode != MusicLocationMode.SAF) return null
val locations = musicLocations
if (locations.isEmpty()) return null
val locations =
unlikelyToBeNull(
sharedPreferences
.getString(getString(R.string.set_key_music_locations), "")
.also { L.d("$it") })
.toOpenedLocations()
val excludedLocations =
unlikelyToBeNull(
sharedPreferences.getString(
getString(R.string.set_key_excluded_locations), ""))
.toUnopenedLocations()
val withHidden =
sharedPreferences.getBoolean(getString(R.string.set_key_with_hidden), false)
return SAF.Query(
source = locations, exclude = excludedLocations, withHidden = withHidden)
}
set(value) {
sharedPreferences.edit {
putString(getString(R.string.set_key_music_locations), value.source.stringify())
putString(getString(R.string.set_key_excluded_locations), value.exclude.stringify())
putBoolean(getString(R.string.set_key_with_hidden), value.withHidden)
}
}
override val mediaStoreQuery: MediaStoreFS.Query?
override var mediaStoreQuery: MediaStoreFS.Query
get() {
// 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
}
sharedPreferences.getInt(
getString(R.string.set_key_filter_mode), IntegerTable.FILTER_MODE_EXCLUDE)
val filteredLocations =
unlikelyToBeNull(
sharedPreferences.getString(
getString(R.string.set_key_filtered_locations), ""))
.toUnopenedLocations()
val excludeNonMusic =
sharedPreferences.getBoolean(getString(R.string.set_key_exclude_non_music), true)
return MediaStoreFS.Query(
mode = filterMode, filtered = excludedLocations, excludeNonMusic = excludeNonMusic)
}
override var musicLocations: List<Location.Opened>
get() {
val locations =
sharedPreferences.getString(getString(R.string.set_key_music_locations), null)
?: return emptyList()
return locations.toOpenedLocations()
}
set(value) {
sharedPreferences.edit {
putString(getString(R.string.set_key_music_locations), value.stringify())
commit()
// Sometimes changing this setting just won't actually trigger the listener.
// Only this one. No idea why.
listener?.onMusicLocationsChanged()
}
}
override var excludedLocations: List<Location.Unopened>
get() {
val locations =
sharedPreferences.getString(getString(R.string.set_key_excluded_locations), null)
?: return emptyList()
return locations.toUnopenedLocations()
mode =
when (filterMode) {
IntegerTable.FILTER_MODE_INCLUDE -> MediaStoreFS.FilterMode.INCLUDE
IntegerTable.FILTER_MODE_EXCLUDE -> MediaStoreFS.FilterMode.EXCLUDE
else -> MediaStoreFS.FilterMode.EXCLUDE
},
filtered = filteredLocations,
excludeNonMusic = excludeNonMusic)
}
set(value) {
sharedPreferences.edit {
val filterMode =
when (value.mode) {
MediaStoreFS.FilterMode.INCLUDE -> IntegerTable.FILTER_MODE_INCLUDE
MediaStoreFS.FilterMode.EXCLUDE -> IntegerTable.FILTER_MODE_EXCLUDE
}
putInt(getString(R.string.set_key_filter_mode), filterMode)
putString(
getString(R.string.set_key_excluded_locations), value.stringifyLocations())
commit()
listener?.onMusicLocationsChanged()
getString(R.string.set_key_filtered_locations), value.filtered.stringify())
putBoolean(getString(R.string.set_key_exclude_non_music), value.excludeNonMusic)
}
}
override val excludeNonMusic: Boolean
get() = sharedPreferences.getBoolean(getString(R.string.set_key_exclude_non_music), true)
override fun onSettingChanged(key: String, listener: MusicSettings.Listener) {
// TODO: Differentiate "hard reloads" (Need the cache) and "Soft reloads"
// (just need to manipulate data)
@@ -216,16 +201,13 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
}
}
private fun List<Location.Opened>.stringify(): String =
private fun List<Location>.stringify(): String =
joinToString(separator = ";") { it.uri.toString().replace(";", "\\;") }
private fun String.toOpenedLocations(): List<Location.Opened> =
splitEscaped { it == ';' }
.mapNotNull { Location.Unopened.from(context, it.toUri())?.open(context) }
private fun List<Location>.stringifyLocations(): String =
joinToString(separator = ";") { it.uri.toString().replace(";", "\\;") }
private fun String.toUnopenedLocations(): List<Location.Unopened> =
splitEscaped { it == ';' }.mapNotNull { Location.Unopened.from(context, it.toUri()) }
@@ -93,6 +93,20 @@ class LocationAdapter<T : Location>(private val listener: Listener) :
}
}
fun clear() {
L.d("Clearing all locations")
val wasEmpty = _locations.isEmpty()
val oldSize = _locations.size
_locations.clear()
if (!wasEmpty) {
if (oldSize == 1) {
notifyItemChanged(0)
} else {
notifyDataSetChanged()
}
}
}
interface Listener {
fun onRemoveLocation(location: Location)
}
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2025 Auxio Project
* LocationMode.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.locations
import org.oxycblt.auxio.IntegerTable
/** Represents the mode for loading music locations. */
enum class LocationMode {
/** Use Storage Access Framework (file picker) to select specific folders */
SAF,
/** Use system MediaStore database to load all music */
MEDIA_STORE;
val intCode: Int
get() =
when (this) {
SAF -> IntegerTable.LOCATION_MODE_SAF
MEDIA_STORE -> IntegerTable.LOCATION_MODE_MEDIA_STORE
}
companion object {
fun fromInt(int: Int): LocationMode? {
return when (int) {
IntegerTable.LOCATION_MODE_SAF -> SAF
IntegerTable.LOCATION_MODE_MEDIA_STORE -> MEDIA_STORE
else -> null
}
}
}
}
@@ -29,19 +29,19 @@ import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.core.view.isVisible
import com.google.android.material.R as MR
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.DialogMusicLocationsBinding
import org.oxycblt.auxio.music.MusicLocationMode
import org.oxycblt.auxio.music.MusicSettings
import org.oxycblt.auxio.ui.ViewBindingMaterialDialogFragment
import org.oxycblt.auxio.util.getAttrColorCompat
import org.oxycblt.auxio.util.showToast
import org.oxycblt.musikr.fs.Location
import org.oxycblt.musikr.fs.mediastore.MediaStoreFS
import org.oxycblt.musikr.fs.saf.SAF
import timber.log.Timber as L
@AndroidEntryPoint
@@ -222,26 +222,15 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
private fun loadInitialState(binding: DialogMusicLocationsBinding) {
// Determine mode based on the locationMode setting
isFilePickerMode = musicSettings.locationMode == MusicLocationMode.SAF
isFilePickerMode = musicSettings.locationMode == LocationMode.SAF
// Load data for the initial mode
loadModeData(binding)
// Set the initial toggle button selection
if (isFilePickerMode) {
// File picker mode - load music locations and excluded locations
includeLocationAdapter.addAll(musicSettings.musicLocations)
excludeLocationAdapter.addAll(musicSettings.excludedLocations)
binding.configSwitchFilePicker.isChecked = musicSettings.withHidden
binding.folderModeGroup.check(R.id.locations_mode_exclude)
} else {
// System database mode - load excluded locations
// Filter locations come from exclude list
filterLocationAdapter.addAll(musicSettings.excludedLocations)
binding.configSwitchSystem.isChecked = !musicSettings.excludeNonMusic
// Determine include/exclude mode based on whether we have exclude locations
// If we have exclude locations, we're in exclude mode
isIncludeMode = musicSettings.excludedLocations.isEmpty()
binding.locationsExcludeModeGroup.check(
if (isIncludeMode) R.id.locations_exclude_mode_exclude
else R.id.locations_exclude_mode_include)
binding.folderModeGroup.check(R.id.locations_mode_include)
}
@@ -249,6 +238,25 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
hasStoragePermission = checkStoragePermission()
}
private fun loadModeData(binding: DialogMusicLocationsBinding) {
// Load SAF data
musicSettings.safQuery.let { query ->
includeLocationAdapter.addAll(query.source)
excludeLocationAdapter.addAll(query.exclude)
binding.configSwitchFilePicker.isChecked = query.withHidden
}
// Load MediaStore data
musicSettings.mediaStoreQuery.let { query ->
filterLocationAdapter.addAll(query.filtered)
binding.configSwitchSystem.isChecked = query.excludeNonMusic
isIncludeMode = query.mode == MediaStoreFS.FilterMode.INCLUDE
binding.locationsExcludeModeGroup.check(
if (isIncludeMode) R.id.locations_exclude_mode_exclude
else R.id.locations_exclude_mode_include)
}
}
override fun onStart() {
super.onStart()
// Update save button state after dialog is shown
@@ -486,42 +494,31 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
// Save the mode setting
musicSettings.locationMode =
if (isFilePickerMode) MusicLocationMode.SAF else MusicLocationMode.SYSTEM
if (isFilePickerMode) LocationMode.SAF else LocationMode.MEDIA_STORE
if (isFilePickerMode) {
// File picker mode - save include and exclude locations
musicSettings.musicLocations = includeLocationAdapter.locations
musicSettings.excludedLocations = excludeLocationAdapter.locations
// Update withHidden setting
val withHidden = binding.configSwitchFilePicker.isChecked
if (musicSettings.withHidden != withHidden) {
binding.root.context.getSharedPreferences("auxio_preferences", 0).edit {
putBoolean(getString(R.string.set_key_with_hidden), withHidden)
apply()
}
}
// File picker mode - create and save SAF query
val safQuery =
SAF.Query(
source = includeLocationAdapter.locations,
exclude = excludeLocationAdapter.locations,
withHidden = binding.configSwitchFilePicker.isChecked)
musicSettings.safQuery = safQuery
} else {
// System database mode - clear music locations and save filter locations
musicSettings.musicLocations = emptyList()
// In include mode, we don't have any excluded locations
// In exclude mode, we save the filter locations as excluded
musicSettings.excludedLocations =
// System database mode - create and save MediaStore query
val filterMode =
if (isIncludeMode) {
emptyList()
MediaStoreFS.FilterMode.INCLUDE
} else {
filterLocationAdapter.locations
MediaStoreFS.FilterMode.EXCLUDE
}
// Update excludeNonMusic setting
val excludeNonMusic = !binding.configSwitchSystem.isChecked
if (musicSettings.excludeNonMusic != excludeNonMusic) {
binding.root.context.getSharedPreferences("auxio_preferences", 0).edit {
putBoolean(getString(R.string.set_key_exclude_non_music), excludeNonMusic)
apply()
}
}
val mediaStoreQuery =
MediaStoreFS.Query(
mode = filterMode,
filtered = filterLocationAdapter.locations,
excludeNonMusic = !binding.configSwitchSystem.isChecked)
musicSettings.mediaStoreQuery = mediaStoreQuery
}
}
@@ -159,13 +159,12 @@ private constructor(
private fun startTracking() {
stopTracking()
val locations = musicSettings.musicLocations
if (locations.isEmpty()) return
val safQuery = musicSettings.safQuery
if (safQuery == null || safQuery.source.isEmpty()) return
trackingJob =
indexScope.launch {
val query = SAF.Query(source = locations, exclude = emptyList(), withHidden = false)
SAF.from(workerContext, query).track().collect {
SAF.from(workerContext, safQuery).track().collect {
if (musicSettings.shouldBeObserving) {
musicRepository.requestIndex(true)
}
+2
View File
@@ -22,6 +22,8 @@
<string name="set_key_with_hidden" translatable="false">auxio_with_hidden</string>
<string name="set_key_music_locations" translatable="false">auxio_music_locations2</string>
<string name="set_key_excluded_locations" translatable="false">auxio_excluded_locations</string>
<string name="set_key_filter_mode" translatable="false">auxio_filter_mode</string>
<string name="set_key_filtered_locations" translatable="false">auxio_filtered_locations</string>
<string name="set_key_separators" translatable="false">auxio_separators</string>
<string name="set_key_auto_sort_names" translatable="false">auxio_auto_sort_names</string>
<string name="set_key_saf_mode" translatable="false">auxio_saf_mode</string>
@@ -9,18 +9,6 @@
app:summary="@string/set_observing_desc"
app:title="@string/set_observing" />
<SwitchPreferenceCompat
app:defaultValue="true"
app:key="@string/set_key_exclude_non_music"
app:summary="@string/set_exclude_non_music_desc"
app:title="@string/set_exclude_non_music" />
<SwitchPreferenceCompat
app:defaultValue="false"
app:key="@string/set_key_with_hidden"
app:summary="@string/set_with_hidden_desc"
app:title="@string/set_with_hidden" />
<org.oxycblt.auxio.settings.ui.WrappedDialogPreference
app:key="@string/set_key_separators"
app:summary="@string/set_separators_desc"