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