Search in sources :

Example 1 with ATTR_LAYOUT_RESOURCE_PREFIX

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;
    }
}
Also used : AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) java.lang.reflect(java.lang.reflect) LinearLayout(android.widget.LinearLayout) ResourceHelper(com.android.tools.idea.res.ResourceHelper) java.util(java.util) SdkConstants(com.android.SdkConstants) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) StringUtil(com.intellij.openapi.util.text.StringUtil) Function(java.util.function.Function) ViewGroup(android.view.ViewGroup) TimeUnit(java.util.concurrent.TimeUnit) Nullable(org.jetbrains.annotations.Nullable) ID(com.android.resources.ResourceType.ID) AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) ATTR_LAYOUT_RESOURCE_PREFIX(com.android.SdkConstants.ATTR_LAYOUT_RESOURCE_PREFIX) Cache(com.google.common.cache.Cache) NotNull(org.jetbrains.annotations.NotNull) Splitter(com.google.common.base.Splitter) Arrays.stream(java.util.Arrays.stream) ResolutionUtils(com.android.tools.idea.editors.theme.ResolutionUtils) Configuration(com.android.tools.idea.configurations.Configuration) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition)

Aggregations

ViewGroup (android.view.ViewGroup)1 LinearLayout (android.widget.LinearLayout)1 SdkConstants (com.android.SdkConstants)1 ATTR_LAYOUT_RESOURCE_PREFIX (com.android.SdkConstants.ATTR_LAYOUT_RESOURCE_PREFIX)1 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 ID (com.android.resources.ResourceType.ID)1 Configuration (com.android.tools.idea.configurations.Configuration)1 ResolutionUtils (com.android.tools.idea.editors.theme.ResolutionUtils)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 ResourceHelper (com.android.tools.idea.res.ResourceHelper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Splitter (com.google.common.base.Splitter)1 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 java.lang.reflect (java.lang.reflect)1 java.util (java.util)1 Arrays.stream (java.util.Arrays.stream)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1