From a713ce21263c29347e633efa871576f31c0d4249 Mon Sep 17 00:00:00 2001 From: bopol Date: Fri, 22 May 2020 15:37:14 +0200 Subject: [PATCH] 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 --- .../org/schabi/newpipe/util/ThemeHelper.java | 91 +++++++++++++++---- app/src/main/res/values-eo/strings.xml | 4 +- app/src/main/res/values-fr/strings.xml | 4 +- app/src/main/res/values/settings_keys.xml | 6 ++ app/src/main/res/values/strings.xml | 2 + 5 files changed, 89 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java index 5ac4de84c..b8246f0b2 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java @@ -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 + *

+ * It will return true if the device 's theme is dark, false otherwise. + *

+ * 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; + } + } } diff --git a/app/src/main/res/values-eo/strings.xml b/app/src/main/res/values-eo/strings.xml index 38e83b662..cb190ea91 100644 --- a/app/src/main/res/values-eo/strings.xml +++ b/app/src/main/res/values-eo/strings.xml @@ -23,6 +23,9 @@ Etoso Malluma Luma + Nigra + Etoson (Malluma) + Etoson de la aparato (Nigra) Elŝuti Ligilo ne subtenita Preferata enhavlingvo @@ -90,7 +93,6 @@ Montri pli altajn rezoluciojn Nur kelkaj aparatoj povas ludi 2K / 4K filmetojn Defaŭlta fomato de filmeto - Nigra Memoru ŝprucfenestran grandecon kaj pozicion Memoru lastan grandecon kaj pozicion de ŝprucfenestro Uzi rapide, ne precizan serĉon diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 86f4fe09c..a538c2721 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -43,6 +43,9 @@ Thème Sombre Clair + Noir + Thème de l\'appareil (Sombre) + Thème de l\'appareil (Noir) Apparence Erreur réseau Dossier de téléchargement audio @@ -103,7 +106,6 @@ Veuillez définir ultérieurement un dossier de téléchargement dans les paramètres Impossible de charger l’image L’application a planté - Noir Tout Chaîne Défi reCAPTCHA diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 0958fce26..195e43e61 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -179,16 +179,22 @@ light_theme dark_theme black_theme + device_dark_theme + device_black_theme @string/dark_theme_key @string/light_theme_key @string/dark_theme_key @string/black_theme_key + @string/device_dark_theme_key + @string/device_black_theme_key @string/light_theme_title @string/dark_theme_title @string/black_theme_title + @string/device_dark_theme_title + @string/device_black_theme_title diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9fb15e463..4bb21c0cf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -82,6 +82,8 @@ Light Dark Black + Device theme (Dark) + Device theme (Black) Remember popup properties Remember last size and position of popup Use fast inexact seek