use of com.android.SdkConstants.ATTR_LAYOUT_RESOURCE_PREFIX in project android by JetBrains.
the class LayoutParamsManager method setAttribute.
/**
* Sets the given attribute in the passed layoutParams instance. This method tries to infer the type to use from the attribute name and
* the field type.
* <p>
* @return whether the method was able to set the attribute or not.
*/
public static boolean setAttribute(@NotNull Object layoutParams, @NotNull String attributeName, @Nullable String value, @NotNull NlModel model) {
// Try to find the attribute definition and retrieve the defined formats
AttributeDefinition attributeDefinition = ResolutionUtils.getAttributeDefinition(model.getModule(), model.getConfiguration(), ATTR_LAYOUT_RESOURCE_PREFIX + attributeName);
EnumSet<AttributeFormat> inferredTypes = attributeDefinition != null ? EnumSet.copyOf(attributeDefinition.getFormats()) : EnumSet.noneOf(AttributeFormat.class);
if (value != null && (value.startsWith(SdkConstants.PREFIX_RESOURCE_REF) || value.startsWith(SdkConstants.PREFIX_THEME_REF)) && model.getConfiguration().getResourceResolver() != null) {
// This is a reference so we resolve the actual value and we try to infer the type from the given reference type
ResourceValue resourceValue = model.getConfiguration().getResourceResolver().findResValue(value, false);
if (resourceValue != null) {
value = resourceValue.getValue();
//noinspection EnumSwitchStatementWhichMissesCases
switch(resourceValue.getResourceType()) {
case INTEGER:
case ID:
case DIMEN:
inferredTypes.add(AttributeFormat.Integer);
break;
case FRACTION:
inferredTypes.add(AttributeFormat.Float);
break;
}
if (resourceValue.getResourceType() == ID) {
Integer resolvedId = AppResourceRepository.getAppResources(model.getFacet(), true).getResourceId(ID, resourceValue.getName());
// TODO: Remove this wrapping/unwrapping
value = resolvedId.toString();
}
}
}
// Now we have a value and an attributeName. We now try to map the given attributeName to the field in the LayoutParams that
// stores its value.
MappedField mappedField = mapField(layoutParams, attributeName);
if (inferredTypes.isEmpty()) {
// If we don't know the type yet, use the field type.
inferredTypes.addAll(mappedField.type);
}
// 3. Lastly, if we do not have a better option, we try to infer the value from the default value in the class
if (inferredTypes.isEmpty()) {
inferredTypes.addAll(inferTypeFromValue(value));
}
if (inferredTypes.isEmpty()) {
inferredTypes.addAll(inferTypeFromField(layoutParams, mappedField));
}
Object defaultValue = null;
try {
defaultValue = getDefaultValue(layoutParams, mappedField);
} catch (NoSuchElementException ignore) {
}
if (defaultValue != null && inferredTypes.isEmpty()) {
inferredTypes.addAll(attributeFormatFromType(defaultValue.getClass()));
}
if (value == null) {
return setField(layoutParams, mappedField, defaultValue);
} else {
boolean fieldSet = false;
for (AttributeFormat type : inferredTypes) {
switch(type) {
case Dimension:
fieldSet = setField(layoutParams, mappedField, getDimensionValue(value, model.getConfiguration()));
break;
case Integer:
try {
fieldSet = setField(layoutParams, mappedField, Integer.parseInt(value));
} catch (NumberFormatException e) {
fieldSet = false;
}
break;
case String:
fieldSet = setField(layoutParams, mappedField, value);
break;
case Boolean:
fieldSet = setField(layoutParams, mappedField, Boolean.parseBoolean(value));
break;
case Float:
try {
fieldSet = setField(layoutParams, mappedField, Float.parseFloat(value));
} catch (NumberFormatException e) {
fieldSet = false;
}
break;
case Enum:
{
Integer intValue = attributeDefinition != null ? attributeDefinition.getValueMapping(value) : null;
if (intValue != null) {
fieldSet = setField(layoutParams, mappedField, intValue);
}
}
break;
case Flag:
{
OptionalInt flagValue = Splitter.on('|').splitToList(value).stream().map(StringUtil::trim).mapToInt(flagName -> {
Integer intValue = attributeDefinition != null ? attributeDefinition.getValueMapping(flagName) : null;
return intValue != null ? intValue : 0;
}).reduce((a, b) -> a + b);
if (flagValue.isPresent()) {
fieldSet = setField(layoutParams, mappedField, flagValue.getAsInt());
}
}
break;
default:
}
if (fieldSet) {
return true;
}
}
return false;
}
}
Aggregations