Search in sources :

Example 1 with DeclareStyleableResourceValue

use of com.android.ide.common.rendering.api.DeclareStyleableResourceValue in project android by JetBrains.

the class ResourceClassGenerator method generateStyleable.

private void generateStyleable(@NotNull ClassWriter cw, @NotNull TObjectIntHashMap<String> styleableIntCache, String className) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("generateStyleable(%s)", anonymizeClassName(className)));
    }
    boolean debug = LOG.isDebugEnabled() && isPublicClass(className);
    Collection<String> declaredStyleables = myAppResources.getItemsOfType(ResourceType.DECLARE_STYLEABLE);
    // Generate all declarations - both int[] and int for the indices into the array.
    for (String styleableName : declaredStyleables) {
        List<ResourceItem> items = myAppResources.getResourceItem(ResourceType.DECLARE_STYLEABLE, styleableName);
        if (items == null || items.isEmpty()) {
            if (debug) {
                LOG.debug("  No items for " + styleableName);
            }
            continue;
        }
        String fieldName = AndroidResourceUtil.getFieldNameByResourceName(styleableName);
        cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, fieldName, "[I", null, null);
        if (debug) {
            LOG.debug("  Defined styleable " + fieldName);
        }
        ResourceValue resourceValue = items.get(0).getResourceValue(false);
        assert resourceValue instanceof DeclareStyleableResourceValue;
        DeclareStyleableResourceValue dv = (DeclareStyleableResourceValue) resourceValue;
        List<AttrResourceValue> attributes = dv.getAllAttributes();
        int idx = 0;
        for (AttrResourceValue value : attributes) {
            Integer initialValue = idx++;
            String styleableEntryName = getResourceName(fieldName, value);
            cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, styleableEntryName, "I", null, initialValue);
            styleableIntCache.put(styleableEntryName, initialValue);
            if (debug) {
                LOG.debug("  Defined styleable " + styleableEntryName);
            }
        }
    }
    // Generate class initializer block to initialize the arrays declared above.
    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    for (String styleableName : declaredStyleables) {
        List<ResourceItem> items = myAppResources.getResourceItem(ResourceType.DECLARE_STYLEABLE, styleableName);
        if (items == null || items.isEmpty()) {
            continue;
        }
        ResourceValue resourceValue = items.get(0).getResourceValue(false);
        assert resourceValue instanceof DeclareStyleableResourceValue;
        DeclareStyleableResourceValue dv = (DeclareStyleableResourceValue) resourceValue;
        List<AttrResourceValue> attributes = dv.getAllAttributes();
        if (attributes.isEmpty()) {
            continue;
        }
        Integer[] valuesArray = myAppResources.getDeclaredArrayValues(attributes, styleableName);
        if (valuesArray == null) {
            valuesArray = new Integer[attributes.size()];
        }
        List<Integer> values = Arrays.asList(valuesArray);
        String fieldName = AndroidResourceUtil.getFieldNameByResourceName(styleableName);
        myStyleableCache.put(fieldName, values);
        int idx = -1;
        for (AttrResourceValue value : attributes) {
            if (valuesArray[++idx] == null || !value.isFramework()) {
                valuesArray[idx] = myAppResources.getResourceId(ResourceType.ATTR, value.getName());
            }
        }
        generateArrayInitialization(mv, className, fieldName, values);
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(4, 0);
    mv.visitEnd();
}
Also used : DeclareStyleableResourceValue(com.android.ide.common.rendering.api.DeclareStyleableResourceValue) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) DeclareStyleableResourceValue(com.android.ide.common.rendering.api.DeclareStyleableResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem)

Aggregations

AttrResourceValue (com.android.ide.common.rendering.api.AttrResourceValue)1 DeclareStyleableResourceValue (com.android.ide.common.rendering.api.DeclareStyleableResourceValue)1 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 ResourceItem (com.android.ide.common.res2.ResourceItem)1 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)1