Merged `DrawableResolver into ThemeHelper`

This commit is contained in:
litetex 2022-03-17 18:34:44 +01:00
parent a2eec54019
commit 284b44414b
4 changed files with 20 additions and 28 deletions

View file

@ -1,26 +0,0 @@
package org.schabi.newpipe.util
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.TypedValue
import androidx.annotation.AttrRes
import androidx.core.content.ContextCompat
/**
* Utility class for resolving [Drawables](Drawable)
*/
object DrawableResolver {
@JvmStatic
fun resolveDrawable(context: Context, @AttrRes attrResId: Int): Drawable? {
return ContextCompat.getDrawable(
context,
TypedValue().apply {
context.theme.resolveAttribute(
attrResId,
this,
true
)
}.resourceId
)
}
}

View file

@ -23,9 +23,11 @@ import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.ActionBar;
@ -227,6 +229,22 @@ public final class ThemeHelper {
return value.data;
}
/**
* Resolves a {@link Drawable} by it's id.
*
* @param context Context
* @param attrResId Resource id
* @return the {@link Drawable}
*/
public static Drawable resolveDrawable(
@NonNull final Context context,
@AttrRes final int attrResId
) {
final TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(attrResId, typedValue, true);
return ContextCompat.getDrawable(context, typedValue.resourceId);
}
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);