Search in sources :

Example 1 with StyleRes

use of android.support.annotation.StyleRes in project NewPipe by TeamNewPipe.

the class ThemeHelper method getThemeForService.

/**
 * Return the selected theme styled according to the serviceId.
 *
 * @param context   context to get the selected theme
 * @param serviceId return a theme styled to this service,
 *                  -1 to get the default
 * @return the selected style (styled)
 */
@StyleRes
public static int getThemeForService(Context context, int serviceId) {
    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 = getSelectedThemeString(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(darkTheme))
        defaultTheme = R.style.DarkTheme;
    if (serviceId <= -1) {
        return defaultTheme;
    }
    final StreamingService service;
    try {
        service = NewPipe.getService(serviceId);
    } catch (ExtractionException ignored) {
        return defaultTheme;
    }
    String themeName = "DarkTheme";
    if (selectedTheme.equals(lightTheme))
        themeName = "LightTheme";
    else if (selectedTheme.equals(blackTheme))
        themeName = "BlackTheme";
    else if (selectedTheme.equals(darkTheme))
        themeName = "DarkTheme";
    themeName += "." + service.getServiceInfo().getName();
    int resourceId = context.getResources().getIdentifier(themeName, "style", context.getPackageName());
    if (resourceId > 0) {
        return resourceId;
    }
    return defaultTheme;
}
Also used : ExtractionException(org.schabi.newpipe.extractor.exceptions.ExtractionException) StreamingService(org.schabi.newpipe.extractor.StreamingService) StyleRes(android.support.annotation.StyleRes)

Aggregations

StyleRes (android.support.annotation.StyleRes)1 StreamingService (org.schabi.newpipe.extractor.StreamingService)1 ExtractionException (org.schabi.newpipe.extractor.exceptions.ExtractionException)1