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;
}
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();
}
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();
}
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();
}
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);
}
Aggregations