Skip to content

Commit d0978a4

Browse files
committed
follow system setting in dark mode
1 parent 830e71f commit d0978a4

6 files changed

Lines changed: 15 additions & 32 deletions

File tree

app/src/main/java/eu/faircode/netguard/ActivitySettings.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,6 @@ else if ("whitelist_roaming".equals(name))
438438
else if ("pause".equals(name))
439439
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_pause, prefs.getString(name, "10")));
440440

441-
else if ("dark_theme".equals(name))
442-
recreate();
443-
444441
else if ("subnet".equals(name))
445442
ServiceSinkhole.reload("changed " + name, this, false);
446443

app/src/main/java/eu/faircode/netguard/ApplicationEx.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,17 @@ public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle saved
129129
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
130130
android.widget.FrameLayout content = activity.findViewById(android.R.id.content);
131131

132-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
133-
boolean dark = prefs.getBoolean("dark_theme", false);
134-
135132
// On Android 15+, setStatusBarColor is ignored
136133
// Set window background to primary color - this shows behind the status bar
137134
int primaryColor = ContextCompat.getColor(activity, R.color.colorPrimary);
138135
activity.getWindow().setBackgroundDrawable(new ColorDrawable(primaryColor));
139136

137+
boolean isNight = (activity.getResources().getConfiguration().uiMode & android.content.res.Configuration.UI_MODE_NIGHT_MASK) == android.content.res.Configuration.UI_MODE_NIGHT_YES;
138+
140139
// Set status bar icons to light (white) since our background is dark
141140
View decor = activity.getWindow().getDecorView();
142141
WindowCompat.getInsetsController(activity.getWindow(), decor).setAppearanceLightStatusBars(false);
143-
WindowCompat.getInsetsController(activity.getWindow(), decor).setAppearanceLightNavigationBars(!dark);
142+
WindowCompat.getInsetsController(activity.getWindow(), decor).setAppearanceLightNavigationBars(!isNight);
144143

145144
ViewCompat.setOnApplyWindowInsetsListener(content, new OnApplyWindowInsetsListener() {
146145
@NonNull
@@ -157,8 +156,9 @@ public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowIn
157156
// Set background on the actual layout (first child), not on the content frame
158157
// This way the padding area shows the window background (primary color)
159158
if (content.getChildCount() > 0) {
159+
boolean isNight = (activity.getResources().getConfiguration().uiMode & android.content.res.Configuration.UI_MODE_NIGHT_MASK) == android.content.res.Configuration.UI_MODE_NIGHT_YES;
160160
View child = content.getChildAt(0);
161-
child.setBackgroundColor(dark ? Color.BLACK : Color.WHITE);
161+
child.setBackgroundColor(isNight ? Color.BLACK : Color.WHITE);
162162
}
163163

164164
return insets;

app/src/main/java/eu/faircode/netguard/Util.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,8 @@ public static boolean ownFault(Context context, Throwable ex) {
470470
}
471471

472472
public static void setTheme(Context context) {
473-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
474-
boolean dark = prefs.getBoolean("dark_theme", false);
475-
context.setTheme(dark ? R.style.AppThemeRedDark : R.style.AppThemeRed);
476-
477-
if (dark) {
478-
AppCompatDelegate.setDefaultNightMode(
479-
AppCompatDelegate.MODE_NIGHT_YES);
480-
} else {
481-
AppCompatDelegate.setDefaultNightMode(
482-
AppCompatDelegate.MODE_NIGHT_NO);
483-
}
473+
context.setTheme(R.style.AppThemeRed);
474+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
484475
}
485476

486477
public static int dips2pixels(int dips, Context context) {

app/src/main/res/values-night/styles.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@
77
<item name="android:windowBackground">@color/black</item>
88
<item name="android:colorBackground">@color/black</item>
99
</style>
10-
</resources>
1110

11+
<style name="AppThemeRed" parent="BaseThemeLight">
12+
<item name="colorPrimary">@color/colorRedPrimary</item>
13+
<item name="colorPrimaryDark">@color/colorRedPrimaryDark</item>
14+
<item name="colorAccent">@color/colorRedAccent</item>
15+
<item name="colorOn">@color/colorRedOff</item>
16+
<item name="colorOff">@color/colorRedOn</item>
17+
</style>
18+
</resources>

app/src/main/res/values/styles.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@
7171
<item name="colorOff">@color/colorRedOn</item>
7272
</style>
7373

74-
<style name="AppThemeRedDark" parent="BaseThemeLight">
75-
<item name="colorPrimary">@color/colorRedPrimary</item>
76-
<item name="colorPrimaryDark">@color/colorRedPrimaryDark</item>
77-
<item name="colorAccent">@color/colorRedAccent</item>
78-
<item name="colorOn">@color/colorRedOff</item>
79-
<item name="colorOff">@color/colorRedOn</item>
80-
</style>
81-
8274
<style name="AppDialog" parent="Theme.AppCompat.Dialog">
8375
<item name="windowActionBar">false</item>
8476
<item name="android:windowNoTitle">true</item>

app/src/main/res/xml/preferences.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
android:key="strict_blocking"
1212
android:summary="@string/summary_strict_blocking"
1313
android:title="@string/setting_strict_blocking" />
14-
<eu.faircode.netguard.SwitchPreference
15-
android:defaultValue="false"
16-
android:key="dark_theme"
17-
android:title="@string/setting_dark" />
1814
<eu.faircode.netguard.SwitchPreference
1915
android:defaultValue="true"
2016
android:key="installed"

0 commit comments

Comments
 (0)