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();
}
Aggregations