diff --git a/app/src/main/java/org/oxycblt/auxio/IntegerTable.kt b/app/src/main/java/org/oxycblt/auxio/IntegerTable.kt index 6ea713010..e4b075a96 100644 --- a/app/src/main/java/org/oxycblt/auxio/IntegerTable.kt +++ b/app/src/main/java/org/oxycblt/auxio/IntegerTable.kt @@ -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 } 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 d670adc34..94f9ac01c 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt @@ -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) 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 e93439b50..89d40bd51 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt @@ -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 { /** 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 { 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 - /** 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. */ @@ -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 - 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 - 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.stringify(): String = + private fun List.stringify(): String = joinToString(separator = ";") { it.uri.toString().replace(";", "\\;") } private fun String.toOpenedLocations(): List = splitEscaped { it == ';' } .mapNotNull { Location.Unopened.from(context, it.toUri())?.open(context) } - private fun List.stringifyLocations(): String = - joinToString(separator = ";") { it.uri.toString().replace(";", "\\;") } - private fun String.toUnopenedLocations(): List = splitEscaped { it == ';' }.mapNotNull { Location.Unopened.from(context, it.toUri()) } diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/LocationAdapter.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationAdapter.kt index 15fe56ed6..8baaf480d 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/locations/LocationAdapter.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationAdapter.kt @@ -93,6 +93,20 @@ class LocationAdapter(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) } diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/LocationMode.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationMode.kt new file mode 100644 index 000000000..23bb50f81 --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationMode.kt @@ -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 . + */ + +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 + } + } + } +} diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt index 4418ed5e3..e77e308fc 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt @@ -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 + 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 : ViewBindingMaterialDialogFragmentauxio_with_hidden auxio_music_locations2 auxio_excluded_locations + auxio_filter_mode + auxio_filtered_locations auxio_separators auxio_auto_sort_names auxio_saf_mode diff --git a/app/src/main/res/xml/preferences_music.xml b/app/src/main/res/xml/preferences_music.xml index b3b5106f5..0619d3872 100644 --- a/app/src/main/res/xml/preferences_music.xml +++ b/app/src/main/res/xml/preferences_music.xml @@ -9,18 +9,6 @@ app:summary="@string/set_observing_desc" app:title="@string/set_observing" /> - - - - , + val filtered: List, val excludeNonMusic: Boolean )