Search in sources :

Example 6 with ConfigurationManager

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());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Example 7 with ConfigurationManager

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);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) Computable(com.intellij.openapi.util.Computable) Pair(com.android.utils.Pair)

Example 8 with ConfigurationManager

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();
}
Also used : AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) IAndroidTarget(com.android.sdklib.IAndroidTarget) Configurable(com.android.ide.common.resources.configuration.Configurable) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceRepository(com.android.ide.common.resources.ResourceRepository) ResourceItem(com.android.ide.common.resources.ResourceItem) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with ConfigurationManager

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Configuration(com.android.tools.idea.configurations.Configuration) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IAndroidTarget(com.android.sdklib.IAndroidTarget) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) Module(com.intellij.openapi.module.Module) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with ConfigurationManager

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)20 Configuration (com.android.tools.idea.configurations.Configuration)14 VirtualFile (com.intellij.openapi.vfs.VirtualFile)14 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)9 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)8 IAndroidTarget (com.android.sdklib.IAndroidTarget)6 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)5 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)4 NotNull (org.jetbrains.annotations.NotNull)4 Module (com.intellij.openapi.module.Module)3 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)2 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 ResourceItem (com.android.ide.common.res2.ResourceItem)1 ResourceItem (com.android.ide.common.resources.ResourceItem)1 ResourceRepository (com.android.ide.common.resources.ResourceRepository)1 Configurable (com.android.ide.common.resources.configuration.Configurable)1 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)1 Device (com.android.sdklib.devices.Device)1