Skip to content

Commit 746a951

Browse files
committed
use a switch preference to follow device theme
1 parent 2881978 commit 746a951

7 files changed

Lines changed: 58 additions & 51 deletions

File tree

app/src/main/java/org/schabi/newpipe/settings/AppearanceSettingsFragment.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.schabi.newpipe.settings;
22

3+
import android.app.Activity;
34
import android.content.ActivityNotFoundException;
45
import android.content.Intent;
56
import android.os.Build;
@@ -37,16 +38,33 @@ public boolean onPreferenceChange(final Preference preference, final Object newV
3738
return false;
3839
}
3940
};
41+
private final Preference.OnPreferenceChangeListener deviceThemePreferenceChange
42+
= new Preference.OnPreferenceChangeListener() {
43+
@Override
44+
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
45+
defaultPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, true).apply();
46+
47+
final Activity activity = getActivity();
48+
if (activity != null) {
49+
activity.recreate();
50+
}
51+
52+
return true;
53+
}
54+
};
4055
private String captionSettingsKey;
4156

4257
@Override
4358
public void onCreate(@Nullable final Bundle savedInstanceState) {
4459
super.onCreate(savedInstanceState);
45-
String themeKey = getString(R.string.theme_key);
60+
final String themeKey = getString(R.string.theme_key);
4661
startThemeKey = defaultPreferences
4762
.getString(themeKey, getString(R.string.default_theme_value));
4863
findPreference(themeKey).setOnPreferenceChangeListener(themePreferenceChange);
4964

65+
findPreference(getString(R.string.use_device_theme_key))
66+
.setOnPreferenceChangeListener(deviceThemePreferenceChange);
67+
5068
captionSettingsKey = getString(R.string.caption_settings_key);
5169
if (!CAPTIONING_SETTINGS_ACCESSIBLE) {
5270
final Preference captionSettings = findPreference(captionSettingsKey);

app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ public static boolean isLightThemeSelected(final Context context) {
7373
final Resources res = context.getResources();
7474

7575
return selectedThemeString.equals(res.getString(R.string.light_theme_key))
76-
|| (selectedThemeString.equals(res.getString(R.string.device_dark_theme_key))
77-
|| selectedThemeString.equals(res.getString(R.string.device_black_theme_key))
78-
&& !isDeviceDarkThemeEnabled(context));
76+
|| (shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context));
7977
}
8078

8179

@@ -138,28 +136,18 @@ public static int getThemeForService(final Context context, final int serviceId)
138136
final String lightTheme = res.getString(R.string.light_theme_key);
139137
final String darkTheme = res.getString(R.string.dark_theme_key);
140138
final String blackTheme = res.getString(R.string.black_theme_key);
141-
final String deviceDarkTheme = res.getString(R.string.device_dark_theme_key);
142-
final String deviceBlackTheme = res.getString(R.string.device_black_theme_key);
143139

144140
final String selectedTheme = getSelectedThemeString(context);
145141

146142
int defaultTheme = R.style.DarkTheme;
147143
if (selectedTheme.equals(lightTheme)) {
148144
defaultTheme = R.style.LightTheme;
149145
} else if (selectedTheme.equals(blackTheme)) {
150-
defaultTheme = R.style.BlackTheme;
151-
} else if (selectedTheme.equals(deviceDarkTheme)) {
152-
if (isDeviceDarkThemeEnabled(context)) {
153-
defaultTheme = R.style.DarkTheme;
154-
} else {
155-
defaultTheme = R.style.LightTheme;
156-
}
157-
} else if (selectedTheme.equals(deviceBlackTheme)) {
158-
if (isDeviceDarkThemeEnabled(context)) {
159-
defaultTheme = R.style.BlackTheme;
160-
} else {
161-
defaultTheme = R.style.LightTheme;
162-
}
146+
defaultTheme = shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context)
147+
? R.style.LightTheme : R.style.BlackTheme;
148+
} else if (selectedTheme.equals(darkTheme)) {
149+
defaultTheme = shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context)
150+
? R.style.LightTheme : R.style.DarkTheme;
163151
}
164152

165153
if (serviceId <= -1) {
@@ -198,29 +186,17 @@ public static int getSettingsThemeStyle(final Context context) {
198186
final String lightTheme = res.getString(R.string.light_theme_key);
199187
final String darkTheme = res.getString(R.string.dark_theme_key);
200188
final String blackTheme = res.getString(R.string.black_theme_key);
201-
final String deviceDarkTheme = res.getString(R.string.device_dark_theme_key);
202-
final String deviceBlackTheme = res.getString(R.string.device_black_theme_key);
203189

204190
final String selectedTheme = getSelectedThemeString(context);
205191

206192
if (selectedTheme.equals(lightTheme)) {
207193
return R.style.LightSettingsTheme;
208194
} else if (selectedTheme.equals(blackTheme)) {
209-
return R.style.BlackSettingsTheme;
195+
return shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context)
196+
? R.style.LightSettingsTheme : R.style.BlackSettingsTheme;
210197
} else if (selectedTheme.equals(darkTheme)) {
211-
return R.style.DarkSettingsTheme;
212-
} else if (selectedTheme.equals(deviceDarkTheme)) {
213-
if (isDeviceDarkThemeEnabled(context)) {
214-
return R.style.DarkSettingsTheme;
215-
} else {
216-
return R.style.LightSettingsTheme;
217-
}
218-
} else if (selectedTheme.equals(deviceBlackTheme)) {
219-
if (isDeviceDarkThemeEnabled(context)) {
220-
return R.style.BlackSettingsTheme;
221-
} else {
222-
return R.style.LightSettingsTheme;
223-
}
198+
return shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context)
199+
? R.style.LightSettingsTheme : R.style.DarkSettingsTheme;
224200
} else {
225201
// Fallback
226202
return R.style.DarkSettingsTheme;
@@ -260,8 +236,8 @@ public static int resolveColorFromAttr(final Context context, @AttrRes final int
260236
}
261237

262238
private static String getSelectedThemeString(final Context context) {
263-
String themeKey = context.getString(R.string.theme_key);
264-
String defaultTheme = context.getResources().getString(R.string.default_theme_value);
239+
final String themeKey = context.getString(R.string.theme_key);
240+
final String defaultTheme = context.getResources().getString(R.string.default_theme_value);
265241
return PreferenceManager.getDefaultSharedPreferences(context)
266242
.getString(themeKey, defaultTheme);
267243
}
@@ -276,8 +252,8 @@ private static String getSelectedThemeString(final Context context) {
276252
* @param context the context to use
277253
* @return true: dark theme, false: light or unknown
278254
*/
279-
private static boolean isDeviceDarkThemeEnabled(final Context context) {
280-
int deviceTheme = context.getResources().getConfiguration().uiMode
255+
public static boolean isDeviceDarkThemeEnabled(final Context context) {
256+
final int deviceTheme = context.getResources().getConfiguration().uiMode
281257
& Configuration.UI_MODE_NIGHT_MASK;
282258
switch (deviceTheme) {
283259
case Configuration.UI_MODE_NIGHT_YES:
@@ -289,4 +265,15 @@ private static boolean isDeviceDarkThemeEnabled(final Context context) {
289265
return false;
290266
}
291267
}
268+
269+
/**
270+
* Tells if the user wants the theme to follow the device theme.
271+
*
272+
* @param context the context to use
273+
* @return whether the user wants the theme to follow the device's theme
274+
*/
275+
public static boolean shouldFollowDeviceTheme(final Context context) {
276+
return PreferenceManager.getDefaultSharedPreferences(context)
277+
.getBoolean(context.getString(R.string.use_device_theme_key), false);
278+
}
292279
}

app/src/main/res/values-eo/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
<string name="dark_theme_title">Malluma</string>
2525
<string name="light_theme_title">Luma</string>
2626
<string name="black_theme_title">Nigra</string>
27-
<string name="device_dark_theme_title">Etoson (Malluma)</string>
28-
<string name="device_black_theme_title">Etoson de la aparato (Nigra)</string>
2927
<string name="download_dialog_title">Elŝuti</string>
3028
<string name="next_video_title">Vica filmeto</string>
3129
<string name="url_not_supported_toast">Ligilo ne subtenita</string>
@@ -584,4 +582,6 @@
584582
<string name="unmute">Malmutigi</string>
585583
<string name="help">Helpo</string>
586584
<string name="content_not_supported">Tio enhavo ne estas ankoraŭ subtenata per NewPipe.\n\nĜi espereble estos en sekvanta versio.</string>
585+
<string name="follow_device_theme_title">Sekvi la etoson de la aparato</string>
586+
<string name="follow_device_theme_summary">La apo sekvos la etoson de la aparato. Ĝi povus malfunkcii se via Android versiono malsupras Android 10.</string>
587587
</resources>

app/src/main/res/values-fr/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
<string name="dark_theme_title">Sombre</string>
4747
<string name="light_theme_title">Clair</string>
4848
<string name="black_theme_title">Noir</string>
49-
<string name="device_dark_theme_title">Thème de l\'appareil (Sombre)</string>
50-
<string name="device_black_theme_title">Thème de l\'appareil (Noir)</string>
5149
<string name="settings_category_appearance_title">Apparence</string>
5250
<string name="network_error">Erreur réseau</string>
5351
<string name="download_path_audio_title">Dossier de téléchargement audio</string>
@@ -615,4 +613,6 @@
615613
<string name="show_original_time_ago_summary">Les textes originaux des services vont être visibles dans les items</string>
616614
<string name="show_original_time_ago_title">Afficher la date originelle sur les items</string>
617615
<string name="youtube_restricted_mode_enabled_title">Mode restreint de YouTube</string>
616+
<string name="follow_device_theme_title">Suivre le thème de l\'appareil</string>
617+
<string name="follow_device_theme_summary">L\'application suivera le thème de votre appareil. Il se peut que ça ne marche pas si vous utiliser une version d\'Android inférieure à 10.</string>
618618
</resources>

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,18 @@
141141
<string name="light_theme_key" translatable="false">light_theme</string>
142142
<string name="dark_theme_key" translatable="false">dark_theme</string>
143143
<string name="black_theme_key" translatable="false">black_theme</string>
144-
<string name="device_dark_theme_key" translatable="false">device_dark_theme</string>
145-
<string name="device_black_theme_key" translatable="false">device_black_theme</string>
146144
<string name="default_theme_value" translatable="false">@string/dark_theme_key</string>
147145
<string-array name="theme_values_list" translatable="false">
148146
<item>@string/light_theme_key</item>
149147
<item>@string/dark_theme_key</item>
150148
<item>@string/black_theme_key</item>
151-
<item>@string/device_dark_theme_key</item>
152-
<item>@string/device_black_theme_key</item>
153149
</string-array>
154150
<string-array name="theme_description_list" translatable="false">
155151
<item>@string/light_theme_title</item>
156152
<item>@string/dark_theme_title</item>
157153
<item>@string/black_theme_title</item>
158-
<item>@string/device_dark_theme_title</item>
159-
<item>@string/device_black_theme_title</item>
160154
</string-array>
155+
<string name="use_device_theme_key" translatable="false">use_device_theme_key</string>
161156

162157
<!-- Caption Size -->
163158
<string name="caption_settings_key" translatable="false">caption_settings_key</string>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@
6666
<string name="light_theme_title">Light</string>
6767
<string name="dark_theme_title">Dark</string>
6868
<string name="black_theme_title">Black</string>
69-
<string name="device_dark_theme_title">Device theme (Dark)</string>
70-
<string name="device_black_theme_title">Device theme (Black)</string>
7169
<string name="popup_remember_size_pos_title">Remember popup size and position</string>
7270
<string name="popup_remember_size_pos_summary">Remember last size and position of popup</string>
7371
<string name="use_inexact_seek_title">Use fast inexact seek</string>
@@ -653,4 +651,6 @@
653651
<string name="detail_sub_channel_thumbnail_view_description">Channel\'s avatar thumbnail</string>
654652
<string name="channel_created_by">Created by %s</string>
655653
<string name="video_detail_by">By %s</string>
654+
<string name="follow_device_theme_title">Follow device theme</string>
655+
<string name="follow_device_theme_summary">The app theme will follow your device theme. It may not work for devices below Android 10.</string>
656656
</resources>

app/src/main/res/xml/appearance_settings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
android:summary="%s"
1414
android:title="@string/theme_title"/>
1515

16+
<SwitchPreference
17+
android:defaultValue="false"
18+
android:key="@string/use_device_theme_key"
19+
android:summary="@string/follow_device_theme_summary"
20+
android:title="@string/follow_device_theme_title"
21+
app:iconSpaceReserved="false" />
22+
1623
<SwitchPreference
1724
app:iconSpaceReserved="false"
1825
android:defaultValue="true"

0 commit comments

Comments
 (0)