music: add permission/save disabling to dialog
This commit is contained in:
@@ -18,13 +18,17 @@
|
|||||||
|
|
||||||
package org.oxycblt.auxio.music.locations
|
package org.oxycblt.auxio.music.locations
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
import android.content.ActivityNotFoundException
|
import android.content.ActivityNotFoundException
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import androidx.activity.result.ActivityResultLauncher
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import com.google.android.material.R as MR
|
import com.google.android.material.R as MR
|
||||||
@@ -47,6 +51,7 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
object : LocationAdapter.Listener {
|
object : LocationAdapter.Listener {
|
||||||
override fun onRemoveLocation(location: Location) {
|
override fun onRemoveLocation(location: Location) {
|
||||||
includeLocationAdapter.remove(location as Location.Opened)
|
includeLocationAdapter.remove(location as Location.Opened)
|
||||||
|
updateSaveButtonState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,6 +59,7 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
object : LocationAdapter.Listener {
|
object : LocationAdapter.Listener {
|
||||||
override fun onRemoveLocation(location: Location) {
|
override fun onRemoveLocation(location: Location) {
|
||||||
excludeLocationAdapter.remove(location as Location.Unopened)
|
excludeLocationAdapter.remove(location as Location.Unopened)
|
||||||
|
updateSaveButtonState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,6 +67,7 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
object : LocationAdapter.Listener {
|
object : LocationAdapter.Listener {
|
||||||
override fun onRemoveLocation(location: Location) {
|
override fun onRemoveLocation(location: Location) {
|
||||||
filterLocationAdapter.remove(location as Location.Unopened)
|
filterLocationAdapter.remove(location as Location.Unopened)
|
||||||
|
updateSaveButtonState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +78,7 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
private val filterLocationAdapter: LocationAdapter<Location.Unopened> =
|
private val filterLocationAdapter: LocationAdapter<Location.Unopened> =
|
||||||
LocationAdapter(filterLocationListener)
|
LocationAdapter(filterLocationListener)
|
||||||
private var openDocumentTreeLauncher: ActivityResultLauncher<Uri?>? = null
|
private var openDocumentTreeLauncher: ActivityResultLauncher<Uri?>? = null
|
||||||
|
private var storagePermissionLauncher: ActivityResultLauncher<String>? = null
|
||||||
@Inject lateinit var musicSettings: MusicSettings
|
@Inject lateinit var musicSettings: MusicSettings
|
||||||
|
|
||||||
private var isFilePickerMode = true
|
private var isFilePickerMode = true
|
||||||
@@ -98,6 +106,14 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
addDocumentTreeUriToDirs(uri)
|
addDocumentTreeUriToDirs(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
storagePermissionLauncher =
|
||||||
|
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
|
||||||
|
L.d("Storage permission granted: $isGranted")
|
||||||
|
hasStoragePermission = isGranted
|
||||||
|
updateModeUI(binding)
|
||||||
|
updateSaveButtonState()
|
||||||
|
}
|
||||||
|
|
||||||
binding.locationsIncludeRecycler.apply {
|
binding.locationsIncludeRecycler.apply {
|
||||||
adapter = includeLocationAdapter
|
adapter = includeLocationAdapter
|
||||||
itemAnimator = null
|
itemAnimator = null
|
||||||
@@ -143,10 +159,12 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
R.id.locations_mode_exclude -> {
|
R.id.locations_mode_exclude -> {
|
||||||
isFilePickerMode = true
|
isFilePickerMode = true
|
||||||
updateModeUI(binding)
|
updateModeUI(binding)
|
||||||
|
updateSaveButtonState()
|
||||||
}
|
}
|
||||||
R.id.locations_mode_include -> {
|
R.id.locations_mode_include -> {
|
||||||
isFilePickerMode = false
|
isFilePickerMode = false
|
||||||
updateModeUI(binding)
|
updateModeUI(binding)
|
||||||
|
updateSaveButtonState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -173,28 +191,33 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
pendingLocationCallback = { location ->
|
pendingLocationCallback = { location ->
|
||||||
location.open(requireContext())?.let { opened ->
|
location.open(requireContext())?.let { opened ->
|
||||||
includeLocationAdapter.add(opened)
|
includeLocationAdapter.add(opened)
|
||||||
|
updateSaveButtonState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onNewLocation()
|
onNewLocation()
|
||||||
}
|
}
|
||||||
binding.locationsExcludeAdd.setOnClickListener {
|
binding.locationsExcludeAdd.setOnClickListener {
|
||||||
pendingLocationCallback = { location -> excludeLocationAdapter.add(location) }
|
pendingLocationCallback = { location ->
|
||||||
|
excludeLocationAdapter.add(location)
|
||||||
|
updateSaveButtonState()
|
||||||
|
}
|
||||||
onNewLocation()
|
onNewLocation()
|
||||||
}
|
}
|
||||||
binding.locationsFilterAdd.setOnClickListener {
|
binding.locationsFilterAdd.setOnClickListener {
|
||||||
pendingLocationCallback = { location -> filterLocationAdapter.add(location) }
|
pendingLocationCallback = { location ->
|
||||||
|
filterLocationAdapter.add(location)
|
||||||
|
updateSaveButtonState()
|
||||||
|
}
|
||||||
onNewLocation()
|
onNewLocation()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up grant permission card click
|
// Set up grant permission card click
|
||||||
binding.locationsPermsContainer.setOnClickListener {
|
binding.locationsPermsCard.setOnClickListener { requestStoragePermission() }
|
||||||
// TODO: Implement permission request
|
|
||||||
L.d("Grant permission clicked")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize UI state
|
// Initialize UI state
|
||||||
updateModeUI(binding)
|
updateModeUI(binding)
|
||||||
updateExtrasVisibility(binding)
|
updateExtrasVisibility(binding)
|
||||||
|
updateSaveButtonState()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadInitialState(binding: DialogMusicLocationsBinding) {
|
private fun loadInitialState(binding: DialogMusicLocationsBinding) {
|
||||||
@@ -222,8 +245,14 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
binding.folderModeGroup.check(R.id.locations_mode_include)
|
binding.folderModeGroup.check(R.id.locations_mode_include)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check storage permission status
|
// Check storage permission status
|
||||||
hasStoragePermission = false
|
hasStoragePermission = checkStoragePermission()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStart() {
|
||||||
|
super.onStart()
|
||||||
|
// Update save button state after dialog is shown
|
||||||
|
updateSaveButtonState()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSaveInstanceState(outState: Bundle) {
|
override fun onSaveInstanceState(outState: Bundle) {
|
||||||
@@ -234,6 +263,7 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
override fun onDestroyBinding(binding: DialogMusicLocationsBinding) {
|
override fun onDestroyBinding(binding: DialogMusicLocationsBinding) {
|
||||||
super.onDestroyBinding(binding)
|
super.onDestroyBinding(binding)
|
||||||
openDocumentTreeLauncher = null
|
openDocumentTreeLauncher = null
|
||||||
|
storagePermissionLauncher = null
|
||||||
binding.locationsIncludeRecycler.adapter = null
|
binding.locationsIncludeRecycler.adapter = null
|
||||||
binding.locationsExcludeRecycler.adapter = null
|
binding.locationsExcludeRecycler.adapter = null
|
||||||
binding.locationsFilterRecycler.adapter = null
|
binding.locationsFilterRecycler.adapter = null
|
||||||
@@ -384,7 +414,7 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
private fun updatePermissionCardVisibility(binding: DialogMusicLocationsBinding) {
|
private fun updatePermissionCardVisibility(binding: DialogMusicLocationsBinding) {
|
||||||
with(binding) {
|
with(binding) {
|
||||||
// Hide the permission card when permissions are granted
|
// Hide the permission card when permissions are granted
|
||||||
locationsPermsContainer.isVisible = !hasStoragePermission
|
locationsPermsCard.isVisible = !hasStoragePermission
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -455,7 +485,8 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
val binding = requireBinding()
|
val binding = requireBinding()
|
||||||
|
|
||||||
// Save the mode setting
|
// Save the mode setting
|
||||||
musicSettings.locationMode = if (isFilePickerMode) MusicLocationMode.SAF else MusicLocationMode.SYSTEM
|
musicSettings.locationMode =
|
||||||
|
if (isFilePickerMode) MusicLocationMode.SAF else MusicLocationMode.SYSTEM
|
||||||
|
|
||||||
if (isFilePickerMode) {
|
if (isFilePickerMode) {
|
||||||
// File picker mode - save include and exclude locations
|
// File picker mode - save include and exclude locations
|
||||||
@@ -493,4 +524,53 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun checkStoragePermission(): Boolean {
|
||||||
|
val permission =
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
Manifest.permission.READ_MEDIA_AUDIO
|
||||||
|
} else {
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE
|
||||||
|
}
|
||||||
|
return ContextCompat.checkSelfPermission(requireContext(), permission) ==
|
||||||
|
PackageManager.PERMISSION_GRANTED
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun requestStoragePermission() {
|
||||||
|
val permission =
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
Manifest.permission.READ_MEDIA_AUDIO
|
||||||
|
} else {
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE
|
||||||
|
}
|
||||||
|
|
||||||
|
val launcher =
|
||||||
|
requireNotNull(storagePermissionLauncher) {
|
||||||
|
"Storage permission launcher was not available"
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
L.d("Requesting storage permission: $permission")
|
||||||
|
launcher.launch(permission)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
L.e("Failed to request storage permission")
|
||||||
|
L.e(e.stackTraceToString())
|
||||||
|
requireContext().showToast(R.string.err_no_app)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateSaveButtonState() {
|
||||||
|
val dialog = dialog as? AlertDialog ?: return
|
||||||
|
|
||||||
|
val isEnabled =
|
||||||
|
if (isFilePickerMode) {
|
||||||
|
// File Picker mode: Enable save only if there's at least one folder
|
||||||
|
includeLocationAdapter.locations.isNotEmpty()
|
||||||
|
} else {
|
||||||
|
// System mode: Enable save only if permission is granted
|
||||||
|
hasStoragePermission
|
||||||
|
}
|
||||||
|
|
||||||
|
dialog.getButton(AlertDialog.BUTTON_POSITIVE)?.isEnabled = isEnabled
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,7 +73,6 @@
|
|||||||
android:paddingStart="@dimen/spacing_large"
|
android:paddingStart="@dimen/spacing_large"
|
||||||
android:paddingTop="@dimen/spacing_medium"
|
android:paddingTop="@dimen/spacing_medium"
|
||||||
android:paddingEnd="@dimen/spacing_large"
|
android:paddingEnd="@dimen/spacing_large"
|
||||||
android:paddingBottom="@dimen/spacing_medium"
|
|
||||||
app:layout_constraintBottom_toTopOf="@+id/locations_include_list_header_divider"
|
app:layout_constraintBottom_toTopOf="@+id/locations_include_list_header_divider"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
@@ -86,6 +85,7 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:clickable="true"
|
android:clickable="true"
|
||||||
android:focusable="true"
|
android:focusable="true"
|
||||||
|
android:layout_marginBottom="@dimen/spacing_medium"
|
||||||
android:textAlignment="viewStart"
|
android:textAlignment="viewStart"
|
||||||
android:textAppearance="@style/TextAppearance.Auxio.BodyLarge"
|
android:textAppearance="@style/TextAppearance.Auxio.BodyLarge"
|
||||||
app:cardBackgroundColor="?attr/colorSecondaryContainer">
|
app:cardBackgroundColor="?attr/colorSecondaryContainer">
|
||||||
|
|||||||
Reference in New Issue
Block a user