use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.
the class ThemeResolverTest method testLocalThemes.
public void testLocalThemes() throws IOException {
VirtualFile myLayout = myFixture.copyFileToProject("themeEditor/layout.xml", "res/layout/layout.xml");
VirtualFile myStyleFile = myFixture.copyFileToProject("themeEditor/styles.xml", "res/values/styles.xml");
ConfigurationManager configurationManager = myFacet.getConfigurationManager();
Configuration configuration = configurationManager.getConfiguration(myLayout);
ThemeResolver themeResolver = new ThemeResolver(configuration);
// We don't have any libraries so this will only include the project theme
assertEquals(1, themeResolver.getLocalThemes().size());
// No library themes
assertEquals(0, themeResolver.getExternalLibraryThemes().size());
assertNull("The theme is an app theme and shouldn't be returned for the android namespace", themeResolver.getTheme("android:Theme.MyTheme"));
ConfiguredThemeEditorStyle theme = themeResolver.getTheme("Theme.MyTheme");
assertEquals("Theme.MyTheme", theme.getName());
assertEquals("Theme", theme.getParent().getName());
assertEquals(1, theme.getConfiguredValues().size());
ConfiguredElement<ItemResourceValue> value = Iterables.get(theme.getConfiguredValues(), 0);
assertEquals("windowBackground", value.getElement().getName());
assertEquals("@drawable/pic", value.getElement().getValue());
// Modify a value.
theme.setValue("android:windowBackground", "@drawable/other");
FileDocumentManager.getInstance().saveAllDocuments();
assertFalse(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/pic"));
assertTrue(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/other"));
// Add a value.
theme.setValue("android:windowBackground2", "@drawable/second_background");
FileDocumentManager.getInstance().saveAllDocuments();
assertTrue(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/other"));
assertTrue(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/second_background"));
}
use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.
the class ResolutionUtils method getAttrType.
@Nullable
public static /*if we can't work out the type, e.g a 'reference' with a '@null' value or enum*/
ResourceType getAttrType(@NotNull ItemResourceValue item, @NotNull Configuration configuration) {
ResourceResolver resolver = configuration.getResourceResolver();
assert resolver != null;
ResourceValue resolvedValue = resolver.resolveResValue(item);
ResourceType attrType = resolvedValue.getResourceType();
if (attrType != null) {
return attrType;
} else {
AttributeDefinition def = getAttributeDefinition(configuration, item);
if (def != null) {
for (AttributeFormat attrFormat : def.getFormats()) {
attrType = AndroidDomUtil.getResourceType(attrFormat);
if (attrType != null) {
return attrType;
}
}
}
}
// sometimes we won't find the type of the attr, this means it's either a reference that points to @null, or a enum
return null;
}
use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.
the class ColorUtils method getContrastItems.
/**
* @param styleAttributeName the name of a style attribute we want to check for contrast issues
* Returns the set of {@link ItemResourceValue} that have to be checked in the current theme for contrast against a particular attribute.
*/
@NotNull
public static ImmutableSet<ItemResourceValue> getContrastItems(@NotNull ThemeEditorContext context, @NotNull String styleAttributeName) {
Set<String> contrastColorSet = CONTRAST_MAP.get(styleAttributeName);
if (contrastColorSet == null) {
return ImmutableSet.of();
}
ImmutableSet.Builder<ItemResourceValue> contrastItemsBuilder = ImmutableSet.builder();
ConfiguredThemeEditorStyle currentTheme = context.getCurrentTheme();
assert currentTheme != null;
for (String contrastColor : contrastColorSet) {
ItemResourceValue contrastItem = ThemeEditorUtils.resolveItemFromParents(currentTheme, contrastColor, false);
if (contrastItem == null) {
contrastItem = ThemeEditorUtils.resolveItemFromParents(currentTheme, contrastColor, true);
}
if (contrastItem != null) {
contrastItemsBuilder.add(contrastItem);
}
}
return contrastItemsBuilder.build();
}
use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.
the class GraphicalResourceRendererEditor method updateComponentInternal.
/**
* Sets the UI state of the passed {@link ResourceComponent} based on the given {@link EditedStyleItem}
*/
private static void updateComponentInternal(@NotNull ResourceComponent component, @NotNull final EditedStyleItem item) {
final String currentVariantColor = ColorUtil.toHex(ThemeEditorConstants.CURRENT_VARIANT_COLOR);
final String notSelectedVariantColor = ColorUtil.toHex(ThemeEditorConstants.NOT_SELECTED_VARIANT_COLOR);
FolderConfiguration restrictedConfig = RestrictedConfiguration.restrict(item.getSelectedItemResourceValue(), item.getAllConfiguredItems());
String description = String.format(ThemeEditorConstants.CURRENT_VARIANT_TEMPLATE, currentVariantColor, item.getSelectedValueConfiguration().toShortDisplayString());
VariantsComboItem selectedItem = new VariantsComboItem(description, restrictedConfig != null ? restrictedConfig : item.getSelectedValueConfiguration(), item.getSelectedValueConfiguration());
// All the not selected elements are sorted alphabetically
TreeSet<VariantsComboItem> notSelectedItems = Sets.newTreeSet(VARIANTS_COMBO_ITEM_COMPARATOR);
for (ConfiguredElement<ItemResourceValue> configuredItem : item.getNonSelectedItemResourceValues()) {
restrictedConfig = RestrictedConfiguration.restrict(configuredItem, item.getAllConfiguredItems());
if (restrictedConfig == null) {
// This type is not visible
LOG.warn(String.format("For item '%1$s': Folder configuration '%2$s' can never be selected. There are no qualifiers combination that would allow selecting it.", item.getName(), configuredItem.getConfiguration()));
continue;
}
description = String.format(ThemeEditorConstants.NOT_SELECTED_VARIANT_TEMPLATE, notSelectedVariantColor, configuredItem.getConfiguration().toShortDisplayString(), " - " + configuredItem.getElement().getValue());
notSelectedItems.add(new VariantsComboItem(description, restrictedConfig, configuredItem.getConfiguration()));
}
ImmutableList<VariantsComboItem> variantList = ImmutableList.<VariantsComboItem>builder().add(selectedItem).addAll(notSelectedItems).build();
component.setVariantsModel(new CollectionComboBoxModel<VariantsComboItem>(variantList, selectedItem));
}
use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.
the class StyleListPaletteCellRenderer method customizeCellRenderer.
@Override
protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) {
super.customizeCellRenderer(list, value, index, selected, hasFocus);
if (!(value instanceof String) || ThemesListModel.isSpecialOption((String) value)) {
myColorPaletteComponent.reset();
return;
}
ThemeResolver themeResolver = myContext.getThemeResolver();
final ConfiguredThemeEditorStyle theme = themeResolver.getTheme((String) value);
if (theme == null) {
myColorPaletteComponent.reset();
setIcon(null);
return;
}
final boolean isFrameworkAttr = !ThemeEditorUtils.isAppCompatTheme(theme);
ItemResourceValue primaryResourceValue = ThemeEditorUtils.resolveItemFromParents(theme, PRIMARY_MATERIAL, isFrameworkAttr);
ItemResourceValue primaryDarkResourceValue = ThemeEditorUtils.resolveItemFromParents(theme, PRIMARY_DARK_MATERIAL, isFrameworkAttr);
ItemResourceValue accentResourceValue = ThemeEditorUtils.resolveItemFromParents(theme, ACCENT_MATERIAL, isFrameworkAttr);
//Check needed in case the xml files are inconsistent and have an item, but not a value
if (primaryResourceValue != null && primaryDarkResourceValue != null && accentResourceValue != null) {
Configuration configuration = theme.getConfiguration();
ResourceResolver resourceResolver = configuration.getConfigurationManager().getResolverCache().getResourceResolver(configuration.getRealTarget(), theme.getStyleResourceUrl(), configuration.getFullConfig());
Color primaryColor = ResourceHelper.resolveColor(resourceResolver, primaryResourceValue, myContext.getProject());
Color primaryDarkColor = ResourceHelper.resolveColor(resourceResolver, primaryDarkResourceValue, myContext.getProject());
Color accentColor = ResourceHelper.resolveColor(resourceResolver, accentResourceValue, myContext.getProject());
if (primaryColor != null && primaryDarkColor != null && accentColor != null) {
myColorPaletteComponent.setValues(primaryColor, primaryDarkColor, accentColor);
}
setIcon(myColorPaletteComponent);
} else {
myColorPaletteComponent.reset();
setIcon(null);
}
if (selected) {
myThemeChangedListener.themeChanged(theme.getQualifiedName());
}
}
Aggregations