use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.
the class GradleInstantRunAndroidTest method testResourceChangeIsDetected.
public void testResourceChangeIsDetected() throws Exception {
myFixture.copyFileToProject(BASEDIR + "AndroidManifest.xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
myFixture.copyFileToProject(BASEDIR + "res/values/strings.xml", "res/values/strings.xml");
myFixture.copyFileToProject(BASEDIR + "res/drawable-hdpi/ic_launcher.png", "res/drawable-hdpi/ic_launcher.png");
HashCode hash = GradleInstantRunContext.getManifestResourcesHash(myFacet);
// change a resource not referenced from manifest
AppResourceRepository repository = AppResourceRepository.create(myFacet);
ResourceValue resValue = repository.getConfiguredValue(ResourceType.STRING, "title_section1", new FolderConfiguration());
resValue.setValue("foo");
assertEquals("Hash should not change if a resource not referenced from the manifest is changed", hash, GradleInstantRunContext.getManifestResourcesHash(myFacet));
// change the app_name referenced from manifest
resValue = repository.getConfiguredValue(ResourceType.STRING, "app_name", new FolderConfiguration());
resValue.setValue("testapp");
assertNotEquals("Hash should change if a resource referenced from the manifest is changed", hash, GradleInstantRunContext.getManifestResourcesHash(myFacet));
// change the contents of the launcher icon referenced from manifest
hash = GradleInstantRunContext.getManifestResourcesHash(myFacet);
myFixture.copyFileToProject(BASEDIR + "res/drawable-mdpi/ic_launcher.png", "res/drawable-hdpi/ic_launcher.png");
assertNotEquals("Hash should change if a resource referenced from the manifest is changed", hash, GradleInstantRunContext.getManifestResourcesHash(myFacet));
}
use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.
the class NlComponent method getIds.
/**
* Looks up the existing set of id's reachable from the given module
*/
public static Collection<String> getIds(@NotNull NlModel model) {
AndroidFacet facet = model.getFacet();
AppResourceRepository resources = AppResourceRepository.getAppResources(facet, true);
Collection<String> ids = resources.getItemsOfType(ResourceType.ID);
Set<String> pendingIds = model.getPendingIds();
if (!pendingIds.isEmpty()) {
List<String> all = Lists.newArrayListWithCapacity(pendingIds.size() + ids.size());
all.addAll(ids);
all.addAll(pendingIds);
ids = all;
}
return ids;
}
use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.
the class ResourceReferenceConverter method getResourceTypesInCurrentModule.
@NotNull
public static Set<ResourceType> getResourceTypesInCurrentModule(@NotNull AndroidFacet facet) {
Set<ResourceType> result = EnumSet.noneOf(ResourceType.class);
AppResourceRepository resourceRepository = facet.getAppResources(true);
for (ResourceType type : ResourceType.values()) {
if (resourceRepository.hasResourcesOfType(type)) {
if (type == ResourceType.DECLARE_STYLEABLE) {
// The ResourceRepository maps tend to hold DECLARE_STYLEABLE, but not STYLEABLE. However, these types are
// used for R inner classes, and declare-styleable isn't a valid inner class name, so convert to styleable.
result.add(ResourceType.STYLEABLE);
} else {
result.add(type);
}
}
}
return result;
}
use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.
the class StyleItemNameConverter method getVariants.
@NotNull
@Override
public Collection<String> getVariants(ConvertContext context) {
List<String> result = Lists.newArrayList();
if (context.getModule() != null && context.getTag() != null) {
// Try to find the parents of the styles where this item is defined and add to the suggestion every non-framework attribute that has been used.
// This is helpful in themes like AppCompat where there is not only a framework attribute defined but also a custom attribute. This
// will show both in the completion list.
AppResourceRepository appResourceRepository = AppResourceRepository.getAppResources(context.getModule(), true);
XmlTag styleTag = context.getTag().getParentTag();
String parent = getParentNameFromTag(styleTag);
List<ResourceItem> parentDefinitions = parent != null && appResourceRepository != null ? appResourceRepository.getResourceItem(ResourceType.STYLE, parent) : null;
if (parentDefinitions != null && !parentDefinitions.isEmpty()) {
HashSet<String> attributeNames = Sets.newHashSet();
LinkedList<ResourceItem> toExplore = Lists.newLinkedList(parentDefinitions);
int i = 0;
while (!toExplore.isEmpty() && i++ < ResourceResolver.MAX_RESOURCE_INDIRECTION) {
ResourceItem parentItem = toExplore.pop();
StyleResourceValue parentValue = (StyleResourceValue) parentItem.getResourceValue(false);
if (parentValue == null || parentValue.isFramework()) {
// No parent or the parent is a framework style
continue;
}
for (ItemResourceValue value : parentValue.getValues()) {
if (!value.isFramework()) {
attributeNames.add(value.getName());
}
}
List<ResourceItem> parents = appResourceRepository.getResourceItem(ResourceType.STYLE, parentValue.getParentStyle());
if (parents != null) {
toExplore.addAll(parents);
}
}
result.addAll(attributeNames);
}
}
ResourceManager manager = SystemResourceManager.getInstance(context);
if (manager != null) {
AttributeDefinitions attrDefs = manager.getAttributeDefinitions();
if (attrDefs != null) {
for (String name : attrDefs.getAttributeNames()) {
result.add(SdkConstants.PREFIX_ANDROID + name);
}
}
}
return result;
}
use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.
the class AndroidColorAnnotator method pickBitmapFromXml.
@Nullable
private static File pickBitmapFromXml(@NotNull File file, @NotNull ResourceResolver resourceResolver, @NotNull Project project, @NonNull AndroidFacet facet, @NonNull ResourceValue resourceValue) {
try {
String xml = Files.toString(file, Charsets.UTF_8);
Document document = XmlUtils.parseDocumentSilently(xml, true);
if (document != null && document.getDocumentElement() != null) {
Element root = document.getDocumentElement();
String tag = root.getTagName();
Element target = null;
String attribute = null;
if ("vector".equals(tag)) {
// Take a look and see if we have a bitmap we can fall back to
AppResourceRepository resourceRepository = AppResourceRepository.getAppResources(facet, true);
List<com.android.ide.common.res2.ResourceItem> items = resourceRepository.getResourceItem(resourceValue.getResourceType(), resourceValue.getName());
if (items != null) {
for (com.android.ide.common.res2.ResourceItem item : items) {
FolderConfiguration configuration = item.getConfiguration();
DensityQualifier densityQualifier = configuration.getDensityQualifier();
if (densityQualifier != null) {
Density density = densityQualifier.getValue();
if (density != null && density.isValidValueForDevice()) {
File bitmap = item.getFile();
if (bitmap != null && bitmap.isFile()) {
return bitmap;
}
}
}
}
}
// Vectors are handled in the icon cache
return file;
} else if ("bitmap".equals(tag) || "nine-patch".equals(tag)) {
target = root;
attribute = ATTR_SRC;
} else if ("selector".equals(tag) || "level-list".equals(tag) || "layer-list".equals(tag) || "transition".equals(tag)) {
NodeList children = root.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
Node item = children.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE && TAG_ITEM.equals(item.getNodeName())) {
target = (Element) item;
if (target.hasAttributeNS(ANDROID_URI, ATTR_DRAWABLE)) {
attribute = ATTR_DRAWABLE;
break;
}
}
}
} else if ("clip".equals(tag) || "inset".equals(tag) || "scale".equals(tag)) {
target = root;
attribute = ATTR_DRAWABLE;
} else {
// <shape> etc - no bitmap to be found
return null;
}
if (attribute != null && target.hasAttributeNS(ANDROID_URI, attribute)) {
String src = target.getAttributeNS(ANDROID_URI, attribute);
ResourceValue value = resourceResolver.findResValue(src, false);
if (value != null) {
return ResourceHelper.resolveDrawable(resourceResolver, value, project);
}
}
}
} catch (Throwable ignore) {
// Not logging for now; afraid to risk unexpected crashes in upcoming preview. TODO: Re-enable.
//Logger.getInstance(AndroidColorAnnotator.class).warn(String.format("Could not read/render icon image %1$s", file), e);
}
return null;
}
Aggregations