2019-03-03 09:20:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Hemanth Savarala.
|
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
|
|
|
* This 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 software 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.
|
|
|
|
*/
|
|
|
|
|
2018-12-06 08:52:57 +00:00
|
|
|
package code.name.monkey.retromusic.dialogs
|
|
|
|
|
2019-05-15 20:55:57 +00:00
|
|
|
import android.app.Dialog
|
2019-07-31 16:42:19 +00:00
|
|
|
import android.content.Intent
|
|
|
|
import android.net.Uri
|
2018-12-06 08:52:57 +00:00
|
|
|
import android.os.Bundle
|
2020-03-01 11:29:50 +00:00
|
|
|
import androidx.core.text.HtmlCompat
|
2019-05-15 20:55:57 +00:00
|
|
|
import androidx.fragment.app.DialogFragment
|
2020-05-23 22:26:45 +00:00
|
|
|
import code.name.monkey.retromusic.EXTRA_SONG
|
2018-12-06 08:52:57 +00:00
|
|
|
import code.name.monkey.retromusic.R
|
2019-07-31 16:42:19 +00:00
|
|
|
import code.name.monkey.retromusic.activities.saf.SAFGuideActivity
|
2020-07-19 11:21:15 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.colorButtons
|
2020-05-23 21:47:23 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.extraNotNull
|
2020-07-19 14:39:04 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.materialDialog
|
2019-07-31 16:42:19 +00:00
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
2018-12-06 08:52:57 +00:00
|
|
|
import code.name.monkey.retromusic.model.Song
|
|
|
|
import code.name.monkey.retromusic.util.MusicUtil
|
2019-07-31 16:42:19 +00:00
|
|
|
import code.name.monkey.retromusic.util.SAFUtil
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2019-05-15 20:55:57 +00:00
|
|
|
class DeleteSongsDialog : DialogFragment() {
|
2019-07-31 16:42:19 +00:00
|
|
|
@JvmField
|
|
|
|
var currentSong: Song? = null
|
2020-05-23 13:53:10 +00:00
|
|
|
|
2019-07-31 16:42:19 +00:00
|
|
|
@JvmField
|
|
|
|
var songsToRemove: List<Song>? = null
|
|
|
|
|
|
|
|
private var deleteSongsAsyncTask: DeleteSongsAsyncTask? = null
|
|
|
|
|
2019-05-15 20:55:57 +00:00
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
2020-05-23 22:26:45 +00:00
|
|
|
val songs = extraNotNull<List<Song>>(EXTRA_SONG).value
|
2019-07-31 16:42:19 +00:00
|
|
|
var title = 0
|
2020-05-23 21:47:23 +00:00
|
|
|
var message: CharSequence = ""
|
|
|
|
if (songs.size > 1) {
|
|
|
|
title = R.string.delete_songs_title
|
|
|
|
message = HtmlCompat.fromHtml(
|
2020-05-23 22:26:45 +00:00
|
|
|
String.format(getString(R.string.delete_x_songs), songs.size),
|
2020-05-23 21:47:23 +00:00
|
|
|
HtmlCompat.FROM_HTML_MODE_LEGACY
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
title = R.string.delete_song_title
|
|
|
|
message = HtmlCompat.fromHtml(
|
2020-05-23 22:26:45 +00:00
|
|
|
String.format(getString(R.string.delete_song_x), songs[0].title),
|
2020-05-23 21:47:23 +00:00
|
|
|
HtmlCompat.FROM_HTML_MODE_LEGACY
|
|
|
|
)
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
2019-07-31 16:42:19 +00:00
|
|
|
|
2020-07-19 14:39:04 +00:00
|
|
|
return materialDialog(title)
|
2020-05-23 21:47:23 +00:00
|
|
|
.setMessage(message)
|
|
|
|
.setCancelable(false)
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.setPositiveButton(R.string.action_delete) { _, _ ->
|
|
|
|
if ((songs.size == 1) && MusicPlayerRemote.isPlaying(songs[0])) {
|
|
|
|
MusicPlayerRemote.playNextSong()
|
2019-07-31 16:42:19 +00:00
|
|
|
}
|
|
|
|
songsToRemove = songs
|
|
|
|
deleteSongsAsyncTask = DeleteSongsAsyncTask(this@DeleteSongsDialog)
|
|
|
|
deleteSongsAsyncTask?.execute(DeleteSongsAsyncTask.LoadingInfo(songs, null))
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
2020-05-23 21:47:23 +00:00
|
|
|
.create()
|
2020-07-19 11:21:15 +00:00
|
|
|
.colorButtons()
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 16:42:19 +00:00
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
when (requestCode) {
|
|
|
|
SAFGuideActivity.REQUEST_CODE_SAF_GUIDE -> {
|
|
|
|
SAFUtil.openTreePicker(this)
|
|
|
|
}
|
|
|
|
SAFUtil.REQUEST_SAF_PICK_TREE,
|
|
|
|
SAFUtil.REQUEST_SAF_PICK_FILE -> {
|
|
|
|
if (deleteSongsAsyncTask != null) {
|
|
|
|
deleteSongsAsyncTask?.cancel(true)
|
|
|
|
}
|
|
|
|
deleteSongsAsyncTask = DeleteSongsAsyncTask(this)
|
2020-02-26 16:49:24 +00:00
|
|
|
deleteSongsAsyncTask?.execute(
|
|
|
|
DeleteSongsAsyncTask.LoadingInfo(
|
|
|
|
requestCode,
|
|
|
|
resultCode,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
)
|
2019-07-31 16:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteSongs(songs: List<Song>, safUris: List<Uri>?) {
|
2020-01-24 17:27:43 +00:00
|
|
|
MusicUtil.deleteTracks(requireActivity(), songs, safUris) { this.dismiss() }
|
2019-07-31 16:42:19 +00:00
|
|
|
}
|
2018-12-06 08:52:57 +00:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
fun create(song: Song): DeleteSongsDialog {
|
|
|
|
val list = ArrayList<Song>()
|
|
|
|
list.add(song)
|
|
|
|
return create(list)
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:20:08 +00:00
|
|
|
fun create(songs: List<Song>): DeleteSongsDialog {
|
2018-12-06 08:52:57 +00:00
|
|
|
val dialog = DeleteSongsDialog()
|
|
|
|
val args = Bundle()
|
2020-05-23 22:26:45 +00:00
|
|
|
args.putParcelableArrayList(EXTRA_SONG, ArrayList(songs))
|
2018-12-06 08:52:57 +00:00
|
|
|
dialog.arguments = args
|
|
|
|
return dialog
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|