2019-04-20 05:29:45 +00:00
|
|
|
package code.name.monkey.retromusic.fragments.player.color
|
2018-12-05 15:34:26 +00:00
|
|
|
|
|
|
|
import android.animation.ArgbEvaluator
|
|
|
|
import android.animation.ValueAnimator
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.content.Intent
|
2019-02-19 10:38:51 +00:00
|
|
|
import android.graphics.Color
|
2018-12-05 15:34:26 +00:00
|
|
|
import android.graphics.drawable.Drawable
|
|
|
|
import android.os.AsyncTask
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.text.TextUtils
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.appcompat.widget.Toolbar
|
2019-02-19 10:38:51 +00:00
|
|
|
import androidx.palette.graphics.Palette
|
2018-12-05 15:34:26 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.ATHUtil
|
|
|
|
import code.name.monkey.appthemehelper.util.ColorUtil
|
|
|
|
import code.name.monkey.appthemehelper.util.MaterialValueHelper
|
|
|
|
import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper
|
|
|
|
import code.name.monkey.retromusic.R
|
2019-06-17 02:46:50 +00:00
|
|
|
import code.name.monkey.retromusic.activities.LyricsActivity
|
|
|
|
import code.name.monkey.retromusic.fragments.base.AbsPlayerFragment
|
2018-12-05 15:34:26 +00:00
|
|
|
import code.name.monkey.retromusic.glide.RetroMusicColoredTarget
|
2019-09-16 18:02:40 +00:00
|
|
|
import code.name.monkey.retromusic.glide.SongGlideRequest
|
2018-12-05 15:34:26 +00:00
|
|
|
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper
|
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
|
|
|
import code.name.monkey.retromusic.model.Song
|
|
|
|
import code.name.monkey.retromusic.model.lyrics.Lyrics
|
|
|
|
import code.name.monkey.retromusic.util.MusicUtil
|
|
|
|
import code.name.monkey.retromusic.util.RetroColorUtil
|
|
|
|
import code.name.monkey.retromusic.util.ViewUtil
|
2019-09-16 18:02:40 +00:00
|
|
|
import com.bumptech.glide.Glide
|
|
|
|
import com.bumptech.glide.request.animation.GlideAnimation
|
2018-12-05 15:34:26 +00:00
|
|
|
import kotlinx.android.synthetic.main.fragment_color_player.*
|
|
|
|
|
|
|
|
class ColorFragment : AbsPlayerFragment() {
|
2019-03-09 02:32:29 +00:00
|
|
|
override fun playerToolbar(): Toolbar {
|
2018-12-05 15:34:26 +00:00
|
|
|
return playerToolbar
|
|
|
|
}
|
|
|
|
|
|
|
|
override val paletteColor: Int
|
|
|
|
get() = backgroundColor
|
|
|
|
|
|
|
|
override fun onColorChanged(color: Int) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFavoriteToggled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private var lastColor: Int = 0
|
|
|
|
private var backgroundColor: Int = 0
|
|
|
|
|
|
|
|
private var playbackControlsFragment: ColorPlaybackControlsFragment? = null
|
|
|
|
|
|
|
|
private var valueAnimator: ValueAnimator? = null
|
|
|
|
private var updateLyricsAsyncTask: AsyncTask<*, *, *>? = null
|
2019-03-25 12:43:43 +00:00
|
|
|
private var lyricsColor: Lyrics? = null
|
2018-12-05 15:34:26 +00:00
|
|
|
|
|
|
|
override fun onShow() {
|
|
|
|
playbackControlsFragment!!.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onHide() {
|
|
|
|
playbackControlsFragment!!.hide()
|
|
|
|
onBackPressed()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBackPressed(): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun toolbarIconColor(): Int {
|
|
|
|
return lastColor
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun toggleFavorite(song: Song) {
|
|
|
|
super.toggleFavorite(song)
|
|
|
|
if (song.id == MusicPlayerRemote.currentSong.id) {
|
|
|
|
updateIsFavorite()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
if (valueAnimator != null) {
|
|
|
|
valueAnimator!!.cancel()
|
|
|
|
valueAnimator = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?): View? {
|
|
|
|
|
|
|
|
return inflater.inflate(R.layout.fragment_color_player, container, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
setUpSubFragments()
|
|
|
|
setUpPlayerToolbar()
|
|
|
|
setupViews()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setUpSubFragments() {
|
|
|
|
playbackControlsFragment = childFragmentManager.findFragmentById(R.id.playbackControlsFragment) as ColorPlaybackControlsFragment?
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setUpPlayerToolbar() {
|
|
|
|
playerToolbar.apply {
|
|
|
|
inflateMenu(R.menu.menu_player)
|
|
|
|
setNavigationOnClickListener { activity!!.onBackPressed() }
|
|
|
|
setOnMenuItemClickListener(this@ColorFragment)
|
|
|
|
ToolbarContentTintHelper.colorizeToolbar(this, ATHUtil.resolveColor(context, R.attr.iconColor), activity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPlayingMetaChanged() {
|
|
|
|
super.onPlayingMetaChanged()
|
|
|
|
updateSong()
|
|
|
|
updateLyricsLocal()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onServiceConnected() {
|
|
|
|
super.onServiceConnected()
|
|
|
|
updateSong()
|
|
|
|
updateLyricsLocal()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateSong() {
|
|
|
|
|
2019-09-16 18:02:40 +00:00
|
|
|
|
|
|
|
SongGlideRequest.Builder.from(Glide.with(requireActivity()), MusicPlayerRemote.currentSong)
|
|
|
|
.checkIgnoreMediaStore(requireContext())
|
|
|
|
.generatePalette(requireContext())
|
|
|
|
.build()
|
2018-12-12 20:59:07 +00:00
|
|
|
.into(object : RetroMusicColoredTarget(playerImage) {
|
|
|
|
override fun onColorReady(color: Int) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-22 18:56:40 +00:00
|
|
|
override fun onResourceReady(resource: BitmapPaletteWrapper?, glideAnimation: GlideAnimation<in BitmapPaletteWrapper>?) {
|
2018-12-12 20:59:07 +00:00
|
|
|
super.onResourceReady(resource, glideAnimation)
|
2019-09-22 18:56:40 +00:00
|
|
|
resource?.let {
|
|
|
|
val background = resource.palette.getColor()
|
2019-09-16 18:02:40 +00:00
|
|
|
|
2019-02-19 10:38:51 +00:00
|
|
|
|
2019-09-22 18:56:40 +00:00
|
|
|
val palette = resource.palette
|
|
|
|
val swatch = RetroColorUtil.getSwatch(palette)
|
2018-12-12 20:59:07 +00:00
|
|
|
|
2019-09-22 18:56:40 +00:00
|
|
|
val textColor = RetroColorUtil.getTextColor(palette)
|
|
|
|
val backgroundColor = swatch.rgb
|
|
|
|
|
|
|
|
setColors(backgroundColor, textColor)
|
|
|
|
}
|
2018-12-12 20:59:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-22 18:56:40 +00:00
|
|
|
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
|
2019-09-16 18:02:40 +00:00
|
|
|
super.onLoadFailed(e, errorDrawable)
|
2018-12-12 20:59:07 +00:00
|
|
|
val backgroundColor = defaultFooterColor
|
|
|
|
val textColor = if (ColorUtil.isColorLight(defaultFooterColor))
|
|
|
|
MaterialValueHelper.getPrimaryTextColor(context, true)
|
|
|
|
else
|
|
|
|
MaterialValueHelper.getPrimaryTextColor(context, false)
|
|
|
|
|
|
|
|
setColors(backgroundColor, textColor)
|
|
|
|
}
|
|
|
|
})
|
2018-12-05 15:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun setColors(backgroundColor: Int, textColor: Int) {
|
|
|
|
playbackControlsFragment!!.setDark(textColor, backgroundColor)
|
|
|
|
|
2019-01-19 06:18:26 +00:00
|
|
|
colorGradientBackground?.setBackgroundColor(backgroundColor)
|
2018-12-05 15:34:26 +00:00
|
|
|
|
|
|
|
ToolbarContentTintHelper.colorizeToolbar(playerToolbar, textColor, activity)
|
|
|
|
|
|
|
|
lastColor = textColor
|
|
|
|
|
|
|
|
this.backgroundColor = backgroundColor
|
|
|
|
|
|
|
|
if (playerActivity != null) {
|
|
|
|
playerActivity!!.setLightNavigationBar(ColorUtil.isColorLight(backgroundColor))
|
|
|
|
}
|
|
|
|
callbacks!!.onPaletteColorChanged()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun colorize(i: Int) {
|
|
|
|
if (valueAnimator != null) {
|
|
|
|
valueAnimator!!.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
valueAnimator = ValueAnimator.ofObject(ArgbEvaluator(), paletteColor, i)
|
|
|
|
valueAnimator!!.addUpdateListener { animation ->
|
2019-01-19 06:18:26 +00:00
|
|
|
colorGradientBackground?.setBackgroundColor(animation.animatedValue as Int)
|
2018-12-05 15:34:26 +00:00
|
|
|
}
|
|
|
|
valueAnimator!!.setDuration(ViewUtil.RETRO_MUSIC_ANIM_TIME.toLong()).start()
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
|
private fun updateLyricsLocal() {
|
|
|
|
if (updateLyricsAsyncTask != null) {
|
|
|
|
updateLyricsAsyncTask!!.cancel(false)
|
|
|
|
}
|
|
|
|
val song = MusicPlayerRemote.currentSong
|
2018-12-08 03:33:02 +00:00
|
|
|
updateLyricsAsyncTask = object : AsyncTask<Void?, Void?, Lyrics?>() {
|
2018-12-05 15:34:26 +00:00
|
|
|
override fun onPreExecute() {
|
|
|
|
super.onPreExecute()
|
2019-03-25 12:43:43 +00:00
|
|
|
lyricsColor = null
|
2018-12-05 15:34:26 +00:00
|
|
|
playerToolbar.menu.removeItem(R.id.action_show_lyrics)
|
|
|
|
}
|
|
|
|
|
2018-12-08 03:33:02 +00:00
|
|
|
override fun doInBackground(vararg params: Void?): Lyrics? {
|
2018-12-05 15:34:26 +00:00
|
|
|
val data = MusicUtil.getLyrics(song)
|
|
|
|
return if (TextUtils.isEmpty(data)) {
|
|
|
|
null
|
2019-03-04 03:55:09 +00:00
|
|
|
} else Lyrics.parse(song, data!!)
|
2018-12-05 15:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPostExecute(l: Lyrics?) {
|
2019-03-25 12:43:43 +00:00
|
|
|
lyricsColor = l
|
|
|
|
if (lyricsColor == null) {
|
2019-07-23 18:32:41 +00:00
|
|
|
lyricsView?.setText(R.string.no_lyrics_found)
|
2018-12-05 15:34:26 +00:00
|
|
|
} else {
|
2019-07-23 18:32:41 +00:00
|
|
|
lyricsView?.text = lyricsColor!!.text
|
2018-12-05 15:34:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 03:33:02 +00:00
|
|
|
override fun onCancelled(s: Lyrics?) {
|
2018-12-05 15:34:26 +00:00
|
|
|
onPostExecute(null)
|
|
|
|
}
|
|
|
|
}.execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setupViews() {
|
|
|
|
lyricsView.setOnClickListener {
|
|
|
|
if (lyricsContainer!!.visibility == View.GONE) {
|
|
|
|
lyricsContainer!!.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
lyricsContainer!!.visibility = View.GONE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
playerImage.setOnClickListener {
|
|
|
|
if (lyricsContainer!!.visibility == View.GONE) {
|
|
|
|
lyricsContainer!!.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
lyricsContainer!!.visibility = View.GONE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expand.setOnClickListener { startActivity(Intent(context, LyricsActivity::class.java)) }
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
fun newInstance(): ColorFragment {
|
|
|
|
val args = Bundle()
|
|
|
|
val fragment = ColorFragment()
|
|
|
|
fragment.arguments = args
|
|
|
|
return fragment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 10:38:51 +00:00
|
|
|
|
|
|
|
|
2019-03-18 17:46:52 +00:00
|
|
|
fun Palette.getColor(): Int {
|
2019-02-19 10:38:51 +00:00
|
|
|
return when {
|
|
|
|
darkMutedSwatch != null -> darkMutedSwatch!!.rgb
|
|
|
|
mutedSwatch != null -> mutedSwatch!!.rgb
|
|
|
|
lightMutedSwatch != null -> lightMutedSwatch!!.rgb
|
|
|
|
else -> Palette.Swatch(Color.BLACK, 1).rgb
|
|
|
|
}
|
|
|
|
}
|