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

379 lines
15 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
2019-12-01 11:28:57 +00:00
import android.graphics.Color
2018-11-30 01:06:16 +00:00
import android.os.Bundle
2019-12-01 11:28:57 +00:00
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
2020-01-24 16:54:08 +00:00
import android.widget.FrameLayout
2019-02-23 17:39:02 +00:00
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
2019-12-01 11:28:57 +00:00
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.ColorUtil
2019-02-23 17:39:02 +00:00
import code.name.monkey.retromusic.R
2019-12-01 11:28:57 +00:00
import code.name.monkey.retromusic.extensions.hide
import code.name.monkey.retromusic.extensions.show
import code.name.monkey.retromusic.fragments.MiniPlayerFragment
import code.name.monkey.retromusic.fragments.NowPlayingScreen
import code.name.monkey.retromusic.fragments.NowPlayingScreen.*
2019-04-20 05:29:45 +00:00
import code.name.monkey.retromusic.fragments.base.AbsPlayerFragment
2020-05-03 19:24:11 +00:00
import code.name.monkey.retromusic.fragments.player.TestPlayerFragment
2019-04-20 05:29:45 +00:00
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
2020-01-06 18:07:02 +00:00
import code.name.monkey.retromusic.fragments.player.circle.CirclePlayerFragment
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-12-01 15:27:01 +00:00
import code.name.monkey.retromusic.model.CategoryInfo
2019-12-01 11:28:57 +00:00
import code.name.monkey.retromusic.util.DensityUtil
import code.name.monkey.retromusic.util.PreferenceUtil
2019-02-23 17:39:02 +00:00
import code.name.monkey.retromusic.views.BottomNavigationBarTinted
import com.google.android.material.bottomsheet.BottomSheetBehavior
import kotlinx.android.synthetic.main.sliding_music_panel_layout.*
2019-02-23 17:39:02 +00:00
abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity(),
AbsPlayerFragment.Callbacks {
2019-12-01 11:28:57 +00:00
companion object {
val TAG: String = AbsSlidingMusicPanelActivity::class.java.simpleName
}
2020-01-24 16:54:08 +00:00
private lateinit var bottomSheetBehavior: BottomSheetBehavior<FrameLayout>
2019-12-01 11:28:57 +00:00
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
private val panelState: Int
get() = bottomSheetBehavior.state
private val bottomSheetCallbackList = object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
setMiniPlayerAlphaProgress(slideOffset)
dimBackground.show()
dimBackground.alpha = slideOffset
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
when (newState) {
BottomSheetBehavior.STATE_EXPANDED -> {
onPanelExpanded()
}
BottomSheetBehavior.STATE_COLLAPSED -> {
onPanelCollapsed()
dimBackground.hide()
}
else -> {
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(createContentView())
chooseFragmentForTheme()
setupSlidingUpPanel()
updateTabs()
bottomSheetBehavior = BottomSheetBehavior.from(slidingPanel)
val themeColor = ATHUtil.resolveColor(this, android.R.attr.windowBackground, Color.GRAY)
dimBackground.setBackgroundColor(ColorUtil.withAlpha(themeColor, 0.5f))
}
override fun onResume() {
super.onResume()
if (currentNowPlayingScreen != PreferenceUtil.getInstance(this).nowPlayingScreen) {
postRecreate()
}
bottomSheetBehavior.addBottomSheetCallback(bottomSheetCallbackList)
if (bottomSheetBehavior.state == BottomSheetBehavior.STATE_EXPANDED) {
setMiniPlayerAlphaProgress(1f)
}
}
override fun onDestroy() {
super.onDestroy()
bottomSheetBehavior.removeBottomSheetCallback(bottomSheetCallbackList)
2019-12-01 15:27:01 +00:00
if (navigationBarColorAnimator != null) navigationBarColorAnimator?.cancel() // just in case
2019-12-01 11:28:57 +00:00
}
protected fun wrapSlidingMusicPanel(@LayoutRes resId: Int): View {
val slidingMusicPanelLayout =
layoutInflater.inflate(R.layout.sliding_music_panel_layout, null)
val contentContainer =
slidingMusicPanelLayout.findViewById<ViewGroup>(R.id.mainContentFrame)
2019-12-01 11:28:57 +00:00
layoutInflater.inflate(resId, contentContainer)
return slidingMusicPanelLayout
}
private fun collapsePanel() {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
fun expandPanel() {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
setMiniPlayerAlphaProgress(1f)
}
private fun setMiniPlayerAlphaProgress(progress: Float) {
if (miniPlayerFragment?.view == null) return
val alpha = 1 - progress
miniPlayerFragment?.view?.alpha = alpha
// necessary to make the views below clickable
miniPlayerFragment?.view?.visibility = if (alpha == 0f) View.GONE else View.VISIBLE
bottomNavigationView.translationY = progress * 500
2019-12-01 15:27:01 +00:00
//bottomNavigationView.alpha = alpha
2019-12-01 11:28:57 +00:00
}
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() {
slidingPanel.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
2019-12-01 11:28:57 +00:00
override fun onGlobalLayout() {
slidingPanel.viewTreeObserver.removeOnGlobalLayoutListener(this)
if (currentNowPlayingScreen != PEAK) {
val params = slidingPanel.layoutParams as ViewGroup.LayoutParams
params.height = ViewGroup.LayoutParams.MATCH_PARENT
slidingPanel.layoutParams = params
}
when (panelState) {
BottomSheetBehavior.STATE_EXPANDED -> onPanelExpanded()
BottomSheetBehavior.STATE_COLLAPSED -> onPanelCollapsed()
else -> playerFragment!!.onHide()
}
}
})
}
fun toggleBottomNavigationView(toggle: Boolean) {
bottomNavigationView.visibility = if (toggle) View.GONE else View.VISIBLE
}
fun getBottomNavigationView(): BottomNavigationBarTinted {
return bottomNavigationView
}
private fun hideBottomBar(hide: Boolean) {
val heightOfBar = resources.getDimensionPixelSize(R.dimen.mini_player_height)
val heightOfBarWithTabs =
resources.getDimensionPixelSize(R.dimen.mini_player_height_expanded)
2019-12-01 11:28:57 +00:00
if (hide) {
bottomSheetBehavior.isHideable = true
bottomSheetBehavior.peekHeight = 0
collapsePanel()
bottomNavigationView.elevation = DensityUtil.dip2px(this, 10f).toFloat()
} else {
if (MusicPlayerRemote.playingQueue.isNotEmpty()) {
2020-02-25 06:44:46 +00:00
slidingPanel.elevation = DensityUtil.dip2px(this, 10f).toFloat()
2019-12-01 11:28:57 +00:00
bottomNavigationView.elevation = DensityUtil.dip2px(this, 10f).toFloat()
bottomSheetBehavior.isHideable = false
2019-12-30 11:01:50 +00:00
bottomSheetBehavior.peekHeight =
if (bottomNavigationView.visibility == View.VISIBLE) heightOfBarWithTabs else heightOfBar
2019-12-01 11:28:57 +00:00
}
}
}
fun setBottomBarVisibility(gone: Int) {
bottomNavigationView.visibility = gone
hideBottomBar(false)
}
private fun chooseFragmentForTheme() {
currentNowPlayingScreen = PreferenceUtil.getInstance(this).nowPlayingScreen
val fragment: Fragment = when (currentNowPlayingScreen) {
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()
PEAK -> PeakPlayerFragment()
2020-01-06 18:07:02 +00:00
CIRCLE -> CirclePlayerFragment()
2020-05-03 19:24:11 +00:00
EXAMPLE -> TestPlayerFragment()
2019-12-01 11:28:57 +00:00
else -> PlayerFragment()
} // must implement AbsPlayerFragment
supportFragmentManager.beginTransaction().replace(R.id.playerFragmentContainer, fragment)
2019-12-30 11:01:50 +00:00
.commit()
2019-12-01 11:28:57 +00:00
supportFragmentManager.executePendingTransactions()
playerFragment =
supportFragmentManager.findFragmentById(R.id.playerFragmentContainer) as AbsPlayerFragment
miniPlayerFragment =
supportFragmentManager.findFragmentById(R.id.miniPlayerFragment) as MiniPlayerFragment
2019-12-01 11:28:57 +00:00
miniPlayerFragment?.view?.setOnClickListener { expandPanel() }
}
override fun onServiceConnected() {
super.onServiceConnected()
if (MusicPlayerRemote.playingQueue.isNotEmpty()) {
slidingPanel.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
2019-12-01 11:28:57 +00:00
override fun onGlobalLayout() {
slidingPanel.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 {
2020-01-24 17:27:43 +00:00
if (bottomSheetBehavior.peekHeight != 0 && playerFragment!!.onBackPressed()) return true
2019-12-01 11:28:57 +00:00
if (panelState == BottomSheetBehavior.STATE_EXPANDED) {
collapsePanel()
return true
2020-01-24 17:27:43 +00:00
}
2019-12-01 11:28:57 +00:00
return false
}
override fun onPaletteColorChanged() {
if (panelState == BottomSheetBehavior.STATE_EXPANDED) {
val paletteColor = playerFragment!!.paletteColor
super.setTaskDescriptionColor(paletteColor)
val isColorLight = ColorUtil.isColorLight(paletteColor)
if (PreferenceUtil.getInstance(this).adaptiveColor && (currentNowPlayingScreen == NORMAL || currentNowPlayingScreen == FLAT)) {
super.setLightNavigationBar(true)
super.setLightStatusbar(isColorLight)
} else if (currentNowPlayingScreen == FULL || currentNowPlayingScreen == CARD || currentNowPlayingScreen == BLUR || currentNowPlayingScreen == BLUR_CARD) {
2019-12-01 11:28:57 +00:00
super.setLightStatusbar(false)
super.setLightNavigationBar(true)
2020-02-02 12:44:16 +00:00
super.setNavigationbarColor(Color.BLACK)
2019-12-01 11:28:57 +00:00
} else if (currentNowPlayingScreen == COLOR || currentNowPlayingScreen == TINY) {
super.setNavigationbarColor(paletteColor)
super.setLightNavigationBar(isColorLight)
super.setLightStatusbar(isColorLight)
} else if (currentNowPlayingScreen == FIT) {
super.setLightStatusbar(false)
2019-12-01 11:28:57 +00:00
} else {
2019-12-30 11:01:50 +00:00
super.setLightStatusbar(
ColorUtil.isColorLight(
ATHUtil.resolveColor(
this,
android.R.attr.windowBackground
)
)
)
2019-12-01 11:28:57 +00:00
super.setLightNavigationBar(true)
}
}
}
override fun setLightStatusbar(enabled: Boolean) {
lightStatusBar = enabled
if (panelState == BottomSheetBehavior.STATE_COLLAPSED) {
super.setLightStatusbar(enabled)
}
}
override fun setLightNavigationBar(enabled: Boolean) {
lightNavigationBar = enabled
if (panelState == BottomSheetBehavior.STATE_COLLAPSED) {
super.setLightNavigationBar(enabled)
}
}
override fun setNavigationbarColor(color: Int) {
navigationBarColor = color
if (panelState == BottomSheetBehavior.STATE_COLLAPSED) {
if (navigationBarColorAnimator != null) navigationBarColorAnimator!!.cancel()
super.setNavigationbarColor(color)
}
}
override fun setTaskDescriptionColor(color: Int) {
taskColor = color
if (panelState == BottomSheetBehavior.STATE_COLLAPSED) {
super.setTaskDescriptionColor(color)
}
}
private fun updateTabs() {
bottomNavigationView.menu.clear()
2019-12-01 15:27:01 +00:00
val currentTabs: List<CategoryInfo> = PreferenceUtil.getInstance(this).libraryCategoryInfos
2019-12-01 11:28:57 +00:00
for (tab in currentTabs) {
if (tab.visible) {
val menu = tab.category
2019-12-01 15:27:01 +00:00
bottomNavigationView.menu.add(0, menu.id, 0, menu.stringRes).setIcon(menu.icon)
2019-12-01 11:28:57 +00:00
}
}
2019-12-01 15:27:01 +00:00
if (currentTabs.size <= 1) {
toggleBottomNavigationView(true)
}
2019-12-01 11:28:57 +00:00
}
2019-12-06 17:13:09 +00:00
/*override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
2019-12-01 11:28:57 +00:00
if (ev?.action == MotionEvent.ACTION_DOWN) {
if (panelState == BottomSheetBehavior.STATE_EXPANDED) {
val outRect = Rect()
slidingPanel.getGlobalVisibleRect(outRect)
if (!outRect.contains(ev.rawX.toInt(), ev.rawY.toInt())) {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
}
}
return super.dispatchTouchEvent(ev)
2019-12-06 17:13:09 +00:00
}*/
2019-03-25 12:43:43 +00:00
}