Search in sources :

Example 1 with TypedResource

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

the class ShadowAssetManager method resolveStyle.

private Style resolveStyle(@NotNull ResName themeStyleName, Style themeStyleSet) {
    TypedResource themeStyleResource = resourceTable.getValue(themeStyleName, RuntimeEnvironment.getQualifiers());
    if (themeStyleResource == null)
        return null;
    StyleData themeStyleData = (StyleData) themeStyleResource.getData();
    if (themeStyleSet == null) {
        themeStyleSet = new ThemeStyleSet();
    }
    return new StyleResolver(resourceTable, shadowOf(AssetManager.getSystem()).getResourceTable(), themeStyleData, themeStyleSet, themeStyleName, RuntimeEnvironment.getQualifiers());
}
Also used : ThemeStyleSet(org.robolectric.res.ThemeStyleSet) FileTypedResource(org.robolectric.res.FileTypedResource) TypedResource(org.robolectric.res.TypedResource) StyleResolver(org.robolectric.res.StyleResolver) StyleData(org.robolectric.res.StyleData)

Example 2 with TypedResource

use of org.robolectric.res.TypedResource 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)

Example 3 with TypedResource

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

the class ShadowAssetManager method getResourceValue.

@HiddenApi
@Implementation
public boolean getResourceValue(int ident, int density, TypedValue outValue, boolean resolveRefs) {
    TypedResource value = getAndResolve(ident, RuntimeEnvironment.getQualifiers(), resolveRefs);
    if (value == null)
        return false;
    getConverter(value).fillTypedValue(value.getData(), outValue);
    return true;
}
Also used : FileTypedResource(org.robolectric.res.FileTypedResource) TypedResource(org.robolectric.res.TypedResource) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 4 with TypedResource

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

the class ShadowAssetManager method getTypedArrayResource.

protected TypedArray getTypedArrayResource(Resources resources, int resId) {
    TypedResource value = getAndResolve(resId, RuntimeEnvironment.getQualifiers(), true);
    if (value == null) {
        return null;
    }
    TypedResource[] items = getConverter(value).getItems(value);
    return getTypedArray(resources, items, resId);
}
Also used : FileTypedResource(org.robolectric.res.FileTypedResource) TypedResource(org.robolectric.res.TypedResource)

Example 5 with TypedResource

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

the class ShadowResourcesImpl method getQuantityString.

@Implementation
public String getQuantityString(int resId, int quantity) throws Resources.NotFoundException {
    ShadowAssetManager shadowAssetManager = shadowOf(realResourcesImpl.getAssets());
    TypedResource typedResource = shadowAssetManager.getResourceTable().getValue(resId, RuntimeEnvironment.getQualifiers());
    if (typedResource != null && typedResource instanceof PluralResourceLoader.PluralRules) {
        PluralResourceLoader.PluralRules pluralRules = (PluralResourceLoader.PluralRules) typedResource;
        Plural plural = pluralRules.find(quantity);
        if (plural == null) {
            return null;
        }
        TypedResource<?> resolvedTypedResource = shadowAssetManager.resolve(new TypedResource<>(plural.getString(), ResType.CHAR_SEQUENCE, pluralRules.getXmlContext()), RuntimeEnvironment.getQualifiers(), resId);
        return resolvedTypedResource == null ? null : resolvedTypedResource.asString();
    } else {
        return null;
    }
}
Also used : TypedResource(org.robolectric.res.TypedResource) Plural(org.robolectric.res.Plural) PluralResourceLoader(org.robolectric.res.PluralResourceLoader)

Aggregations

TypedResource (org.robolectric.res.TypedResource)8 FileTypedResource (org.robolectric.res.FileTypedResource)7 HiddenApi (org.robolectric.annotation.HiddenApi)3 Implementation (org.robolectric.annotation.Implementation)3 ResName (org.robolectric.res.ResName)2 Resources (android.content.res.Resources)1 TypedValue (android.util.TypedValue)1 AttrData (org.robolectric.res.AttrData)1 AttributeResource (org.robolectric.res.AttributeResource)1 Plural (org.robolectric.res.Plural)1 PluralResourceLoader (org.robolectric.res.PluralResourceLoader)1 ResType (org.robolectric.res.ResType)1 StyleData (org.robolectric.res.StyleData)1 StyleResolver (org.robolectric.res.StyleResolver)1 ThemeStyleSet (org.robolectric.res.ThemeStyleSet)1