Search in sources :

Example 6 with ResourceItem

use of com.android.ide.common.resources.ResourceItem in project platform_frameworks_base by android.

the class Main method setUp.

/**
     * Initialize the bridge and the resource maps.
     */
@BeforeClass
public static void setUp() {
    File data_dir = new File(PLATFORM_DIR, "data");
    File res = new File(data_dir, "res");
    sFrameworkRepo = new FrameworkResources(new FolderWrapper(res));
    sFrameworkRepo.loadResources();
    sFrameworkRepo.loadPublicResources(getLogger());
    sProjectResources = new ResourceRepository(new FolderWrapper(TEST_RES_DIR + APP_TEST_RES), false) {

        @NonNull
        @Override
        protected ResourceItem createResourceItem(@NonNull String name) {
            return new ResourceItem(name);
        }
    };
    sProjectResources.loadResources();
    File fontLocation = new File(data_dir, "fonts");
    File buildProp = new File(PLATFORM_DIR, "build.prop");
    File attrs = new File(res, "values" + File.separator + "attrs.xml");
    sBridge = new Bridge();
    sBridge.init(ConfigGenerator.loadProperties(buildProp), fontLocation, ConfigGenerator.getEnumMap(attrs), getLayoutLog());
}
Also used : FrameworkResources(com.android.ide.common.resources.FrameworkResources) NonNull(android.annotation.NonNull) FolderWrapper(com.android.io.FolderWrapper) ResourceRepository(com.android.ide.common.resources.ResourceRepository) ResourceItem(com.android.ide.common.resources.ResourceItem) File(java.io.File) Bridge(com.android.layoutlib.bridge.Bridge) BeforeClass(org.junit.BeforeClass)

Example 7 with ResourceItem

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

the class AndroidColorAnnotator method findResourceValue.

/** Looks up the resource item of the given type and name for the given configuration, if any */
@Nullable
private static ResourceValue findResourceValue(ResourceType type, String name, boolean isFramework, Module module, Configuration configuration) {
    if (isFramework) {
        ResourceRepository frameworkResources = configuration.getFrameworkResources();
        if (frameworkResources == null) {
            return null;
        }
        if (!frameworkResources.hasResourceItem(type, name)) {
            return null;
        }
        ResourceItem item = frameworkResources.getResourceItem(type, name);
        return item.getResourceValue(type, configuration.getFullConfig(), false);
    } else {
        LocalResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
        if (appResources == null) {
            return null;
        }
        if (!appResources.hasResourceItem(type, name)) {
            return null;
        }
        return appResources.getConfiguredValue(type, name, configuration.getFullConfig());
    }
}
Also used : LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceRepository(com.android.ide.common.resources.ResourceRepository) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.resources.ResourceItem) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with ResourceItem

use of com.android.ide.common.resources.ResourceItem 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

ResourceItem (com.android.ide.common.resources.ResourceItem)8 ResourceRepository (com.android.ide.common.resources.ResourceRepository)7 File (java.io.File)6 NonNull (android.annotation.NonNull)5 FrameworkResources (com.android.ide.common.resources.FrameworkResources)5 FolderWrapper (com.android.io.FolderWrapper)5 Bridge (com.android.layoutlib.bridge.Bridge)5 BeforeClass (org.junit.BeforeClass)5 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)3 Nullable (org.jetbrains.annotations.Nullable)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 Configurable (com.android.ide.common.resources.configuration.Configurable)1 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)1 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)1 Density (com.android.resources.Density)1 IAndroidTarget (com.android.sdklib.IAndroidTarget)1 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)1 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1