Search in sources :

Example 1 with ResName

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

the class ShadowAssetManager method loadXmlResourceParser.

public XmlResourceParser loadXmlResourceParser(int resId, String type) throws Resources.NotFoundException {
    ResName resName = getResName(resId);
    ResName resolvedResName = resolveResName(resName, RuntimeEnvironment.getQualifiers());
    if (resolvedResName == null) {
        throw new RuntimeException("couldn't resolve " + resName.getFullyQualifiedName());
    }
    resName = resolvedResName;
    XmlBlock block = resourceTable.getXml(resName, RuntimeEnvironment.getQualifiers());
    if (block == null) {
        throw new Resources.NotFoundException(resName.getFullyQualifiedName());
    }
    ResourceTable resourceProvider = ResourceIds.isFrameworkResource(resId) ? RuntimeEnvironment.getSystemResourceTable() : RuntimeEnvironment.getCompileTimeResourceTable();
    return getXmlResourceParser(resourceProvider, block, resName.packageName);
}
Also used : XmlBlock(org.robolectric.res.builder.XmlBlock) ResName(org.robolectric.res.ResName) ResourceTable(org.robolectric.res.ResourceTable)

Example 2 with ResName

use of org.robolectric.res.ResName 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 3 with ResName

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

the class ShadowAssetManager method findAttributeValue.

private AttributeResource findAttributeValue(int resId, AttributeSet attributeSet, Style styleAttrStyle, Style defStyleFromAttr, Style defStyleFromRes, @NotNull Style themeStyleSet) {
    if (attributeSet != null) {
        for (int i = 0; i < attributeSet.getAttributeCount(); i++) {
            if (attributeSet.getAttributeNameResource(i) == resId && attributeSet.getAttributeValue(i) != null) {
                String defaultPackageName = ResourceIds.isFrameworkResource(resId) ? "android" : RuntimeEnvironment.application.getPackageName();
                ResName resName = ResName.qualifyResName(attributeSet.getAttributeName(i), defaultPackageName, "attr");
                Integer referenceResId = null;
                if (AttributeResource.isResourceReference(attributeSet.getAttributeValue(i))) {
                    referenceResId = attributeSet.getAttributeResourceValue(i, -1);
                }
                return new AttributeResource(resName, attributeSet.getAttributeValue(i), "fixme!!!", referenceResId);
            }
        }
    }
    ResName attrName = resourceTable.getResName(resId);
    if (attrName == null)
        return null;
    if (styleAttrStyle != null) {
        AttributeResource attribute = styleAttrStyle.getAttrValue(attrName);
        if (attribute != null) {
            return attribute;
        }
    }
    // else if attr in defStyleFromAttr, use its value
    if (defStyleFromAttr != null) {
        AttributeResource attribute = defStyleFromAttr.getAttrValue(attrName);
        if (attribute != null) {
            return attribute;
        }
    }
    if (defStyleFromRes != null) {
        AttributeResource attribute = defStyleFromRes.getAttrValue(attrName);
        if (attribute != null) {
            return attribute;
        }
    }
    // else if attr in theme, use its value
    return themeStyleSet.getAttrValue(attrName);
}
Also used : AttributeResource(org.robolectric.res.AttributeResource) ResName(org.robolectric.res.ResName)

Example 4 with ResName

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

the class DefaultPackageManager method getPermissionInfo.

@Override
public PermissionInfo getPermissionInfo(String name, int flags) throws NameNotFoundException {
    PermissionInfo permissionInfo = extraPermissions.get(name);
    if (permissionInfo != null) {
        return permissionInfo;
    }
    PermissionItemData permissionItemData = applicationManifest.getPermissions().get(name);
    if (permissionItemData == null) {
        throw new NameNotFoundException(name);
    }
    permissionInfo = new PermissionInfo();
    String packageName = applicationManifest.getPackageName();
    permissionInfo.packageName = packageName;
    permissionInfo.name = name;
    permissionInfo.group = permissionItemData.getPermissionGroup();
    permissionInfo.protectionLevel = decodeProtectionLevel(permissionItemData.getProtectionLevel());
    String descriptionRef = permissionItemData.getDescription();
    if (descriptionRef != null) {
        ResName descResName = AttributeResource.getResourceReference(descriptionRef, packageName, "string");
        permissionInfo.descriptionRes = appResourceTable.getResourceId(descResName);
    }
    String labelRefOrString = permissionItemData.getLabel();
    if (labelRefOrString != null) {
        if (AttributeResource.isResourceReference(labelRefOrString)) {
            ResName labelResName = AttributeResource.getResourceReference(labelRefOrString, packageName, "string");
            permissionInfo.labelRes = appResourceTable.getResourceId(labelResName);
        } else {
            permissionInfo.nonLocalizedLabel = labelRefOrString;
        }
    }
    if ((flags & GET_META_DATA) != 0) {
        permissionInfo.metaData = metaDataToBundle(permissionItemData.getMetaData().getValueMap());
    }
    return permissionInfo;
}
Also used : PermissionItemData(org.robolectric.manifest.PermissionItemData) PermissionInfo(android.content.pm.PermissionInfo) ResName(org.robolectric.res.ResName)

Example 5 with ResName

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

the class ShadowAssetManager method getTypedArray.

private TypedArray getTypedArray(Resources resources, TypedResource[] typedResources, int resId) {
    final CharSequence[] stringData = new CharSequence[typedResources.length];
    final int totalLen = typedResources.length * ShadowAssetManager.STYLE_NUM_ENTRIES;
    final int[] data = new int[totalLen];
    for (int i = 0; i < typedResources.length; i++) {
        final int offset = i * ShadowAssetManager.STYLE_NUM_ENTRIES;
        TypedResource typedResource = typedResources[i];
        // Classify the item.
        int type = getResourceType(typedResource);
        if (type == -1) {
            // This type is unsupported; leave empty.
            continue;
        }
        final TypedValue typedValue = new TypedValue();
        if (type == TypedValue.TYPE_REFERENCE) {
            final String reference = typedResource.asString();
            ResName refResName = AttributeResource.getResourceReference(reference, typedResource.getXmlContext().getPackageName(), null);
            typedValue.resourceId = resourceTable.getResourceId(refResName);
            typedValue.data = typedValue.resourceId;
            typedResource = resolve(typedResource, RuntimeEnvironment.getQualifiers(), typedValue.resourceId);
            if (typedResource != null) {
                // Reclassify to a non-reference type.
                type = getResourceType(typedResource);
                if (type == TypedValue.TYPE_ATTRIBUTE) {
                    type = TypedValue.TYPE_REFERENCE;
                } else if (type == -1) {
                    // This type is unsupported; leave empty.
                    continue;
                }
            }
        }
        if (type == TypedValue.TYPE_ATTRIBUTE) {
            final String reference = typedResource.asString();
            final ResName attrResName = AttributeResource.getStyleReference(reference, typedResource.getXmlContext().getPackageName(), "attr");
            typedValue.data = resourceTable.getResourceId(attrResName);
        }
        if (typedResource != null && type != TypedValue.TYPE_NULL && type != TypedValue.TYPE_ATTRIBUTE) {
            getConverter(typedResource).fillTypedValue(typedResource.getData(), typedValue);
        }
        data[offset + ShadowAssetManager.STYLE_TYPE] = type;
        data[offset + ShadowAssetManager.STYLE_RESOURCE_ID] = typedValue.resourceId;
        data[offset + ShadowAssetManager.STYLE_DATA] = typedValue.data;
        data[offset + ShadowAssetManager.STYLE_ASSET_COOKIE] = typedValue.assetCookie;
        data[offset + ShadowAssetManager.STYLE_CHANGING_CONFIGURATIONS] = typedValue.changingConfigurations;
        data[offset + ShadowAssetManager.STYLE_DENSITY] = typedValue.density;
        stringData[i] = typedResource == null ? null : typedResource.asString();
    }
    int[] indices = new int[typedResources.length + 1];
    /* keep zeroed out */
    return ShadowTypedArray.create(resources, null, data, indices, typedResources.length, stringData);
}
Also used : FileTypedResource(org.robolectric.res.FileTypedResource) TypedResource(org.robolectric.res.TypedResource) ResName(org.robolectric.res.ResName) TypedValue(android.util.TypedValue)

Aggregations

ResName (org.robolectric.res.ResName)26 AttributeResource (org.robolectric.res.AttributeResource)9 TypedValue (android.util.TypedValue)6 FileTypedResource (org.robolectric.res.FileTypedResource)6 ResourceTable (org.robolectric.res.ResourceTable)6 HiddenApi (org.robolectric.annotation.HiddenApi)4 Implementation (org.robolectric.annotation.Implementation)4 TypedResource (org.robolectric.res.TypedResource)4 SuppressLint (android.annotation.SuppressLint)2 PermissionInfo (android.content.pm.PermissionInfo)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 PermissionItemData (org.robolectric.manifest.PermissionItemData)2 AttrData (org.robolectric.res.AttrData)2 EmptyStyle (org.robolectric.res.EmptyStyle)2 ResType (org.robolectric.res.ResType)2 Style (org.robolectric.res.Style)2 ThemeStyleSet (org.robolectric.res.ThemeStyleSet)2