From fc1a963ed18586844ca3222ff0da7886cac69939 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 18 Mar 2021 12:35:53 +0100 Subject: [PATCH] Refactor ThemeHelper --- .../newpipe/settings/SettingsActivity.java | 2 +- .../org/schabi/newpipe/util/ThemeHelper.java | 117 +++++------------- 2 files changed, 29 insertions(+), 90 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java index 4de166a5599..c445928c4df 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java @@ -48,7 +48,7 @@ public static void initSettings(final Context context) { @Override protected void onCreate(final Bundle savedInstanceBundle) { - setTheme(ThemeHelper.getSettingsThemeStyle(this)); + ThemeHelper.setTheme(this); assureCorrectAppLanguage(this); super.onCreate(savedInstanceBundle); 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 aaa196c60de..f442819b579 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java @@ -25,7 +25,6 @@ import android.content.res.Resources; import android.content.res.TypedArray; import android.util.TypedValue; -import android.view.ContextThemeWrapper; import androidx.annotation.AttrRes; import androidx.annotation.Nullable; @@ -73,37 +72,15 @@ public static void setTheme(final Context context, final int serviceId) { * @return whether the light theme is selected */ public static boolean isLightThemeSelected(final Context context) { - final String selectedThemeString = getSelectedThemeString(context); + final String selectedThemeKey = getSelectedThemeKey(context); final Resources res = context.getResources(); - return selectedThemeString.equals(res.getString(R.string.light_theme_key)) - || (selectedThemeString.equals(res.getString(R.string.auto_device_theme_key)) + return selectedThemeKey.equals(res.getString(R.string.light_theme_key)) + || (selectedThemeKey.equals(res.getString(R.string.auto_device_theme_key)) && !isDeviceDarkThemeEnabled(context)); } - /** - * Create and return a wrapped context with the default selected theme set. - * - * @param baseContext the base context for the wrapper - * @return a wrapped-styled context - */ - public static Context getThemedContext(final Context baseContext) { - return new ContextThemeWrapper(baseContext, getThemeForService(baseContext, -1)); - } - - /** - * Return the selected theme without being styled to any service. - * See {@link #getThemeForService(Context, int)}. - * - * @param context context to get the selected theme - * @return the selected style (the default one) - */ - @StyleRes - public static int getDefaultTheme(final Context context) { - return getThemeForService(context, -1); - } - /** * Return a dialog theme styled according to the (default) selected theme. * @@ -138,96 +115,59 @@ public static int getMinWidthDialogTheme(final Context context) { @StyleRes public static int getThemeForService(final Context context, final int serviceId) { 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 automaticDeviceTheme = res.getString(R.string.auto_device_theme_key); + final String lightThemeKey = res.getString(R.string.light_theme_key); + final String blackThemeKey = res.getString(R.string.black_theme_key); + final String automaticDeviceThemeKey = res.getString(R.string.auto_device_theme_key); - final String selectedTheme = getSelectedThemeString(context); + final String selectedThemeKey = getSelectedThemeKey(context); - int defaultTheme = R.style.DarkTheme; - if (selectedTheme.equals(lightTheme)) { - defaultTheme = R.style.LightTheme; - } else if (selectedTheme.equals(blackTheme)) { - defaultTheme = R.style.BlackTheme; - } else if (selectedTheme.equals(automaticDeviceTheme)) { + int baseTheme = R.style.DarkTheme; // default to dark theme + if (selectedThemeKey.equals(lightThemeKey)) { + baseTheme = R.style.LightTheme; + } else if (selectedThemeKey.equals(blackThemeKey)) { + baseTheme = R.style.BlackTheme; + } else if (selectedThemeKey.equals(automaticDeviceThemeKey)) { if (isDeviceDarkThemeEnabled(context)) { - final String selectedNightTheme = getSelectedNightThemeString(context); - if (selectedNightTheme.equals(blackTheme)) { - defaultTheme = R.style.BlackTheme; + // use the dark theme variant preferred by the user + final String selectedNightThemeKey = getSelectedNightThemeKey(context); + if (selectedNightThemeKey.equals(blackThemeKey)) { + baseTheme = R.style.BlackTheme; } else { - defaultTheme = R.style.DarkTheme; + baseTheme = R.style.DarkTheme; } } else { // there is only one day theme - defaultTheme = R.style.LightTheme; + baseTheme = R.style.LightTheme; } } if (serviceId <= -1) { - return defaultTheme; + return baseTheme; } final StreamingService service; try { service = NewPipe.getService(serviceId); } catch (final ExtractionException ignored) { - return defaultTheme; + return baseTheme; } - String themeName = "DarkTheme"; - if (defaultTheme == R.style.LightTheme) { + String themeName = "DarkTheme"; // default + if (baseTheme == R.style.LightTheme) { themeName = "LightTheme"; - } else if (defaultTheme == R.style.BlackTheme) { + } else if (baseTheme == R.style.BlackTheme) { themeName = "BlackTheme"; } themeName += "." + service.getServiceInfo().getName(); - final int resourceId = context - .getResources() + final int resourceId = context.getResources() .getIdentifier(themeName, "style", context.getPackageName()); if (resourceId > 0) { return resourceId; } - - return defaultTheme; - } - - @StyleRes - public static int getSettingsThemeStyle(final Context context) { - 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 automaticDeviceTheme = res.getString(R.string.auto_device_theme_key); - - - final String selectedTheme = getSelectedThemeString(context); - - if (selectedTheme.equals(lightTheme)) { - return R.style.LightSettingsTheme; - } else if (selectedTheme.equals(blackTheme)) { - return R.style.BlackSettingsTheme; - } else if (selectedTheme.equals(darkTheme)) { - return R.style.DarkSettingsTheme; - } else if (selectedTheme.equals(automaticDeviceTheme)) { - if (isDeviceDarkThemeEnabled(context)) { - final String selectedNightTheme = getSelectedNightThemeString(context); - if (selectedNightTheme.equals(blackTheme)) { - return R.style.BlackSettingsTheme; - } else { - return R.style.DarkSettingsTheme; - } - } else { - // there is only one day theme - return R.style.LightSettingsTheme; - } - } else { - // Fallback - return R.style.DarkSettingsTheme; - } + return baseTheme; } /** @@ -262,14 +202,14 @@ public static int resolveColorFromAttr(final Context context, @AttrRes final int return value.data; } - private static String getSelectedThemeString(final Context context) { + private static String getSelectedThemeKey(final Context context) { final String themeKey = context.getString(R.string.theme_key); final String defaultTheme = context.getResources().getString(R.string.default_theme_value); return PreferenceManager.getDefaultSharedPreferences(context) .getString(themeKey, defaultTheme); } - private static String getSelectedNightThemeString(final Context context) { + private static String getSelectedNightThemeKey(final Context context) { final String nightThemeKey = context.getString(R.string.night_theme_key); final String defaultNightTheme = context.getResources() .getString(R.string.default_night_theme_value); @@ -310,7 +250,6 @@ public static boolean isDeviceDarkThemeEnabled(final Context context) { switch (deviceTheme) { case Configuration.UI_MODE_NIGHT_YES: return true; - case Configuration.UI_MODE_NIGHT_UNDEFINED: case Configuration.UI_MODE_NIGHT_NO: default: