use of com.android.tools.idea.configurations.ConfigurationManager in project android by JetBrains.
the class ConfiguredThemeEditorStyleTest method testGetConfiguredValues.
/**
* Tests {@link ConfiguredThemeEditorStyle#getConfiguredValues()}
* Tests values coming from different modules.
* Dependency used in the test: mainModule -> moduleA, mainModule -> moduleB
*/
public void testGetConfiguredValues() {
myFixture.copyFileToProject("themeEditor/themeEditorStyle/styles_4.xml", "additionalModules/moduleB/res/values-v19/styles.xml");
VirtualFile virtualFile = myFixture.copyFileToProject("themeEditor/themeEditorStyle/styles_3.xml", "additionalModules/moduleA/res/values/styles.xml");
ConfigurationManager configurationManager = myFacet.getConfigurationManager();
Configuration configuration = configurationManager.getConfiguration(virtualFile);
ThemeResolver resolver = new ThemeResolver(configuration);
ConfiguredThemeEditorStyle theme = resolver.getTheme("AppTheme");
assertNotNull(theme);
assertEquals(3, theme.getConfiguredValues().size());
}
use of com.android.tools.idea.configurations.ConfigurationManager in project android by JetBrains.
the class OverrideResourceAction method forkResourceFile.
private static void forkResourceFile(@NotNull Project project, @NotNull final ResourceFolderType folderType, @NotNull final VirtualFile file, @Nullable final XmlFile xmlFile, @Nullable String myNewFolder, @Nullable Configuration configuration, boolean open) {
final FolderConfiguration folderConfig;
if (myNewFolder == null) {
// Open a file chooser to select the configuration to be created
VirtualFile parentFolder = file.getParent();
assert parentFolder != null;
VirtualFile res = parentFolder.getParent();
folderConfig = selectFolderConfig(project, res, folderType);
} else {
folderConfig = FolderConfiguration.getConfigForFolder(myNewFolder);
}
if (folderConfig == null) {
return;
}
final Computable<Pair<String, VirtualFile>> computable = new Computable<Pair<String, VirtualFile>>() {
@Override
public Pair<String, VirtualFile> compute() {
String folderName = folderConfig.getFolderName(folderType);
try {
VirtualFile parentFolder = file.getParent();
assert parentFolder != null;
VirtualFile res = parentFolder.getParent();
VirtualFile newParentFolder = res.findChild(folderName);
if (newParentFolder == null) {
newParentFolder = res.createChildDirectory(this, folderName);
if (newParentFolder == null) {
String message = String.format("Could not create folder %1$s in %2$s", folderName, res.getPath());
return Pair.of(message, null);
}
}
final VirtualFile existing = newParentFolder.findChild(file.getName());
if (existing != null && existing.exists()) {
String message = String.format("File 'res/%1$s/%2$s' already exists!", folderName, file.getName());
return Pair.of(message, null);
}
// Attempt to get the document from the PSI file rather than the file on disk: get edited contents too
String text;
if (xmlFile != null) {
text = xmlFile.getText();
} else {
text = StreamUtil.readText(file.getInputStream(), "UTF-8");
}
VirtualFile newFile = newParentFolder.createChildData(this, file.getName());
VfsUtil.saveText(newFile, text);
return Pair.of(null, newFile);
} catch (IOException e2) {
String message = String.format("Failed to create File 'res/%1$s/%2$s' : %3$s", folderName, file.getName(), e2.getMessage());
return Pair.of(message, null);
}
}
};
WriteCommandAction<Pair<String, VirtualFile>> action = new WriteCommandAction<Pair<String, VirtualFile>>(project, "Add Resource") {
@Override
protected void run(@NotNull Result<Pair<String, VirtualFile>> result) throws Throwable {
result.setResult(computable.compute());
}
};
Pair<String, VirtualFile> result = action.execute().getResultObject();
String error = result.getFirst();
VirtualFile newFile = result.getSecond();
if (error != null) {
Messages.showErrorDialog(project, error, "Create Resource");
} else {
// First create a compatible configuration based on the current configuration
if (configuration != null) {
ConfigurationManager configurationManager = configuration.getConfigurationManager();
configurationManager.createSimilar(newFile, file);
}
if (open) {
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, newFile, -1);
FileEditorManager.getInstance(project).openEditor(descriptor, true);
}
}
}
use of com.android.tools.idea.configurations.ConfigurationManager in project android by JetBrains.
the class ResolutionUtils method getFolderConfiguration.
/**
* Gets the {@link FolderConfiguration} of a ResourceValue
* e.g. if we resolve a drawable using a mdpi configuration, yet that drawable only exists inside xhdpi, this method will return xhdpi
* @param configuration the FolderConfiguration that was used for resolving the ResourceValue
* @return the FolderConfiguration of the ResourceValue
*/
@NotNull
public static FolderConfiguration getFolderConfiguration(@NotNull AndroidFacet facet, @NotNull ResourceValue resolvedValue, @NotNull FolderConfiguration configuration) {
List<? extends Configurable> configurables;
if (resolvedValue.isFramework()) {
ConfigurationManager configurationManager = facet.getConfigurationManager();
// same as getHighestApiTarget();
IAndroidTarget target = configurationManager.getDefaultTarget();
assert target != null;
ResourceRepository resourceRepository = configurationManager.getResolverCache().getFrameworkResources(configuration, target);
assert resourceRepository != null;
ResourceItem resourceItem = resourceRepository.getResourceItem(resolvedValue.getResourceType(), resolvedValue.getName());
configurables = resourceItem.getSourceFileList();
} else {
AppResourceRepository appResourceRepository = facet.getAppResources(true);
configurables = appResourceRepository.getResourceItem(resolvedValue.getResourceType(), resolvedValue.getName());
}
Configurable configurable = configuration.findMatchingConfigurable(configurables);
assert configurable != null;
return configurable.getConfiguration();
}
use of com.android.tools.idea.configurations.ConfigurationManager 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.ConfigurationManager in project android by JetBrains.
the class ThemeEditorUtils method getConfigurationForModule.
@NotNull
public static Configuration getConfigurationForModule(@NotNull Module module) {
Project project = module.getProject();
final AndroidFacet facet = AndroidFacet.getInstance(module);
assert facet != null : "moduleComboModel must contain only Android modules";
ConfigurationManager configurationManager = facet.getConfigurationManager();
// Using the project virtual file to set up configuration for the theme editor
// That fact is hard-coded in computeBestDevice() method in Configuration.java
// BEWARE if attempting to modify to use a different virtual file
final VirtualFile projectFile = project.getProjectFile();
assert projectFile != null;
return configurationManager.getConfiguration(projectFile);
}
Aggregations