Skip to content

Commit

Permalink
add support for system night mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lictex committed Nov 12, 2019
1 parent b7e1606 commit c850956
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
25 changes: 24 additions & 1 deletion app/src/main/java/pw/lictex/osuplayer/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ui_mode">
<item>@string/pref_theme_array_default</item>
<item>@string/pref_theme_array_dark</item>
<item>@string/pref_theme_array_light</item>
</string-array>
<string-array name="ui_mode_value">
<item>default</item>
<item>dark</item>
<item>light</item>
</string-array>
</resources>
7 changes: 5 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
<string name="pref_rebuild_database_summary">如果添加或删除了歌曲</string>
<string name="pref_use_unicode_metadata_title">使用原语言显示歌曲信息</string>
<string name="pref_use_unicode_metadata_summary">仅在文件中包含这些信息时</string>
<string name="pref_use_light_theme_title">浅色模式</string>
<string name="pref_use_light_theme_summary">使用白色作为界面背景</string>
<string name="pref_theme_title">主题</string>
<string name="pref_theme_array_default">跟随系统</string>
<string name="pref_theme_array_dark">深色</string>
<string name="pref_theme_array_light">浅色</string>
<string name="pref_theme_summary">更改背景颜色模式</string>

<string name="pref_category_player">播放器</string>
<string name="pref_audio_latency_title">音频延迟</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@
<style name="DarkDialog" parent="Theme.AppCompat.Dialog.Alert">
<item name="android:colorBackground">@color/backgroundColorDark</item>
<item name="android:textColor">@color/textColorDark</item>
<item name="textColorAlertDialogListItem">@color/textColorDark</item>
<item name="colorAccent">@color/textColorDark</item>
<item name="android:textColorSecondary">@color/textColorDark</item>
</style>

<style name="LightDialog" parent="Theme.AppCompat.Dialog.Alert">
<item name="android:colorBackground">@color/backgroundColorLight</item>
<item name="android:textColor">@color/textColorLight</item>
<item name="textColorAlertDialogListItem">@color/textColorLight</item>
<item name="colorAccent">@color/textColorLight</item>
<item name="android:textColorSecondary">@color/textColorLight</item>
</style>

<style name="DarkEditText" parent="android:style/Widget.EditText">
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/res/xml/preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
android:title="@string/pref_use_unicode_metadata_title"
android:widgetLayout="@layout/item_preference_checkbox" />

<CheckBoxPreference
android:defaultValue="false"
android:key="use_light_theme"
<ListPreference
android:defaultValue="dark"
android:entries="@array/ui_mode"
android:entryValues="@array/ui_mode_value"
android:key="theme"
android:layout="@layout/item_preference"
android:summary="@string/pref_use_light_theme_summary"
android:title="@string/pref_use_light_theme_title"
android:widgetLayout="@layout/item_preference_checkbox" />
android:summary="@string/pref_theme_summary"
android:title="@string/pref_theme_title" />
</PreferenceCategory>

<PreferenceCategory
Expand Down

0 comments on commit c850956

Please sign in to comment.