Search in sources :

Example 1 with AttrResourceValue

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

the class AppResourceRepositoryTest method testGetDeclaredArrayValues.

public void testGetDeclaredArrayValues() throws IOException {
    final AppResourceRepository appResources = createTestAppResourceRepository(myFacet);
    ImmutableList.Builder<AttrResourceValue> builder = ImmutableList.builder();
    // simple styleable test.
    ImmutableList<AttrResourceValue> attrList = builder.add(new AttrResourceValue(ResourceType.ATTR, "some-attr", false, null)).build();
    Integer[] foundValues = appResources.getDeclaredArrayValues(attrList, "Styleable1");
    assertOrderedEquals(foundValues, 0x7f010000);
    // Declared styleables mismatch
    attrList = builder.add(new AttrResourceValue(ResourceType.ATTR, "some-attr", false, null), new AttrResourceValue(ResourceType.ATTR, "other-attr", false, null)).build();
    assertNull(appResources.getDeclaredArrayValues(attrList, "Styleable1"));
    // slightly complex test.
    builder = ImmutableList.builder();
    attrList = builder.add(new AttrResourceValue(ResourceType.ATTR, "app_attr1", false, null), new AttrResourceValue(ResourceType.ATTR, "app_attr2", false, null), new AttrResourceValue(ResourceType.ATTR, "framework-attr1", true, null), new AttrResourceValue(ResourceType.ATTR, "app_attr3", false, null), new AttrResourceValue(ResourceType.ATTR, "framework_attr2", true, null)).build();
    foundValues = appResources.getDeclaredArrayValues(attrList, "Styleable_with_underscore");
    assertOrderedEquals(foundValues, 0x7f010000, 0x7f010068, 0x01010125, 0x7f010069, 0x01010142);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue)

Example 2 with AttrResourceValue

use of com.android.ide.common.rendering.api.AttrResourceValue in project platform_frameworks_base by android.

the class BridgeTypedArray method resolveEnumAttribute.

/**
     * Searches for the string in the attributes (flag or enums) and returns the integer.
     * If found, it will return an integer matching the value.
     *
     * @param index Index of attribute to retrieve.
     *
     * @return Attribute int value, or null if not defined.
     */
private Integer resolveEnumAttribute(int index) {
    // Get the map of attribute-constant -> IntegerValue
    Map<String, Integer> map = null;
    if (mIsFramework[index]) {
        map = Bridge.getEnumValues(mNames[index]);
    } else {
        // get the styleable matching the resolved name
        RenderResources res = mContext.getRenderResources();
        ResourceValue attr = res.getProjectResource(ResourceType.ATTR, mNames[index]);
        if (attr instanceof AttrResourceValue) {
            map = ((AttrResourceValue) attr).getAttributeValues();
        }
    }
    if (map != null) {
        // accumulator to store the value of the 1+ constants.
        int result = 0;
        boolean found = false;
        // split the value in case this is a mix of several flags.
        String[] keywords = mResourceData[index].getValue().split("\\|");
        for (String keyword : keywords) {
            Integer i = map.get(keyword.trim());
            if (i != null) {
                result |= i;
                found = true;
            }
        // TODO: We should act smartly and log a warning for incorrect keywords. However,
        // this method is currently called even if the resourceValue is not an enum.
        }
        if (found) {
            return result;
        }
    }
    return null;
}
Also used : AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) RenderResources(com.android.ide.common.rendering.api.RenderResources) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue)

Example 3 with AttrResourceValue

use of com.android.ide.common.rendering.api.AttrResourceValue in project android_frameworks_base by ParanoidAndroid.

the class BridgeTypedArray method getInt.

/**
     * Retrieve the integer value for the attribute at <var>index</var>.
     *
     * @param index Index of attribute to retrieve.
     * @param defValue Value to return if the attribute is not defined.
     *
     * @return Attribute int value, or defValue if not defined.
     */
@Override
public int getInt(int index, int defValue) {
    if (index < 0 || index >= mResourceData.length) {
        return defValue;
    }
    if (mResourceData[index] == null) {
        return defValue;
    }
    String s = mResourceData[index].getValue();
    if (RenderResources.REFERENCE_NULL.equals(s)) {
        return defValue;
    }
    if (s == null || s.length() == 0) {
        return defValue;
    }
    try {
        return XmlUtils.convertValueToInt(s, defValue);
    } catch (NumberFormatException e) {
    // pass
    }
    // Field is not null and is not an integer.
    // Check for possible constants and try to find them.
    // Get the map of attribute-constant -> IntegerValue
    Map<String, Integer> map = null;
    if (mIsFramework[index]) {
        map = Bridge.getEnumValues(mNames[index]);
    } else {
        // get the styleable matching the resolved name
        RenderResources res = mContext.getRenderResources();
        ResourceValue attr = res.getProjectResource(ResourceType.ATTR, mNames[index]);
        if (attr instanceof AttrResourceValue) {
            map = ((AttrResourceValue) attr).getAttributeValues();
        }
    }
    if (map != null) {
        // accumulator to store the value of the 1+ constants.
        int result = 0;
        // split the value in case this is a mix of several flags.
        String[] keywords = s.split("\\|");
        for (String keyword : keywords) {
            Integer i = map.get(keyword.trim());
            if (i != null) {
                result |= i.intValue();
            } else {
                Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, String.format("\"%s\" in attribute \"%2$s\" is not a valid value", keyword, mNames[index]), null);
            }
        }
        return result;
    }
    return defValue;
}
Also used : AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) RenderResources(com.android.ide.common.rendering.api.RenderResources) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue)

Example 4 with AttrResourceValue

use of com.android.ide.common.rendering.api.AttrResourceValue 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)

Example 5 with AttrResourceValue

use of com.android.ide.common.rendering.api.AttrResourceValue in project android_frameworks_base by DirtyUnicorns.

the class BridgeTypedArray method resolveEnumAttribute.

/**
     * Searches for the string in the attributes (flag or enums) and returns the integer.
     * If found, it will return an integer matching the value.
     *
     * @param index Index of attribute to retrieve.
     *
     * @return Attribute int value, or null if not defined.
     */
private Integer resolveEnumAttribute(int index) {
    // Get the map of attribute-constant -> IntegerValue
    Map<String, Integer> map = null;
    if (mIsFramework[index]) {
        map = Bridge.getEnumValues(mNames[index]);
    } else {
        // get the styleable matching the resolved name
        RenderResources res = mContext.getRenderResources();
        ResourceValue attr = res.getProjectResource(ResourceType.ATTR, mNames[index]);
        if (attr instanceof AttrResourceValue) {
            map = ((AttrResourceValue) attr).getAttributeValues();
        }
    }
    if (map != null) {
        // accumulator to store the value of the 1+ constants.
        int result = 0;
        boolean found = false;
        // split the value in case this is a mix of several flags.
        String[] keywords = mResourceData[index].getValue().split("\\|");
        for (String keyword : keywords) {
            Integer i = map.get(keyword.trim());
            if (i != null) {
                result |= i;
                found = true;
            }
        // TODO: We should act smartly and log a warning for incorrect keywords. However,
        // this method is currently called even if the resourceValue is not an enum.
        }
        if (found) {
            return result;
        }
    }
    return null;
}
Also used : AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) RenderResources(com.android.ide.common.rendering.api.RenderResources) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue)

Aggregations

AttrResourceValue (com.android.ide.common.rendering.api.AttrResourceValue)7 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)6 RenderResources (com.android.ide.common.rendering.api.RenderResources)5 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)5 ArrayResourceValue (com.android.ide.common.rendering.api.ArrayResourceValue)4 DeclareStyleableResourceValue (com.android.ide.common.rendering.api.DeclareStyleableResourceValue)1 ResourceItem (com.android.ide.common.res2.ResourceItem)1 ImmutableList (com.google.common.collect.ImmutableList)1 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)1