Search in sources :

Example 31 with ResourceType

use of com.android.resources.ResourceType in project android by JetBrains.

the class AndroidXmlDocumentationProvider method generateDoc.

@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    if (element instanceof ProvidedDocumentationPsiElement) {
        return ((ProvidedDocumentationPsiElement) element).getDocumentation();
    }
    if (element instanceof LazyValueResourceElementWrapper) {
        LazyValueResourceElementWrapper wrapper = (LazyValueResourceElementWrapper) element;
        ValueResourceInfo resourceInfo = wrapper.getResourceInfo();
        ResourceType type = resourceInfo.getType();
        String name = resourceInfo.getName();
        Module module = ModuleUtilCore.findModuleForPsiElement(element);
        if (module == null) {
            return null;
        }
        AndroidFacet facet = AndroidFacet.getInstance(element);
        if (facet == null) {
            return null;
        }
        ResourceUrl url;
        ResourceUrl originalUrl = originalElement != null ? ResourceUrl.parse(originalElement.getText()) : null;
        if (originalUrl != null && name.equals(originalUrl.name)) {
            url = originalUrl;
        } else {
            boolean isFramework = false;
            if (originalUrl != null) {
                isFramework = originalUrl.framework;
            } else {
                // Figure out if this resource is a framework file.
                // We really should store that info in the ValueResourceInfo instances themselves.
                // For now, attempt to figure it out
                SystemResourceManager systemResourceManager = facet.getSystemResourceManager();
                VirtualFile containingFile = resourceInfo.getContainingFile();
                if (systemResourceManager != null) {
                    VirtualFile parent = containingFile.getParent();
                    if (parent != null) {
                        VirtualFile resDir = parent.getParent();
                        if (resDir != null) {
                            isFramework = systemResourceManager.isResourceDir(resDir);
                        }
                    }
                }
            }
            url = ResourceUrl.create(type, name, isFramework, false);
        }
        return generateDoc(element, url);
    } else if (element instanceof MyResourceElement) {
        return getResourceDocumentation(element, ((MyResourceElement) element).myResource);
    } else if (element instanceof XmlAttributeValue) {
        return getResourceDocumentation(element, ((XmlAttributeValue) element).getValue());
    }
    if (originalElement instanceof XmlToken) {
        XmlToken token = (XmlToken) originalElement;
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_START_DELIMITER) {
            PsiElement next = token.getNextSibling();
            if (next instanceof XmlToken) {
                token = (XmlToken) next;
            }
        } else if (token.getTokenType() == XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            PsiElement prev = token.getPrevSibling();
            if (prev instanceof XmlToken) {
                token = (XmlToken) prev;
            }
        }
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_TOKEN) {
            String documentation = getResourceDocumentation(originalElement, token.getText());
            if (documentation != null) {
                return documentation;
            }
        } else if (token.getTokenType() == XML_DATA_CHARACTERS) {
            String text = token.getText().trim();
            String documentation = getResourceDocumentation(originalElement, text);
            if (documentation != null) {
                return documentation;
            }
        }
    }
    if (element instanceof PomTargetPsiElement && originalElement != null) {
        final PomTarget target = ((PomTargetPsiElement) element).getTarget();
        if (target instanceof DomAttributeChildDescription) {
            synchronized (ANDROID_ATTRIBUTE_DOCUMENTATION_CACHE_KEY) {
                return generateDocForXmlAttribute((DomAttributeChildDescription) target, originalElement);
            }
        }
    }
    if (element instanceof MyDocElement) {
        return ((MyDocElement) element).myDocumentation;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) DomAttributeChildDescription(com.intellij.util.xml.reflect.DomAttributeChildDescription) XmlToken(com.intellij.psi.xml.XmlToken) PomTarget(com.intellij.pom.PomTarget) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Module(com.intellij.openapi.module.Module) ResourceUrl(com.android.ide.common.resources.ResourceUrl) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) FakePsiElement(com.intellij.psi.impl.FakePsiElement) PsiElement(com.intellij.psi.PsiElement) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) ValueResourceInfo(org.jetbrains.android.resourceManagers.ValueResourceInfo)

Example 32 with ResourceType

use of com.android.resources.ResourceType in project android by JetBrains.

the class AndroidXmlDocumentationProvider method getResourceDocumentation.

@Nullable
private static String getResourceDocumentation(PsiElement element, String value) {
    ResourceUrl url = ResourceUrl.parse(value);
    if (url != null) {
        return generateDoc(element, url);
    } else {
        XmlAttribute attribute = PsiTreeUtil.getParentOfType(element, XmlAttribute.class, false);
        XmlTag tag = null;
        // True if getting the documentation of the XML value (not the value of the name attribute)
        boolean isXmlValue = false;
        // get the XmlAttribute using the containing tag
        if (element instanceof XmlToken && XML_DATA_CHARACTERS.equals(((XmlToken) element).getTokenType())) {
            tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
            attribute = tag == null ? null : tag.getAttribute(ATTR_NAME);
            isXmlValue = true;
        }
        if (attribute == null) {
            return null;
        }
        if (ATTR_NAME.equals(attribute.getName())) {
            tag = tag != null ? tag : attribute.getParent();
            XmlTag parentTag = tag.getParentTag();
            if (parentTag == null) {
                return null;
            }
            if (TAG_RESOURCES.equals(parentTag.getName())) {
                // Handle ID definitions, http://developer.android.com/guide/topics/resources/more-resources.html#Id
                String typeName = tag.getName();
                if (TAG_ITEM.equals(typeName)) {
                    typeName = tag.getAttributeValue(ATTR_TYPE);
                }
                ResourceType type = ResourceType.getEnum(typeName);
                if (type != null) {
                    return generateDoc(element, type, value, false);
                }
            } else if (TAG_STYLE.equals(parentTag.getName())) {
                // String used to get attribute definitions
                String targetValue = value;
                if (isXmlValue && attribute.getValue() != null) {
                    // In this case, the target is the name attribute of the <item> tag, which contains the key of the attr enum
                    targetValue = attribute.getValue();
                }
                if (targetValue.startsWith(ANDROID_NS_NAME_PREFIX)) {
                    targetValue = targetValue.substring(ANDROID_NS_NAME_PREFIX.length());
                }
                // Handle style item definitions, http://developer.android.com/guide/topics/resources/style-resource.html
                AttributeDefinition attributeDefinition = getAttributeDefinitionForElement(element, targetValue);
                if (attributeDefinition == null) {
                    return null;
                }
                // Return the doc of the value if searching for an enum value, otherwise return the doc of the enum itself
                return StringUtil.trim(isXmlValue ? attributeDefinition.getValueDoc(value) : attributeDefinition.getDocValue(null));
            }
        }
        // Display documentation for enum values defined in attrs.xml file, if it's present
        if (ANDROID_URI.equals(attribute.getNamespace())) {
            AttributeDefinition attributeDefinition = getAttributeDefinitionForElement(element, attribute.getLocalName());
            if (attributeDefinition == null) {
                return null;
            }
            return StringUtil.trim(attributeDefinition.getValueDoc(value));
        }
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceType(com.android.resources.ResourceType) ResourceUrl(com.android.ide.common.resources.ResourceUrl) XmlTag(com.intellij.psi.xml.XmlTag) XmlToken(com.intellij.psi.xml.XmlToken) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with ResourceType

use of com.android.resources.ResourceType in project android by JetBrains.

the class AndroidDomUtil method getResourceReferenceConverter.

@Nullable
public static ResourceReferenceConverter getResourceReferenceConverter(@NotNull AttributeDefinition attr) {
    boolean containsReference = false;
    boolean containsNotReference = false;
    Set<ResourceType> resourceTypes = EnumSet.noneOf(ResourceType.class);
    Set<AttributeFormat> formats = attr.getFormats();
    for (AttributeFormat format : formats) {
        if (format == AttributeFormat.Reference) {
            containsReference = true;
        } else {
            containsNotReference = true;
        }
        ResourceType type = getResourceType(format);
        if (type != null) {
            resourceTypes.add(type);
        }
    }
    ResourceType specialResourceType = getSpecialResourceType(attr.getName());
    if (specialResourceType != null) {
        resourceTypes.add(specialResourceType);
    }
    if (containsReference) {
        if (resourceTypes.contains(ResourceType.COLOR)) {
            resourceTypes.add(ResourceType.DRAWABLE);
        }
        if (resourceTypes.contains(ResourceType.DRAWABLE)) {
            resourceTypes.add(ResourceType.MIPMAP);
        }
        if (resourceTypes.size() == 0) {
            resourceTypes.addAll(AndroidResourceUtil.REFERRABLE_RESOURCE_TYPES);
        }
    }
    if (resourceTypes.size() > 0) {
        final ResourceReferenceConverter converter = new ResourceReferenceConverter(resourceTypes, attr);
        converter.setAllowLiterals(containsNotReference);
        return converter;
    }
    return null;
}
Also used : AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) ResourceType(com.android.resources.ResourceType) Nullable(org.jetbrains.annotations.Nullable)

Example 34 with ResourceType

use of com.android.resources.ResourceType in project android by JetBrains.

the class AndroidResourceReference method handleElementRename.

@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    if (newElementName.startsWith(SdkConstants.NEW_ID_PREFIX)) {
        newElementName = AndroidResourceUtil.getResourceNameByReferenceText(newElementName);
    }
    ResourceValue value = myValue.getValue();
    assert value != null;
    final ResourceType resType = value.getType();
    if (resType != null && newElementName != null) {
        // todo: do not allow new value resource name to contain dot, because it is impossible to check if it file or value otherwise
        final String newResName;
        // Does renamed resource point to a file?
        ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(resType);
        if (folderType != null && newElementName.contains(".")) {
            // If it does, we need to chop off its extension when inserting the new value.
            newResName = AndroidCommonUtils.getResourceName(resType.getName(), newElementName);
        } else {
            newResName = newElementName;
        }
        // Note: We're using value.getResourceType(), not resType.getName() here, because we want the "+" in the new name
        myValue.setValue(ResourceValue.referenceTo(value.getPrefix(), value.getNamespace(), value.getResourceType(), newResName));
    }
    return myValue.getXmlTag();
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 35 with ResourceType

use of com.android.resources.ResourceType in project android by JetBrains.

the class ViewLoader method loadAndParseRClass.

@VisibleForTesting
void loadAndParseRClass(@NotNull String className) throws ClassNotFoundException, InconvertibleClassError {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("loadAndParseRClass(%s)", anonymizeClassName(className)));
    }
    Class<?> aClass = myLoadedClasses.get(className);
    AppResourceRepository appResources = AppResourceRepository.getAppResources(myModule, true);
    if (aClass == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("  The R class is not loaded.");
        }
        final ModuleClassLoader moduleClassLoader = getModuleClassLoader();
        final boolean isClassLoaded = moduleClassLoader.isClassLoaded(className);
        aClass = moduleClassLoader.loadClass(className);
        if (!isClassLoaded && aClass != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("  Class found in module %s, first time load.", anonymize(myModule)));
            }
            // This is the first time we've found the resources. The dynamic R classes generated for aar libraries are now stale and must be
            // regenerated. Clear the ModuleClassLoader and reload the R class.
            myLoadedClasses.clear();
            ModuleClassLoader.clearCache(myModule);
            myModuleClassLoader = null;
            aClass = getModuleClassLoader().loadClass(className);
            if (appResources != null) {
                appResources.resetDynamicIds(true);
            }
        } else {
            if (LOG.isDebugEnabled()) {
                if (isClassLoaded) {
                    LOG.debug(String.format("  Class already loaded in module %s.", anonymize(myModule)));
                }
            }
        }
        if (aClass != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("  Class loaded");
            }
            assert myLogger != null;
            myLoadedClasses.put(className, aClass);
            myLogger.setHasLoadedClasses(true);
        }
    }
    if (aClass != null) {
        final Map<ResourceType, TObjectIntHashMap<String>> res2id = new EnumMap<>(ResourceType.class);
        final TIntObjectHashMap<Pair<ResourceType, String>> id2res = new TIntObjectHashMap<>();
        final Map<IntArrayWrapper, String> styleableId2res = new HashMap<>();
        if (parseClass(aClass, id2res, styleableId2res, res2id)) {
            if (appResources != null) {
                appResources.setCompiledResources(id2res, styleableId2res, res2id);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("END loadAndParseRClass(%s)", anonymizeClassName(className)));
    }
}
Also used : TObjectIntHashMap(gnu.trove.TObjectIntHashMap) HashMap(java.util.HashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceType(com.android.resources.ResourceType) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) IntArrayWrapper(com.android.ide.common.resources.IntArrayWrapper) EnumMap(java.util.EnumMap) Pair(com.android.util.Pair) VisibleForTesting(com.android.annotations.VisibleForTesting)

Aggregations

ResourceType (com.android.resources.ResourceType)137 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)31 NotNull (org.jetbrains.annotations.NotNull)16 Field (java.lang.reflect.Field)13 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)12 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 Map (java.util.Map)10 Nullable (org.jetbrains.annotations.Nullable)10 Nullable (com.android.annotations.Nullable)8 ResourceFolderType (com.android.resources.ResourceFolderType)8 File (java.io.File)8 EnumMap (java.util.EnumMap)7 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)7 Context (android.content.Context)6 View (android.view.View)6 AbsListView (android.widget.AbsListView)6 AdapterView (android.widget.AdapterView)6 ExpandableListView (android.widget.ExpandableListView)6 FrameLayout (android.widget.FrameLayout)6