Search in sources :

Example 1 with IntArrayFieldInitializer

use of com.google.devtools.build.android.resources.IntArrayFieldInitializer in project bazel by bazelbuild.

the class AndroidResourceClassWriter method getStyleableInitializers.

private List<FieldInitializer> getStyleableInitializers(Map<String, Integer> attrAssignments, Collection<String> styleableFields) throws AttrLookupException {
    ImmutableList.Builder<FieldInitializer> initList = ImmutableList.builder();
    for (String field : styleableFields) {
        Set<String> attrs = styleableAttrs.get(field).keySet();
        ImmutableMap.Builder<String, Integer> arrayInitValues = ImmutableMap.builder();
        for (String attr : attrs) {
            Integer attrId = attrAssignments.get(attr);
            if (attrId == null) {
                // It should be a framework resource, otherwise we don't know about the resource.
                if (!attr.startsWith(NORMALIZED_ANDROID_PREFIX)) {
                    throw new AttrLookupException("App attribute not found: " + attr);
                }
                String attrWithoutPrefix = attr.substring(NORMALIZED_ANDROID_PREFIX.length());
                attrId = androidIdProvider.getAttrId(attrWithoutPrefix);
            }
            arrayInitValues.put(attr, attrId);
        }
        // The styleable array should be sorted by ID value.
        // Make sure that if we have android: framework attributes, their IDs are listed first.
        ImmutableMap<String, Integer> arrayInitMap = arrayInitValues.orderEntriesByValue(Ordering.<Integer>natural()).build();
        initList.add(new IntArrayFieldInitializer(field, arrayInitMap.values()));
        int index = 0;
        for (String attr : arrayInitMap.keySet()) {
            initList.add(new IntFieldInitializer(field + "_" + attr, index));
            ++index;
        }
    }
    return initList.build();
}
Also used : IntArrayFieldInitializer(com.google.devtools.build.android.resources.IntArrayFieldInitializer) ImmutableList(com.google.common.collect.ImmutableList) FieldInitializer(com.google.devtools.build.android.resources.FieldInitializer) IntArrayFieldInitializer(com.google.devtools.build.android.resources.IntArrayFieldInitializer) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer) ImmutableMap(com.google.common.collect.ImmutableMap) AttrLookupException(com.google.devtools.build.android.AndroidFrameworkAttrIdProvider.AttrLookupException) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 AttrLookupException (com.google.devtools.build.android.AndroidFrameworkAttrIdProvider.AttrLookupException)1 FieldInitializer (com.google.devtools.build.android.resources.FieldInitializer)1 IntArrayFieldInitializer (com.google.devtools.build.android.resources.IntArrayFieldInitializer)1 IntFieldInitializer (com.google.devtools.build.android.resources.IntFieldInitializer)1