use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ThemeEditorUtilsTest method testCopyThemeVersionOverride.
/**
* Tests copyTheme method for following cases:
* 1. copyTheme(21, "values-en-night")
* 2. copyTheme(21, "values-v19")
*/
public void testCopyThemeVersionOverride() {
myFixture.copyFileToProject("themeEditor/styles_1.xml", "res/values-en-night/styles.xml");
myFixture.copyFileToProject("themeEditor/styles_1.xml", "res/values-v19/styles.xml");
LocalResourceRepository repository = AppResourceRepository.getAppResources(myModule, true);
assertNotNull(repository);
final List<ResourceItem> styleItems = repository.getResourceItem(ResourceType.STYLE, "AppTheme");
assertNotNull(styleItems);
assertEquals(2, styleItems.size());
new WriteCommandAction.Simple(myModule.getProject(), "Copy a theme") {
@Override
protected void run() throws Throwable {
for (ResourceItem styleItem : styleItems) {
XmlTag styleTag = LocalResourceRepository.getItemTag(getProject(), styleItem);
assertNotNull(styleTag);
ThemeEditorUtils.copyTheme(21, styleTag);
}
}
}.execute();
myFixture.checkResultByFile("res/values-en-night-v21/styles.xml", "themeEditor/styles_1.xml", true);
myFixture.checkResultByFile("res/values-v21/styles.xml", "themeEditor/styles_1.xml", true);
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class StringResourceParser method parseUnderReadLock.
@NotNull
private static StringResourceData parseUnderReadLock(AndroidFacet facet, LocalResourceRepository repository) {
List<String> keys = Lists.newArrayList(repository.getItemsOfType(ResourceType.STRING));
Collections.sort(keys);
Map<String, StringResource> keyToResourceMap = new HashMap<>();
Project project = facet.getModule().getProject();
for (String key : keys) {
List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
if (items == null) {
continue;
}
StringResource stringResource = new StringResource(key);
for (ResourceItem item : items) {
XmlTag tag = LocalResourceRepository.getItemTag(project, item);
if (tag != null && SdkConstants.VALUE_FALSE.equals(tag.getAttributeValue(SdkConstants.ATTR_TRANSLATABLE))) {
stringResource.setTranslatable(false);
}
String itemStringRepresentation = resourceToString(project, item);
FolderConfiguration config = item.getConfiguration();
LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
if (qualifier == null) {
stringResource.setDefaultValue(item, itemStringRepresentation);
} else {
Locale locale = Locale.create(qualifier);
stringResource.putTranslation(locale, item, itemStringRepresentation);
}
}
keyToResourceMap.put(key, stringResource);
}
return new StringResourceData(facet, keyToResourceMap);
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class StringsWriteUtils method getStringResourceItem.
@Nullable
private static ResourceItem getStringResourceItem(@NotNull AndroidFacet facet, @NotNull String key, @Nullable Locale locale) {
LocalResourceRepository repository = facet.getModuleResources(true);
// Ensure that items *just* created are processed by the resource repository
repository.sync();
List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
if (items == null) {
return null;
}
for (ResourceItem item : items) {
FolderConfiguration config = item.getConfiguration();
LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
if (qualifier == null) {
if (locale == null) {
return item;
} else {
continue;
}
}
Locale l = Locale.create(qualifier);
if (l.equals(locale)) {
return item;
}
}
return null;
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class StringsWriteUtils method createItem.
/**
* Creates a string resource in the specified locale.
*
* @return the resource item that was created, null if it wasn't created or could not be read back
*/
@Nullable
public static ResourceItem createItem(@NotNull final AndroidFacet facet, @NotNull VirtualFile resFolder, @Nullable final Locale locale, @NotNull final String name, @NotNull final String value, final boolean translatable) {
Project project = facet.getModule().getProject();
XmlFile resourceFile = getStringResourceFile(project, resFolder, locale);
if (resourceFile == null) {
return null;
}
final XmlTag root = resourceFile.getRootTag();
if (root == null) {
return null;
}
new WriteCommandAction.Simple(project, "Creating string " + name, resourceFile) {
@Override
public void run() {
XmlTag child = root.createChildTag(ResourceType.STRING.getName(), root.getNamespace(), escapeResourceStringAsXml(value), false);
child.setAttribute(SdkConstants.ATTR_NAME, name);
// XmlTagImpl handles a null value by deleting the attribute, which is our desired behavior
//noinspection ConstantConditions
child.setAttribute(SdkConstants.ATTR_TRANSLATABLE, translatable ? null : SdkConstants.VALUE_FALSE);
root.addSubTag(child, false);
}
}.execute();
if (ApplicationManager.getApplication().isReadAccessAllowed()) {
return getStringResourceItem(facet, name, locale);
} else {
return ApplicationManager.getApplication().runReadAction(new Computable<ResourceItem>() {
@Override
public ResourceItem compute() {
return getStringResourceItem(facet, name, locale);
}
});
}
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class StringsWriteUtils method setAttributeForItems.
/**
* Sets the value of an attribute for resource items. If SdkConstants.ATTR_NAME is set to null or "", the items are deleted.
*
* @param attribute The attribute whose value we wish to change
* @param value The desired attribute value
* @param items The resource items
* @return True if the value was successfully set, false otherwise
*/
public static boolean setAttributeForItems(@NotNull Project project, @NotNull final String attribute, @Nullable final String value, @NotNull List<ResourceItem> items) {
if (items.isEmpty()) {
return false;
}
final List<XmlTag> tags = Lists.newArrayListWithExpectedSize(items.size());
final Set<PsiFile> files = Sets.newHashSetWithExpectedSize(items.size());
for (ResourceItem item : items) {
XmlTag tag = LocalResourceRepository.getItemTag(project, item);
if (tag == null) {
return false;
}
tags.add(tag);
files.add(tag.getContainingFile());
}
final boolean deleteTag = attribute.equals(SdkConstants.ATTR_NAME) && (value == null || value.isEmpty());
new WriteCommandAction.Simple(project, "Setting attribute " + attribute, files.toArray(new PsiFile[files.size()])) {
@Override
public void run() {
for (XmlTag tag : tags) {
if (deleteTag) {
tag.delete();
} else {
// XmlTagImpl handles a null value by deleting the attribute, which is our desired behavior
//noinspection ConstantConditions
tag.setAttribute(attribute, value);
}
}
}
}.execute();
return true;
}
Aggregations