Add support for Android 10 dark theme.

This is still untested please take a look.
This commit is contained in:
Paolo Valerdi 2019-09-26 16:02:49 -05:00
parent df7cb7ab49
commit 752843a497
6 changed files with 48 additions and 12 deletions

View file

@ -19,13 +19,14 @@ import code.name.monkey.appthemehelper.util.VersionUtils
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
import code.name.monkey.retromusic.util.RetroUtil import code.name.monkey.retromusic.util.RetroUtil
import code.name.monkey.retromusic.util.ThemeManager
abstract class AbsThemeActivity : ATHActivity(), Runnable { abstract class AbsThemeActivity : ATHActivity(), Runnable {
private val handler = Handler() private val handler = Handler()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
setTheme(PreferenceUtil.getInstance(this).generalTheme) setTheme(ThemeManager.getThemeResValue(this))
hideStatusBar() hideStatusBar()
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
//MaterialDialogsUtil.updateMaterialDialogsThemeSingleton(this) //MaterialDialogsUtil.updateMaterialDialogsThemeSingleton(this)

View file

@ -46,8 +46,8 @@ class ThemeSettingsFragment : AbsSettingsFragment() {
setSummary(it) setSummary(it)
it.setOnPreferenceChangeListener { _, newValue -> it.setOnPreferenceChangeListener { _, newValue ->
val theme = newValue as String val theme = newValue as String
setSummary(generalTheme, newValue) setSummary(it, newValue)
val color = when (theme) { /* val color = when (theme) {
"light" -> Color.WHITE "light" -> Color.WHITE
"black" -> Color.BLACK "black" -> Color.BLACK
"dark" -> ContextCompat.getColor(requireContext(), R.color.md_grey_900) "dark" -> ContextCompat.getColor(requireContext(), R.color.md_grey_900)
@ -58,6 +58,7 @@ class ThemeSettingsFragment : AbsSettingsFragment() {
.activityTheme(PreferenceUtil.getThemeResFromPrefValue(theme)) .activityTheme(PreferenceUtil.getThemeResFromPrefValue(theme))
.primaryColor(color) .primaryColor(color)
.commit() .commit()
*/
ThemeStore.markChanged(requireContext()) ThemeStore.markChanged(requireContext())

View file

@ -157,8 +157,9 @@ public final class PreferenceUtil {
switch (themePrefValue) { switch (themePrefValue) {
case "light": case "light":
return R.style.Theme_RetroMusic_Light; return R.style.Theme_RetroMusic_Light;
case "black": /* Drop black theme as of now, you may want to add a toggle for that later
return R.style.Theme_RetroMusic_Black; case "black":
return R.style.Theme_RetroMusic_Black;*/
case "dark": case "dark":
default: default:
return R.style.Theme_RetroMusic; return R.style.Theme_RetroMusic;
@ -558,6 +559,10 @@ public final class PreferenceUtil {
return getThemeResFromPrefValue(mPreferences.getString(GENERAL_THEME, "dark")); return getThemeResFromPrefValue(mPreferences.getString(GENERAL_THEME, "dark"));
} }
public String getGeneralThemeValue() {
return mPreferences.getString(GENERAL_THEME, "dark");
}
public void setGeneralTheme(String theme) { public void setGeneralTheme(String theme) {
final SharedPreferences.Editor editor = mPreferences.edit(); final SharedPreferences.Editor editor = mPreferences.edit();
editor.putString(GENERAL_THEME, theme); editor.putString(GENERAL_THEME, theme);

View file

@ -0,0 +1,34 @@
package code.name.monkey.retromusic.util
import android.content.Context
import android.content.res.Configuration.UI_MODE_NIGHT_MASK
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.os.PowerManager
import androidx.annotation.StyleRes
import code.name.monkey.retromusic.R
/**
* @author Paolo Valerdi
*/
object ThemeManager {
@StyleRes
fun getThemeResValue(context: Context): Int = when(PreferenceUtil.getInstance(context).generalThemeValue) {
"light" -> R.style.Theme_RetroMusic_Light
"auto" -> if (isSystemDarkModeEnabled(context)) R.style.Theme_RetroMusic else R.style.Theme_RetroMusic_Light
else -> R.style.Theme_RetroMusic
/**
* To add a toggle for amoled theme just add an if statement such as
* if(PreferenceUtil.getInstance(context).useAmoled) blablabla
*/
}
fun isSystemDarkModeEnabled(context: Context): Boolean {
val isBatterySaverEnabled = (context.getSystemService(Context.POWER_SERVICE) as PowerManager?)?.isPowerSaveMode ?: false
val isDarkModeEnabled = (context.resources.configuration.uiMode and UI_MODE_NIGHT_MASK) == UI_MODE_NIGHT_YES
return isBatterySaverEnabled or isDarkModeEnabled
}
}

View file

@ -43,12 +43,12 @@
<string-array name="pref_general_theme_list_titles"> <string-array name="pref_general_theme_list_titles">
<item>@string/light_theme_name</item> <item>@string/light_theme_name</item>
<item>@string/dark_theme_name</item> <item>@string/dark_theme_name</item>
<item>@string/black_theme_name</item> <item>Automatic</item>
</string-array> </string-array>
<string-array name="pref_general_theme_list_values"> <string-array name="pref_general_theme_list_values">
<item>light</item> <item>light</item>
<item>dark</item> <item>dark</item>
<item>black</item> <item>auto</item>
</string-array> </string-array>
<string-array name="pref_grid_style_list_titles"> <string-array name="pref_grid_style_list_titles">
<item>@string/normal_style</item> <item>@string/normal_style</item>

View file

@ -14,12 +14,7 @@ open class ATHActivity : AppCompatActivity() {
private var updateTime: Long = -1 private var updateTime: Long = -1
private val themeRes: Int
@StyleRes
get() = ThemeStore.activityTheme(this)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
setTheme(themeRes)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
updateTime = System.currentTimeMillis() updateTime = System.currentTimeMillis()
} }