Refactor ThemeHelper
This commit is contained in:
parent
a85e8a29ff
commit
731c65cd59
2 changed files with 29 additions and 91 deletions
|
@ -48,7 +48,7 @@ public class SettingsActivity extends AppCompatActivity
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(final Bundle savedInstanceBundle) {
|
protected void onCreate(final Bundle savedInstanceBundle) {
|
||||||
setTheme(ThemeHelper.getSettingsThemeStyle(this));
|
ThemeHelper.setTheme(this);
|
||||||
assureCorrectAppLanguage(this);
|
assureCorrectAppLanguage(this);
|
||||||
super.onCreate(savedInstanceBundle);
|
super.onCreate(savedInstanceBundle);
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,6 @@ import android.content.res.Configuration;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.content.res.TypedArray;
|
import android.content.res.TypedArray;
|
||||||
import android.util.TypedValue;
|
import android.util.TypedValue;
|
||||||
import android.view.ContextThemeWrapper;
|
|
||||||
|
|
||||||
import androidx.annotation.AttrRes;
|
import androidx.annotation.AttrRes;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
@ -73,37 +72,14 @@ public final class ThemeHelper {
|
||||||
* @return whether the light theme is selected
|
* @return whether the light theme is selected
|
||||||
*/
|
*/
|
||||||
public static boolean isLightThemeSelected(final Context context) {
|
public static boolean isLightThemeSelected(final Context context) {
|
||||||
final String selectedThemeString = getSelectedThemeString(context);
|
final String selectedThemeKey = getSelectedThemeKey(context);
|
||||||
final Resources res = context.getResources();
|
final Resources res = context.getResources();
|
||||||
|
|
||||||
return selectedThemeString.equals(res.getString(R.string.light_theme_key))
|
return selectedThemeKey.equals(res.getString(R.string.light_theme_key))
|
||||||
|| (selectedThemeString.equals(res.getString(R.string.auto_device_theme_key))
|
|| (selectedThemeKey.equals(res.getString(R.string.auto_device_theme_key))
|
||||||
&& !isDeviceDarkThemeEnabled(context));
|
&& !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.
|
* Return a dialog theme styled according to the (default) selected theme.
|
||||||
*
|
*
|
||||||
|
@ -138,96 +114,59 @@ public final class ThemeHelper {
|
||||||
@StyleRes
|
@StyleRes
|
||||||
public static int getThemeForService(final Context context, final int serviceId) {
|
public static int getThemeForService(final Context context, final int serviceId) {
|
||||||
final Resources res = context.getResources();
|
final Resources res = context.getResources();
|
||||||
final String lightTheme = res.getString(R.string.light_theme_key);
|
final String lightThemeKey = res.getString(R.string.light_theme_key);
|
||||||
final String darkTheme = res.getString(R.string.dark_theme_key);
|
final String blackThemeKey = res.getString(R.string.black_theme_key);
|
||||||
final String blackTheme = res.getString(R.string.black_theme_key);
|
final String automaticDeviceThemeKey = res.getString(R.string.auto_device_theme_key);
|
||||||
final String automaticDeviceTheme = res.getString(R.string.auto_device_theme_key);
|
|
||||||
|
|
||||||
final String selectedTheme = getSelectedThemeString(context);
|
final String selectedThemeKey = getSelectedThemeKey(context);
|
||||||
|
|
||||||
int defaultTheme = R.style.DarkTheme;
|
int baseTheme = R.style.DarkTheme; // default to dark theme
|
||||||
if (selectedTheme.equals(lightTheme)) {
|
if (selectedThemeKey.equals(lightThemeKey)) {
|
||||||
defaultTheme = R.style.LightTheme;
|
baseTheme = R.style.LightTheme;
|
||||||
} else if (selectedTheme.equals(blackTheme)) {
|
} else if (selectedThemeKey.equals(blackThemeKey)) {
|
||||||
defaultTheme = R.style.BlackTheme;
|
baseTheme = R.style.BlackTheme;
|
||||||
} else if (selectedTheme.equals(automaticDeviceTheme)) {
|
} else if (selectedThemeKey.equals(automaticDeviceThemeKey)) {
|
||||||
|
|
||||||
if (isDeviceDarkThemeEnabled(context)) {
|
if (isDeviceDarkThemeEnabled(context)) {
|
||||||
final String selectedNightTheme = getSelectedNightThemeString(context);
|
// use the dark theme variant preferred by the user
|
||||||
if (selectedNightTheme.equals(blackTheme)) {
|
final String selectedNightThemeKey = getSelectedNightThemeKey(context);
|
||||||
defaultTheme = R.style.BlackTheme;
|
if (selectedNightThemeKey.equals(blackThemeKey)) {
|
||||||
|
baseTheme = R.style.BlackTheme;
|
||||||
} else {
|
} else {
|
||||||
defaultTheme = R.style.DarkTheme;
|
baseTheme = R.style.DarkTheme;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// there is only one day theme
|
// there is only one day theme
|
||||||
defaultTheme = R.style.LightTheme;
|
baseTheme = R.style.LightTheme;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (serviceId <= -1) {
|
if (serviceId <= -1) {
|
||||||
return defaultTheme;
|
return baseTheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
final StreamingService service;
|
final StreamingService service;
|
||||||
try {
|
try {
|
||||||
service = NewPipe.getService(serviceId);
|
service = NewPipe.getService(serviceId);
|
||||||
} catch (final ExtractionException ignored) {
|
} catch (final ExtractionException ignored) {
|
||||||
return defaultTheme;
|
return baseTheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
String themeName = "DarkTheme";
|
String themeName = "DarkTheme"; // default
|
||||||
if (defaultTheme == R.style.LightTheme) {
|
if (baseTheme == R.style.LightTheme) {
|
||||||
themeName = "LightTheme";
|
themeName = "LightTheme";
|
||||||
} else if (defaultTheme == R.style.BlackTheme) {
|
} else if (baseTheme == R.style.BlackTheme) {
|
||||||
themeName = "BlackTheme";
|
themeName = "BlackTheme";
|
||||||
}
|
}
|
||||||
|
|
||||||
themeName += "." + service.getServiceInfo().getName();
|
themeName += "." + service.getServiceInfo().getName();
|
||||||
final int resourceId = context
|
final int resourceId = context.getResources()
|
||||||
.getResources()
|
|
||||||
.getIdentifier(themeName, "style", context.getPackageName());
|
.getIdentifier(themeName, "style", context.getPackageName());
|
||||||
|
|
||||||
if (resourceId > 0) {
|
if (resourceId > 0) {
|
||||||
return resourceId;
|
return resourceId;
|
||||||
}
|
}
|
||||||
|
return baseTheme;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -262,14 +201,14 @@ public final class ThemeHelper {
|
||||||
return value.data;
|
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 themeKey = context.getString(R.string.theme_key);
|
||||||
final String defaultTheme = context.getResources().getString(R.string.default_theme_value);
|
final String defaultTheme = context.getResources().getString(R.string.default_theme_value);
|
||||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
.getString(themeKey, defaultTheme);
|
.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 nightThemeKey = context.getString(R.string.night_theme_key);
|
||||||
final String defaultNightTheme = context.getResources()
|
final String defaultNightTheme = context.getResources()
|
||||||
.getString(R.string.default_night_theme_value);
|
.getString(R.string.default_night_theme_value);
|
||||||
|
@ -310,7 +249,6 @@ public final class ThemeHelper {
|
||||||
switch (deviceTheme) {
|
switch (deviceTheme) {
|
||||||
case Configuration.UI_MODE_NIGHT_YES:
|
case Configuration.UI_MODE_NIGHT_YES:
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case Configuration.UI_MODE_NIGHT_UNDEFINED:
|
case Configuration.UI_MODE_NIGHT_UNDEFINED:
|
||||||
case Configuration.UI_MODE_NIGHT_NO:
|
case Configuration.UI_MODE_NIGHT_NO:
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Add table
Reference in a new issue