From bb1868fd679a2ea3060a8356c559aeafc4dadee8 Mon Sep 17 00:00:00 2001 From: Mylloon <29067904+Mylloon@users.noreply.github.com> Date: Wed, 13 Sep 2023 11:14:20 +0200 Subject: [PATCH] Add system black theme (#3957) Close #1222 --- .../com/keylesspalace/tusky/BaseActivity.java | 2 +- .../tusky/settings/SettingsConstants.kt | 3 ++- .../com/keylesspalace/tusky/util/ThemeUtils.kt | 16 +++++++++++++++- app/src/main/res/values/string-arrays.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index ea5e73d5..e91cc576 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -79,7 +79,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab * views are created. */ String theme = preferences.getString(APP_THEME, ThemeUtils.APP_THEME_DEFAULT); Log.d("activeTheme", theme); - if (theme.equals("black")) { + if (ThemeUtils.isBlack(getResources().getConfiguration(), theme)) { setTheme(R.style.TuskyBlackTheme); } diff --git a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt index 5b9cfc32..4782601a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt +++ b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt @@ -5,7 +5,8 @@ enum class AppTheme(val value: String) { DAY("day"), BLACK("black"), AUTO("auto"), - AUTO_SYSTEM("auto_system"); + AUTO_SYSTEM("auto_system"), + AUTO_SYSTEM_BLACK("auto_system_black"); companion object { fun stringValues() = values().map { it.value }.toTypedArray() diff --git a/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.kt b/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.kt index 10f8df1a..ae984708 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.kt @@ -17,6 +17,7 @@ package com.keylesspalace.tusky.util import android.content.Context +import android.content.res.Configuration import android.graphics.Color import android.graphics.PorterDuff import android.graphics.drawable.Drawable @@ -35,6 +36,7 @@ const val THEME_DAY = "day" const val THEME_BLACK = "black" const val THEME_AUTO = "auto" const val THEME_SYSTEM = "auto_system" +const val THEME_SYSTEM_BLACK = "auto_system_black" const val APP_THEME_DEFAULT = THEME_SYSTEM fun getDimension(context: Context, @AttrRes attribute: Int): Int { @@ -59,9 +61,21 @@ fun setAppNightMode(flavor: String?) { THEME_AUTO -> AppCompatDelegate.setDefaultNightMode( AppCompatDelegate.MODE_NIGHT_AUTO_TIME ) - THEME_SYSTEM -> AppCompatDelegate.setDefaultNightMode( + THEME_SYSTEM, THEME_SYSTEM_BLACK -> AppCompatDelegate.setDefaultNightMode( AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM ) else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) } } + +fun isBlack(config: Configuration, theme: String?): Boolean { + return when (theme) { + THEME_BLACK -> true + THEME_SYSTEM_BLACK -> when (config.uiMode and Configuration.UI_MODE_NIGHT_MASK) { + Configuration.UI_MODE_NIGHT_NO -> false + Configuration.UI_MODE_NIGHT_YES -> true + else -> false + } + else -> false + } +} diff --git a/app/src/main/res/values/string-arrays.xml b/app/src/main/res/values/string-arrays.xml index 1f0c46b7..4748a0cb 100644 --- a/app/src/main/res/values/string-arrays.xml +++ b/app/src/main/res/values/string-arrays.xml @@ -7,6 +7,7 @@ @string/app_theme_black @string/app_theme_auto @string/app_theme_system + @string/app_theme_system_black diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 25729ae7..19df7ed8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -302,6 +302,7 @@ Black Automatic at sunset Use System Design + Use System Design (black) Browser Use Chrome Custom Tabs