Search in sources :

Example 11 with AppResourceRepository

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));
}
Also used : HashCode(com.google.common.hash.HashCode) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 12 with AppResourceRepository

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;
}
Also used : AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 13 with AppResourceRepository

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;
}
Also used : ResourceType(com.android.resources.ResourceType) AndroidResourceType(org.jetbrains.android.dom.AndroidResourceType) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with AppResourceRepository

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;
}
Also used : AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with AppResourceRepository

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;
}
Also used : DomElement(com.intellij.util.xml.DomElement) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) PsiElement(com.intellij.psi.PsiElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Document(org.w3c.dom.Document) Density(com.android.resources.Density) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceItem(com.android.ide.common.resources.ResourceItem) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) DensityQualifier(com.android.ide.common.resources.configuration.DensityQualifier) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)17 NotNull (org.jetbrains.annotations.NotNull)5 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)3 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)3 ResourceType (com.android.resources.ResourceType)3 Module (com.intellij.openapi.module.Module)3 File (java.io.File)3 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)2 ResourceItem (com.android.ide.common.res2.ResourceItem)2 ResourceItem (com.android.ide.common.resources.ResourceItem)2 ResourceRepository (com.android.ide.common.resources.ResourceRepository)2 IAndroidTarget (com.android.sdklib.IAndroidTarget)2 ResourceClassRegistry (com.android.tools.idea.res.ResourceClassRegistry)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 HardwareConfigHelper (com.android.ide.common.rendering.HardwareConfigHelper)1