Search in sources :

Example 96 with ResourceType

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

the class ResourceNameConverter method getErrorMessage.

@Override
public String getErrorMessage(@Nullable String s, ConvertContext context) {
    ResourceType type = null;
    XmlTag tag = context.getTag();
    if (tag != null && SdkConstants.TAG_ATTR.equals(tag.getName())) {
        // The resource name validator needs to know if we're dealing with
        // an <attr>
        type = ResourceType.ATTR;
    }
    return s == null ? null : ValueResourceNameValidator.getErrorText(s, type);
}
Also used : ResourceType(com.android.resources.ResourceType) XmlTag(com.intellij.psi.xml.XmlTag)

Example 97 with ResourceType

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

the class ViewLoader method parseClass.

private static boolean parseClass(@NotNull Class<?> rClass, @NotNull TIntObjectHashMap<Pair<ResourceType, String>> id2res, @NotNull Map<IntArrayWrapper, String> styleableId2Res, @NotNull Map<ResourceType, TObjectIntHashMap<String>> res2id) throws ClassNotFoundException {
    try {
        final Class<?>[] nestedClasses;
        try {
            nestedClasses = rClass.getDeclaredClasses();
        } catch (LinkageError e) {
            final Throwable cause = e.getCause();
            LOG.debug(e);
            if (cause instanceof ClassNotFoundException) {
                throw (ClassNotFoundException) cause;
            }
            throw e;
        }
        for (Class<?> resClass : nestedClasses) {
            final ResourceType resType = ResourceType.getEnum(resClass.getSimpleName());
            if (resType == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("  '%s' is not a valid resource type", anonymizeClassName(resClass.getSimpleName())));
                }
                continue;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("  Defining resource type '%s'", anonymizeClassName(resClass.getSimpleName())));
            }
            final TObjectIntHashMap<String> resName2Id = new TObjectIntHashMap<>();
            res2id.put(resType, resName2Id);
            for (Field field : resClass.getDeclaredFields()) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    // May not be final in library projects
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' field is not static, skipping", field.getName()));
                    }
                    continue;
                }
                final Class<?> type = field.getType();
                if (type.isArray() && type.getComponentType() == int.class) {
                    styleableId2Res.put(new IntArrayWrapper((int[]) field.get(null)), field.getName());
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' defined as int[]", field.getName()));
                    }
                } else if (type == int.class) {
                    final Integer value = (Integer) field.get(null);
                    id2res.put(value, Pair.of(resType, field.getName()));
                    resName2Id.put(field.getName(), value);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' defined as int", field.getName()));
                    }
                } else {
                    LOG.error("Unknown field type in R class: " + type);
                }
            }
        }
    } catch (IllegalAccessException e) {
        LOG.info(e);
        return false;
    }
    return true;
}
Also used : ResourceType(com.android.resources.ResourceType) Field(java.lang.reflect.Field) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) PsiClass(com.intellij.psi.PsiClass) IntArrayWrapper(com.android.ide.common.resources.IntArrayWrapper)

Example 98 with ResourceType

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

the class AndroidPsiAugmentProvider method getAugments.

@SuppressWarnings("unchecked")
@NotNull
@Override
public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element, @NotNull Class<Psi> type) {
    if ((type != PsiClass.class && type != PsiField.class) || !(element instanceof PsiExtensibleClass)) {
        return Collections.emptyList();
    }
    final PsiExtensibleClass aClass = (PsiExtensibleClass) element;
    final String className = aClass.getName();
    final boolean rClassAugment = AndroidUtils.R_CLASS_NAME.equals(className) && type == PsiClass.class;
    if (DumbService.isDumb(element.getProject())) {
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because of dumb mode");
        }
        return Collections.emptyList();
    }
    final AndroidFacet facet = AndroidFacet.getInstance(element);
    if (facet == null) {
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because no facet");
        }
        return Collections.emptyList();
    }
    final PsiFile containingFile = element.getContainingFile();
    if (containingFile == null) {
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because of no containing file");
        }
        return Collections.emptyList();
    }
    if (type == PsiClass.class) {
        if (AndroidUtils.R_CLASS_NAME.equals(className) && AndroidResourceUtil.isRJavaFile(facet, containingFile)) {
            final Set<String> existingInnerClasses = getOwnInnerClasses(aClass);
            final Set<ResourceType> types = ResourceReferenceConverter.getResourceTypesInCurrentModule(facet);
            final List<Psi> result = new ArrayList<Psi>();
            for (ResourceType resType : types) {
                if (!existingInnerClasses.contains(resType.getName())) {
                    final AndroidLightClass resClass = new ResourceTypeClass(facet, resType.getName(), aClass);
                    result.add((Psi) resClass);
                }
            }
            if (rClassAugment) {
                LOG.debug("R_CLASS_AUGMENT: " + result.size() + " classes added");
            }
            return result;
        } else if (AndroidUtils.MANIFEST_CLASS_NAME.equals(className) && AndroidResourceUtil.isManifestJavaFile(facet, containingFile)) {
            return Arrays.asList((Psi) new PermissionClass(facet, aClass), (Psi) new PermissionGroupClass(facet, aClass));
        }
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because containing file is not actual R.java file");
        }
    } else if (type == PsiField.class && !(aClass instanceof AndroidLightClass)) {
        // extend existing inner classes, not provided by this augment (ex. they can be generated by maven)
        final PsiClass parentClass = aClass.getContainingClass();
        if (parentClass != null && AndroidUtils.R_CLASS_NAME.equals(parentClass.getName()) && AndroidResourceUtil.isRJavaFile(facet, containingFile)) {
            final String resClassName = aClass.getName();
            if (resClassName != null && ResourceType.getEnum(resClassName) != null) {
                final Set<String> existingFields = getOwnFields(aClass);
                final PsiField[] newFields = ResourceTypeClass.buildLocalResourceFields(facet, resClassName, aClass);
                final List<Psi> result = new ArrayList<Psi>();
                for (PsiField field : newFields) {
                    if (!existingFields.contains(field.getName())) {
                        result.add((Psi) field);
                    }
                }
                return result;
            }
        }
    }
    return Collections.emptyList();
}
Also used : HashSet(com.intellij.util.containers.HashSet) PsiClass(com.intellij.psi.PsiClass) ResourceType(com.android.resources.ResourceType) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) PsiField(com.intellij.psi.PsiField) PsiExtensibleClass(com.intellij.psi.impl.source.PsiExtensibleClass) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 99 with ResourceType

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

the class AndroidColorAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        String tagName = tag.getName();
        if ((ResourceType.COLOR.getName().equals(tagName) || ResourceType.DRAWABLE.getName().equals(tagName) || ResourceType.MIPMAP.getName().equals(tagName))) {
            DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
            if (domElement instanceof ResourceElement || ApplicationManager.getApplication().isUnitTestMode()) {
                String value = tag.getValue().getText().trim();
                annotateXml(element, holder, value);
            }
        } else if (TAG_ITEM.equals(tagName)) {
            XmlTagValue value = tag.getValue();
            String text = value.getText();
            annotateXml(element, holder, text);
        }
    } else if (element instanceof XmlAttributeValue) {
        XmlAttributeValue v = (XmlAttributeValue) element;
        String value = v.getValue();
        if (value == null || value.isEmpty()) {
            return;
        }
        annotateXml(element, holder, value);
    } else if (element instanceof PsiReferenceExpression) {
        ResourceReferenceType referenceType = AndroidPsiUtils.getResourceReferenceType(element);
        if (referenceType != ResourceReferenceType.NONE) {
            // (isResourceReference will return true for both "R.drawable.foo" and the foo literal leaf in the
            // same expression, which would result in both elements getting annotated and the icon showing up
            // in the gutter twice. Instead we only count the outer one.
            ResourceType type = AndroidPsiUtils.getResourceType(element);
            if (type == ResourceType.COLOR || type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) {
                String name = AndroidPsiUtils.getResourceName(element);
                annotateResourceReference(type, holder, element, name, referenceType == ResourceReferenceType.FRAMEWORK);
            }
        }
    }
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) DomElement(com.intellij.util.xml.DomElement) XmlTagValue(com.intellij.psi.xml.XmlTagValue) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) ResourceReferenceType(com.android.tools.idea.AndroidPsiUtils.ResourceReferenceType) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 100 with ResourceType

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

the class AndroidJavaCompletionContributor method filterPrivateResources.

public void filterPrivateResources(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet resultSet, AndroidFacet facet) {
    final ResourceVisibilityLookup lookup = AppResourceRepository.getAppResources(facet, true).getResourceVisibility(facet);
    if (lookup.isEmpty()) {
        return;
    }
    resultSet.runRemainingContributors(parameters, new Consumer<CompletionResult>() {

        @Override
        public void consume(CompletionResult result) {
            final Object obj = result.getLookupElement().getObject();
            if (obj instanceof PsiField) {
                PsiField field = (PsiField) obj;
                PsiClass containingClass = field.getContainingClass();
                if (containingClass != null) {
                    PsiClass rClass = containingClass.getContainingClass();
                    if (rClass != null && rClass.getName().equals(R_CLASS)) {
                        ResourceType type = ResourceType.getEnum(containingClass.getName());
                        if (type != null && lookup.isPrivate(type, field.getName())) {
                            return;
                        }
                    }
                }
            }
            resultSet.passResult(result);
        }
    });
}
Also used : CompletionResult(com.intellij.codeInsight.completion.CompletionResult) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) ResourceType(com.android.resources.ResourceType) ResourceVisibilityLookup(com.android.ide.common.repository.ResourceVisibilityLookup)

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