From c4dd91f51ed5a7845f3229e0a6f1e92a6a3d52d7 Mon Sep 17 00:00:00 2001 From: Bill Farmer Date: Sun, 21 May 2023 18:38:37 +0100 Subject: [PATCH] Add system theme --- build.gradle | 2 +- .../org/billthefarmer/tuner/Settings.java | 18 +++++++++++++ .../java/org/billthefarmer/tuner/Tuner.java | 25 ++++++++++++++++--- src/main/res/values/arrays.xml | 2 ++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index e51416b..010ae17 100644 --- a/build.gradle +++ b/build.gradle @@ -42,7 +42,7 @@ android { } lintOptions { - disable 'IconDensities', 'ClickableViewAccessibility', + disable 'IconDensities', 'ClickableViewAccessibility', 'AndroidGradlePluginVersion', 'CustomViewStyleable', 'TypographyFractions', 'OldTargetApi', 'NonConstantResourceId', 'ExpiredTargetSdkVersion', 'MissingInflatedId' // abortOnError false diff --git a/src/main/java/org/billthefarmer/tuner/Settings.java b/src/main/java/org/billthefarmer/tuner/Settings.java index 611dd9c..646266d 100644 --- a/src/main/java/org/billthefarmer/tuner/Settings.java +++ b/src/main/java/org/billthefarmer/tuner/Settings.java @@ -26,6 +26,7 @@ import android.app.ActionBar; import android.app.Activity; import android.content.SharedPreferences; +import android.content.res.Configuration; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.MenuItem; @@ -46,6 +47,10 @@ protected void onCreate(Bundle savedInstanceState) int theme = Integer.parseInt(preferences.getString(Tuner.PREF_THEME, "0")); + + Configuration config = getResources().getConfiguration(); + int night = config.uiMode & Configuration.UI_MODE_NIGHT_MASK; + switch (theme) { case Tuner.LIGHT: @@ -56,6 +61,19 @@ protected void onCreate(Bundle savedInstanceState) setTheme(R.style.AppDarkTheme); break; + case Tuner.SYSTEM: + switch (night) + { + case Configuration.UI_MODE_NIGHT_NO: + setTheme(R.style.AppTheme); + break; + + case Configuration.UI_MODE_NIGHT_YES: + setTheme(R.style.AppDarkTheme); + break; + } + break; + case Tuner.WHITE: setTheme(R.style.AppWhiteTheme); break; diff --git a/src/main/java/org/billthefarmer/tuner/Tuner.java b/src/main/java/org/billthefarmer/tuner/Tuner.java index 07dbccb..3f4c603 100644 --- a/src/main/java/org/billthefarmer/tuner/Tuner.java +++ b/src/main/java/org/billthefarmer/tuner/Tuner.java @@ -276,10 +276,11 @@ public class Tuner extends Activity 1.585609487, 1.676104963, 1.779786472, 1.881364210}, }; - public final static int LIGHT = 0; - public final static int DARK = 1; - public final static int WHITE = 2; - public final static int BLACK = 3; + public final static int LIGHT = 0; + public final static int DARK = 1; + public final static int SYSTEM = 2; + public final static int WHITE = 3; + public final static int BLACK = 4; private double temperaments[][]; private String names[]; @@ -308,6 +309,9 @@ protected void onCreate(Bundle savedInstanceState) // Get preferences getPreferences(); + Configuration config = getResources().getConfiguration(); + int night = config.uiMode & Configuration.UI_MODE_NIGHT_MASK; + switch (theme) { case LIGHT: @@ -318,6 +322,19 @@ protected void onCreate(Bundle savedInstanceState) setTheme(R.style.AppDarkTheme); break; + case SYSTEM: + switch (night) + { + case Configuration.UI_MODE_NIGHT_NO: + setTheme(R.style.AppTheme); + break; + + case Configuration.UI_MODE_NIGHT_YES: + setTheme(R.style.AppDarkTheme); + break; + } + break; + case WHITE: setTheme(R.style.AppWhiteTheme); break; diff --git a/src/main/res/values/arrays.xml b/src/main/res/values/arrays.xml index bbb4c28..9733df4 100644 --- a/src/main/res/values/arrays.xml +++ b/src/main/res/values/arrays.xml @@ -188,6 +188,7 @@ Light Dark + System White Black @@ -196,6 +197,7 @@ 1 2 3 + 4