Search in sources :

Example 61 with ResourceType

use of com.android.resources.ResourceType in project platform_frameworks_base by android.

the class BridgeInflater method inflate.

@Override
public View inflate(int resource, ViewGroup root) {
    Context context = getContext();
    context = getBaseContext(context);
    if (context instanceof BridgeContext) {
        BridgeContext bridgeContext = (BridgeContext) context;
        ResourceValue value = null;
        @SuppressWarnings("deprecation") Pair<ResourceType, String> layoutInfo = Bridge.resolveResourceId(resource);
        if (layoutInfo != null) {
            value = bridgeContext.getRenderResources().getFrameworkResource(ResourceType.LAYOUT, layoutInfo.getSecond());
        } else {
            layoutInfo = mLayoutlibCallback.resolveResourceId(resource);
            if (layoutInfo != null) {
                value = bridgeContext.getRenderResources().getProjectResource(ResourceType.LAYOUT, layoutInfo.getSecond());
            }
        }
        if (value != null) {
            File f = new File(value.getValue());
            if (f.isFile()) {
                try {
                    XmlPullParser parser = ParserFactory.create(f, true);
                    BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(parser, bridgeContext, value.isFramework());
                    return inflate(bridgeParser, root);
                } catch (Exception e) {
                    Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed to parse file " + f.getAbsolutePath(), e, null);
                    return null;
                }
            }
        }
    }
    return null;
}
Also used : Context(android.content.Context) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) BridgeContext.getBaseContext(com.android.layoutlib.bridge.android.BridgeContext.getBaseContext) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) XmlPullParser(org.xmlpull.v1.XmlPullParser) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) File(java.io.File) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser)

Example 62 with ResourceType

use of com.android.resources.ResourceType in project platform_frameworks_base by android.

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)

Example 63 with ResourceType

use of com.android.resources.ResourceType in project platform_frameworks_base by android.

the class LayoutLibTestCallback method initResources.

public void initResources() throws ClassNotFoundException {
    Class<?> rClass = mModuleClassLoader.loadClass(PACKAGE_NAME + ".R");
    Class<?>[] nestedClasses = rClass.getDeclaredClasses();
    for (Class<?> resClass : nestedClasses) {
        final ResourceType resType = ResourceType.getEnum(resClass.getSimpleName());
        if (resType != null) {
            final Map<String, Integer> resName2Id = Maps.newHashMap();
            mResources.put(resType, resName2Id);
            for (Field field : resClass.getDeclaredFields()) {
                final int modifiers = field.getModifiers();
                if (Modifier.isStatic(modifiers)) {
                    // May not be final in library projects
                    final Class<?> type = field.getType();
                    try {
                        if (type.isArray() && type.getComponentType() == int.class) {
                            mStyleableValueToNameMap.put(new IntArrayWrapper((int[]) field.get(null)), field.getName());
                        } else if (type == int.class) {
                            final Integer value = (Integer) field.get(null);
                            mProjectResources.put(value, Pair.of(resType, field.getName()));
                            resName2Id.put(field.getName(), value);
                        } else {
                            mLog.error(null, "Unknown field type in R class: %1$s", type);
                        }
                    } catch (IllegalAccessException ignored) {
                        mLog.error(ignored, "Malformed R class: %1$s", PACKAGE_NAME + ".R");
                    }
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) ResourceType(com.android.resources.ResourceType) IntArrayWrapper(com.android.ide.common.resources.IntArrayWrapper)

Example 64 with ResourceType

use of com.android.resources.ResourceType in project platform_frameworks_base by android.

the class CustomBar method getColor.

private static int getColor(RenderResources renderResources, String attr) {
    // From ?attr/foo to @color/bar. This is most likely an ItemResourceValue.
    ResourceValue resource = renderResources.findItemInTheme(attr, true);
    // Form @color/bar to the #AARRGGBB
    resource = renderResources.resolveResValue(resource);
    if (resource != null) {
        ResourceType type = resource.getResourceType();
        if (type == null || type == ResourceType.COLOR) {
            // file, rather than referencing a color resource value.
            try {
                return ResourceHelper.getColor(resource.getValue());
            } catch (NumberFormatException e) {
                // Conversion failed.
                Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, "Theme attribute @android:" + attr + " does not reference a color, instead is '" + resource.getValue() + "'.", resource);
            }
        }
    }
    return 0;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 65 with ResourceType

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

the class PublicXmlResourceValue method from.

@SuppressWarnings("deprecation")
public static XmlResourceValue from(SerializeFormat.DataValueXml proto) {
    Map<String, String> protoValues = proto.getMappedStringValue();
    ImmutableMap.Builder<ResourceType, Optional<Integer>> typeToId = ImmutableMap.builder();
    for (Entry<String, String> entry : protoValues.entrySet()) {
        ResourceType type = ResourceType.getEnum(entry.getKey());
        Preconditions.checkNotNull(type);
        Optional<Integer> id = MISSING_ID_VALUE.equals(entry.getValue()) ? Optional.<Integer>absent() : Optional.of(Integer.decode(entry.getValue()));
        typeToId.put(type, id);
    }
    return of(typeToId.build());
}
Also used : Optional(com.google.common.base.Optional) ResourceType(com.android.resources.ResourceType) ImmutableMap(com.google.common.collect.ImmutableMap)

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