use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.
the class AndroidThemePreviewToolWindowManager method getBestConfiguration.
@Nullable
private static /*if there is no available configuration that would select the passed file*/
Configuration getBestConfiguration(@Nullable PsiFile psiFile) {
Module module = psiFile != null ? ModuleUtilCore.findModuleForPsiElement(psiFile) : null;
if (module == null) {
return null;
}
AndroidFacet facet = AndroidFacet.getInstance(module);
if (facet == null) {
return null;
}
VirtualFile virtualFile = psiFile.getVirtualFile();
ConfigurationManager manager = facet.getConfigurationManager();
List<VirtualFile> variations = ResourceHelper.getResourceVariations(virtualFile, false);
if (variations.isEmpty()) {
return manager.getConfiguration(virtualFile);
}
// There is more than one resource folder available so make sure we select a configuration that only matches the current file.
Collection<FolderConfiguration> incompatible = Collections2.transform(variations, new Function<VirtualFile, FolderConfiguration>() {
@Nullable
@Override
public FolderConfiguration apply(VirtualFile input) {
assert input != null;
return ResourceHelper.getFolderConfiguration(input);
}
});
FolderConfiguration selectedFileFolderConfiguration = ResourceHelper.getFolderConfiguration(psiFile);
if (selectedFileFolderConfiguration == null) {
// This folder probably has invalid qualifiers or they are in the wrong order
return null;
}
RestrictedConfiguration restrictedConfiguration = RestrictedConfiguration.restrict(selectedFileFolderConfiguration, incompatible);
if (restrictedConfiguration == null) {
// Unable to create a configuration that only matches this file
return null;
}
FolderConfiguration restricted = restrictedConfiguration.getAny();
Configuration newConfiguration = Configuration.create(manager, virtualFile, null, restricted);
VersionQualifier newVersionQualifier = restricted.getVersionQualifier();
if (newVersionQualifier != null) {
IAndroidTarget realTarget = manager.getHighestApiTarget() != null ? manager.getHighestApiTarget() : manager.getTarget();
assert realTarget != null;
newConfiguration.setTarget(new CompatibilityRenderTarget(realTarget, newVersionQualifier.getVersion(), null));
}
return newConfiguration;
}
use of com.android.tools.idea.configurations.Configuration 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.tools.idea.configurations.Configuration in project android by JetBrains.
the class ResourceDrawablePanel method updateResolutionChain.
private void updateResolutionChain(@NotNull ResourceChooserItem item) {
// Resource resolver
myResolvedPanel.removeAll();
ResourceValue resourceValue = item.getResourceValue();
Configuration configuration = myDialog.getConfiguration();
ResourceRepository frameworkResources = configuration.getFrameworkResources();
if (frameworkResources != null) {
AppResourceRepository appResources = AppResourceRepository.getAppResources(myDialog.geFacet(), true);
ResourceItemResolver resolver = new ResourceItemResolver(configuration.getFullConfig(), frameworkResources, appResources, null);
List<ResourceValue> lookupChain = Lists.newArrayList();
lookupChain.add(resourceValue);
resolver.setLookupChainList(lookupChain);
resolver.resolveResValue(resourceValue);
String prev = null;
int indent = 0;
if (lookupChain.size() >= 2) {
for (ResourceValue element : lookupChain) {
if (element == null) {
continue;
}
String value = element.getValue();
if (value == null) {
continue;
}
String text = value;
if (text.equals(prev)) {
continue;
}
// Strip paths
if (!(text.startsWith(PREFIX_THEME_REF) || text.startsWith(PREFIX_RESOURCE_REF))) {
if (indent == 0) {
break;
}
int end = Math.max(text.lastIndexOf('/'), text.lastIndexOf('\\'));
if (end != -1) {
text = text.substring(end + 1);
}
}
if (indent > 0) {
// 21D2: Rightwards arrow
text = "⇒ " + text;
}
JBLabel label = new JBLabel(text);
label.setBorder(IdeBorderFactory.createEmptyBorder(0, JBUI.scale(indent * 12), 0, 0));
myResolvedPanel.add(label);
indent++;
prev = value;
}
}
}
}
use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.
the class ThemeAttributeResolverTest method testResolveAllEnum.
/**
* Tests {@link ThemeAttributeResolver#resolveAll(ConfiguredThemeEditorStyle, ThemeResolver)}
*/
public void testResolveAllEnum() {
VirtualFile myFile = myFixture.copyFileToProject("themeEditor/styles.xml", "res/values/styles.xml");
VirtualFile resourceDir = myFile.getParent().getParent();
Configuration configuration = myFacet.getConfigurationManager().getConfiguration(myFile);
createNewStyle(resourceDir, "ThemeA", "android:Theme", "red", Lists.newArrayList("values-port", "values-square", "values-land"));
createNewStyle(resourceDir, "ThemeB", "ThemeA", null, Lists.newArrayList("values", "values-port"));
// ResourceFolderRepository needs to rescan the files to pick up the changes.
UIUtil.dispatchAllInvocationEvents();
ThemeResolver themeResolver = new ThemeResolver(configuration);
ConfiguredThemeEditorStyle style = themeResolver.getTheme("ThemeB");
assertNotNull(style);
Set<String> answer = Sets.newHashSet("-port:red", "-land:red", "-square:red");
List<EditedStyleItem> items = ThemeAttributeResolver.resolveAll(style, configuration.getConfigurationManager());
boolean foundColorPrimary = false;
for (EditedStyleItem item : items) {
if (item.getName().equals("colorPrimary") && item.getAttrGroup().equals("Other non-theme attributes.")) {
foundColorPrimary = true;
assertEquals(answer.size(), item.getAllConfiguredItems().size());
for (ConfiguredElement<ItemResourceValue> value : item.getAllConfiguredItems()) {
assertTrue(answer.contains(value.getConfiguration().getUniqueKey() + ":" + value.getElement().getValue()));
}
}
}
assertTrue(foundColorPrimary);
}
use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.
the class ConfiguredThemeEditorStyleTest method testGetParentNamesParenInName.
/**
* Test {@link ConfiguredThemeEditorStyle#getParentNames()} for a style:
* <style name="ATheme.Red"></style>, i.e parent defined in the name
*/
public void testGetParentNamesParenInName() {
VirtualFile virtualFile = myFixture.copyFileToProject("themeEditor/themeEditorStyle/styles.xml", "res/values/styles.xml");
myFixture.copyFileToProject("themeEditor/themeEditorStyle/styles_1.xml", "res/values-v19/styles.xml");
ConfigurationManager configurationManager = myFacet.getConfigurationManager();
Configuration configuration = configurationManager.getConfiguration(virtualFile);
ThemeResolver resolver = new ThemeResolver(configuration);
ConfiguredThemeEditorStyle theme = resolver.getTheme("ATheme.Red");
assertNotNull(theme);
HashSet<String> parents = Sets.newHashSet();
for (ConfiguredElement<String> parent : theme.getParentNames()) {
parents.add(parent.getElement());
}
assertEquals(2, parents.size());
assertContainsElements(parents, "ATheme", "BTheme");
}
Aggregations