Add settings for device theme (dark & black)

fix bugs related to isLightThemeSelected not handling device themes
such as license having dark background when it should be white
This commit is contained in:
bopol 2020-05-22 15:37:14 +02:00 committed by Stypox
parent 4fac3cf304
commit a713ce2126
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
5 changed files with 89 additions and 18 deletions

View File

@ -21,6 +21,8 @@ package org.schabi.newpipe.util;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
@ -39,7 +41,8 @@ import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
public final class ThemeHelper {
private ThemeHelper() { }
private ThemeHelper() {
}
/**
* Apply the selected theme (on NewPipe settings) in the context
@ -70,8 +73,13 @@ public final class ThemeHelper {
* @return whether the light theme is selected
*/
public static boolean isLightThemeSelected(final Context context) {
return getSelectedThemeString(context).equals(context.getResources()
.getString(R.string.light_theme_key));
final String selectedThemeString = getSelectedThemeString(context);
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));
}
@ -130,9 +138,12 @@ public final class ThemeHelper {
*/
@StyleRes
public static int getThemeForService(final Context context, final int serviceId) {
final String lightTheme = context.getResources().getString(R.string.light_theme_key);
final String darkTheme = context.getResources().getString(R.string.dark_theme_key);
final String blackTheme = context.getResources().getString(R.string.black_theme_key);
final Resources res = context.getResources();
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);
@ -141,8 +152,18 @@ public final class ThemeHelper {
defaultTheme = R.style.LightTheme;
} else if (selectedTheme.equals(blackTheme)) {
defaultTheme = R.style.BlackTheme;
} else if (selectedTheme.equals(darkTheme)) {
defaultTheme = R.style.DarkTheme;
} 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;
}
}
if (serviceId <= -1) {
@ -157,12 +178,10 @@ public final class ThemeHelper {
}
String themeName = "DarkTheme";
if (selectedTheme.equals(lightTheme)) {
if (defaultTheme == R.style.LightTheme) {
themeName = "LightTheme";
} else if (selectedTheme.equals(blackTheme)) {
} else if (defaultTheme == R.style.BlackTheme) {
themeName = "BlackTheme";
} else if (selectedTheme.equals(darkTheme)) {
themeName = "DarkTheme";
}
themeName += "." + service.getServiceInfo().getName();
@ -179,9 +198,12 @@ public final class ThemeHelper {
@StyleRes
public static int getSettingsThemeStyle(final Context context) {
final String lightTheme = context.getResources().getString(R.string.light_theme_key);
final String darkTheme = context.getResources().getString(R.string.dark_theme_key);
final String blackTheme = context.getResources().getString(R.string.black_theme_key);
final Resources res = context.getResources();
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);
@ -191,6 +213,18 @@ public final class ThemeHelper {
return 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;
}
} else {
// Fallback
return R.style.DarkSettingsTheme;
@ -239,8 +273,9 @@ public final class ThemeHelper {
/**
* Sets the title to the activity, if the activity is an {@link AppCompatActivity} and has an
* action bar.
*
* @param activity the activity to set the title of
* @param title the title to set to the activity
* @param title the title to set to the activity
*/
public static void setTitleToAppCompatActivity(@Nullable final Activity activity,
final CharSequence title) {
@ -251,4 +286,28 @@ public final class ThemeHelper {
}
}
}
/**
* Get the device theme
* <p>
* It will return true if the device 's theme is dark, false otherwise.
* <p>
* From https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#java
*
* @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
& Configuration.UI_MODE_NIGHT_MASK;
switch (deviceTheme) {
case Configuration.UI_MODE_NIGHT_YES:
return true;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
case Configuration.UI_MODE_NIGHT_NO:
default:
return false;
}
}
}

View File

@ -23,6 +23,9 @@
<string name="theme_title">Etoso</string>
<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>
@ -90,7 +93,6 @@
<string name="show_higher_resolutions_title">Montri pli altajn rezoluciojn</string>
<string name="show_higher_resolutions_summary">Nur kelkaj aparatoj povas ludi 2K / 4K filmetojn</string>
<string name="default_video_format_title">Defaŭlta fomato de filmeto</string>
<string name="black_theme_title">Nigra</string>
<string name="popup_remember_size_pos_title">Memoru ŝprucfenestran grandecon kaj pozicion</string>
<string name="popup_remember_size_pos_summary">Memoru lastan grandecon kaj pozicion de ŝprucfenestro</string>
<string name="use_inexact_seek_title">Uzi rapide, ne precizan serĉon</string>

View File

@ -43,6 +43,9 @@
<string name="theme_title">Thème</string>
<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>
@ -103,7 +106,6 @@
<string name="no_available_dir">Veuillez définir ultérieurement un dossier de téléchargement dans les paramètres</string>
<string name="could_not_load_image">Impossible de charger limage</string>
<string name="app_ui_crash">Lapplication a planté</string>
<string name="black_theme_title">Noir</string>
<string name="all">Tout</string>
<string name="channel">Chaîne</string>
<string name="title_activity_recaptcha">Défi reCAPTCHA</string>

View File

@ -179,16 +179,22 @@
<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>
<!-- Caption Size -->

View File

@ -82,6 +82,8 @@
<string name="light_theme_title">Light</string>
<string name="dark_theme_title">Dark</string>
<string name="black_theme_title">Black</string>
<string name="device_dark_theme_title">Device theme (Dark)</string>
<string name="device_black_theme_title">Device theme (Black)</string>
<string name="popup_remember_size_pos_title">Remember popup properties</string>
<string name="popup_remember_size_pos_summary">Remember last size and position of popup</string>
<string name="use_inexact_seek_title">Use fast inexact seek</string>