2019-04-20 05:29:45 +00:00
|
|
|
package code.name.monkey.retromusic.fragments.base
|
2018-11-30 01:06:16 +00:00
|
|
|
|
|
|
|
import android.content.ContentUris
|
|
|
|
import android.content.Intent
|
|
|
|
import android.media.MediaMetadataRetriever
|
2019-08-05 08:43:12 +00:00
|
|
|
import android.os.Build
|
2018-11-30 01:06:16 +00:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.provider.MediaStore
|
2019-03-25 12:43:43 +00:00
|
|
|
import android.text.TextUtils
|
2018-11-30 01:06:16 +00:00
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.Toast
|
2020-07-30 14:11:19 +00:00
|
|
|
import androidx.annotation.LayoutRes
|
2018-11-30 01:06:16 +00:00
|
|
|
import androidx.appcompat.widget.Toolbar
|
2020-08-11 18:29:44 +00:00
|
|
|
import androidx.core.os.bundleOf
|
2020-08-20 20:02:40 +00:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2020-08-11 18:29:44 +00:00
|
|
|
import androidx.navigation.findNavController
|
|
|
|
import code.name.monkey.retromusic.EXTRA_ALBUM_ID
|
|
|
|
import code.name.monkey.retromusic.EXTRA_ARTIST_ID
|
2018-11-30 01:06:16 +00:00
|
|
|
import code.name.monkey.retromusic.R
|
2019-07-31 16:42:19 +00:00
|
|
|
import code.name.monkey.retromusic.activities.tageditor.AbsTagEditorActivity
|
|
|
|
import code.name.monkey.retromusic.activities.tageditor.SongTagEditorActivity
|
2020-08-21 11:43:51 +00:00
|
|
|
import code.name.monkey.retromusic.db.PlaylistEntity
|
2020-02-26 16:49:24 +00:00
|
|
|
import code.name.monkey.retromusic.dialogs.*
|
2019-08-05 08:43:12 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.hide
|
2020-08-21 11:43:51 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.whichFragment
|
2020-07-28 19:18:34 +00:00
|
|
|
import code.name.monkey.retromusic.fragments.LibraryViewModel
|
2020-08-21 17:07:10 +00:00
|
|
|
import code.name.monkey.retromusic.fragments.ReloadType.Playlists
|
2019-07-31 16:42:19 +00:00
|
|
|
import code.name.monkey.retromusic.fragments.player.PlayerAlbumCoverFragment
|
2018-11-30 01:06:16 +00:00
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
|
|
|
import code.name.monkey.retromusic.interfaces.PaletteColorHolder
|
|
|
|
import code.name.monkey.retromusic.model.Song
|
2019-03-25 12:43:43 +00:00
|
|
|
import code.name.monkey.retromusic.model.lyrics.Lyrics
|
2020-08-20 20:02:40 +00:00
|
|
|
import code.name.monkey.retromusic.repository.RealRepository
|
2020-08-21 11:43:51 +00:00
|
|
|
import code.name.monkey.retromusic.service.MusicService
|
2020-02-26 16:49:24 +00:00
|
|
|
import code.name.monkey.retromusic.util.*
|
|
|
|
import kotlinx.android.synthetic.main.shadow_statusbar_toolbar.*
|
2020-08-21 11:43:51 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2020-08-20 20:02:40 +00:00
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import org.koin.android.ext.android.get
|
2020-08-21 11:43:51 +00:00
|
|
|
import org.koin.android.ext.android.inject
|
2020-07-28 19:18:34 +00:00
|
|
|
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
|
2019-03-25 12:43:43 +00:00
|
|
|
import java.io.FileNotFoundException
|
2019-02-23 17:39:02 +00:00
|
|
|
|
2020-08-11 21:31:09 +00:00
|
|
|
abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMainActivityFragment(layout),
|
|
|
|
Toolbar.OnMenuItemClickListener, PaletteColorHolder, PlayerAlbumCoverFragment.Callbacks {
|
2020-07-28 19:18:34 +00:00
|
|
|
|
2019-03-25 12:43:43 +00:00
|
|
|
private var playerAlbumCoverFragment: PlayerAlbumCoverFragment? = null
|
2020-07-28 19:18:34 +00:00
|
|
|
protected val libraryViewModel by sharedViewModel<LibraryViewModel>()
|
2018-11-30 01:06:16 +00:00
|
|
|
|
2019-08-02 18:34:18 +00:00
|
|
|
override fun onMenuItemClick(
|
2020-01-06 18:07:02 +00:00
|
|
|
item: MenuItem
|
2019-08-02 18:34:18 +00:00
|
|
|
): Boolean {
|
2018-11-30 01:06:16 +00:00
|
|
|
val song = MusicPlayerRemote.currentSong
|
|
|
|
when (item.itemId) {
|
|
|
|
R.id.action_toggle_favorite -> {
|
|
|
|
toggleFavorite(song)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_share -> {
|
2020-02-02 12:44:16 +00:00
|
|
|
SongShareDialog.create(song).show(childFragmentManager, "SHARE_SONG")
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-03-01 08:54:39 +00:00
|
|
|
R.id.action_go_to_drive_mode -> {
|
|
|
|
NavigationUtil.gotoDriveMode(requireActivity())
|
|
|
|
return true
|
|
|
|
}
|
2018-11-30 01:06:16 +00:00
|
|
|
R.id.action_delete_from_device -> {
|
2020-02-02 12:44:16 +00:00
|
|
|
DeleteSongsDialog.create(song).show(childFragmentManager, "DELETE_SONGS")
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_add_to_playlist -> {
|
2020-08-21 11:43:51 +00:00
|
|
|
lifecycleScope.launch(IO) {
|
2020-08-20 20:02:40 +00:00
|
|
|
val playlists = get<RealRepository>().roomPlaylists()
|
2020-08-31 12:30:07 +00:00
|
|
|
withContext(Main) {
|
2020-08-20 20:02:40 +00:00
|
|
|
AddToRetroPlaylist.create(playlists, song)
|
|
|
|
.show(childFragmentManager, "ADD_PLAYLIST")
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_clear_playing_queue -> {
|
|
|
|
MusicPlayerRemote.clearQueue()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_save_playing_queue -> {
|
2020-08-31 12:30:07 +00:00
|
|
|
CreateRetroPlaylist.create(ArrayList(MusicPlayerRemote.playingQueue))
|
2020-02-02 12:44:16 +00:00
|
|
|
.show(childFragmentManager, "ADD_TO_PLAYLIST")
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_tag_editor -> {
|
|
|
|
val intent = Intent(activity, SongTagEditorActivity::class.java)
|
|
|
|
intent.putExtra(AbsTagEditorActivity.EXTRA_ID, song.id)
|
|
|
|
startActivity(intent)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_details -> {
|
2020-02-02 12:44:16 +00:00
|
|
|
SongDetailDialog.create(song).show(childFragmentManager, "SONG_DETAIL")
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_go_to_album -> {
|
2020-08-11 22:20:22 +00:00
|
|
|
mainActivity.collapsePanel()
|
2020-08-11 18:29:44 +00:00
|
|
|
requireActivity().findNavController(R.id.fragment_container).navigate(
|
|
|
|
R.id.albumDetailsFragment,
|
|
|
|
bundleOf(EXTRA_ALBUM_ID to song.albumId)
|
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_go_to_artist -> {
|
2020-08-11 22:20:22 +00:00
|
|
|
mainActivity.collapsePanel()
|
2020-08-11 18:29:44 +00:00
|
|
|
requireActivity().findNavController(R.id.fragment_container).navigate(
|
|
|
|
R.id.artistDetailsFragment,
|
|
|
|
bundleOf(EXTRA_ARTIST_ID to song.artistId)
|
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.now_playing -> {
|
2019-07-31 16:42:19 +00:00
|
|
|
NavigationUtil.goToPlayingQueue(requireActivity())
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_show_lyrics -> {
|
2019-10-28 17:08:09 +00:00
|
|
|
NavigationUtil.goToLyrics(requireActivity())
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_equalizer -> {
|
2019-07-31 16:42:19 +00:00
|
|
|
NavigationUtil.openEqualizer(requireActivity())
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_sleep_timer -> {
|
2020-03-01 12:05:43 +00:00
|
|
|
SleepTimerDialog().show(parentFragmentManager, TAG)
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_set_as_ringtone -> {
|
2019-07-31 16:42:19 +00:00
|
|
|
if (RingtoneManager.requiresDialog(requireActivity())) {
|
|
|
|
RingtoneManager.getDialog(requireActivity())
|
2019-04-05 05:49:40 +00:00
|
|
|
}
|
2019-07-31 16:42:19 +00:00
|
|
|
val ringtoneManager = RingtoneManager(requireActivity())
|
2019-04-05 05:49:40 +00:00
|
|
|
ringtoneManager.setRingtone(song)
|
2018-11-30 01:06:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.action_go_to_genre -> {
|
|
|
|
val retriever = MediaMetadataRetriever()
|
2020-01-06 18:07:02 +00:00
|
|
|
val trackUri =
|
2020-02-26 16:49:24 +00:00
|
|
|
ContentUris.withAppendedId(
|
|
|
|
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
|
|
|
song.id.toLong()
|
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
retriever.setDataSource(activity, trackUri)
|
2020-02-26 16:49:24 +00:00
|
|
|
var genre: String? =
|
|
|
|
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)
|
2018-11-30 01:06:16 +00:00
|
|
|
if (genre == null) {
|
|
|
|
genre = "Not Specified"
|
|
|
|
}
|
|
|
|
Toast.makeText(context, genre, Toast.LENGTH_SHORT).show()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
protected open fun toggleFavorite(song: Song) {
|
2020-08-21 11:43:51 +00:00
|
|
|
lifecycleScope.launch(IO) {
|
2020-08-31 12:30:07 +00:00
|
|
|
val playlist: PlaylistEntity? = repository.favoritePlaylist()
|
|
|
|
if (playlist != null) {
|
|
|
|
val songEntity = song.toSongEntity(playlist.playListId)
|
|
|
|
val isFavorite = repository.isFavoriteSong(songEntity).isNotEmpty()
|
|
|
|
if (isFavorite) {
|
|
|
|
repository.removeSongFromPlaylist(songEntity)
|
|
|
|
} else {
|
|
|
|
repository.insertSongs(listOf(song.toSongEntity(playlist.playListId)))
|
|
|
|
}
|
2020-08-21 11:43:51 +00:00
|
|
|
}
|
2020-08-31 12:30:07 +00:00
|
|
|
libraryViewModel.forceReload(Playlists)
|
2020-08-21 11:43:51 +00:00
|
|
|
requireContext().sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED))
|
|
|
|
}
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 18:07:02 +00:00
|
|
|
abstract fun playerToolbar(): Toolbar?
|
2018-12-05 04:29:55 +00:00
|
|
|
|
2018-11-30 01:06:16 +00:00
|
|
|
abstract fun onShow()
|
|
|
|
|
|
|
|
abstract fun onHide()
|
|
|
|
|
|
|
|
abstract fun onBackPressed(): Boolean
|
|
|
|
|
|
|
|
abstract fun toolbarIconColor(): Int
|
|
|
|
|
|
|
|
override fun onServiceConnected() {
|
|
|
|
updateIsFavorite()
|
2019-03-25 12:43:43 +00:00
|
|
|
updateLyrics()
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPlayingMetaChanged() {
|
|
|
|
updateIsFavorite()
|
2019-03-25 12:43:43 +00:00
|
|
|
updateLyrics()
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
2020-08-21 11:45:38 +00:00
|
|
|
|
2018-11-30 01:06:16 +00:00
|
|
|
fun updateIsFavorite() {
|
2020-08-21 11:43:51 +00:00
|
|
|
lifecycleScope.launch(IO) {
|
2020-08-31 12:30:07 +00:00
|
|
|
val playlist: PlaylistEntity = repository.favoritePlaylist()
|
2020-08-21 11:43:51 +00:00
|
|
|
val song = MusicPlayerRemote.currentSong.toSongEntity(playlist.playListId)
|
|
|
|
val isFavorite = repository.isFavoriteSong(song).isNotEmpty()
|
2020-08-31 12:30:07 +00:00
|
|
|
withContext(Main) {
|
|
|
|
val icon = if (isFavorite) R.drawable.ic_favorite
|
|
|
|
else R.drawable.ic_favorite_border
|
2020-02-26 16:49:24 +00:00
|
|
|
val drawable =
|
2020-08-31 12:30:07 +00:00
|
|
|
RetroUtil.getTintedVectorDrawable(
|
|
|
|
requireContext(),
|
|
|
|
icon,
|
|
|
|
toolbarIconColor()
|
|
|
|
)
|
2020-08-21 11:43:51 +00:00
|
|
|
if (playerToolbar() != null) {
|
|
|
|
playerToolbar()?.menu?.findItem(R.id.action_toggle_favorite)
|
|
|
|
?.setIcon(drawable)?.title =
|
|
|
|
if (isFavorite) getString(R.string.action_remove_from_favorites)
|
|
|
|
else getString(R.string.action_add_to_favorites)
|
|
|
|
}
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
2020-08-21 11:43:51 +00:00
|
|
|
}
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 12:43:43 +00:00
|
|
|
private fun updateLyrics() {
|
2020-08-21 11:43:51 +00:00
|
|
|
setLyrics(null)
|
|
|
|
lifecycleScope.launch(IO) {
|
|
|
|
val song = MusicPlayerRemote.currentSong
|
|
|
|
val lyrics = try {
|
|
|
|
var data: String? = LyricUtil.getStringFromFile(song.title, song.artistName)
|
|
|
|
if (TextUtils.isEmpty(data)) {
|
|
|
|
data = MusicUtil.getLyrics(song)
|
|
|
|
if (TextUtils.isEmpty(data)) {
|
|
|
|
null
|
|
|
|
} else {
|
|
|
|
Lyrics.parse(song, data)
|
|
|
|
}
|
|
|
|
} else Lyrics.parse(song, data!!)
|
|
|
|
} catch (err: FileNotFoundException) {
|
|
|
|
null
|
2019-03-25 12:43:43 +00:00
|
|
|
}
|
2020-08-21 11:43:51 +00:00
|
|
|
withContext(Main) {
|
|
|
|
setLyrics(lyrics)
|
2019-03-25 12:43:43 +00:00
|
|
|
}
|
2020-08-21 11:43:51 +00:00
|
|
|
}
|
2019-03-25 12:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
open fun setLyrics(l: Lyrics?) {
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:43:51 +00:00
|
|
|
private val repository by inject<RealRepository>()
|
|
|
|
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
|
super.onActivityCreated(savedInstanceState)
|
|
|
|
lifecycleScope.launch(IO) {
|
|
|
|
if (repository.checkPlaylistExists(getString(R.string.favorites)).isEmpty()) {
|
|
|
|
repository.createPlaylist(PlaylistEntity(getString(R.string.favorites)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 01:06:16 +00:00
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2020-06-06 18:57:28 +00:00
|
|
|
if (PreferenceUtil.isFullScreenMode &&
|
2020-01-06 18:07:02 +00:00
|
|
|
view.findViewById<View>(R.id.status_bar) != null
|
|
|
|
) {
|
2019-02-23 17:39:02 +00:00
|
|
|
view.findViewById<View>(R.id.status_bar).visibility = View.GONE
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
2020-08-21 11:43:51 +00:00
|
|
|
playerAlbumCoverFragment = whichFragment(R.id.playerAlbumCoverFragment)
|
2019-03-25 12:43:43 +00:00
|
|
|
playerAlbumCoverFragment?.setCallbacks(this)
|
2019-08-05 08:43:12 +00:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
2019-08-05 16:13:44 +00:00
|
|
|
statusBarShadow?.hide()
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
val TAG: String = AbsPlayerFragment::class.java.simpleName
|
2019-03-25 12:43:43 +00:00
|
|
|
const val VISIBILITY_ANIM_DURATION: Long = 300
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 17:39:02 +00:00
|
|
|
protected fun getUpNextAndQueueTime(): String {
|
|
|
|
val duration = MusicPlayerRemote.getQueueDurationMillis(MusicPlayerRemote.position)
|
2020-08-21 12:12:40 +00:00
|
|
|
|
2019-02-23 17:39:02 +00:00
|
|
|
return MusicUtil.buildInfoString(
|
2020-01-06 18:07:02 +00:00
|
|
|
resources.getString(R.string.up_next),
|
|
|
|
MusicUtil.getReadableDurationString(duration)
|
2019-02-23 17:39:02 +00:00
|
|
|
)
|
|
|
|
}
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|