PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsSlidingMusicPanelActivit...

331 lines
13 KiB
Kotlin
Raw Normal View History

2019-04-20 05:29:45 +00:00
package code.name.monkey.retromusic.activities.base
2018-11-30 01:06:16 +00:00
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
2019-02-23 17:39:02 +00:00
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.retromusic.R
2019-04-20 05:29:45 +00:00
import code.name.monkey.retromusic.fragments.MiniPlayerFragment
import code.name.monkey.retromusic.fragments.NowPlayingScreen
import code.name.monkey.retromusic.fragments.NowPlayingScreen.*
import code.name.monkey.retromusic.fragments.base.AbsPlayerFragment
import code.name.monkey.retromusic.fragments.player.adaptive.AdaptiveFragment
import code.name.monkey.retromusic.fragments.player.blur.BlurPlayerFragment
import code.name.monkey.retromusic.fragments.player.card.CardFragment
import code.name.monkey.retromusic.fragments.player.cardblur.CardBlurFragment
2019-05-08 09:57:39 +00:00
import code.name.monkey.retromusic.fragments.player.classic.ClassicPlayerFragment
2019-04-20 05:29:45 +00:00
import code.name.monkey.retromusic.fragments.player.color.ColorFragment
import code.name.monkey.retromusic.fragments.player.fit.FitFragment
import code.name.monkey.retromusic.fragments.player.flat.FlatPlayerFragment
import code.name.monkey.retromusic.fragments.player.full.FullPlayerFragment
import code.name.monkey.retromusic.fragments.player.material.MaterialFragment
import code.name.monkey.retromusic.fragments.player.normal.PlayerFragment
2019-10-04 10:58:28 +00:00
import code.name.monkey.retromusic.fragments.player.peak.PeakPlayerFragment
2019-04-20 05:29:45 +00:00
import code.name.monkey.retromusic.fragments.player.plain.PlainPlayerFragment
import code.name.monkey.retromusic.fragments.player.simple.SimplePlayerFragment
import code.name.monkey.retromusic.fragments.player.tiny.TinyPlayerFragment
2019-05-19 17:10:21 +00:00
import code.name.monkey.retromusic.helper.MusicPlayerRemote
2019-02-23 17:39:02 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
import code.name.monkey.retromusic.views.BottomNavigationBarTinted
import com.sothree.slidinguppanel.SlidingUpPanelLayout
import kotlinx.android.synthetic.main.sliding_music_panel_layout.*
abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity(), SlidingUpPanelLayout.PanelSlideListener, AbsPlayerFragment.Callbacks {
companion object {
val TAG: String = AbsSlidingMusicPanelActivity::class.java.simpleName
}
private var miniPlayerFragment: MiniPlayerFragment? = null
private var playerFragment: AbsPlayerFragment? = null
private var currentNowPlayingScreen: NowPlayingScreen? = null
private var navigationBarColor: Int = 0
private var taskColor: Int = 0
private var lightStatusBar: Boolean = false
private var lightNavigationBar: Boolean = false
private var navigationBarColorAnimator: ValueAnimator? = null
protected abstract fun createContentView(): View
val panelState: SlidingUpPanelLayout.PanelState?
get() = slidingLayout.panelState
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(createContentView())
chooseFragmentForTheme()
setupSlidingUpPanel()
2019-07-22 15:26:37 +00:00
updateTabs()
2019-02-23 17:39:02 +00:00
}
override fun onResume() {
super.onResume()
if (currentNowPlayingScreen != PreferenceUtil.getInstance(this).nowPlayingScreen) {
2019-02-23 17:39:02 +00:00
postRecreate()
}
}
override fun onDestroy() {
super.onDestroy()
if (navigationBarColorAnimator != null) navigationBarColorAnimator!!.cancel() // just in case
}
protected fun wrapSlidingMusicPanel(@LayoutRes resId: Int): View {
@SuppressLint("InflateParams")
val slidingMusicPanelLayout = layoutInflater.inflate(R.layout.sliding_music_panel_layout, null)
val contentContainer = slidingMusicPanelLayout.findViewById<ViewGroup>(R.id.mainContentFrame)
layoutInflater.inflate(resId, contentContainer)
return slidingMusicPanelLayout
}
fun setAntiDragView(antiDragView: View) {
2019-03-18 17:46:52 +00:00
slidingLayout.setAntiDragView(antiDragView)
2019-02-23 17:39:02 +00:00
}
private fun collapsePanel() {
slidingLayout.panelState = SlidingUpPanelLayout.PanelState.COLLAPSED
}
fun expandPanel() {
slidingLayout.panelState = SlidingUpPanelLayout.PanelState.EXPANDED
}
private fun setMiniPlayerAlphaProgress(progress: Float) {
2019-09-20 19:15:00 +00:00
if (miniPlayerFragment?.view == null) return
2019-02-23 17:39:02 +00:00
val alpha = 1 - progress
2019-09-20 19:15:00 +00:00
miniPlayerFragment?.view?.alpha = alpha
2019-02-23 17:39:02 +00:00
// necessary to make the views below clickable
2019-09-20 19:15:00 +00:00
miniPlayerFragment?.view?.visibility = if (alpha == 0f) View.GONE else View.VISIBLE
2019-02-23 17:39:02 +00:00
bottomNavigationView.translationY = progress * 500
bottomNavigationView.alpha = alpha
}
open fun onPanelCollapsed() {
// restore values
super.setLightStatusbar(lightStatusBar)
super.setTaskDescriptionColor(taskColor)
super.setNavigationbarColor(navigationBarColor)
super.setLightNavigationBar(lightNavigationBar)
playerFragment!!.setMenuVisibility(false)
playerFragment!!.userVisibleHint = false
playerFragment!!.onHide()
}
open fun onPanelExpanded() {
val playerFragmentColor = playerFragment!!.paletteColor
super.setTaskDescriptionColor(playerFragmentColor)
playerFragment!!.setMenuVisibility(true)
playerFragment!!.userVisibleHint = true
playerFragment!!.onShow()
onPaletteColorChanged()
}
private fun setupSlidingUpPanel() {
slidingLayout.viewTreeObserver
.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
slidingLayout.viewTreeObserver.removeOnGlobalLayoutListener(this)
2019-10-04 10:58:28 +00:00
if (currentNowPlayingScreen != PEAK) {
val params = slidingPanel.layoutParams as ViewGroup.LayoutParams
params.height = ViewGroup.LayoutParams.MATCH_PARENT
slidingPanel.layoutParams = params
}
when (panelState) {
SlidingUpPanelLayout.PanelState.EXPANDED -> {
onPanelSlide(slidingLayout, 1f)
onPanelExpanded()
}
SlidingUpPanelLayout.PanelState.COLLAPSED -> onPanelCollapsed()
else -> playerFragment!!.onHide()
2019-02-23 17:39:02 +00:00
}
}
})
slidingLayout.addPanelSlideListener(this)
}
fun toggleBottomNavigationView(toggle: Boolean) {
bottomNavigationView.visibility = if (toggle) View.GONE else View.VISIBLE
}
fun getBottomNavigationView(): BottomNavigationBarTinted {
return bottomNavigationView
}
fun hideBottomBar(hide: Boolean) {
val heightOfBar = resources.getDimensionPixelSize(R.dimen.mini_player_height)
val heightOfBarWithTabs = resources.getDimensionPixelSize(R.dimen.mini_player_height_expanded)
if (hide) {
slidingLayout.panelHeight = 0
collapsePanel()
} else {
2019-08-02 13:08:47 +00:00
if (MusicPlayerRemote.playingQueue.isNotEmpty()) {
2019-02-23 17:39:02 +00:00
slidingLayout.panelHeight = if (bottomNavigationView.visibility == View.VISIBLE) heightOfBarWithTabs else heightOfBar
}
}
}
fun setBottomBarVisibility(gone: Int) {
bottomNavigationView.visibility = gone
hideBottomBar(false)
}
private fun chooseFragmentForTheme() {
currentNowPlayingScreen = PreferenceUtil.getInstance(this).nowPlayingScreen
2019-02-23 17:39:02 +00:00
val fragment: Fragment = when (currentNowPlayingScreen) {
2019-03-18 17:46:52 +00:00
BLUR -> BlurPlayerFragment()
ADAPTIVE -> AdaptiveFragment()
NORMAL -> PlayerFragment()
CARD -> CardFragment()
BLUR_CARD -> CardBlurFragment()
FIT -> FitFragment()
FLAT -> FlatPlayerFragment()
FULL -> FullPlayerFragment()
PLAIN -> PlainPlayerFragment()
SIMPLE -> SimplePlayerFragment()
MATERIAL -> MaterialFragment()
COLOR -> ColorFragment()
TINY -> TinyPlayerFragment()
2019-05-08 09:57:39 +00:00
CLASSIC -> ClassicPlayerFragment()
2019-10-04 10:58:28 +00:00
PEAK -> PeakPlayerFragment()
2019-02-23 17:39:02 +00:00
else -> PlayerFragment()
} // must implement AbsPlayerFragment
supportFragmentManager.beginTransaction().replace(R.id.playerFragmentContainer, fragment).commit()
supportFragmentManager.executePendingTransactions()
playerFragment = supportFragmentManager.findFragmentById(R.id.playerFragmentContainer) as AbsPlayerFragment
miniPlayerFragment = supportFragmentManager.findFragmentById(R.id.miniPlayerFragment) as MiniPlayerFragment
miniPlayerFragment!!.view!!.setOnClickListener { expandPanel() }
}
override fun onServiceConnected() {
super.onServiceConnected()
if (!MusicPlayerRemote.playingQueue.isEmpty()) {
slidingLayout.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
slidingLayout.viewTreeObserver.removeOnGlobalLayoutListener(this)
hideBottomBar(false)
}
})
} // don't call hideBottomBar(true) here as it causes a bug with the SlidingUpPanelLayout
}
override fun onQueueChanged() {
super.onQueueChanged()
hideBottomBar(MusicPlayerRemote.playingQueue.isEmpty())
}
override fun onBackPressed() {
if (!handleBackPress())
super.onBackPressed()
}
open fun handleBackPress(): Boolean {
if (slidingLayout.panelHeight != 0 && playerFragment!!.onBackPressed())
return true
if (panelState == SlidingUpPanelLayout.PanelState.EXPANDED) {
collapsePanel()
return true
}
return false
}
override fun onPanelSlide(panel: View?, slideOffset: Float) {
setMiniPlayerAlphaProgress(slideOffset)
}
override fun onPanelStateChanged(panel: View, previousState: SlidingUpPanelLayout.PanelState, newState: SlidingUpPanelLayout.PanelState) {
when (newState) {
SlidingUpPanelLayout.PanelState.COLLAPSED -> onPanelCollapsed()
SlidingUpPanelLayout.PanelState.EXPANDED -> onPanelExpanded()
SlidingUpPanelLayout.PanelState.ANCHORED -> collapsePanel() // this fixes a bug where the panel would get stuck for some reason
else -> {
}
}
}
override fun onPaletteColorChanged() {
if (panelState == SlidingUpPanelLayout.PanelState.EXPANDED) {
val paletteColor = playerFragment!!.paletteColor
super.setTaskDescriptionColor(paletteColor)
val isColorLight = ColorUtil.isColorLight(paletteColor)
2019-08-05 17:44:05 +00:00
if (PreferenceUtil.getInstance(this).adaptiveColor &&
2019-03-18 17:46:52 +00:00
(currentNowPlayingScreen == NORMAL || currentNowPlayingScreen == FLAT)) {
2019-02-23 17:39:02 +00:00
super.setLightNavigationBar(true)
super.setLightStatusbar(isColorLight)
2019-03-25 12:43:43 +00:00
} else if (currentNowPlayingScreen == FULL || currentNowPlayingScreen == CARD ||
2019-05-19 17:10:21 +00:00
currentNowPlayingScreen == FIT || currentNowPlayingScreen == CLASSIC ||
2019-03-18 17:46:52 +00:00
currentNowPlayingScreen == BLUR || currentNowPlayingScreen == BLUR_CARD) {
2019-02-23 17:39:02 +00:00
super.setLightStatusbar(false)
super.setLightNavigationBar(true)
2019-08-05 17:44:05 +00:00
} else if (currentNowPlayingScreen == COLOR || currentNowPlayingScreen == TINY) {
2019-02-23 17:39:02 +00:00
super.setNavigationbarColor(paletteColor)
super.setLightNavigationBar(isColorLight)
super.setLightStatusbar(isColorLight)
} else {
super.setLightStatusbar(ColorUtil.isColorLight(ThemeStore.primaryColor(this)))
super.setLightNavigationBar(true)
}
}
}
override fun setLightStatusbar(enabled: Boolean) {
lightStatusBar = enabled
if (panelState == SlidingUpPanelLayout.PanelState.COLLAPSED) {
super.setLightStatusbar(enabled)
}
}
override fun setLightNavigationBar(enabled: Boolean) {
lightNavigationBar = enabled
if (panelState == SlidingUpPanelLayout.PanelState.COLLAPSED) {
super.setLightNavigationBar(enabled)
}
}
override fun setNavigationbarColor(color: Int) {
navigationBarColor = color
if (panelState == SlidingUpPanelLayout.PanelState.COLLAPSED) {
if (navigationBarColorAnimator != null) navigationBarColorAnimator!!.cancel()
super.setNavigationbarColor(color)
}
}
override fun setTaskDescriptionColor(color: Int) {
taskColor = color
if (panelState == SlidingUpPanelLayout.PanelState.COLLAPSED) {
super.setTaskDescriptionColor(color)
}
}
2019-03-18 17:46:52 +00:00
2019-07-22 15:26:37 +00:00
2019-08-05 17:44:05 +00:00
private fun updateTabs() {
2019-07-22 15:26:37 +00:00
bottomNavigationView.menu.clear()
val currentTabs = PreferenceUtil.getInstance(this).libraryCategoryInfos
2019-07-22 15:26:37 +00:00
for (tab in currentTabs) {
if (tab.visible) {
val menu = tab.category;
bottomNavigationView.menu.add(0, menu.id, 0, menu.stringRes)
.setIcon(menu.icon)
}
}
}
2019-03-25 12:43:43 +00:00
}