Search in sources :

Example 11 with CompatibilityRenderTarget

use of com.android.tools.idea.rendering.multi.CompatibilityRenderTarget 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 12 with CompatibilityRenderTarget

use of com.android.tools.idea.rendering.multi.CompatibilityRenderTarget in project android by JetBrains.

the class ThemeResolverTest method testConfigurationUpdate.

/** Check that, after a configuration update, the resolver updates the list of themes */
public void testConfigurationUpdate() {
    myFixture.copyFileToProject("themeEditor/attributeResolution/styles-v17.xml", "res/values-v17/styles.xml");
    myFixture.copyFileToProject("themeEditor/attributeResolution/styles-v19.xml", "res/values-v19/styles.xml");
    VirtualFile file = myFixture.copyFileToProject("themeEditor/attributeResolution/styles-v20.xml", "res/values-v20/styles.xml");
    ConfigurationManager configurationManager = myFacet.getConfigurationManager();
    Configuration configuration = configurationManager.getConfiguration(file);
    ThemeEditorContext context = new ThemeEditorContext(configuration);
    ThemeResolver resolver = context.getThemeResolver();
    assertNotNull(resolver.getTheme("V20OnlyTheme"));
    assertNotNull(resolver.getTheme("V19OnlyTheme"));
    assertNotNull(resolver.getTheme("V17OnlyTheme"));
    // Set API level 17 and check that only the V17 theme can be resolved
    //noinspection ConstantConditions
    configuration.setTarget(new CompatibilityRenderTarget(configurationManager.getHighestApiTarget(), 17, null));
    context = new ThemeEditorContext(configuration);
    resolver = context.getThemeResolver();
    assertNull(resolver.getTheme("V20OnlyTheme"));
    assertNull(resolver.getTheme("V19OnlyTheme"));
    assertNotNull(resolver.getTheme("V17OnlyTheme"));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Configuration(com.android.tools.idea.configurations.Configuration) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager)

Aggregations

CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)12 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Configuration (com.android.tools.idea.configurations.Configuration)5 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)4 NotNull (org.jetbrains.annotations.NotNull)4 ResourceResolver (com.android.ide.common.resources.ResourceResolver)3 Module (com.intellij.openapi.module.Module)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Nullable (org.jetbrains.annotations.Nullable)3 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)2 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)2 Device (com.android.sdklib.devices.Device)2 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)2 IOException (java.io.IOException)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 HardwareConfigHelper (com.android.ide.common.rendering.HardwareConfigHelper)1 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)1 ResourceRepository (com.android.ide.common.resources.ResourceRepository)1 ResourceValueMap (com.android.ide.common.resources.ResourceValueMap)1 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)1