Search in sources :

Example 11 with ResourceType

use of com.android.resources.ResourceType in project bazel by bazelbuild.

the class FullyQualifiedName method createTypeFrom.

private static Type createTypeFrom(String rawType) {
    ResourceType resourceType = ResourceType.getEnum(rawType);
    VirtualType virtualType = VirtualType.getEnum(rawType);
    if (resourceType != null) {
        return new ResourceTypeWrapper(resourceType);
    } else if (virtualType != null) {
        return virtualType;
    }
    return null;
}
Also used : ResourceType(com.android.resources.ResourceType)

Example 12 with ResourceType

use of com.android.resources.ResourceType in project bazel by bazelbuild.

the class XmlResourceValues method parsePublic.

static XmlResourceValue parsePublic(XMLEventReader eventReader, StartElement start, Namespaces.Collector namespacesCollector) throws XMLStreamException {
    namespacesCollector.collectFrom(start);
    // The tag should be unary.
    if (!isEndTag(eventReader.peek(), start.getName())) {
        throw new XMLStreamException(String.format("<public> tag should be unary %s", start), start.getLocation());
    }
    // The tag should have a valid type attribute, and optionally an id attribute.
    ImmutableMap<String, String> attributes = ImmutableMap.copyOf(parseTagAttributes(start));
    String typeAttr = attributes.get(SdkConstants.ATTR_TYPE);
    ResourceType type;
    if (typeAttr != null) {
        type = ResourceType.getEnum(typeAttr);
        if (type == null || type == ResourceType.PUBLIC) {
            throw new XMLStreamException(String.format("<public> tag has invalid type attribute %s", start), start.getLocation());
        }
    } else {
        throw new XMLStreamException(String.format("<public> tag missing type attribute %s", start), start.getLocation());
    }
    String idValueAttr = attributes.get(SdkConstants.ATTR_ID);
    Optional<Integer> id = Optional.absent();
    if (idValueAttr != null) {
        try {
            id = Optional.of(Integer.decode(idValueAttr));
        } catch (NumberFormatException e) {
            throw new XMLStreamException(String.format("<public> has invalid id number %s", start), start.getLocation());
        }
    }
    if (attributes.size() > 2) {
        throw new XMLStreamException(String.format("<public> has unexpected attributes %s", start), start.getLocation());
    }
    return PublicXmlResourceValue.create(type, id);
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) ResourceType(com.android.resources.ResourceType)

Example 13 with ResourceType

use of com.android.resources.ResourceType in project bazel by bazelbuild.

the class AndroidResourceClassWriter method assignTypeIdsForPublic.

private Map<ResourceType, Integer> assignTypeIdsForPublic() {
    Map<ResourceType, Integer> allocatedTypeIds = new EnumMap<>(ResourceType.class);
    if (publicIds.isEmpty()) {
        return allocatedTypeIds;
    }
    // Keep track of the reverse mapping from Int -> Type for validation.
    Map<Integer, ResourceType> assignedIds = new HashMap<>();
    for (Map.Entry<ResourceType, SortedMap<String, Optional<Integer>>> publicTypeEntry : publicIds.entrySet()) {
        ResourceType currentType = publicTypeEntry.getKey();
        Integer reservedTypeSlot = null;
        String previousResource = null;
        for (Map.Entry<String, Optional<Integer>> publicEntry : publicTypeEntry.getValue().entrySet()) {
            Optional<Integer> reservedId = publicEntry.getValue();
            if (!reservedId.isPresent()) {
                continue;
            }
            Integer typePortion = extractTypeId(reservedId.get());
            if (reservedTypeSlot == null) {
                reservedTypeSlot = typePortion;
                previousResource = publicEntry.getKey();
            } else {
                if (!reservedTypeSlot.equals(typePortion)) {
                    logger.warning(String.format("%s has conflicting type codes for its public identifiers (%s=%s vs %s=%s)", currentType.getName(), previousResource, reservedTypeSlot, publicEntry.getKey(), typePortion));
                }
            }
        }
        if (currentType == ResourceType.ATTR && reservedTypeSlot != null && !reservedTypeSlot.equals(ATTR_TYPE_ID)) {
            logger.warning(String.format("Cannot force ATTR to have type code other than 0x%02x (got 0x%02x from %s)", ATTR_TYPE_ID, reservedTypeSlot, previousResource));
        }
        allocatedTypeIds.put(currentType, reservedTypeSlot);
        ResourceType alreadyAssigned = assignedIds.put(reservedTypeSlot, currentType);
        if (alreadyAssigned != null) {
            logger.warning(String.format("Multiple type names declared for public type identifier 0x%x (%s vs %s)", reservedTypeSlot, alreadyAssigned, currentType));
        }
    }
    return allocatedTypeIds;
}
Also used : Optional(com.google.common.base.Optional) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ResourceType(com.android.resources.ResourceType) SortedMap(java.util.SortedMap) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 14 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by ParanoidAndroid.

the class BridgeContext method resolveThemeAttribute.

public boolean resolveThemeAttribute(int resid, TypedValue outValue, boolean resolveRefs) {
    Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(resid);
    boolean isFrameworkRes = true;
    if (resourceInfo == null) {
        resourceInfo = mProjectCallback.resolveResourceId(resid);
        isFrameworkRes = false;
    }
    if (resourceInfo == null) {
        return false;
    }
    ResourceValue value = mRenderResources.findItemInTheme(resourceInfo.getSecond(), isFrameworkRes);
    if (resolveRefs) {
        value = mRenderResources.resolveResValue(value);
    }
    // check if this is a style resource
    if (value instanceof StyleResourceValue) {
        // get the id that will represent this style.
        outValue.resourceId = getDynamicIdByStyle((StyleResourceValue) value);
        return true;
    }
    int a;
    // if this is a framework value.
    if (value.isFramework()) {
        // look for idName in the android R classes.
        // use 0 a default res value as it's not a valid id value.
        a = getFrameworkResourceValue(value.getResourceType(), value.getName(), 0);
    } else {
        // look for idName in the project R class.
        // use 0 a default res value as it's not a valid id value.
        a = getProjectResourceValue(value.getResourceType(), value.getName(), 0);
    }
    if (a != 0) {
        outValue.resourceId = a;
        return true;
    }
    return false;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 15 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by ResurrectionRemix.

the class RenderDrawable method render.

public Result render() {
    checkLock();
    // get the drawable resource value
    DrawableParams params = getParams();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    ResourceValue drawableResource = params.getDrawable();
    // resolve it
    BridgeContext context = getContext();
    drawableResource = context.getRenderResources().resolveResValue(drawableResource);
    if (drawableResource == null) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    ResourceType resourceType = drawableResource.getResourceType();
    if (resourceType != ResourceType.DRAWABLE && resourceType != ResourceType.MIPMAP) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    Drawable d = ResourceHelper.getDrawable(drawableResource, context);
    final Boolean allStates = params.getFlag(RenderParamsFlags.FLAG_KEY_RENDER_ALL_DRAWABLE_STATES);
    if (allStates == Boolean.TRUE) {
        final List<BufferedImage> result;
        if (d instanceof StateListDrawable) {
            result = new ArrayList<BufferedImage>();
            final StateListDrawable stateList = (StateListDrawable) d;
            for (int i = 0; i < stateList.getStateCount(); i++) {
                final Drawable stateDrawable = stateList.getStateDrawable(i);
                result.add(renderImage(hardwareConfig, stateDrawable, context));
            }
        } else {
            result = Collections.singletonList(renderImage(hardwareConfig, d, context));
        }
        return Status.SUCCESS.createResult(result);
    } else {
        BufferedImage image = renderImage(hardwareConfig, d, context);
        return Status.SUCCESS.createResult(image);
    }
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) StateListDrawable(android.graphics.drawable.StateListDrawable) BufferedImage(java.awt.image.BufferedImage) DrawableParams(com.android.ide.common.rendering.api.DrawableParams)

Aggregations

ResourceType (com.android.resources.ResourceType)137 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)31 NotNull (org.jetbrains.annotations.NotNull)16 Field (java.lang.reflect.Field)13 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)12 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 Map (java.util.Map)10 Nullable (org.jetbrains.annotations.Nullable)10 Nullable (com.android.annotations.Nullable)8 ResourceFolderType (com.android.resources.ResourceFolderType)8 File (java.io.File)8 EnumMap (java.util.EnumMap)7 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)7 Context (android.content.Context)6 View (android.view.View)6 AbsListView (android.widget.AbsListView)6 AdapterView (android.widget.AdapterView)6 ExpandableListView (android.widget.ExpandableListView)6 FrameLayout (android.widget.FrameLayout)6