Search in sources :

Example 16 with ItemResourceValue

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

the class StyleItemNameConverter method getVariants.

@NotNull
@Override
public Collection<String> getVariants(ConvertContext context) {
    List<String> result = Lists.newArrayList();
    if (context.getModule() != null && context.getTag() != null) {
        // Try to find the parents of the styles where this item is defined and add to the suggestion every non-framework attribute that has been used.
        // This is helpful in themes like AppCompat where there is not only a framework attribute defined but also a custom attribute. This
        // will show both in the completion list.
        AppResourceRepository appResourceRepository = AppResourceRepository.getAppResources(context.getModule(), true);
        XmlTag styleTag = context.getTag().getParentTag();
        String parent = getParentNameFromTag(styleTag);
        List<ResourceItem> parentDefinitions = parent != null && appResourceRepository != null ? appResourceRepository.getResourceItem(ResourceType.STYLE, parent) : null;
        if (parentDefinitions != null && !parentDefinitions.isEmpty()) {
            HashSet<String> attributeNames = Sets.newHashSet();
            LinkedList<ResourceItem> toExplore = Lists.newLinkedList(parentDefinitions);
            int i = 0;
            while (!toExplore.isEmpty() && i++ < ResourceResolver.MAX_RESOURCE_INDIRECTION) {
                ResourceItem parentItem = toExplore.pop();
                StyleResourceValue parentValue = (StyleResourceValue) parentItem.getResourceValue(false);
                if (parentValue == null || parentValue.isFramework()) {
                    // No parent or the parent is a framework style
                    continue;
                }
                for (ItemResourceValue value : parentValue.getValues()) {
                    if (!value.isFramework()) {
                        attributeNames.add(value.getName());
                    }
                }
                List<ResourceItem> parents = appResourceRepository.getResourceItem(ResourceType.STYLE, parentValue.getParentStyle());
                if (parents != null) {
                    toExplore.addAll(parents);
                }
            }
            result.addAll(attributeNames);
        }
    }
    ResourceManager manager = SystemResourceManager.getInstance(context);
    if (manager != null) {
        AttributeDefinitions attrDefs = manager.getAttributeDefinitions();
        if (attrDefs != null) {
            for (String name : attrDefs.getAttributeNames()) {
                result.add(SdkConstants.PREFIX_ANDROID + name);
            }
        }
    }
    return result;
}
Also used : AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with ItemResourceValue

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

the class ThemeAttributeResolver method resolveAll.

@NotNull
private List<EditedStyleItem> resolveAll() {
    ThemeEditorStyle theme = new ThemeEditorStyle(myManager, myStyle.getQualifiedName());
    for (FolderConfiguration folder : theme.getFolders()) {
        resolveFromInheritance(myStyle, folder, new RestrictedConfiguration(), new HashSet<String>());
    }
    List<EditedStyleItem> result = Lists.newArrayList();
    FolderConfiguration configuration = myStyle.getConfiguration().getFullConfig();
    for (String key : myItemValueMap.keySet()) {
        Collection<ConfiguredElement<ItemResourceValue>> itemValues = myItemValueMap.get(key);
        final ConfiguredElement<ItemResourceValue> selectedValue = (ConfiguredElement<ItemResourceValue>) configuration.findMatchingConfigurable(Lists.<Configurable>newArrayList(itemValues));
        if (selectedValue == null) {
            // TODO: there is NO value for this attribute in the current config,so instead we need to show "no value for current device"
            result.add(new EditedStyleItem(itemValues.iterator().next(), itemValues, myStyle));
        } else {
            itemValues.remove(selectedValue);
            assert !itemValues.contains(selectedValue);
            result.add(new EditedStyleItem(selectedValue, itemValues, myStyle));
        }
    }
    return result;
}
Also used : ConfiguredElement(com.android.tools.idea.editors.theme.datamodels.ConfiguredElement) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ThemeEditorStyle) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configurable(com.android.ide.common.resources.configuration.Configurable) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with ItemResourceValue

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

the class ThemeEditorUtils method resolveItemFromParents.

/**
   * Finds an ItemResourceValue for a given name in a theme inheritance tree
   */
@Nullable
public static /*if there is not an item with that name*/
ItemResourceValue resolveItemFromParents(@NotNull final ConfiguredThemeEditorStyle theme, @NotNull String name, boolean isFrameworkAttr) {
    ConfiguredThemeEditorStyle currentTheme = theme;
    for (int i = 0; (i < ResourceResolver.MAX_RESOURCE_INDIRECTION) && currentTheme != null; i++) {
        ItemResourceValue item = currentTheme.getItem(name, isFrameworkAttr);
        if (item != null) {
            return item;
        }
        currentTheme = currentTheme.getParent();
    }
    return null;
}
Also used : ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with ItemResourceValue

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

the class ColorUtils method getContrastColorsWithDescription.

/**
   * @param styleAttributeName the name of a style attribute we want to check for contrast issues
   * @return all the colors the attribute needs to be checked against, each associated with the appropriate description
   */
@NotNull
public static ImmutableMap<String, Color> getContrastColorsWithDescription(@NotNull ThemeEditorContext context, @NotNull String styleAttributeName) {
    ImmutableMap.Builder<String, Color> contrastColorsBuilder = ImmutableMap.builder();
    ResourceResolver styleResourceResolver = context.getResourceResolver();
    assert styleResourceResolver != null;
    Project project = context.getProject();
    Set<ItemResourceValue> contrastItems = getContrastItems(context, styleAttributeName);
    for (ItemResourceValue contrastItem : contrastItems) {
        ResourceHelper.StateList stateList = ResourceHelper.resolveStateList(styleResourceResolver, contrastItem, project);
        if (stateList != null) {
            List<ResourceHelper.StateListState> disabledStates = stateList.getDisabledStates();
            for (ResourceHelper.StateListState stateListState : stateList.getStates()) {
                Color stateListColor = ResourceHelper.resolveColor(styleResourceResolver, styleResourceResolver.findResValue(stateListState.getValue(), false), project);
                if (stateListColor != null) {
                    try {
                        stateListColor = ResourceHelper.makeColorWithAlpha(styleResourceResolver, stateListColor, stateListState.getAlpha());
                    } catch (NumberFormatException e) {
                        // If the alpha value is not valid, Android uses 1.0, so nothing more needs to be done, we can use stateListColor directly
                        LOG.warn(String.format(ResourceHelper.ALPHA_FLOATING_ERROR_FORMAT, stateList.getDirName(), stateList.getFileName()));
                    }
                    String disabledPrefix = disabledStates.contains(stateListState) ? DISABLED_PREFIX : "";
                    contrastColorsBuilder.put(disabledPrefix + ThemeEditorUtils.generateWordEnumeration(stateListState.getAttributesNames(false)) + " <b>" + contrastItem.getName() + "</b>", stateListColor);
                }
            }
        } else {
            Color resolvedColor = ResourceHelper.resolveColor(styleResourceResolver, contrastItem, project);
            if (resolvedColor != null) {
                contrastColorsBuilder.put("<b>" + contrastItem.getName() + "</b>", resolvedColor);
            }
        }
    }
    return contrastColorsBuilder.build();
}
Also used : Project(com.intellij.openapi.project.Project) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ResourceHelper(com.android.tools.idea.res.ResourceHelper) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with ItemResourceValue

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

the class ResolutionUtils method getThemeAttributes.

@NotNull
public static Collection<ItemResourceValue> getThemeAttributes(@NotNull ResourceResolver resolver, @NotNull final String themeUrl) {
    Map<String, ItemResourceValue> allItems = new HashMap<>();
    String themeName = getQualifiedNameFromResourceUrl(themeUrl);
    do {
        StyleResourceValue theme = resolver.getStyle(getNameFromQualifiedName(themeName), themeName.startsWith(PREFIX_ANDROID));
        if (theme == null) {
            break;
        }
        Collection<ItemResourceValue> themeItems = theme.getValues();
        for (ItemResourceValue item : themeItems) {
            String itemName = getQualifiedItemName(item);
            if (!allItems.containsKey(itemName)) {
                allItems.put(itemName, item);
            }
        }
        themeName = getParentQualifiedName(theme);
    } while (themeName != null);
    return allItems.values();
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) HashMap(java.util.HashMap) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)28 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 NotNull (org.jetbrains.annotations.NotNull)8 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)7 Configuration (com.android.tools.idea.configurations.Configuration)7 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)5 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)4 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)4 ResourceItem (com.android.ide.common.res2.ResourceItem)3 ResourceResolver (com.android.ide.common.resources.ResourceResolver)3 ResourceType (com.android.resources.ResourceType)3 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2 ThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ThemeEditorStyle)2 RestrictedConfiguration (com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration)2 Nullable (org.jetbrains.annotations.Nullable)2 Nullable (com.android.annotations.Nullable)1 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)1 ResourceFile (com.android.ide.common.resources.ResourceFile)1 Configurable (com.android.ide.common.resources.configuration.Configurable)1