use of com.android.ide.common.resources.ResourceResolver 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.resources.ResourceResolver in project android by JetBrains.
the class ChooseResourceDialog method ensurePickersInitialized.
private void ensurePickersInitialized() {
boolean allowDrawables = allowDrawables();
boolean allowColors = allowColors();
if (allowColors || allowDrawables) {
if (myStateListPicker != null || myColorPicker != null) {
return;
}
Configuration configuration = getConfiguration();
ResourceResolver resolver = configuration.getResourceResolver();
assert resolver != null;
ResourceValue resValue = null;
if (myInitialValue != null) {
resValue = resolver.findResValue(myInitialValue, myInitialValueIsFramework);
}
final ResourceType stateListType;
final ResourceFolderType stateListFolderType;
if (allowDrawables) {
stateListType = ResourceType.DRAWABLE;
stateListFolderType = ResourceFolderType.DRAWABLE;
} else {
stateListType = ResourceType.COLOR;
stateListFolderType = ResourceFolderType.COLOR;
}
initializeStateListPicker(configuration, resolver, resValue, stateListType, stateListFolderType);
initializeColorPicker(myInitialValue, myResourceNameVisibility, resolver, resValue);
}
}
use of com.android.ide.common.resources.ResourceResolver in project android by JetBrains.
the class StateListPicker method updateIcon.
private void updateIcon(@NotNull StateComponent component) {
component.showAlphaError(false);
ResourceResolver resourceResolver = myConfiguration.getResourceResolver();
assert resourceResolver != null;
component.setValueIcon(getSwatchIcon(component.getResourceValue(), resourceResolver, myRenderTask));
String alphaValue = component.getAlphaValue();
if (!StringUtil.isEmpty(alphaValue)) {
try {
float alpha = Float.parseFloat(ResourceHelper.resolveStringValue(resourceResolver, alphaValue));
Font iconFont = JBUI.Fonts.smallFont().asBold();
component.getAlphaComponent().setSwatchIcon(new SwatchComponent.TextIcon(String.format("%.2f", alpha), iconFont));
} catch (NumberFormatException e) {
component.showAlphaError(true);
component.getAlphaComponent().setSwatchIcon(SwatchComponent.WARNING_ICON);
}
} else {
Font iconFont = JBUI.Fonts.smallFont().asBold();
component.getAlphaComponent().setSwatchIcon(new SwatchComponent.TextIcon("1.00", iconFont));
}
}
use of com.android.ide.common.resources.ResourceResolver in project android by JetBrains.
the class ResourceResolverCacheTest method test.
public void test() throws Exception {
VirtualFile file1 = myFixture.copyFileToProject("render/layout1.xml", "res/layout/layout1.xml");
VirtualFile file2 = myFixture.copyFileToProject("render/layout2.xml", "res/layout/layout2.xml");
VirtualFile file3 = myFixture.copyFileToProject("javadoc/strings/strings.xml", "res/values/strings.xml");
assertNotNull(file1);
assertNotNull(file2);
assertNotNull(file3);
AndroidFacet facet = AndroidFacet.getInstance(myModule);
assertNotNull(facet);
Project project = getProject();
PsiFile psiFile1 = PsiManager.getInstance(project).findFile(file1);
assertNotNull(psiFile1);
PsiFile psiFile2 = PsiManager.getInstance(project).findFile(file2);
assertNotNull(psiFile2);
final PsiFile psiFile3 = PsiManager.getInstance(project).findFile(file3);
assertNotNull(psiFile3);
ConfigurationManager configurationManager = facet.getConfigurationManager();
assertNotNull(configurationManager);
final Configuration configuration1 = configurationManager.getConfiguration(file1);
Configuration configuration2 = configurationManager.getConfiguration(file2);
assertNotNull(configuration1.getTheme());
assertEquals(configuration2.getTheme(), configuration1.getTheme());
ResourceResolver resolver1 = configuration1.getResourceResolver();
ResourceResolver resolver2 = configuration2.getResourceResolver();
assertSame(resolver1, resolver2);
assertSame(resolver1, configuration1.getResourceResolver());
configuration1.setTheme("Theme.Light");
final ResourceResolver resolver1b = configuration1.getResourceResolver();
assertNotSame(resolver1b, resolver1);
assertNotSame(resolver1b, resolver2);
assertSame(resolver1b, configuration1.getResourceResolver());
configuration2.setTheme("Theme.Light");
assertSame(resolver1b, configuration2.getResourceResolver());
// Test project resource changes, should invalidate
final LocalResourceRepository resources = myFacet.getModuleResources(true);
assertNotNull(resources);
final long generation = resources.getModificationCount();
assertEquals("Cancel", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue());
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
//noinspection ConstantConditions
XmlTagValue value = ((XmlFile) psiFile3).getRootTag().getSubTags()[1].getValue();
assertEquals("Cancel", value.getTrimmedText());
value.setText("\"FooBar\"");
}
});
assertTrue(resources.isScanPending(psiFile3));
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
assertTrue(generation < resources.getModificationCount());
assertNotSame(resolver1b, configuration1.getResourceResolver());
assertEquals("FooBar", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue());
}
});
ResourceResolverCache cache = configuration1.getConfigurationManager().getResolverCache();
assertSame(cache, configuration2.getConfigurationManager().getResolverCache());
ResourceRepository frameworkResources = cache.getFrameworkResources(configuration1.getFullConfig(), configuration1.getTarget());
assertTrue(frameworkResources instanceof FrameworkResourceLoader.IdeFrameworkResources);
assertTrue(((FrameworkResourceLoader.IdeFrameworkResources) frameworkResources).getSkippedLocales());
}
use of com.android.ide.common.resources.ResourceResolver in project android by JetBrains.
the class NlPreviewImagePanel method getBackgroundColor.
@NotNull
private Color getBackgroundColor() {
Configuration configuration = myDesignSurface != null ? myDesignSurface.getConfiguration() : null;
ResourceResolver resolver = configuration != null ? configuration.getResourceResolver() : null;
if (resolver == null) {
return UIUtil.getPanelBackground();
}
ResourceValue windowBackground = resolver.findItemInTheme("colorBackground", true);
Color background = ResourceHelper.resolveColor(resolver, windowBackground, myDesignSurface.getProject());
return background != null ? background : UIUtil.getPanelBackground();
}
Aggregations