From c85095679d8528c7306a3703443d9491d5a07f42 Mon Sep 17 00:00:00 2001 From: Lictex Steaven Date: Tue, 12 Nov 2019 22:18:39 +0800 Subject: [PATCH] add support for system night mode --- .../osuplayer/activity/MainActivity.java | 25 ++++++++++++++++++- .../activity/PreferenceFragment.java | 3 +-- app/src/main/res/values/arrays.xml | 13 ++++++++++ app/src/main/res/values/strings.xml | 7 ++++-- app/src/main/res/values/styles.xml | 6 +++++ app/src/main/res/xml/preference.xml | 13 +++++----- 6 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 app/src/main/res/values/arrays.xml diff --git a/app/src/main/java/pw/lictex/osuplayer/activity/MainActivity.java b/app/src/main/java/pw/lictex/osuplayer/activity/MainActivity.java index ce37960..0bd02e7 100644 --- a/app/src/main/java/pw/lictex/osuplayer/activity/MainActivity.java +++ b/app/src/main/java/pw/lictex/osuplayer/activity/MainActivity.java @@ -4,6 +4,7 @@ import android.animation.*; import android.content.*; import android.content.pm.*; +import android.content.res.*; import android.graphics.*; import android.graphics.drawable.*; import android.os.*; @@ -90,7 +91,29 @@ public void onBackPressed() { protected void onCreate(Bundle savedInstanceState) { if (savedInstanceState != null) savedInstanceState.remove("android:support:fragments"); super.onCreate(savedInstanceState); - setTheme(PreferenceManager.getDefaultSharedPreferences(this).getBoolean("use_light_theme", false) ? R.style.LightTheme : R.style.DarkTheme); + + String theme = PreferenceManager.getDefaultSharedPreferences(this).getString("theme", "dark"); + switch (theme) { + case "default": + switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) { + case Configuration.UI_MODE_NIGHT_YES: + setTheme(R.style.DarkTheme); + break; + case Configuration.UI_MODE_NIGHT_UNDEFINED: + case Configuration.UI_MODE_NIGHT_NO: + setTheme(R.style.LightTheme); + break; + } + break; + case "light": + setTheme(R.style.LightTheme); + break; + default: + case "dark": + setTheme(R.style.DarkTheme); + break; + } + setContentView(R.layout.activity_home); ButterKnife.bind(this); diff --git a/app/src/main/java/pw/lictex/osuplayer/activity/PreferenceFragment.java b/app/src/main/java/pw/lictex/osuplayer/activity/PreferenceFragment.java index 971b1f0..9d9763c 100644 --- a/app/src/main/java/pw/lictex/osuplayer/activity/PreferenceFragment.java +++ b/app/src/main/java/pw/lictex/osuplayer/activity/PreferenceFragment.java @@ -36,8 +36,7 @@ public class PreferenceFragment extends PreferenceFragmentCompat { activity.getPlayerService().rebuildNotification(); activity.updateStatus(); break; - case "use_light_theme": - activity.setTheme(sharedPreference.getBoolean("use_light_theme", false) ? R.style.LightTheme : R.style.DarkTheme); + case "theme": activity.recreate(); break; } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 0000000..502f2ce --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,13 @@ + + + + @string/pref_theme_array_default + @string/pref_theme_array_dark + @string/pref_theme_array_light + + + default + dark + light + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index da71754..1063d0c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,8 +18,11 @@ 如果添加或删除了歌曲 使用原语言显示歌曲信息 仅在文件中包含这些信息时 - 浅色模式 - 使用白色作为界面背景 + 主题 + 跟随系统 + 深色 + 浅色 + 更改背景颜色模式 播放器 音频延迟 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index a635164..1d97633 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -48,11 +48,17 @@