Use a switch preference to follow device theme

This commit is contained in:
bopol 2020-07-03 18:44:34 +02:00 committed by Stypox
parent a713ce2126
commit 22b2f52f8c
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
7 changed files with 55 additions and 46 deletions

View File

@ -1,5 +1,6 @@
package org.schabi.newpipe.settings;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Build;
@ -38,6 +39,20 @@ public class AppearanceSettingsFragment extends BasePreferenceFragment {
return false;
}
};
private final Preference.OnPreferenceChangeListener deviceThemePreferenceChange
= new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
defaultPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, true).apply();
final Activity activity = getActivity();
if (activity != null) {
activity.recreate();
}
return true;
}
};
private String captionSettingsKey;
@Override
@ -48,6 +63,9 @@ public class AppearanceSettingsFragment extends BasePreferenceFragment {
.getString(themeKey, getString(R.string.default_theme_value));
findPreference(themeKey).setOnPreferenceChangeListener(themePreferenceChange);
findPreference(getString(R.string.use_device_theme_key))
.setOnPreferenceChangeListener(deviceThemePreferenceChange);
captionSettingsKey = getString(R.string.caption_settings_key);
if (!CAPTIONING_SETTINGS_ACCESSIBLE) {
final Preference captionSettings = findPreference(captionSettingsKey);

View File

@ -77,9 +77,7 @@ public final class ThemeHelper {
final Resources res = context.getResources();
return selectedThemeString.equals(res.getString(R.string.light_theme_key))
|| (selectedThemeString.equals(res.getString(R.string.device_dark_theme_key))
|| selectedThemeString.equals(res.getString(R.string.device_black_theme_key))
&& !isDeviceDarkThemeEnabled(context));
|| (shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context));
}
@ -142,8 +140,6 @@ public final class ThemeHelper {
final String lightTheme = res.getString(R.string.light_theme_key);
final String darkTheme = res.getString(R.string.dark_theme_key);
final String blackTheme = res.getString(R.string.black_theme_key);
final String deviceDarkTheme = res.getString(R.string.device_dark_theme_key);
final String deviceBlackTheme = res.getString(R.string.device_black_theme_key);
final String selectedTheme = getSelectedThemeString(context);
@ -151,19 +147,11 @@ public final class ThemeHelper {
if (selectedTheme.equals(lightTheme)) {
defaultTheme = R.style.LightTheme;
} else if (selectedTheme.equals(blackTheme)) {
defaultTheme = R.style.BlackTheme;
} else if (selectedTheme.equals(deviceDarkTheme)) {
if (isDeviceDarkThemeEnabled(context)) {
defaultTheme = R.style.DarkTheme;
} else {
defaultTheme = R.style.LightTheme;
}
} else if (selectedTheme.equals(deviceBlackTheme)) {
if (isDeviceDarkThemeEnabled(context)) {
defaultTheme = R.style.BlackTheme;
} else {
defaultTheme = R.style.LightTheme;
}
defaultTheme = shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context)
? R.style.LightTheme : R.style.BlackTheme;
} else if (selectedTheme.equals(darkTheme)) {
defaultTheme = shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context)
? R.style.LightTheme : R.style.DarkTheme;
}
if (serviceId <= -1) {
@ -202,29 +190,17 @@ public final class ThemeHelper {
final String lightTheme = res.getString(R.string.light_theme_key);
final String darkTheme = res.getString(R.string.dark_theme_key);
final String blackTheme = res.getString(R.string.black_theme_key);
final String deviceDarkTheme = res.getString(R.string.device_dark_theme_key);
final String deviceBlackTheme = res.getString(R.string.device_black_theme_key);
final String selectedTheme = getSelectedThemeString(context);
if (selectedTheme.equals(lightTheme)) {
return R.style.LightSettingsTheme;
} else if (selectedTheme.equals(blackTheme)) {
return R.style.BlackSettingsTheme;
return shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context)
? R.style.LightSettingsTheme : R.style.BlackSettingsTheme;
} else if (selectedTheme.equals(darkTheme)) {
return R.style.DarkSettingsTheme;
} else if (selectedTheme.equals(deviceDarkTheme)) {
if (isDeviceDarkThemeEnabled(context)) {
return R.style.DarkSettingsTheme;
} else {
return R.style.LightSettingsTheme;
}
} else if (selectedTheme.equals(deviceBlackTheme)) {
if (isDeviceDarkThemeEnabled(context)) {
return R.style.BlackSettingsTheme;
} else {
return R.style.LightSettingsTheme;
}
return shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context)
? R.style.LightSettingsTheme : R.style.DarkSettingsTheme;
} else {
// Fallback
return R.style.DarkSettingsTheme;
@ -297,8 +273,8 @@ public final class ThemeHelper {
* @param context the context to use
* @return true:dark theme, false:light or unknown
*/
private static boolean isDeviceDarkThemeEnabled(final Context context) {
int deviceTheme = context.getResources().getConfiguration().uiMode
public static boolean isDeviceDarkThemeEnabled(final Context context) {
final int deviceTheme = context.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
switch (deviceTheme) {
case Configuration.UI_MODE_NIGHT_YES:
@ -310,4 +286,15 @@ public final class ThemeHelper {
return false;
}
}
/**
* Tells if the user wants the theme to follow the device theme.
*
* @param context the context to use
* @return whether the user wants the theme to follow the device's theme
*/
public static boolean shouldFollowDeviceTheme(final Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(context.getString(R.string.use_device_theme_key), false);
}
}

View File

@ -24,8 +24,6 @@
<string name="dark_theme_title">Malluma</string>
<string name="light_theme_title">Luma</string>
<string name="black_theme_title">Nigra</string>
<string name="device_dark_theme_title">Etoson (Malluma)</string>
<string name="device_black_theme_title">Etoson de la aparato (Nigra)</string>
<string name="download_dialog_title">Elŝuti</string>
<string name="unsupported_url">Ligilo ne subtenita</string>
<string name="content_language_title">Preferata enhavlingvo</string>
@ -581,4 +579,6 @@
<string name="artists">Artistoj</string>
<string name="albums">Albumoj</string>
<string name="songs">Kantoj</string>
<string name="follow_device_theme_title">Sekvi la etoson de la aparato</string>
<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>
</resources>

View File

@ -44,8 +44,6 @@
<string name="dark_theme_title">Sombre</string>
<string name="light_theme_title">Clair</string>
<string name="black_theme_title">Noir</string>
<string name="device_dark_theme_title">Thème de l\'appareil (Sombre)</string>
<string name="device_black_theme_title">Thème de l\'appareil (Noir)</string>
<string name="settings_category_appearance_title">Apparence</string>
<string name="network_error">Erreur réseau</string>
<string name="download_path_audio_title">Dossier de téléchargement audio</string>
@ -671,4 +669,6 @@
<string name="paid_content">Ce contenu n\'est disponible que pour les abonnés, il ne peut donc pas être diffusé en continu ni téléchargé par NewPipe.</string>
<string name="youtube_music_premium_content">Cette vidéo n\'est disponible que pour les membres de YouTube Music Premium, elle ne peut donc pas être diffusée en continu ni téléchargée par NewPipe.</string>
<string name="private_content">Ce contenu est privé, il ne peut donc pas être diffusé en continu ni téléchargé par NewPipe.</string>
<string name="follow_device_theme_title">Suivre le thème de l\'appareil</string>
<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>
</resources>

View File

@ -179,23 +179,18 @@
<string name="light_theme_key" translatable="false">light_theme</string>
<string name="dark_theme_key" translatable="false">dark_theme</string>
<string name="black_theme_key" translatable="false">black_theme</string>
<string name="device_dark_theme_key" translatable="false">device_dark_theme</string>
<string name="device_black_theme_key" translatable="false">device_black_theme</string>
<string name="default_theme_value" translatable="false">@string/dark_theme_key</string>
<string-array name="theme_values_list" translatable="false">
<item>@string/light_theme_key</item>
<item>@string/dark_theme_key</item>
<item>@string/black_theme_key</item>
<item>@string/device_dark_theme_key</item>
<item>@string/device_black_theme_key</item>
</string-array>
<string-array name="theme_description_list" translatable="false">
<item>@string/light_theme_title</item>
<item>@string/dark_theme_title</item>
<item>@string/black_theme_title</item>
<item>@string/device_dark_theme_title</item>
<item>@string/device_black_theme_title</item>
</string-array>
<string name="use_device_theme_key" translatable="false">use_device_theme_key</string>
<!-- Caption Size -->
<string name="caption_settings_key" translatable="false">caption_settings_key</string>

View File

@ -711,4 +711,6 @@
<string name="paid_content">This content is only available to users who have paid, so it cannot be streamed or downloaded by NewPipe.</string>
<string name="featured">Featured</string>
<string name="radio">Radio</string>
<string name="follow_device_theme_title">Follow device theme</string>
<string name="follow_device_theme_summary">The app theme will follow your device theme. It may not work for devices below Android 10.</string>
</resources>

View File

@ -12,6 +12,13 @@
android:title="@string/theme_title"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/use_device_theme_key"
android:summary="@string/follow_device_theme_summary"
android:title="@string/follow_device_theme_title"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="@string/show_hold_to_append_key"