Search in sources :

Example 1 with AttrData

use of org.robolectric.res.AttrData 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 2 with AttrData

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

the class ShadowLegacyAssetManager method convertAndFill.

private void convertAndFill(AttributeResource attribute, TypedValue outValue, ResTable_config config, 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();
    outValue.changingConfigurations = 0;
    // 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 = getResourceTable().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 = getResourceTable().getValue(resName, config);
        if (dereferencedRef == null) {
            Logger.strict("couldn't resolve %s from %s", resName.getFullyQualifiedName(), attribute);
            return;
        } 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 = getAttrTypeData(attribute.resName);
    if (attrTypeData != null) {
        AttrData attrData = (AttrData) attrTypeData.getData();
        String format = attrData.getFormat();
        String[] types = format.split("\\|");
        Arrays.sort(types, ATTRIBUTE_TYPE_PRECIDENCE);
        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 : AttributeResource(org.robolectric.res.AttributeResource) AttrData(org.robolectric.res.AttrData) TypedResource(org.robolectric.res.TypedResource) FileTypedResource(org.robolectric.res.FileTypedResource) FileNotFoundException(java.io.FileNotFoundException) ResName(org.robolectric.res.ResName) ResType(org.robolectric.res.ResType)

Aggregations

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