Search in sources :

Example 6 with TypedResource

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

the class ShadowAssetManager method getResourceTextArray.

@HiddenApi
@Implementation
public CharSequence[] getResourceTextArray(int resId) {
    TypedResource value = getAndResolve(resId, RuntimeEnvironment.getQualifiers(), true);
    if (value == null)
        return null;
    TypedResource[] items = getConverter(value).getItems(value);
    CharSequence[] charSequences = new CharSequence[items.length];
    for (int i = 0; i < items.length; i++) {
        TypedResource typedResource = resolve(items[i], RuntimeEnvironment.getQualifiers(), resId);
        charSequences[i] = getConverter(typedResource).asCharSequence(typedResource);
    }
    return charSequences;
}
Also used : FileTypedResource(org.robolectric.res.FileTypedResource) TypedResource(org.robolectric.res.TypedResource) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 7 with TypedResource

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

the class ShadowAssetManager method convertAndFill.

private void convertAndFill(AttributeResource attribute, TypedValue outValue, String qualifiers, boolean resolveRefs) {
    if (attribute.isNull()) {
        outValue.type = TypedValue.TYPE_NULL;
        outValue.data = TypedValue.DATA_NULL_UNDEFINED;
        return;
    } else if (attribute.isEmpty()) {
        outValue.type = TypedValue.TYPE_NULL;
        outValue.data = TypedValue.DATA_NULL_EMPTY;
        return;
    }
    // short-circuit Android caching of loaded resources cuz our string positions don't remain stable...
    outValue.assetCookie = Converter.getNextStringCookie();
    // TODO: Handle resource and style references
    if (attribute.isStyleReference()) {
        return;
    }
    while (attribute.isResourceReference()) {
        Integer resourceId;
        ResName resName = attribute.getResourceReference();
        if (attribute.getReferenceResId() != null) {
            resourceId = attribute.getReferenceResId();
        } else {
            resourceId = resourceTable.getResourceId(resName);
        }
        if (resourceId == null) {
            throw new Resources.NotFoundException("unknown resource " + resName);
        }
        outValue.type = TypedValue.TYPE_REFERENCE;
        if (!resolveRefs) {
            // Just return the resourceId if resolveRefs is false.
            outValue.data = resourceId;
            return;
        }
        outValue.resourceId = resourceId;
        TypedResource dereferencedRef = resourceTable.getValue(resName, qualifiers);
        if (dereferencedRef == null) {
            Logger.strict("couldn't resolve %s from %s", resName.getFullyQualifiedName(), attribute);
            if (resName.type.equals("id")) {
                return;
            } else if (resName.type.equals("layout")) {
                // resourceId is good enough, right?
                return;
            } else if (resName.type.equals("dimen")) {
                return;
            } else if (resName.type.equals("transition")) {
                return;
            } else if (resName.type.equals("interpolator")) {
                return;
            } else if (resName.type.equals("menu")) {
                return;
            } else if (resName.type.equals("raw")) {
                return;
            } else if (DrawableResourceLoader.isStillHandledHere(resName.type)) {
                // wtf. color and drawable references reference are all kinds of stupid.
                TypedResource drawableResource = resourceTable.getValue(resName, qualifiers);
                if (drawableResource == null) {
                    throw new Resources.NotFoundException("can't find file for " + resName);
                } else {
                    outValue.type = TypedValue.TYPE_STRING;
                    outValue.data = 0;
                    outValue.assetCookie = Converter.getNextStringCookie();
                    outValue.string = (CharSequence) drawableResource.getData();
                    return;
                }
            } else {
                throw new RuntimeException("huh? " + resName);
            }
        } else {
            if (dereferencedRef.isFile()) {
                outValue.type = TypedValue.TYPE_STRING;
                outValue.data = 0;
                outValue.assetCookie = Converter.getNextStringCookie();
                outValue.string = dereferencedRef.asString();
                return;
            } else if (dereferencedRef.getData() instanceof String) {
                attribute = new AttributeResource(attribute.resName, dereferencedRef.asString(), resName.packageName);
                if (attribute.isResourceReference()) {
                    continue;
                }
                if (resolveRefs) {
                    Converter.getConverter(dereferencedRef.getResType()).fillTypedValue(attribute.value, outValue);
                    return;
                }
            }
        }
        break;
    }
    if (attribute.isNull()) {
        outValue.type = TypedValue.TYPE_NULL;
        return;
    }
    TypedResource attrTypeData = resourceTable.getValue(attribute.resName, qualifiers);
    if (attrTypeData != null) {
        AttrData attrData = (AttrData) attrTypeData.getData();
        String format = attrData.getFormat();
        String[] types = format.split("\\|");
        for (String type : types) {
            // already handled above
            if ("reference".equals(type))
                continue;
            Converter converter = Converter.getConverterFor(attrData, type);
            if (converter != null) {
                if (converter.fillTypedValue(attribute.value, outValue)) {
                    return;
                }
            }
        }
    } else {
        /**
       * In cases where the runtime framework doesn't know this attribute, e.g: viewportHeight (added in 21) on a
       * KitKat runtine, then infer the attribute type from the value.
       *
       * TODO: When we are able to pass the SDK resources from the build environment then we can remove this
       * and replace the NullResourceLoader with simple ResourceProvider that only parses attribute type information.
       */
        ResType resType = ResType.inferFromValue(attribute.value);
        Converter.getConverter(resType).fillTypedValue(attribute.value, outValue);
    }
}
Also used : AttrData(org.robolectric.res.AttrData) FileTypedResource(org.robolectric.res.FileTypedResource) TypedResource(org.robolectric.res.TypedResource) ResType(org.robolectric.res.ResType) AttributeResource(org.robolectric.res.AttributeResource) ResName(org.robolectric.res.ResName) Resources(android.content.res.Resources)

Example 8 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