Search in sources :

Example 1 with Style

use of org.robolectric.res.Style in project robolectric by robolectric.

the class ShadowAssetManager method buildTypedValue.

private TypedValue buildTypedValue(AttributeSet set, int resId, int defStyleAttr, Style themeStyleSet, int defStyleRes) {
    /*
     * When determining the final value of a particular attribute, there are four inputs that come into play:
     *
     * 1. Any attribute values in the given AttributeSet.
     * 2. The style resource specified in the AttributeSet (named "style").
     * 3. The default style specified by defStyleAttr and defStyleRes
     * 4. The base values in this theme.
     */
    Style defStyleFromAttr = null;
    Style defStyleFromRes = null;
    Style styleAttrStyle = null;
    if (defStyleAttr != 0) {
        // Load the theme attribute for the default style attributes. E.g., attr/buttonStyle
        ResName defStyleName = getResName(defStyleAttr);
        // Load the style for the default style attribute. E.g. "@style/Widget.Robolectric.Button";
        AttributeResource defStyleAttribute = themeStyleSet.getAttrValue(defStyleName);
        if (defStyleAttribute != null) {
            while (defStyleAttribute.isStyleReference()) {
                AttributeResource other = themeStyleSet.getAttrValue(defStyleAttribute.getStyleReference());
                if (other == null) {
                    throw new RuntimeException("couldn't dereference " + defStyleAttribute);
                }
                defStyleAttribute = other;
            }
            if (defStyleAttribute.isResourceReference()) {
                ResName defStyleResName = defStyleAttribute.getResourceReference();
                defStyleFromAttr = resolveStyle(defStyleResName, themeStyleSet);
            }
        }
    }
    if (set != null && set.getStyleAttribute() != 0) {
        ResName styleAttributeResName = getResName(set.getStyleAttribute());
        while (styleAttributeResName.type.equals("attr")) {
            AttributeResource attrValue = themeStyleSet.getAttrValue(styleAttributeResName);
            if (attrValue == null) {
                throw new RuntimeException("no value for " + styleAttributeResName.getFullyQualifiedName() + " in " + themeStyleSet);
            }
            if (attrValue.isResourceReference()) {
                styleAttributeResName = attrValue.getResourceReference();
            } else if (attrValue.isStyleReference()) {
                styleAttributeResName = attrValue.getStyleReference();
            }
        }
        styleAttrStyle = resolveStyle(styleAttributeResName, themeStyleSet);
    }
    if (defStyleRes != 0) {
        ResName resName = getResName(defStyleRes);
        if (resName.type.equals("attr")) {
            AttributeResource attributeValue = findAttributeValue(defStyleRes, set, styleAttrStyle, defStyleFromAttr, defStyleFromAttr, themeStyleSet);
            if (attributeValue != null) {
                if (attributeValue.isStyleReference()) {
                    resName = themeStyleSet.getAttrValue(attributeValue.getStyleReference()).getResourceReference();
                } else if (attributeValue.isResourceReference()) {
                    resName = attributeValue.getResourceReference();
                }
            }
        }
        defStyleFromRes = resolveStyle(resName, themeStyleSet);
    }
    AttributeResource attribute = findAttributeValue(resId, set, styleAttrStyle, defStyleFromAttr, defStyleFromRes, themeStyleSet);
    while (attribute != null && attribute.isStyleReference()) {
        ResName otherAttrName = attribute.getStyleReference();
        if (attribute.resName.equals(otherAttrName)) {
            Logger.info("huh... circular reference for %s?", attribute.resName.getFullyQualifiedName());
            return null;
        }
        ResName resName = resourceTable.getResName(resId);
        AttributeResource otherAttr = themeStyleSet.getAttrValue(otherAttrName);
        if (otherAttr == null) {
            strictError("no such attr %s in %s while resolving value for %s", attribute.value, themeStyleSet, resName.getFullyQualifiedName());
            attribute = null;
        } else {
            attribute = new AttributeResource(resName, otherAttr.value, otherAttr.contextPackageName);
        }
    }
    if (attribute == null || attribute.isNull()) {
        return null;
    } else {
        TypedValue typedValue = new TypedValue();
        convertAndFill(attribute, typedValue, RuntimeEnvironment.getQualifiers(), true);
        return typedValue;
    }
}
Also used : AttributeResource(org.robolectric.res.AttributeResource) EmptyStyle(org.robolectric.res.EmptyStyle) Style(org.robolectric.res.Style) ResName(org.robolectric.res.ResName) TypedValue(android.util.TypedValue)

Example 2 with Style

use of org.robolectric.res.Style in project robolectric by robolectric.

the class ShadowAssetManager method attrsToTypedArray.

TypedArray attrsToTypedArray(Resources resources, AttributeSet set, int[] attrs, int defStyleAttr, long nativeTheme, int defStyleRes) {
    CharSequence[] stringData = new CharSequence[attrs.length];
    int[] data = new int[attrs.length * ShadowAssetManager.STYLE_NUM_ENTRIES];
    int[] indices = new int[attrs.length + 1];
    int nextIndex = 0;
    Style themeStyleSet = nativeTheme == 0 ? new EmptyStyle() : getNativeTheme(nativeTheme).themeStyleSet;
    for (int i = 0; i < attrs.length; i++) {
        int offset = i * ShadowAssetManager.STYLE_NUM_ENTRIES;
        TypedValue typedValue = buildTypedValue(set, attrs[i], defStyleAttr, themeStyleSet, defStyleRes);
        if (typedValue != null) {
            //noinspection PointlessArithmeticExpression
            data[offset + ShadowAssetManager.STYLE_TYPE] = typedValue.type;
            data[offset + ShadowAssetManager.STYLE_DATA] = typedValue.type == TypedValue.TYPE_STRING ? i : typedValue.data;
            data[offset + ShadowAssetManager.STYLE_ASSET_COOKIE] = typedValue.assetCookie;
            data[offset + ShadowAssetManager.STYLE_RESOURCE_ID] = typedValue.resourceId;
            data[offset + ShadowAssetManager.STYLE_CHANGING_CONFIGURATIONS] = typedValue.changingConfigurations;
            data[offset + ShadowAssetManager.STYLE_DENSITY] = typedValue.density;
            stringData[i] = typedValue.string;
            indices[nextIndex + 1] = i;
            nextIndex++;
        }
    }
    indices[0] = nextIndex;
    TypedArray typedArray = ShadowTypedArray.create(resources, attrs, data, indices, nextIndex, stringData);
    if (set != null) {
        shadowOf(typedArray).positionDescription = set.getPositionDescription();
    }
    return typedArray;
}
Also used : EmptyStyle(org.robolectric.res.EmptyStyle) TypedArray(android.content.res.TypedArray) EmptyStyle(org.robolectric.res.EmptyStyle) Style(org.robolectric.res.Style) TypedValue(android.util.TypedValue)

Example 3 with Style

use of org.robolectric.res.Style in project robolectric by robolectric.

the class ShadowAssetManager method applyThemeStyle.

@HiddenApi
@Implementation(minSdk = LOLLIPOP)
public static void applyThemeStyle(long themePtr, int styleRes, boolean force) {
    NativeTheme nativeTheme = getNativeTheme(themePtr);
    Style style = nativeTheme.getShadowAssetManager().resolveStyle(styleRes, null);
    nativeTheme.themeStyleSet.apply(style, force);
}
Also used : EmptyStyle(org.robolectric.res.EmptyStyle) Style(org.robolectric.res.Style) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 4 with Style

use of org.robolectric.res.Style in project robolectric by robolectric.

the class ShadowLegacyAssetManager method buildTypedValue.

private TypedValue buildTypedValue(AttributeSet set, int resId, int defStyleAttr, Style themeStyleSet, int defStyleRes) {
    /*
     * When determining the final value of a particular attribute, there are four inputs that come into play:
     *
     * 1. Any attribute values in the given AttributeSet.
     * 2. The style resource specified in the AttributeSet (named "style").
     * 3. The default style specified by defStyleAttr and defStyleRes
     * 4. The base values in this theme.
     */
    Style defStyleFromAttr = null;
    Style defStyleFromRes = null;
    Style styleAttrStyle = null;
    if (defStyleAttr != 0) {
        // Load the theme attribute for the default style attributes. E.g., attr/buttonStyle
        ResName defStyleName = getResName(defStyleAttr);
        // Load the style for the default style attribute. E.g. "@style/Widget.Robolectric.Button";
        AttributeResource defStyleAttribute = themeStyleSet.getAttrValue(defStyleName);
        if (defStyleAttribute != null) {
            while (defStyleAttribute.isStyleReference()) {
                AttributeResource other = themeStyleSet.getAttrValue(defStyleAttribute.getStyleReference());
                if (other == null) {
                    throw new RuntimeException("couldn't dereference " + defStyleAttribute);
                }
                defStyleAttribute = other;
            }
            if (defStyleAttribute.isResourceReference()) {
                ResName defStyleResName = defStyleAttribute.getResourceReference();
                defStyleFromAttr = resolveStyle(defStyleResName, themeStyleSet);
            }
        }
    }
    if (set != null && set.getStyleAttribute() != 0) {
        ResName styleAttributeResName = getResName(set.getStyleAttribute());
        while (styleAttributeResName.type.equals("attr")) {
            AttributeResource attrValue = themeStyleSet.getAttrValue(styleAttributeResName);
            if (attrValue == null) {
                throw new RuntimeException("no value for " + styleAttributeResName.getFullyQualifiedName() + " in " + themeStyleSet);
            }
            if (attrValue.isResourceReference()) {
                styleAttributeResName = attrValue.getResourceReference();
            } else if (attrValue.isStyleReference()) {
                styleAttributeResName = attrValue.getStyleReference();
            }
        }
        styleAttrStyle = resolveStyle(styleAttributeResName, themeStyleSet);
    }
    if (defStyleRes != 0) {
        ResName resName = getResName(defStyleRes);
        if (resName.type.equals("attr")) {
            // todo: this should be a style resId, not an attr
            System.out.println("WARN: " + resName.getFullyQualifiedName() + " should be a style resId");
        // AttributeResource attributeValue = findAttributeValue(defStyleRes, set, styleAttrStyle, defStyleFromAttr, defStyleFromAttr, themeStyleSet);
        // if (attributeValue != null) {
        // if (attributeValue.isStyleReference()) {
        // resName = themeStyleSet.getAttrValue(attributeValue.getStyleReference()).getResourceReference();
        // } else if (attributeValue.isResourceReference()) {
        // resName = attributeValue.getResourceReference();
        // }
        // }
        } else if (resName.type.equals("style")) {
            defStyleFromRes = resolveStyle(resName, themeStyleSet);
        }
    }
    AttributeResource attribute = findAttributeValue(resId, set, styleAttrStyle, defStyleFromAttr, defStyleFromRes, themeStyleSet);
    while (attribute != null && attribute.isStyleReference()) {
        ResName otherAttrName = attribute.getStyleReference();
        if (attribute.resName.equals(otherAttrName)) {
            Logger.info("huh... circular reference for %s?", attribute.resName.getFullyQualifiedName());
            return null;
        }
        ResName resName = getResourceTable().getResName(resId);
        AttributeResource otherAttr = themeStyleSet.getAttrValue(otherAttrName);
        if (otherAttr == null) {
            strictError("no such attr %s in %s while resolving value for %s", attribute.value, themeStyleSet, resName.getFullyQualifiedName());
            attribute = null;
        } else {
            attribute = new AttributeResource(resName, otherAttr.value, otherAttr.contextPackageName);
        }
    }
    if (attribute == null || attribute.isNull()) {
        return null;
    } else {
        TypedValue typedValue = new TypedValue();
        convertAndFill(attribute, typedValue, config, true);
        return typedValue;
    }
}
Also used : AttributeResource(org.robolectric.res.AttributeResource) EmptyStyle(org.robolectric.res.EmptyStyle) Style(org.robolectric.res.Style) ResName(org.robolectric.res.ResName) TypedValue(android.util.TypedValue)

Example 5 with Style

use of org.robolectric.res.Style in project robolectric by robolectric.

the class ShadowLegacyAssetManager method applyThemeStyle.

@HiddenApi
@Implementation(minSdk = LOLLIPOP, maxSdk = O_MR1)
public static void applyThemeStyle(long themePtr, int styleRes, boolean force) {
    NativeTheme nativeTheme = getNativeTheme(themePtr);
    Style style = nativeTheme.getShadowAssetManager().resolveStyle(styleRes, null);
    nativeTheme.themeStyleSet.apply(style, force);
}
Also used : EmptyStyle(org.robolectric.res.EmptyStyle) Style(org.robolectric.res.Style) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Aggregations

EmptyStyle (org.robolectric.res.EmptyStyle)6 Style (org.robolectric.res.Style)6 TypedValue (android.util.TypedValue)4 TypedArray (android.content.res.TypedArray)2 HiddenApi (org.robolectric.annotation.HiddenApi)2 Implementation (org.robolectric.annotation.Implementation)2 AttributeResource (org.robolectric.res.AttributeResource)2 ResName (org.robolectric.res.ResName)2 SuppressLint (android.annotation.SuppressLint)1