NewPipe/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java

62 lines
2.3 KiB
Java
Raw Normal View History

2017-02-27 12:55:15 +01:00
package org.schabi.newpipe.util;
import android.content.Context;
2018-01-23 01:40:00 +01:00
import android.content.res.TypedArray;
2017-02-27 12:55:15 +01:00
import android.preference.PreferenceManager;
2018-01-23 01:40:00 +01:00
import android.support.annotation.AttrRes;
import android.support.annotation.StyleRes;
2017-02-27 12:55:15 +01:00
import org.schabi.newpipe.R;
public class ThemeHelper {
/**
* Apply the selected theme (on NewPipe settings) in the context
*
2017-04-26 21:32:20 +02:00
* @param context context that the theme will be applied
*/
2017-04-26 21:32:20 +02:00
public static void setTheme(Context context) {
2018-01-23 01:40:00 +01:00
context.setTheme(getSelectedThemeStyle(context));
2017-04-26 21:32:20 +02:00
}
/**
* Return true if the selected theme (on NewPipe settings) is the Light theme
*
* @param context context to get the preference
*/
public static boolean isLightThemeSelected(Context context) {
return getSelectedTheme(context).equals(context.getResources().getString(R.string.light_theme_key));
}
2017-04-26 21:32:20 +02:00
2018-01-23 01:40:00 +01:00
@StyleRes
public static int getSelectedThemeStyle(Context context) {
String lightTheme = context.getResources().getString(R.string.light_theme_key);
String darkTheme = context.getResources().getString(R.string.dark_theme_key);
String blackTheme = context.getResources().getString(R.string.black_theme_key);
String selectedTheme = getSelectedTheme(context);
if (selectedTheme.equals(lightTheme)) return R.style.LightTheme;
else if (selectedTheme.equals(blackTheme)) return R.style.BlackTheme;
else if (selectedTheme.equals(darkTheme)) return R.style.DarkTheme;
// Fallback
else return R.style.DarkTheme;
}
public static String getSelectedTheme(Context context) {
String themeKey = context.getString(R.string.theme_key);
String defaultTheme = context.getResources().getString(R.string.default_theme_value);
return PreferenceManager.getDefaultSharedPreferences(context).getString(themeKey, defaultTheme);
2017-02-27 12:55:15 +01:00
}
2018-01-23 01:40:00 +01:00
/**
* Get a resource id from a resource styled according to the the context's theme.
*/
public static int resolveResourceIdFromAttr(Context context, @AttrRes int attr) {
TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
int attributeResourceId = a.getResourceId(0, 0);
a.recycle();
return attributeResourceId;
}
2017-02-27 12:55:15 +01:00
}