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

221 lines
7.5 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.activities.base
2018-11-30 01:06:16 +00:00
2020-04-24 18:11:14 +00:00
import android.content.Context
2018-11-30 01:06:16 +00:00
import android.graphics.Color
2019-12-01 11:28:57 +00:00
import android.os.Bundle
import android.os.Handler
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
2018-11-30 01:06:16 +00:00
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode
2019-12-01 11:28:57 +00:00
import code.name.monkey.appthemehelper.ATH
import code.name.monkey.appthemehelper.ThemeStore
2019-11-08 15:38:34 +00:00
import code.name.monkey.appthemehelper.common.ATHToolbarActivity
2019-12-01 11:28:57 +00:00
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.ColorUtil
2020-05-24 18:04:50 +00:00
import code.name.monkey.appthemehelper.util.MaterialDialogsUtil
2019-12-01 11:28:57 +00:00
import code.name.monkey.appthemehelper.util.VersionUtils
2020-04-24 18:11:14 +00:00
import code.name.monkey.retromusic.LanguageContextWrapper
2018-11-30 01:06:16 +00:00
import code.name.monkey.retromusic.R
2020-06-06 18:57:28 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
2019-12-01 11:28:57 +00:00
import code.name.monkey.retromusic.util.RetroUtil
import code.name.monkey.retromusic.util.theme.ThemeManager
2020-04-24 18:11:14 +00:00
import java.util.*
2018-11-30 01:06:16 +00:00
2019-11-08 15:38:34 +00:00
abstract class AbsThemeActivity : ATHToolbarActivity(), Runnable {
2018-11-30 01:06:16 +00:00
2019-12-01 11:28:57 +00:00
private val handler = Handler()
override fun onCreate(savedInstanceState: Bundle?) {
updateTheme()
2019-12-01 11:28:57 +00:00
hideStatusBar()
super.onCreate(savedInstanceState)
setImmersiveFullscreen()
registerSystemUiVisibility()
toggleScreenOn()
2020-05-24 18:04:50 +00:00
MaterialDialogsUtil.updateMaterialDialogsThemeSingleton(this)
2019-12-01 11:28:57 +00:00
}
private fun updateTheme() {
setTheme(ThemeManager.getThemeResValue(this))
setDefaultNightMode(ThemeManager.getNightMode(this))
}
2019-12-01 11:28:57 +00:00
private fun toggleScreenOn() {
2020-06-06 18:57:28 +00:00
if (PreferenceUtil.isScreenOnEnabled) {
2019-12-01 11:28:57 +00:00
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideStatusBar()
handler.removeCallbacks(this)
handler.postDelayed(this, 300)
} else {
handler.removeCallbacks(this)
}
}
fun hideStatusBar() {
2020-06-06 18:57:28 +00:00
hideStatusBar(PreferenceUtil.isFullScreenMode)
2019-12-01 11:28:57 +00:00
}
private fun hideStatusBar(fullscreen: Boolean) {
val statusBar = window.decorView.rootView.findViewById<View>(R.id.status_bar)
if (statusBar != null) {
statusBar.visibility = if (fullscreen) View.GONE else View.VISIBLE
}
}
fun setDrawUnderStatusBar() {
RetroUtil.setAllowDrawUnderStatusBar(window)
}
fun setDrawUnderNavigationBar() {
RetroUtil.setAllowDrawUnderNavigationBar(window)
}
/**
* This will set the color of the view with the id "status_bar" on KitKat and Lollipop. On
* Lollipop if no such view is found it will set the statusbar color using the native method.
*
* @param color the new statusbar color (will be shifted down on Lollipop and above)
*/
fun setStatusbarColor(color: Int) {
val statusBar = window.decorView.rootView.findViewById<View>(R.id.status_bar)
if (statusBar != null) {
when {
2019-12-10 17:36:20 +00:00
VersionUtils.hasMarshmallow() -> statusBar.setBackgroundColor(color)
VersionUtils.hasLollipop() -> statusBar.setBackgroundColor(
ColorUtil.darkenColor(
color
)
)
2019-12-01 11:28:57 +00:00
else -> statusBar.setBackgroundColor(color)
}
} else {
when {
VersionUtils.hasMarshmallow() -> window.statusBarColor = color
else -> window.statusBarColor = ColorUtil.darkenColor(color)
}
}
2019-12-05 18:18:54 +00:00
setLightStatusbarAuto(ATHUtil.resolveColor(this, R.attr.colorSurface))
2019-12-01 11:28:57 +00:00
}
fun setStatusbarColorAuto() {
// we don't want to use statusbar color because we are doing the color darkening on our own to support KitKat
2019-12-10 17:36:20 +00:00
setStatusbarColor(ATHUtil.resolveColor(this, R.attr.colorSurface))
2019-12-05 18:18:54 +00:00
setLightStatusbarAuto(ATHUtil.resolveColor(this, R.attr.colorSurface))
2019-12-01 11:28:57 +00:00
}
open fun setTaskDescriptionColor(@ColorInt color: Int) {
ATH.setTaskDescriptionColor(this, color)
}
fun setTaskDescriptionColorAuto() {
2019-12-05 18:18:54 +00:00
setTaskDescriptionColor(ATHUtil.resolveColor(this, R.attr.colorSurface))
2019-12-01 11:28:57 +00:00
}
open fun setNavigationbarColor(color: Int) {
if (ThemeStore.coloredNavigationBar(this)) {
ATH.setNavigationbarColor(this, color)
} else {
ATH.setNavigationbarColor(this, Color.BLACK)
}
}
fun setNavigationbarColorAuto() {
2019-12-05 18:18:54 +00:00
setNavigationbarColor(ATHUtil.resolveColor(this, R.attr.colorSurface))
2019-12-01 11:28:57 +00:00
}
open fun setLightStatusbar(enabled: Boolean) {
ATH.setLightStatusbar(this, enabled)
}
fun setLightStatusbarAuto(bgColor: Int) {
setLightStatusbar(ColorUtil.isColorLight(bgColor))
}
open fun setLightNavigationBar(enabled: Boolean) {
if (!ATHUtil.isWindowBackgroundDark(this) and ThemeStore.coloredNavigationBar(this)) {
ATH.setLightNavigationbar(this, enabled)
}
}
private fun registerSystemUiVisibility() {
val decorView = window.decorView
decorView.setOnSystemUiVisibilityChangeListener { visibility ->
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
setImmersiveFullscreen()
}
}
}
private fun unregisterSystemUiVisibility() {
val decorView = window.decorView
decorView.setOnSystemUiVisibilityChangeListener(null)
}
private fun setImmersiveFullscreen() {
2019-12-30 11:01:50 +00:00
val flags =
(View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
2019-12-01 11:28:57 +00:00
2020-06-06 18:57:28 +00:00
if (PreferenceUtil.isFullScreenMode) {
2019-12-01 11:28:57 +00:00
window.decorView.systemUiVisibility = flags
}
}
private fun exitFullscreen() {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
}
override fun run() {
setImmersiveFullscreen()
}
override fun onStop() {
handler.removeCallbacks(this)
super.onStop()
}
public override fun onDestroy() {
super.onDestroy()
unregisterSystemUiVisibility()
exitFullscreen()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
handler.removeCallbacks(this)
handler.postDelayed(this, 500)
}
return super.onKeyDown(keyCode, event)
}
2020-04-24 18:11:14 +00:00
override fun attachBaseContext(newBase: Context?) {
2020-06-06 18:57:28 +00:00
val code = PreferenceUtil.languageCode
2020-04-24 18:11:14 +00:00
if (code != "auto") {
super.attachBaseContext(LanguageContextWrapper.wrap(newBase, Locale(code)))
} else super.attachBaseContext(newBase)
}
2020-10-06 08:46:04 +00:00
}