Fixed crash on song deletion
This commit is contained in:
parent
5731b777f0
commit
63263b270b
3 changed files with 68 additions and 55 deletions
|
@ -18,6 +18,9 @@ import android.app.Activity
|
||||||
import android.app.Dialog
|
import android.app.Dialog
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.provider.MediaStore
|
||||||
|
import androidx.activity.result.IntentSenderRequest
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.core.os.bundleOf
|
import androidx.core.os.bundleOf
|
||||||
import androidx.core.text.HtmlCompat
|
import androidx.core.text.HtmlCompat
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
|
@ -61,56 +64,75 @@ class DeleteSongsDialog : DialogFragment() {
|
||||||
libraryViewModel = activity?.getViewModel() as LibraryViewModel
|
libraryViewModel = activity?.getViewModel() as LibraryViewModel
|
||||||
val songs = extraNotNull<List<Song>>(EXTRA_SONG).value
|
val songs = extraNotNull<List<Song>>(EXTRA_SONG).value
|
||||||
if (VersionUtils.hasR()) {
|
if (VersionUtils.hasR()) {
|
||||||
dismiss()
|
val deleteResultLauncher =
|
||||||
MusicUtil.deleteTracksR(requireActivity(), songs)
|
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
|
||||||
reloadTabs()
|
if (result.resultCode == Activity.RESULT_OK) {
|
||||||
}
|
if ((songs.size == 1) && MusicPlayerRemote.isPlaying(songs[0])) {
|
||||||
val pair = if (songs.size > 1) {
|
MusicPlayerRemote.playNextSong()
|
||||||
Pair(
|
}
|
||||||
R.string.delete_songs_title,
|
MusicPlayerRemote.removeFromQueue(songs)
|
||||||
HtmlCompat.fromHtml(
|
|
||||||
String.format(getString(R.string.delete_x_songs), songs.size),
|
|
||||||
HtmlCompat.FROM_HTML_MODE_LEGACY
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
Pair(
|
|
||||||
R.string.delete_song_title,
|
|
||||||
HtmlCompat.fromHtml(
|
|
||||||
String.format(getString(R.string.delete_song_x), songs[0].title),
|
|
||||||
HtmlCompat.FROM_HTML_MODE_LEGACY
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return materialDialog()
|
|
||||||
.title(pair.first)
|
|
||||||
.message(text = pair.second)
|
|
||||||
.noAutoDismiss()
|
|
||||||
.negativeButton(android.R.string.cancel) {
|
|
||||||
dismiss()
|
|
||||||
}
|
|
||||||
.positiveButton(R.string.action_delete) {
|
|
||||||
if ((songs.size == 1) && MusicPlayerRemote.isPlaying(songs[0])) {
|
|
||||||
MusicPlayerRemote.playNextSong()
|
|
||||||
}
|
|
||||||
if (!SAFUtil.isSAFRequiredForSongs(songs)) {
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
|
||||||
dismiss()
|
|
||||||
MusicUtil.deleteTracks(requireContext(), songs)
|
|
||||||
reloadTabs()
|
reloadTabs()
|
||||||
}
|
}
|
||||||
} else {
|
dismiss()
|
||||||
if (SAFUtil.isSDCardAccessGranted(requireActivity())) {
|
}
|
||||||
deleteSongs(songs)
|
val pendingIntent =
|
||||||
|
MediaStore.createDeleteRequest(requireActivity().contentResolver, songs.map {
|
||||||
|
MusicUtil.getSongFileUri(it.id)
|
||||||
|
})
|
||||||
|
deleteResultLauncher.launch(
|
||||||
|
IntentSenderRequest.Builder(pendingIntent.intentSender).build()
|
||||||
|
)
|
||||||
|
return super.onCreateDialog(savedInstanceState)
|
||||||
|
} else {
|
||||||
|
val pair = if (songs.size > 1) {
|
||||||
|
Pair(
|
||||||
|
R.string.delete_songs_title,
|
||||||
|
HtmlCompat.fromHtml(
|
||||||
|
String.format(getString(R.string.delete_x_songs), songs.size),
|
||||||
|
HtmlCompat.FROM_HTML_MODE_LEGACY
|
||||||
|
)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Pair(
|
||||||
|
R.string.delete_song_title,
|
||||||
|
HtmlCompat.fromHtml(
|
||||||
|
String.format(getString(R.string.delete_song_x), songs[0].title),
|
||||||
|
HtmlCompat.FROM_HTML_MODE_LEGACY
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return materialDialog()
|
||||||
|
.title(pair.first)
|
||||||
|
.message(text = pair.second)
|
||||||
|
.noAutoDismiss()
|
||||||
|
.negativeButton(android.R.string.cancel)
|
||||||
|
{
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
.positiveButton(R.string.action_delete)
|
||||||
|
{
|
||||||
|
if ((songs.size == 1) && MusicPlayerRemote.isPlaying(songs[0])) {
|
||||||
|
MusicPlayerRemote.playNextSong()
|
||||||
|
}
|
||||||
|
if (!SAFUtil.isSAFRequiredForSongs(songs)) {
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
dismiss()
|
||||||
|
MusicUtil.deleteTracks(requireContext(), songs)
|
||||||
|
reloadTabs()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
startActivityForResult(
|
if (SAFUtil.isSDCardAccessGranted(requireActivity())) {
|
||||||
Intent(requireActivity(), SAFGuideActivity::class.java),
|
deleteSongs(songs)
|
||||||
SAFGuideActivity.REQUEST_CODE_SAF_GUIDE
|
} else {
|
||||||
)
|
startActivityForResult(
|
||||||
|
Intent(requireActivity(), SAFGuideActivity::class.java),
|
||||||
|
SAFGuideActivity.REQUEST_CODE_SAF_GUIDE
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
|
|
@ -982,7 +982,7 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
|
|
||||||
val originalDeletePosition = originalPlayingQueue.indexOf(song)
|
val originalDeletePosition = originalPlayingQueue.indexOf(song)
|
||||||
if (originalDeletePosition != -1) {
|
if (originalDeletePosition != -1) {
|
||||||
playingQueue.removeAt(originalDeletePosition)
|
originalPlayingQueue.removeAt(originalDeletePosition)
|
||||||
rePosition(originalDeletePosition)
|
rePosition(originalDeletePosition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -524,15 +524,6 @@ object MusicUtil : KoinComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.R)
|
|
||||||
fun deleteTracksR(activity: Activity, songs: List<Song>) {
|
|
||||||
removeFromQueue(songs)
|
|
||||||
val pendingIntent = MediaStore.createDeleteRequest(activity.contentResolver, songs.map {
|
|
||||||
getSongFileUri(it.id)
|
|
||||||
})
|
|
||||||
activity.startIntentSenderForResult(pendingIntent.intentSender, 45, null, 0, 0, 0, null)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun songByGenre(genreId: Long): Song {
|
fun songByGenre(genreId: Long): Song {
|
||||||
return repository.getSongByGenre(genreId)
|
return repository.getSongByGenre(genreId)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue