PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsPlayerFragment.kt

446 lines
17 KiB
Kotlin
Raw Normal View History

2020-10-06 08:46:04 +00:00
/*
* Copyright (c) 2020 Hemanth Savarla.
*
* 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.
*
*/
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.annotation.SuppressLint
import android.app.Activity
2018-11-30 01:06:16 +00:00
import android.content.ContentUris
import android.content.Context
2018-11-30 01:06:16 +00:00
import android.content.Intent
import android.graphics.drawable.AnimatedVectorDrawable
import android.graphics.drawable.Drawable
2018-11-30 01:06:16 +00:00
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
import android.view.GestureDetector
2018-11-30 01:06:16 +00:00
import android.view.MenuItem
import android.view.MotionEvent
2018-11-30 01:06:16 +00:00
import android.view.View
import android.widget.RelativeLayout
2018-11-30 01:06:16 +00:00
import android.widget.Toast
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 androidx.navigation.navOptions
import androidx.viewpager.widget.ViewPager
2020-08-11 18:29:44 +00:00
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
import code.name.monkey.retromusic.activities.MainActivity
2019-07-31 16:42:19 +00:00
import code.name.monkey.retromusic.activities.tageditor.AbsTagEditorActivity
import code.name.monkey.retromusic.activities.tageditor.SongTagEditorActivity
import code.name.monkey.retromusic.db.PlaylistEntity
import code.name.monkey.retromusic.db.SongEntity
2020-09-18 11:01:55 +00:00
import code.name.monkey.retromusic.db.toSongEntity
import code.name.monkey.retromusic.dialogs.*
import code.name.monkey.retromusic.extensions.currentFragment
2019-08-05 08:43:12 +00:00
import code.name.monkey.retromusic.extensions.hide
import code.name.monkey.retromusic.extensions.whichFragment
import code.name.monkey.retromusic.fragments.ReloadType
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.IPaletteColorHolder
2018-11-30 01:06:16 +00:00
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
import code.name.monkey.retromusic.service.MusicService
import code.name.monkey.retromusic.util.*
import com.google.android.material.bottomsheet.BottomSheetBehavior
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
import java.io.FileNotFoundException
import kotlin.math.abs
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, IPaletteColorHolder, PlayerAlbumCoverFragment.Callbacks {
2020-07-28 19:18:34 +00:00
2019-03-25 12:43:43 +00:00
private var playerAlbumCoverFragment: PlayerAlbumCoverFragment? = null
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_lyrics -> {
PreferenceUtil.showLyrics = !PreferenceUtil.showLyrics
showLyricsIcon(item)
return true
}
2018-11-30 01:06:16 +00:00
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 -> {
lifecycleScope.launch(IO) {
2020-09-05 14:03:12 +00:00
val playlists = get<RealRepository>().fetchPlaylists()
2020-08-31 12:30:07 +00:00
withContext(Main) {
2020-09-05 18:24:05 +00:00
AddToPlaylistDialog.create(playlists, song)
2020-08-20 20:02:40 +00:00
.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-09-05 18:24:05 +00:00
CreatePlaylistDialog.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 -> {
//Hide Bottom Bar First, else Bottom Sheet doesn't collapse fully
2021-10-25 12:38:10 +00:00
mainActivity.setBottomNavVisibility(false)
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 -> {
goToArtist(requireActivity())
2018-11-30 01:06:16 +00:00
return true
}
R.id.now_playing -> {
2021-09-24 04:37:52 +00:00
requireActivity().findNavController(R.id.fragment_container).navigate(
R.id.playing_queue_fragment,
null
)
2018-11-30 01:06:16 +00:00
return true
}
R.id.action_show_lyrics -> {
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 =
ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
song.id
)
2018-11-30 01:06:16 +00:00
retriever.setDataSource(activity, trackUri)
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
}
private fun showLyricsIcon(item: MenuItem) {
val icon =
if (PreferenceUtil.showLyrics) R.drawable.ic_lyrics else R.drawable.ic_lyrics_outline
val drawable: Drawable = RetroUtil.getTintedVectorDrawable(
requireContext(),
icon,
toolbarIconColor()
)
item.isChecked = PreferenceUtil.showLyrics
item.icon = drawable
}
2020-01-06 18:07:02 +00:00
abstract fun playerToolbar(): Toolbar?
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()
}
override fun onPlayingMetaChanged() {
updateIsFavorite()
2019-03-25 12:43:43 +00:00
updateLyrics()
2018-11-30 01:06:16 +00:00
}
override fun onFavoriteStateChanged() {
updateIsFavorite(animate = true)
}
protected open fun toggleFavorite(song: Song) {
lifecycleScope.launch(IO) {
val playlist: PlaylistEntity = libraryViewModel.favoritePlaylist()
if (playlist != null) {
val songEntity = song.toSongEntity(playlist.playListId)
val isFavorite = libraryViewModel.isFavoriteSong(songEntity).isNotEmpty()
if (isFavorite) {
libraryViewModel.removeSongFromPlaylist(songEntity)
} else {
libraryViewModel.insertSongs(listOf(song.toSongEntity(playlist.playListId)))
}
}
libraryViewModel.forceReload(ReloadType.Playlists)
requireContext().sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED))
}
}
fun updateIsFavorite(animate: Boolean = false) {
lifecycleScope.launch(IO) {
val playlist: PlaylistEntity = libraryViewModel.favoritePlaylist()
if (playlist != null) {
val song: SongEntity =
MusicPlayerRemote.currentSong.toSongEntity(playlist.playListId)
val isFavorite: Boolean = libraryViewModel.isFavoriteSong(song).isNotEmpty()
withContext(Main) {
val icon = if (animate) {
if (isFavorite) R.drawable.avd_favorite else R.drawable.avd_unfavorite
} else {
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
}
val drawable: Drawable = RetroUtil.getTintedVectorDrawable(
2020-09-05 18:24:05 +00:00
requireContext(),
icon,
toolbarIconColor()
)
if (playerToolbar() != null) {
playerToolbar()?.menu?.findItem(R.id.action_toggle_favorite)?.apply {
setIcon(drawable)
title =
if (isFavorite) getString(R.string.action_remove_from_favorites)
else getString(R.string.action_add_to_favorites)
getIcon().also {
if (it is AnimatedVectorDrawable) {
it.start()
}
}
}
}
}
2018-11-30 01:06:16 +00:00
}
}
2018-11-30 01:06:16 +00:00
}
2019-03-25 12:43:43 +00:00
private fun updateLyrics() {
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
}
withContext(Main) {
setLyrics(lyrics)
2019-03-25 12:43:43 +00:00
}
}
2019-03-25 12:43:43 +00:00
}
open fun setLyrics(l: Lyrics?) {
}
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
}
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)
view.findViewById<RelativeLayout>(R.id.statusBarShadow)?.hide()
}
@SuppressLint("ClickableViewAccessibility")
override fun onStart() {
super.onStart()
requireView().setOnTouchListener(
SwipeDetector(
requireContext(),
playerAlbumCoverFragment?.viewPager,
requireView()
)
)
2021-10-25 12:42:41 +00:00
playerToolbar()?.menu?.findItem(R.id.action_toggle_lyrics)?.let { showLyricsIcon(it) }
}
class SwipeDetector(val context: Context, val viewPager: ViewPager?, val view: View) :
View.OnTouchListener {
private var flingPlayBackController: GestureDetector = GestureDetector(
context,
object : GestureDetector.SimpleOnGestureListener() {
override fun onScroll(
e1: MotionEvent?,
e2: MotionEvent?,
distanceX: Float,
distanceY: Float
): Boolean {
return when {
abs(distanceX) > abs(distanceY) -> {
// Disallow Intercept Touch Event so that parent(BottomSheet) doesn't consume the events
view.parent.requestDisallowInterceptTouchEvent(true)
true
}
else -> {
false
}
}
}
})
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View, event: MotionEvent?): Boolean {
viewPager?.dispatchTouchEvent(event)
return flingPlayBackController.onTouchEvent(event)
}
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
}
fun goToArtist(activity: Activity) {
if (activity !is MainActivity) return
val song = MusicPlayerRemote.currentSong
activity.apply {
// Remove exit transition of current fragment so
// it doesn't exit with a weird transition
currentFragment(R.id.fragment_container)?.exitTransition = null
//Hide Bottom Bar First, else Bottom Sheet doesn't collapse fully
2021-10-25 12:38:10 +00:00
setBottomNavVisibility(false)
if (getBottomSheetBehavior().state == BottomSheetBehavior.STATE_EXPANDED) {
collapsePanel()
}
findNavController(R.id.fragment_container).navigate(
R.id.artistDetailsFragment,
bundleOf(EXTRA_ARTIST_ID to song.artistId),
navOptions {
launchSingleTop = true
},
null
)
}
}
fun goToAlbum(activity: Activity) {
if (activity !is MainActivity) return
val song = MusicPlayerRemote.currentSong
activity.apply {
currentFragment(R.id.fragment_container)?.exitTransition = null
//Hide Bottom Bar First, else Bottom Sheet doesn't collapse fully
2021-10-25 12:38:10 +00:00
setBottomNavVisibility(false)
if (getBottomSheetBehavior().state == BottomSheetBehavior.STATE_EXPANDED) {
collapsePanel()
}
findNavController(R.id.fragment_container).navigate(
R.id.albumDetailsFragment,
bundleOf(EXTRA_ALBUM_ID to song.albumId),
navOptions {
launchSingleTop = true
},
null
)
}
}
fun goToLyrics(activity: Activity) {
if (activity !is MainActivity) return
activity.apply {
//Hide Bottom Bar First, else Bottom Sheet doesn't collapse fully
setBottomNavVisibility(false)
if (getBottomSheetBehavior().state == BottomSheetBehavior.STATE_EXPANDED) {
collapsePanel()
}
findNavController(R.id.fragment_container).navigate(
R.id.lyrics_fragment,
null,
navOptions { launchSingleTop = true },
null
)
}
}