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 d1c57b8cf..ac08842ce 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt @@ -27,7 +27,6 @@ import javax.inject.Inject import org.oxycblt.auxio.R import org.oxycblt.auxio.settings.Settings import org.oxycblt.musikr.fs.Location -import org.oxycblt.musikr.fs.OpenedLocation import timber.log.Timber as L /** @@ -39,9 +38,9 @@ interface MusicSettings : Settings { /** The current library revision. */ var revision: UUID? /** The locations of music to load. */ - var musicLocations: List + var musicLocations: List /** The locations to exclude from music loading. */ - var excludedLocations: List + var excludedLocations: List /** Whether to exclude non-music audio files from the music library. */ val excludeNonMusic: Boolean /** Whether to ignore hidden files and directories during music loading. */ @@ -78,7 +77,7 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont } } - override var musicLocations: List + override var musicLocations: List get() { val locations = sharedPreferences.getString(getString(R.string.set_key_music_locations), null) @@ -95,12 +94,12 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont } } - override var excludedLocations: List + override var excludedLocations: List get() { val locations = sharedPreferences.getString(getString(R.string.set_key_excluded_locations), null) ?: return emptyList() - return locations.toLocations() + return locations.toUnopenedLocations() } set(value) { sharedPreferences.edit { @@ -157,17 +156,17 @@ 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.from(context, it.toUri())?.open(context) } + 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.toLocations(): List = - splitEscaped { it == ';' }.mapNotNull { Location.from(context, it.toUri()) } + private fun String.toUnopenedLocations(): List = + splitEscaped { it == ';' }.mapNotNull { Location.Unopened.from(context, it.toUri()) } private inline fun String.splitEscaped(selector: (Char) -> Boolean): List { val split = mutableListOf() diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationAdapter.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationAdapter.kt index 754ce915a..4ad1f384f 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationAdapter.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationAdapter.kt @@ -18,98 +18,29 @@ package org.oxycblt.auxio.music.locations -import android.view.View import android.view.ViewGroup -import androidx.recyclerview.widget.RecyclerView import org.oxycblt.auxio.databinding.ItemMusicLocationBinding -import org.oxycblt.auxio.list.recycler.DialogRecyclerView -import org.oxycblt.auxio.util.context import org.oxycblt.auxio.util.inflater import org.oxycblt.musikr.fs.Location -import timber.log.Timber as L /** - * [RecyclerView.Adapter] that manages a list of [Location] excluded directory instances. + * [LocationAdapter] that manages a list of [Location.Unopened] excluded directory instances. * - * @param listener A [ExcludedLocationAdapter.Listener] to bind interactions to. + * @param listener A [LocationAdapter.Listener] to bind interactions to. * @author Alexander Capehart (OxygenCobalt) */ -class ExcludedLocationAdapter(private val listener: Listener) : RecyclerView.Adapter() { - private val _locations = mutableListOf() - /** - * The current list of [Location]s, may not line up with [Location]s due to - * removals. - */ - val locations: List = _locations - - override fun getItemCount() = locations.size - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = - ExcludedDirViewHolder.from(parent) - - override fun onBindViewHolder(holder: ExcludedDirViewHolder, position: Int) = - holder.bind(locations[position], listener) - - /** - * Add a [Location] to the end of the list. - * - * @param location The [Location] to add. - */ - fun add(location: Location) { - if (_locations.contains(location)) return - L.d("Adding $location") - _locations.add(location) - notifyItemInserted(_locations.lastIndex) - } - - /** - * Add a list of [Location] instances to the end of the list. - * - * @param locations The [Location] instances to add. - */ - fun addAll(locations: List) { - L.d("Adding ${locations.size} locations") - val oldLastIndex = _locations.size - _locations.addAll(locations) - notifyItemRangeInserted(oldLastIndex, locations.size) - } - - /** - * Remove a [Location] from the list. - * - * @param location The [Location] to remove. Must exist in the list. - */ - fun remove(location: Location) { - L.d("Removing $location") - val idx = _locations.indexOf(location) - _locations.removeAt(idx) - notifyItemRemoved(idx) - } - - /** A Listener for [ExcludedLocationAdapter] interactions. */ - interface Listener { - /** Called when the delete button on a directory item is clicked. */ - fun onRemoveLocation(location: Location) - } +class ExcludedLocationAdapter(listener: LocationAdapter.Listener) : LocationAdapter(listener) { + override fun createViewHolder(parent: ViewGroup): LocationViewHolder = + ExcludedLocationViewHolder.from(parent) } /** - * A [RecyclerView.Recycler] that displays a [Location]. Use [from] to create an instance. + * A [LocationViewHolder] that displays a [Location.Unopened]. Use [from] to create an instance. * * @author Alexander Capehart (OxygenCobalt) */ -class ExcludedDirViewHolder private constructor(private val binding: ItemMusicLocationBinding) : - DialogRecyclerView.ViewHolder(binding.root) { - /** - * Bind new data to this instance. - * - * @param location The new [Location] to bind. - * @param listener A [ExcludedLocationAdapter.Listener] to bind interactions to. - */ - fun bind(location: Location, listener: ExcludedLocationAdapter.Listener) { - binding.locationPath.text = location.path.resolve(binding.context) - binding.locationDelete.setOnClickListener { listener.onRemoveLocation(location) } - } +class ExcludedLocationViewHolder private constructor(binding: ItemMusicLocationBinding) : + LocationViewHolder(binding) { companion object { /** @@ -119,6 +50,6 @@ class ExcludedDirViewHolder private constructor(private val binding: ItemMusicLo * @return A new instance. */ fun from(parent: ViewGroup) = - ExcludedDirViewHolder(ItemMusicLocationBinding.inflate(parent.context.inflater, parent, false)) + ExcludedLocationViewHolder(ItemMusicLocationBinding.inflate(parent.context.inflater, parent, false)) } } \ No newline at end of file diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationsDialog.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationsDialog.kt index 16eb9b4e3..7c031b236 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationsDialog.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationsDialog.kt @@ -18,25 +18,14 @@ package org.oxycblt.auxio.music.locations -import android.content.ActivityNotFoundException +import android.content.Context import android.net.Uri -import android.os.Bundle -import android.view.LayoutInflater -import androidx.activity.result.ActivityResultLauncher -import androidx.activity.result.contract.ActivityResultContracts -import androidx.appcompat.app.AlertDialog -import androidx.core.net.toUri -import androidx.recyclerview.widget.ConcatAdapter import dagger.hilt.android.AndroidEntryPoint -import javax.inject.Inject import org.oxycblt.auxio.BuildConfig import org.oxycblt.auxio.R -import org.oxycblt.auxio.databinding.DialogMusicLocationsBinding import org.oxycblt.auxio.music.MusicSettings -import org.oxycblt.auxio.ui.ViewBindingMaterialDialogFragment -import org.oxycblt.auxio.util.showToast import org.oxycblt.musikr.fs.Location -import timber.log.Timber as L +import javax.inject.Inject /** * Dialog that manages the excluded locations setting. @@ -44,101 +33,31 @@ import timber.log.Timber as L * @author Alexander Capehart (OxygenCobalt) */ @AndroidEntryPoint -class ExcludedLocationsDialog : - ViewBindingMaterialDialogFragment(), - ExcludedLocationAdapter.Listener, - NewLocationFooterAdapter.Listener { - private val locationAdapter = ExcludedLocationAdapter(this) - private val locationFooterAdapter = NewLocationFooterAdapter(this) - private var openDocumentTreeLauncher: ActivityResultLauncher? = null - @Inject lateinit var musicSettings: MusicSettings +class ExcludedLocationsDialog : LocationsDialog() { + override val locationAdapter = ExcludedLocationAdapter(this) - override fun onCreateBinding(inflater: LayoutInflater) = - DialogMusicLocationsBinding.inflate(inflater) + @Inject + override lateinit var musicSettings: MusicSettings - override fun onConfigDialog(builder: AlertDialog.Builder) { - builder - .setTitle(R.string.set_excluded_locations) - .setNegativeButton(R.string.lbl_cancel, null) - .setPositiveButton(R.string.lbl_save) { _, _ -> - val newDirs = locationAdapter.locations - musicSettings.excludedLocations = newDirs - } + override fun getDialogTitle(): Int = R.string.set_excluded_locations + + override fun getCurrentLocations(): List = musicSettings.excludedLocations + + override fun saveLocations(locations: List) { + musicSettings.excludedLocations = locations } - override fun onBindingCreated( - binding: DialogMusicLocationsBinding, - savedInstanceState: Bundle? - ) { - openDocumentTreeLauncher = - registerForActivityResult( - ActivityResultContracts.OpenDocumentTree(), ::addDocumentTreeUriToDirs) + override fun getPendingLocationsKey(): String = KEY_PENDING_EXCLUDED_LOCATIONS - binding.locationsRecycler.apply { - adapter = ConcatAdapter(locationAdapter, locationFooterAdapter) - itemAnimator = null - } - - val locations = - savedInstanceState?.getStringArrayList(KEY_PENDING_LOCATIONS)?.mapNotNull { - Location.from(requireContext(), it.toUri()) - } ?: musicSettings.excludedLocations - - locationAdapter.addAll(locations) + override fun convertUriToLocation(uri: Uri): Location.Unopened? { + return Location.Unopened.from(requireContext(), uri) } - override fun onSaveInstanceState(outState: Bundle) { - super.onSaveInstanceState(outState) - outState.putStringArrayList( - KEY_PENDING_LOCATIONS, ArrayList(locationAdapter.locations.map { it.uri.toString() })) - } - - override fun onDestroyBinding(binding: DialogMusicLocationsBinding) { - super.onDestroyBinding(binding) - openDocumentTreeLauncher = null - binding.locationsRecycler.adapter = null - } - - override fun onRemoveLocation(location: Location) { - locationAdapter.remove(location) - } - - override fun onNewLocation() { - L.d("Opening launcher") - val launcher = - requireNotNull(openDocumentTreeLauncher) { "Document tree launcher was not available" } - - try { - launcher.launch(null) - } catch (e: ActivityNotFoundException) { - // User doesn't have a capable file manager. - requireContext().showToast(R.string.err_no_app) - } - } - - /** - * Add a Document Tree [Uri] chosen by the user to the current [Location]s. - * - * @param uri The document tree [Uri] to add, chosen by the user. Will do nothing if the [Uri] - * is null or not valid. - */ - private fun addDocumentTreeUriToDirs(uri: Uri?) { - if (uri == null) { - // A null URI means that the user left the file picker without picking a directory - L.d("No URI given (user closed the dialog)") - return - } - val context = requireContext() - val location = Location.from(context, uri) - - if (location != null) { - locationAdapter.add(location) - } else { - requireContext().showToast(R.string.err_bad_location) - } + override fun createLocationFromUri(context: Context, uri: Uri): Location.Unopened? { + return Location.Unopened.from(context, uri) } private companion object { - const val KEY_PENDING_LOCATIONS = BuildConfig.APPLICATION_ID + ".key.PENDING_EXCLUDED_LOCATIONS" + const val KEY_PENDING_EXCLUDED_LOCATIONS = BuildConfig.APPLICATION_ID + ".key.PENDING_EXCLUDED_LOCATIONS" } } \ No newline at end of file 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 ee4b8d97b..37e0abe48 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 @@ -1,6 +1,6 @@ /* - * Copyright (c) 2021 Auxio Project - * LocationAdapter.kt is part of Auxio. + * Copyright (c) 2024 Auxio Project + * BaseLocationAdapter.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 @@ -18,107 +18,59 @@ package org.oxycblt.auxio.music.locations -import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import org.oxycblt.auxio.databinding.ItemMusicLocationBinding import org.oxycblt.auxio.list.recycler.DialogRecyclerView import org.oxycblt.auxio.util.context -import org.oxycblt.auxio.util.inflater -import org.oxycblt.musikr.fs.OpenedLocation +import org.oxycblt.musikr.fs.Location import timber.log.Timber as L -/** - * [RecyclerView.Adapter] that manages a list of [OpenedLocation] music directory instances. - * - * @param listener A [LocationAdapter.Listener] to bind interactions to. - * @author Alexander Capehart (OxygenCobalt) - */ -class LocationAdapter(private val listener: Listener) : RecyclerView.Adapter() { - private val _locations = mutableListOf() - /** - * The current list of [OpenedLocation]s, may not line up with [OpenedLocation]s due to - * removals. - */ - val locations: List = _locations +abstract class LocationAdapter(private val listener: Listener) : RecyclerView.Adapter>() { + private val _locations = mutableListOf() + val locations: List = _locations override fun getItemCount() = locations.size override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = - MusicDirViewHolder.from(parent) + createViewHolder(parent) - override fun onBindViewHolder(holder: MusicDirViewHolder, position: Int) = + override fun onBindViewHolder(holder: LocationViewHolder, position: Int) = holder.bind(locations[position], listener) - /** - * Add a [OpenedLocation] to the end of the list. - * - * @param location The [OpenedLocation] to add. - */ - fun add(location: OpenedLocation) { + fun add(location: T) { if (_locations.contains(location)) return L.d("Adding $location") _locations.add(location) notifyItemInserted(_locations.lastIndex) } - /** - * Add a list of [OpenedLocation] instances to the end of the list. - * - * @param locations The [OpenedLocation] instances to add. - */ - fun addAll(locations: List) { + fun addAll(locations: List) { L.d("Adding ${locations.size} locations") - val oldLastIndex = locations.lastIndex + val oldSize = _locations.size _locations.addAll(locations) - notifyItemRangeInserted(oldLastIndex, locations.size) + notifyItemRangeInserted(oldSize, locations.size) } - /** - * Remove a [OpenedLocation] from the list. - * - * @param location The [OpenedLocation] to remove. Must exist in the list. - */ - fun remove(location: OpenedLocation) { + fun remove(location: T) { L.d("Removing $location") val idx = _locations.indexOf(location) _locations.removeAt(idx) notifyItemRemoved(idx) } - /** A Listener for [LocationAdapter] interactions. */ - interface Listener { - /** Called when the delete button on a directory item is clicked. */ - fun onRemoveLocation(location: OpenedLocation) + protected abstract fun createViewHolder(parent: ViewGroup): LocationViewHolder + + interface Listener { + fun onRemoveLocation(location: T) } } -/** - * A [RecyclerView.Recycler] that displays a [OpenedLocation]. Use [from] to create an instance. - * - * @author Alexander Capehart (OxygenCobalt) - */ -class MusicDirViewHolder private constructor(private val binding: ItemMusicLocationBinding) : +abstract class LocationViewHolder(protected val binding: ItemMusicLocationBinding) : DialogRecyclerView.ViewHolder(binding.root) { - /** - * Bind new data to this instance. - * - * @param location The new [OpenedLocation] to bind. - * @param listener A [LocationAdapter.Listener] to bind interactions to. - */ - fun bind(location: OpenedLocation, listener: LocationAdapter.Listener) { + + fun bind(location: T, listener: LocationAdapter.Listener) { binding.locationPath.text = location.path.resolve(binding.context) binding.locationDelete.setOnClickListener { listener.onRemoveLocation(location) } } - - companion object { - /** - * Create a new instance. - * - * @param parent The parent to inflate this instance from. - * @return A new instance. - */ - fun from(parent: View) = - MusicDirViewHolder(ItemMusicLocationBinding.inflate(parent.context.inflater)) - } -} +} \ No newline at end of file diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/MusicSourcesDialog.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt similarity index 69% rename from app/src/main/java/org/oxycblt/auxio/music/locations/MusicSourcesDialog.kt rename to app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt index b506f84ae..c5847338d 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/locations/MusicSourcesDialog.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/LocationsDialog.kt @@ -1,6 +1,6 @@ /* - * Copyright (c) 2021 Auxio Project - * MusicSourcesDialog.kt is part of Auxio. + * Copyright (c) 2024 Auxio Project + * BaseLocationsDialog.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 @@ -27,43 +27,33 @@ import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.core.net.toUri import androidx.recyclerview.widget.ConcatAdapter -import dagger.hilt.android.AndroidEntryPoint -import javax.inject.Inject -import org.oxycblt.auxio.BuildConfig import org.oxycblt.auxio.R import org.oxycblt.auxio.databinding.DialogMusicLocationsBinding import org.oxycblt.auxio.music.MusicSettings import org.oxycblt.auxio.ui.ViewBindingMaterialDialogFragment import org.oxycblt.auxio.util.showToast import org.oxycblt.musikr.fs.Location -import org.oxycblt.musikr.fs.OpenedLocation import timber.log.Timber as L -/** - * Dialog that manages the music locations setting. - * - * @author Alexander Capehart (OxygenCobalt) - */ -@AndroidEntryPoint -class MusicSourcesDialog : +abstract class LocationsDialog : ViewBindingMaterialDialogFragment(), - LocationAdapter.Listener, + LocationAdapter.Listener, NewLocationFooterAdapter.Listener { - private val locationAdapter = LocationAdapter(this) + protected abstract val locationAdapter: LocationAdapter private val locationFooterAdapter = NewLocationFooterAdapter(this) private var openDocumentTreeLauncher: ActivityResultLauncher? = null - @Inject lateinit var musicSettings: MusicSettings + abstract val musicSettings: MusicSettings override fun onCreateBinding(inflater: LayoutInflater) = DialogMusicLocationsBinding.inflate(inflater) override fun onConfigDialog(builder: AlertDialog.Builder) { builder - .setTitle(R.string.set_locations) + .setTitle(getDialogTitle()) .setNegativeButton(R.string.lbl_cancel, null) .setPositiveButton(R.string.lbl_save) { _, _ -> val newDirs = locationAdapter.locations - musicSettings.musicLocations = newDirs + saveLocations(newDirs) } } @@ -81,10 +71,9 @@ class MusicSourcesDialog : } val locations = - savedInstanceState?.getStringArrayList(KEY_PENDING_LOCATIONS)?.mapNotNull { - val context = requireContext() - Location.from(context, it.toUri())?.open(context) - } ?: musicSettings.musicLocations + savedInstanceState?.getStringArrayList(getPendingLocationsKey())?.mapNotNull { + convertUriToLocation(it.toUri()) + } ?: getCurrentLocations() locationAdapter.addAll(locations) } @@ -92,7 +81,7 @@ class MusicSourcesDialog : override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putStringArrayList( - KEY_PENDING_LOCATIONS, ArrayList(locationAdapter.locations.map { it.uri.toString() })) + getPendingLocationsKey(), ArrayList(locationAdapter.locations.map { it.uri.toString() })) } override fun onDestroyBinding(binding: DialogMusicLocationsBinding) { @@ -101,10 +90,6 @@ class MusicSourcesDialog : binding.locationsRecycler.adapter = null } - override fun onRemoveLocation(location: OpenedLocation) { - locationAdapter.remove(location) - } - override fun onNewLocation() { L.d("Opening launcher") val launcher = @@ -113,25 +98,17 @@ class MusicSourcesDialog : try { launcher.launch(null) } catch (e: ActivityNotFoundException) { - // User doesn't have a capable file manager. requireContext().showToast(R.string.err_no_app) } } - /** - * Add a Document Tree [Uri] chosen by the user to the current [OpenedLocation]s. - * - * @param uri The document tree [Uri] to add, chosen by the user. Will do nothing if the [Uri] - * is null or not valid. - */ private fun addDocumentTreeUriToDirs(uri: Uri?) { if (uri == null) { - // A null URI means that the user left the file picker without picking a locationectory L.d("No URI given (user closed the dialog)") return } val context = requireContext() - val location = Location.from(context, uri)?.open(context) + val location = createLocationFromUri(context, uri) if (location != null) { locationAdapter.add(location) @@ -140,7 +117,14 @@ class MusicSourcesDialog : } } - private companion object { - const val KEY_PENDING_LOCATIONS = BuildConfig.APPLICATION_ID + ".key.PENDING_LOCATIONS" + override fun onRemoveLocation(location: T) { + locationAdapter.remove(location) } -} + + protected abstract fun getDialogTitle(): Int + protected abstract fun getCurrentLocations(): List + protected abstract fun saveLocations(locations: List) + protected abstract fun getPendingLocationsKey(): String + protected abstract fun convertUriToLocation(uri: Uri): T? + protected abstract fun createLocationFromUri(context: android.content.Context, uri: Uri): T? +} \ No newline at end of file diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/MusicLocationAdapter.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/MusicLocationAdapter.kt new file mode 100644 index 000000000..af89cd172 --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/MusicLocationAdapter.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 Auxio Project + * LocationAdapter.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.view.ViewGroup +import org.oxycblt.auxio.databinding.ItemMusicLocationBinding +import org.oxycblt.auxio.util.inflater +import org.oxycblt.musikr.fs.Location + +/** + * [LocationAdapter] that manages a list of [Location.Opened] music directory instances. + * + * @param listener A [LocationAdapter.Listener] to bind interactions to. + * @author Alexander Capehart (OxygenCobalt) + */ +class MusicLocationAdapter(listener: LocationAdapter.Listener) : LocationAdapter(listener) { + override fun createViewHolder(parent: ViewGroup): LocationViewHolder = + MusicLocationViewHolder.from(parent) +} + +/** + * A [LocationViewHolder] that displays a [Location.Opened]. Use [from] to create an instance. + * + * @author Alexander Capehart (OxygenCobalt) + */ +class MusicLocationViewHolder private constructor(binding: ItemMusicLocationBinding) : + LocationViewHolder(binding) { + + companion object { + /** + * Create a new instance. + * + * @param parent The parent to inflate this instance from. + * @return A new instance. + */ + fun from(parent: ViewGroup) = + MusicLocationViewHolder(ItemMusicLocationBinding.inflate(parent.context.inflater, parent, false)) + } +} diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/MusicLocationsDialog.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/MusicLocationsDialog.kt new file mode 100644 index 000000000..90bb054c4 --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/MusicLocationsDialog.kt @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021 Auxio Project + * MusicSourcesDialog.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 android.net.Uri +import dagger.hilt.android.AndroidEntryPoint +import org.oxycblt.auxio.BuildConfig +import org.oxycblt.auxio.R +import org.oxycblt.auxio.music.MusicSettings +import org.oxycblt.musikr.fs.Location +import javax.inject.Inject + +/** + * Dialog that manages the music locations setting. + * + * @author Alexander Capehart (OxygenCobalt) + */ +@AndroidEntryPoint +class MusicLocationsDialog : LocationsDialog() { + override val locationAdapter = MusicLocationAdapter(this) + + @Inject + override lateinit var musicSettings: MusicSettings + + override fun getDialogTitle(): Int = R.string.set_locations + + override fun getCurrentLocations(): List = musicSettings.musicLocations + + override fun saveLocations(locations: List) { + musicSettings.musicLocations = locations + } + + override fun getPendingLocationsKey(): String = KEY_PENDING_LOCATIONS + + override fun convertUriToLocation(uri: Uri): Location.Opened? { + val context = requireContext() + return Location.Unopened.from(context, uri)?.open(context) + } + + override fun createLocationFromUri(context: Context, uri: Uri): Location.Opened? { + return Location.Unopened.from(context, uri)?.open(context) + } + + private companion object { + const val KEY_PENDING_LOCATIONS = BuildConfig.APPLICATION_ID + ".key.PENDING_LOCATIONS" + } +} diff --git a/app/src/main/java/org/oxycblt/auxio/music/service/IndexingHolder.kt b/app/src/main/java/org/oxycblt/auxio/music/service/IndexingHolder.kt index 7d5226067..7788e10e8 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/service/IndexingHolder.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/service/IndexingHolder.kt @@ -36,7 +36,7 @@ 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.OpenedLocation +import org.oxycblt.musikr.fs.Location import org.oxycblt.musikr.track.UpdateTracker import timber.log.Timber as L @@ -168,7 +168,7 @@ private constructor( } } - override fun onUpdate(location: OpenedLocation) { + override fun onUpdate(location: Location.Opened) { if (musicSettings.shouldBeObserving) { musicRepository.requestIndex(true) } diff --git a/app/src/main/res/navigation/inner.xml b/app/src/main/res/navigation/inner.xml index aa10ee2f9..61fcfd3a5 100644 --- a/app/src/main/res/navigation/inner.xml +++ b/app/src/main/res/navigation/inner.xml @@ -570,7 +570,7 @@ \ No newline at end of file diff --git a/app/src/main/res/navigation/outer.xml b/app/src/main/res/navigation/outer.xml index 4f75640ec..60d81d6b2 100644 --- a/app/src/main/res/navigation/outer.xml +++ b/app/src/main/res/navigation/outer.xml @@ -94,7 +94,7 @@ tools:layout="@layout/dialog_pre_amp" /> , val exclude: List) +data class Query(val source: List, val exclude: List) /** Side-effect laden [Storage] for use during music loading and [MutableLibrary] operation. */ data class Storage( diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/Location.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/Location.kt index dd4b56474..df25b2adc 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/Location.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/Location.kt @@ -25,52 +25,52 @@ import android.provider.DocumentsContract import org.oxycblt.musikr.fs.device.contentResolverSafe import org.oxycblt.musikr.fs.path.DocumentPathFactory -class Location private constructor(val uri: Uri, val path: Path) { +sealed class Location(val uri: Uri, val path: Path) { override fun equals(other: Any?) = other is Location && uri == other.uri override fun hashCode() = 31 * uri.hashCode() override fun toString(): String = uri.toString() - fun open(context: Context): OpenedLocation? { - if (isUnopened(context, uri)) { - try { - context.contentResolverSafe.takePersistableUriPermission( - uri, - Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION) - } catch (e: Exception) { - // Not fully sure what happens if I'm disallowed to take the permission, - // check for both circumstances (error or no-op) - return null - } - + class Unopened private constructor(uri: Uri, path: Path) : Location(uri, path) { + fun open(context: Context): Opened? { if (isUnopened(context, uri)) { - return null + try { + context.contentResolverSafe.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION) + } catch (e: Exception) { + // Not fully sure what happens if I'm disallowed to take the permission, + // check for both circumstances (error or no-op) + return null + } + + if (isUnopened(context, uri)) { + return null + } + } + return Opened(uri, path) + } + + private fun isUnopened(context: Context, uri: Uri) = + context.contentResolverSafe.persistedUriPermissions.none { + it.uri == uri && it.isReadPermission && it.isWritePermission + } + + companion object { + fun from(context: Context, uri: Uri): Unopened? { + if (!DocumentsContract.isTreeUri(uri)) return null + val documentPathFactory = DocumentPathFactory.from(context) + val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null + return Unopened(uri, path) } } - return OpenedLocation(uri, path) } - private fun isUnopened(context: Context, uri: Uri) = - context.contentResolverSafe.persistedUriPermissions.none { - it.uri == uri && it.isReadPermission && it.isWritePermission - } + class Opened internal constructor(uri: Uri, path: Path) : Location(uri, path) { + override fun equals(other: Any?) = + other is Opened && uri == other.uri && path == other.path - companion object { - fun from(context: Context, uri: Uri): Location? { - if (!DocumentsContract.isTreeUri(uri)) return null - val documentPathFactory = DocumentPathFactory.from(context) - val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null - return Location(uri, path) - } + override fun hashCode() = 31 * uri.hashCode() + path.hashCode() } } - -class OpenedLocation internal constructor(val uri: Uri, val path: Path) { - override fun equals(other: Any?) = - other is OpenedLocation && uri == other.uri && path == other.path - - override fun hashCode() = 31 * uri.hashCode() + path.hashCode() - - override fun toString(): String = uri.toString() -} diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt index 99e4a6c27..cdb5f2bb1 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt @@ -57,7 +57,7 @@ private class DeviceFSImpl( null) } - private fun exploreDirectoryImpl( + private suspend fun exploreDirectoryImpl( rootUri: Uri, treeDocumentId: String, relativePath: Path, diff --git a/musikr/src/main/java/org/oxycblt/musikr/track/LocationObserver.kt b/musikr/src/main/java/org/oxycblt/musikr/track/LocationObserver.kt index a3b4a0e00..29ae53bbf 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/track/LocationObserver.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/track/LocationObserver.kt @@ -22,11 +22,11 @@ import android.content.Context import android.database.ContentObserver import android.os.Handler import android.os.Looper -import org.oxycblt.musikr.fs.OpenedLocation +import org.oxycblt.musikr.fs.Location internal class LocationObserver( private val context: Context, - private val location: OpenedLocation, + private val location: Location.Opened, private val listener: UpdateTracker.Callback ) : ContentObserver(Handler(Looper.getMainLooper())), Runnable { private val handler = Handler(Looper.getMainLooper()) diff --git a/musikr/src/main/java/org/oxycblt/musikr/track/UpdateTracker.kt b/musikr/src/main/java/org/oxycblt/musikr/track/UpdateTracker.kt index a3e4f2b69..48f22a1fb 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/track/UpdateTracker.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/track/UpdateTracker.kt @@ -19,15 +19,15 @@ package org.oxycblt.musikr.track import android.content.Context -import org.oxycblt.musikr.fs.OpenedLocation +import org.oxycblt.musikr.fs.Location interface UpdateTracker { - fun track(locations: List) + fun track(locations: List) fun release() interface Callback { - fun onUpdate(location: OpenedLocation) + fun onUpdate(location: Location.Opened) } companion object { @@ -42,7 +42,7 @@ private class UpdateTrackerImpl( ) : UpdateTracker { private val observers = mutableListOf() - override fun track(locations: List) { + override fun track(locations: List) { release() observers.addAll(locations.map { LocationObserver(context, it, callback) }) }