Search in sources :

Example 1 with ResType

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

the class ShadowAssetManager method getResourceType.

private int getResourceType(TypedResource typedResource) {
    if (typedResource == null) {
        return -1;
    }
    final ResType resType = typedResource.getResType();
    int type;
    if (typedResource.getData() == null || resType == ResType.NULL) {
        type = TypedValue.TYPE_NULL;
    } else if (typedResource.isReference()) {
        type = TypedValue.TYPE_REFERENCE;
    } else if (resType == ResType.STYLE) {
        type = TypedValue.TYPE_ATTRIBUTE;
    } else if (resType == ResType.CHAR_SEQUENCE || resType == ResType.DRAWABLE) {
        type = TypedValue.TYPE_STRING;
    } else if (resType == ResType.INTEGER) {
        type = TypedValue.TYPE_INT_DEC;
    } else if (resType == ResType.FLOAT || resType == ResType.FRACTION) {
        type = TypedValue.TYPE_FLOAT;
    } else if (resType == ResType.BOOLEAN) {
        type = TypedValue.TYPE_INT_BOOLEAN;
    } else if (resType == ResType.DIMEN) {
        type = TypedValue.TYPE_DIMENSION;
    } else if (resType == ResType.COLOR) {
        type = TypedValue.TYPE_INT_COLOR_ARGB8;
    } else if (resType == ResType.TYPED_ARRAY || resType == ResType.CHAR_SEQUENCE_ARRAY) {
        type = TypedValue.TYPE_REFERENCE;
    } else {
        type = -1;
    }
    return type;
}
Also used : ResType(org.robolectric.res.ResType)

Example 2 with ResType

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

Aggregations

ResType (org.robolectric.res.ResType)2 Resources (android.content.res.Resources)1 AttrData (org.robolectric.res.AttrData)1 AttributeResource (org.robolectric.res.AttributeResource)1 FileTypedResource (org.robolectric.res.FileTypedResource)1 ResName (org.robolectric.res.ResName)1 TypedResource (org.robolectric.res.TypedResource)1