Search in sources :

Example 1 with RestrictedConfiguration

use of com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration in project android by JetBrains.

the class ThemeAttributeResolver method resolveFromInheritance.

private void resolveFromInheritance(@NotNull ThemeEditorStyle themeEditorStyle, @NotNull FolderConfiguration configuration, @NotNull RestrictedConfiguration restricted, @NotNull Set<String> seenAttributes) {
    RestrictedConfiguration styleRestricted = getRestrictedConfiguration(themeEditorStyle, configuration);
    if (styleRestricted == null) {
        LOG.warn(configuration + " is unreachable");
        return;
    }
    styleRestricted = restricted.intersect(styleRestricted);
    if (styleRestricted == null) {
        return;
    }
    Set<String> newSeenAttributes = new HashSet<String>(seenAttributes);
    for (ItemResourceValue item : themeEditorStyle.getValues(configuration)) {
        String itemName = ResolutionUtils.getQualifiedItemName(item);
        if (!newSeenAttributes.contains(itemName)) {
            myItemValueMap.putValue(itemName, ConfiguredElement.create(styleRestricted.getAny(), item));
            newSeenAttributes.add(itemName);
        }
    }
    String parentName = themeEditorStyle.getParentName(configuration);
    if (parentName == null) {
        // We have reached the top of the theme hierarchy (i.e "android:Theme")
        return;
    }
    ThemeEditorStyle parent = new ThemeEditorStyle(myManager, parentName);
    for (FolderConfiguration folder : parent.getFolders()) {
        resolveFromInheritance(parent, folder, styleRestricted, newSeenAttributes);
    }
}
Also used : ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ThemeEditorStyle) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration)

Example 2 with RestrictedConfiguration

use of com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration in project android by JetBrains.

the class ThemeAttributeResolver method resolveAll.

@NotNull
private List<EditedStyleItem> resolveAll() {
    ThemeEditorStyle theme = new ThemeEditorStyle(myManager, myStyle.getQualifiedName());
    for (FolderConfiguration folder : theme.getFolders()) {
        resolveFromInheritance(myStyle, folder, new RestrictedConfiguration(), new HashSet<String>());
    }
    List<EditedStyleItem> result = Lists.newArrayList();
    FolderConfiguration configuration = myStyle.getConfiguration().getFullConfig();
    for (String key : myItemValueMap.keySet()) {
        Collection<ConfiguredElement<ItemResourceValue>> itemValues = myItemValueMap.get(key);
        final ConfiguredElement<ItemResourceValue> selectedValue = (ConfiguredElement<ItemResourceValue>) configuration.findMatchingConfigurable(Lists.<Configurable>newArrayList(itemValues));
        if (selectedValue == null) {
            // TODO: there is NO value for this attribute in the current config,so instead we need to show "no value for current device"
            result.add(new EditedStyleItem(itemValues.iterator().next(), itemValues, myStyle));
        } else {
            itemValues.remove(selectedValue);
            assert !itemValues.contains(selectedValue);
            result.add(new EditedStyleItem(selectedValue, itemValues, myStyle));
        }
    }
    return result;
}
Also used : ConfiguredElement(com.android.tools.idea.editors.theme.datamodels.ConfiguredElement) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ThemeEditorStyle) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configurable(com.android.ide.common.resources.configuration.Configurable) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with RestrictedConfiguration

use of com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration 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)

Aggregations

FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)3 RestrictedConfiguration (com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration)3 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)2 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)2 ThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ThemeEditorStyle)2 Configurable (com.android.ide.common.resources.configuration.Configurable)1 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)1 IAndroidTarget (com.android.sdklib.IAndroidTarget)1 Configuration (com.android.tools.idea.configurations.Configuration)1 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)1 ConfiguredElement (com.android.tools.idea.editors.theme.datamodels.ConfiguredElement)1 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)1 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)1 Module (com.intellij.openapi.module.Module)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1