From 752843a497780da40647741208e3f154703fbf50 Mon Sep 17 00:00:00 2001 From: Paolo Valerdi Date: Thu, 26 Sep 2019 16:02:49 -0500 Subject: [PATCH] Add support for Android 10 dark theme. This is still untested please take a look. --- .../activities/base/AbsThemeActivity.kt | 3 +- .../settings/ThemeSettingsFragment.kt | 5 +-- .../retromusic/util/PreferenceUtil.java | 9 +++-- .../monkey/retromusic/util/ThemeManager.kt | 34 +++++++++++++++++++ app/src/main/res/values/arrays.xml | 4 +-- .../name/monkey/appthemehelper/ATHActivity.kt | 5 --- 6 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/code/name/monkey/retromusic/util/ThemeManager.kt diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsThemeActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsThemeActivity.kt index 333198a9..928fcf05 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsThemeActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsThemeActivity.kt @@ -19,13 +19,14 @@ import code.name.monkey.appthemehelper.util.VersionUtils import code.name.monkey.retromusic.R import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.RetroUtil +import code.name.monkey.retromusic.util.ThemeManager abstract class AbsThemeActivity : ATHActivity(), Runnable { private val handler = Handler() override fun onCreate(savedInstanceState: Bundle?) { - setTheme(PreferenceUtil.getInstance(this).generalTheme) + setTheme(ThemeManager.getThemeResValue(this)) hideStatusBar() super.onCreate(savedInstanceState) //MaterialDialogsUtil.updateMaterialDialogsThemeSingleton(this) diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/settings/ThemeSettingsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/settings/ThemeSettingsFragment.kt index c25329e7..c4e93942 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/settings/ThemeSettingsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/settings/ThemeSettingsFragment.kt @@ -46,8 +46,8 @@ class ThemeSettingsFragment : AbsSettingsFragment() { setSummary(it) it.setOnPreferenceChangeListener { _, newValue -> val theme = newValue as String - setSummary(generalTheme, newValue) - val color = when (theme) { + setSummary(it, newValue) + /* val color = when (theme) { "light" -> Color.WHITE "black" -> Color.BLACK "dark" -> ContextCompat.getColor(requireContext(), R.color.md_grey_900) @@ -58,6 +58,7 @@ class ThemeSettingsFragment : AbsSettingsFragment() { .activityTheme(PreferenceUtil.getThemeResFromPrefValue(theme)) .primaryColor(color) .commit() + */ ThemeStore.markChanged(requireContext()) diff --git a/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.java b/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.java index 219c5d04..ac492710 100644 --- a/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.java +++ b/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.java @@ -157,8 +157,9 @@ public final class PreferenceUtil { switch (themePrefValue) { case "light": return R.style.Theme_RetroMusic_Light; - case "black": - return R.style.Theme_RetroMusic_Black; + /* Drop black theme as of now, you may want to add a toggle for that later + case "black": + return R.style.Theme_RetroMusic_Black;*/ case "dark": default: return R.style.Theme_RetroMusic; @@ -558,6 +559,10 @@ public final class PreferenceUtil { return getThemeResFromPrefValue(mPreferences.getString(GENERAL_THEME, "dark")); } + public String getGeneralThemeValue() { + return mPreferences.getString(GENERAL_THEME, "dark"); + } + public void setGeneralTheme(String theme) { final SharedPreferences.Editor editor = mPreferences.edit(); editor.putString(GENERAL_THEME, theme); diff --git a/app/src/main/java/code/name/monkey/retromusic/util/ThemeManager.kt b/app/src/main/java/code/name/monkey/retromusic/util/ThemeManager.kt new file mode 100644 index 00000000..d3621996 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/util/ThemeManager.kt @@ -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 + } + + +} \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 1b165aa7..9c09c93b 100755 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -43,12 +43,12 @@ @string/light_theme_name @string/dark_theme_name - @string/black_theme_name + Automatic light dark - black + auto @string/normal_style diff --git a/appthemehelper/src/main/java/code/name/monkey/appthemehelper/ATHActivity.kt b/appthemehelper/src/main/java/code/name/monkey/appthemehelper/ATHActivity.kt index 5ca5cdbc..b0497ccf 100755 --- a/appthemehelper/src/main/java/code/name/monkey/appthemehelper/ATHActivity.kt +++ b/appthemehelper/src/main/java/code/name/monkey/appthemehelper/ATHActivity.kt @@ -14,12 +14,7 @@ open class ATHActivity : AppCompatActivity() { private var updateTime: Long = -1 - private val themeRes: Int - @StyleRes - get() = ThemeStore.activityTheme(this) - override fun onCreate(savedInstanceState: Bundle?) { - setTheme(themeRes) super.onCreate(savedInstanceState) updateTime = System.currentTimeMillis() }