Search in sources :

Example 16 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ConfigurationMenuAction method createPopupActionGroup.

@Override
@NotNull
protected DefaultActionGroup createPopupActionGroup() {
    DefaultActionGroup group = new DefaultActionGroup("Configuration", true);
    Configuration configuration = mySurface.getConfiguration();
    if (configuration == null) {
        return group;
    }
    VirtualFile virtualFile = configuration.getFile();
    if (virtualFile != null) {
        Module module = configuration.getModule();
        if (module == null) {
            return group;
        }
        Project project = module.getProject();
        List<VirtualFile> variations = ResourceHelper.getResourceVariations(virtualFile, true);
        if (variations.size() > 1) {
            for (VirtualFile file : variations) {
                String title = String.format("Switch to %1$s", file.getParent().getName());
                group.add(new SwitchToVariationAction(title, project, file, virtualFile.equals(file)));
            }
            group.addSeparator();
        }
        ResourceFolderType folderType = ResourceHelper.getFolderType(configuration.getFile());
        if (folderType == ResourceFolderType.LAYOUT) {
            boolean haveLandscape = false;
            boolean haveLarge = false;
            for (VirtualFile file : variations) {
                String name = file.getParent().getName();
                if (name.startsWith(FD_RES_LAYOUT)) {
                    FolderConfiguration config = FolderConfiguration.getConfigForFolder(name);
                    if (config != null) {
                        ScreenOrientationQualifier orientation = config.getScreenOrientationQualifier();
                        if (orientation != null && orientation.getValue() == ScreenOrientation.LANDSCAPE) {
                            haveLandscape = true;
                            if (haveLarge) {
                                break;
                            }
                        }
                        ScreenSizeQualifier size = config.getScreenSizeQualifier();
                        if (size != null && size.getValue() == ScreenSize.XLARGE) {
                            haveLarge = true;
                            if (haveLandscape) {
                                break;
                            }
                        }
                    }
                }
            }
            // Do statistics on what is needed!
            if (!haveLandscape) {
                group.add(new CreateVariationAction(mySurface, "Create Landscape Variation", "layout-land"));
            }
            if (!haveLarge) {
                group.add(new CreateVariationAction(mySurface, "Create layout-xlarge Variation", "layout-xlarge"));
            //group.add(new CreateVariationAction(mySurface, "Create layout-sw600dp Variation...", "layout-sw600dp"));
            }
            group.add(new CreateVariationAction(mySurface, "Create Other...", null));
        } else {
            group.add(new CreateVariationAction(mySurface, "Create Alternative...", null));
        }
    /* TODO: Restore multi-configuration editing
      if (mySurface.supportsPreviews()) {
        addMultiConfigActions(group);
      }
      */
    }
    return group;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ScreenSizeQualifier(com.android.ide.common.resources.configuration.ScreenSizeQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup) Project(com.intellij.openapi.project.Project) ScreenOrientationQualifier(com.android.ide.common.resources.configuration.ScreenOrientationQualifier) ResourceFolderType(com.android.resources.ResourceFolderType) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ConfiguredThemeEditorStyle method getConfiguredValues.

/**
   * Returns all the style attributes and its values. For each attribute, multiple {@link ConfiguredElement} can be returned
   * representing the multiple values in different configurations for each item.
   * TODO: needs to be deleted, as we don't use this method except tests
   */
@NotNull
public ImmutableCollection<ConfiguredElement<ItemResourceValue>> getConfiguredValues() {
    // Get a list of all the items indexed by the item name. Each item contains a list of the
    // possible values in this theme in different configurations.
    //
    // If item1 has multiple values in different configurations, there will be an
    // item1 = {folderConfiguration1 -> value1, folderConfiguration2 -> value2}
    final ImmutableList.Builder<ConfiguredElement<ItemResourceValue>> itemResourceValues = ImmutableList.builder();
    if (isFramework()) {
        assert myConfiguration.getFrameworkResources() != null;
        com.android.ide.common.resources.ResourceItem styleItem = myConfiguration.getFrameworkResources().getResourceItem(ResourceType.STYLE, myStyleResourceValue.getName());
        // Go over all the files containing the resource.
        for (ResourceFile file : styleItem.getSourceFileList()) {
            ResourceValue styleResourceValue = file.getValue(ResourceType.STYLE, styleItem.getName());
            FolderConfiguration folderConfiguration = file.getConfiguration();
            if (styleResourceValue instanceof StyleResourceValue) {
                for (final ItemResourceValue value : ((StyleResourceValue) styleResourceValue).getValues()) {
                    itemResourceValues.add(ConfiguredElement.create(folderConfiguration, value));
                }
            }
        }
    } else {
        for (ResourceItem styleDefinition : getStyleResourceItems()) {
            ResourceValue styleResourceValue = styleDefinition.getResourceValue(isFramework());
            FolderConfiguration folderConfiguration = styleDefinition.getConfiguration();
            if (styleResourceValue instanceof StyleResourceValue) {
                for (final ItemResourceValue value : ((StyleResourceValue) styleResourceValue).getValues()) {
                    // We use the qualified name since apps and libraries can use the same attribute name twice with and without "android:"
                    itemResourceValues.add(ConfiguredElement.create(folderConfiguration, value));
                }
            }
        }
    }
    return itemResourceValues.build();
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceFile(com.android.ide.common.resources.ResourceFile) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ConfiguredThemeEditorStyle method getParentNames.

/**
   * Returns the names of all the parents of this style. Parents might differ depending on the folder configuration, this returns all the
   * variants for this style.
   */
public Collection<ConfiguredElement<String>> getParentNames() {
    if (isFramework()) {
        // Framework themes do not have multiple parents so we just get the only one.
        ConfiguredThemeEditorStyle parent = getParent();
        if (parent != null) {
            return ImmutableList.of(ConfiguredElement.create(new FolderConfiguration(), parent.getQualifiedName()));
        }
        // The theme has no parent (probably the main "Theme" style)
        return Collections.emptyList();
    }
    ImmutableList.Builder<ConfiguredElement<String>> parents = ImmutableList.builder();
    for (final ResourceItem styleItem : getStyleResourceItems()) {
        StyleResourceValue style = (StyleResourceValue) styleItem.getResourceValue(false);
        assert style != null;
        String parentName = ResolutionUtils.getParentQualifiedName(style);
        if (parentName != null) {
            parents.add(ConfiguredElement.create(styleItem.getConfiguration(), parentName));
        }
    }
    return parents.build();
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 19 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ThemeEditorStyle method setValue.

/**
   * Sets the value of given attribute in all possible folders where this style is defined. If attribute or value can be used from certain API level,
   * folders below that level won't be modified, instead new folder with certain API will be created.
   * Note: {@link LocalResourceRepository}'s won't get updated immediately
   *
   * @param attribute the style attribute name
   * @param value     the style attribute value
   */
public void setValue(@NotNull final String attribute, @NotNull final String value) {
    if (!isProjectStyle()) {
        throw new UnsupportedOperationException("Non project styles can not be modified");
    }
    final Project project = myManager.getProject();
    int maxApi = Math.max(ResolutionUtils.getOriginalApiLevel(value, myManager.getProject()), ResolutionUtils.getOriginalApiLevel(attribute, project));
    int minSdk = ThemeEditorUtils.getMinApiLevel(myManager.getModule());
    // When api level of both attribute and value is not greater that Minimum SDK,
    // we should modify every FolderConfiguration, thus we set desiredApi to -1
    final int desiredApi = (maxApi <= minSdk) ? -1 : maxApi;
    new WriteCommandAction.Simple(project, "Setting value of " + attribute) {

        @Override
        protected void run() {
            // Makes the command global even if only one xml file is modified
            // That way, the Undo is always available from the theme editor
            CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
            Collection<FolderConfiguration> toBeCopied = findToBeCopied(desiredApi);
            for (FolderConfiguration configuration : toBeCopied) {
                XmlTag styleTag = findXmlTagFromConfiguration(configuration);
                assert styleTag != null;
                ThemeEditorUtils.copyTheme(desiredApi, styleTag);
            }
            if (!toBeCopied.isEmpty()) {
                // We need to refreshResource, to get all copied styles
                // Otherwise, LocalResourceRepositories won't get updated, so we won't get copied styles
                AndroidFacet facet = AndroidFacet.getInstance(myManager.getModule());
                if (facet != null) {
                    facet.refreshResources();
                    // This is because the ResourceFolderRepository may initialize through the file instead of Psi.
                    GradleBuildInvoker.saveAllFilesSafely();
                }
            }
            Collection<ResourceItem> styleItems = getStyleResourceItems();
            for (ResourceItem style : styleItems) {
                FolderConfiguration configuration = style.getConfiguration();
                int version = ThemeEditorUtils.getVersionFromConfiguration(configuration);
                // it means than we can modify 'attribute' to value 'value'.
                if (version >= desiredApi) {
                    setValue(configuration, attribute, value);
                }
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) ImmutableCollection(com.google.common.collect.ImmutableCollection) Collection(java.util.Collection) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) XmlTag(com.intellij.psi.xml.XmlTag)

Example 20 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ThemeEditorStyle method getParentName.

/**
   * @param configuration FolderConfiguration of the style to lookup
   * @return parent this style with a FolderConfiguration configuration
   */
@Nullable
public /*if there is no of this style*/
String getParentName(@NotNull FolderConfiguration configuration) {
    if (isFramework()) {
        IAndroidTarget target = myManager.getHighestApiTarget();
        assert target != null;
        com.android.ide.common.resources.ResourceItem styleItem = myManager.getResolverCache().getFrameworkResources(new FolderConfiguration(), target).getResourceItem(ResourceType.STYLE, getName());
        for (ResourceFile file : styleItem.getSourceFileList()) {
            if (file.getConfiguration().equals(configuration)) {
                StyleResourceValue style = (StyleResourceValue) file.getValue(ResourceType.STYLE, getName());
                return ResolutionUtils.getParentQualifiedName(style);
            }
        }
        throw new IllegalArgumentException("bad folder config " + configuration);
    }
    for (final ResourceItem styleItem : getStyleResourceItems()) {
        if (configuration.equals(styleItem.getConfiguration())) {
            StyleResourceValue style = (StyleResourceValue) styleItem.getResourceValue(false);
            assert style != null;
            return ResolutionUtils.getParentQualifiedName(style);
        }
    }
    throw new IllegalArgumentException("bad folder config " + configuration);
}
Also used : ResourceFile(com.android.ide.common.resources.ResourceFile) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) IAndroidTarget(com.android.sdklib.IAndroidTarget) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 NotNull (org.jetbrains.annotations.NotNull)14 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)12 Nullable (org.jetbrains.annotations.Nullable)12 ResourceItem (com.android.ide.common.res2.ResourceItem)10 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)10 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)9 ResourceFolderType (com.android.resources.ResourceFolderType)9 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)7 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Project (com.intellij.openapi.project.Project)7 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)6 ResourceResolver (com.android.ide.common.resources.ResourceResolver)6 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)6 ScreenOrientationQualifier (com.android.ide.common.resources.configuration.ScreenOrientationQualifier)6 ScreenSizeQualifier (com.android.ide.common.resources.configuration.ScreenSizeQualifier)6 SessionParams (com.android.ide.common.rendering.api.SessionParams)5 CountryCodeQualifier (com.android.ide.common.resources.configuration.CountryCodeQualifier)5