Search in sources :

Example 86 with ResourceType

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

the class ResourceReferenceConverter method getResourceTypes.

@NotNull
public Set<ResourceType> getResourceTypes(@NotNull DomElement element) {
    AndroidResourceType resourceType = element.getAnnotation(AndroidResourceType.class);
    Set<ResourceType> types = EnumSet.copyOf(myResourceTypes);
    if (resourceType != null) {
        String s = resourceType.value();
        if (s != null) {
            ResourceType t = ResourceType.getEnum(s);
            if (t != null) {
                types.add(t);
            }
        }
    }
    if (types.size() == 0) {
        return VALUE_RESOURCE_TYPES;
    } else if (types.contains(ResourceType.DRAWABLE)) {
        types.add(ResourceType.COLOR);
    }
    return types;
}
Also used : AndroidResourceType(org.jetbrains.android.dom.AndroidResourceType) ResourceType(com.android.resources.ResourceType) AndroidResourceType(org.jetbrains.android.dom.AndroidResourceType) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with ResourceType

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

the class ResourceReferenceConverter method getVariants.

@Override
@NotNull
public Collection<? extends ResourceValue> getVariants(ConvertContext context) {
    Set<ResourceValue> result = new HashSet<>();
    Module module = context.getModule();
    if (module == null)
        return result;
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null)
        return result;
    final Set<ResourceType> recommendedTypes = getResourceTypes(context);
    if (recommendedTypes.contains(ResourceType.BOOL) && recommendedTypes.size() < VALUE_RESOURCE_TYPES.size()) {
        // Is this resource reference expected to be a @bool reference? Specifically
        // check that it's not allowed to be *all* resource types since that's a fallback
        // for when we don't have metadata, and we don't want to show true and false
        // as possible completions for things like com.google.android.gms.version
        result.add(ResourceValue.literal(VALUE_TRUE));
        result.add(ResourceValue.literal(VALUE_FALSE));
    }
    // hack to check if it is a real id attribute
    if (recommendedTypes.contains(ResourceType.ID) && recommendedTypes.size() == 1) {
        result.add(ResourceValue.reference(NEW_ID_PREFIX));
    }
    XmlElement element = context.getXmlElement();
    if (element == null)
        return result;
    String value = getValue(element);
    assert value != null;
    boolean startsWithRefChar = StringUtil.startsWithChar(value, '@');
    if (!myQuiet || startsWithRefChar) {
        String resourcePackage = null;
        // Retrieve the system prefix depending on the prefix settings ("@android:" or "android:")
        String systemPrefix = getPackagePrefix(SYSTEM_RESOURCE_PACKAGE, myWithPrefix || startsWithRefChar);
        if (value.startsWith(systemPrefix)) {
            resourcePackage = SYSTEM_RESOURCE_PACKAGE;
        } else {
            result.add(ResourceValue.literal(systemPrefix));
        }
        final char prefix = myWithPrefix || startsWithRefChar ? '@' : 0;
        if (value.startsWith(NEW_ID_PREFIX)) {
            addVariantsForIdDeclaration(result, facet, prefix, value);
        }
        if (recommendedTypes.size() >= 1 && myExpandedCompletionSuggestion) {
            // We will add the resource type (e.g. @style/) if the current value starts like a reference using @
            final boolean explicitResourceType = startsWithRefChar || myWithExplicitResourceType;
            for (final ResourceType type : recommendedTypes) {
                addResourceReferenceValues(facet, prefix, type, resourcePackage, result, explicitResourceType);
            }
        } else {
            final Set<ResourceType> filteringSet = SYSTEM_RESOURCE_PACKAGE.equals(resourcePackage) ? null : getResourceTypesInCurrentModule(facet);
            for (ResourceType resourceType : ResourceType.values()) {
                final String type = resourceType.getName();
                String typePrefix = getTypePrefix(resourcePackage, type);
                if (value.startsWith(typePrefix)) {
                    addResourceReferenceValues(facet, prefix, resourceType, resourcePackage, result, true);
                } else if (recommendedTypes.contains(resourceType) && (filteringSet == null || filteringSet.contains(resourceType))) {
                    result.add(ResourceValue.literal(typePrefix));
                }
            }
        }
    }
    if (myAllowAttributeReferences) {
        completeAttributeReferences(value, facet, result);
    }
    final ResolvingConverter<String> additionalConverter = getAdditionalConverter(context);
    if (additionalConverter != null) {
        for (String variant : additionalConverter.getVariants(context)) {
            result.add(ResourceValue.literal(variant));
        }
    }
    return result;
}
Also used : ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) XmlElement(com.intellij.psi.xml.XmlElement) ResourceType(com.android.resources.ResourceType) AndroidResourceType(org.jetbrains.android.dom.AndroidResourceType) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 88 with ResourceType

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

the class AndroidResourceReferenceBase method resolveInner.

@NotNull
private ResolveResult[] resolveInner() {
    final List<PsiElement> elements = new ArrayList<>();
    final boolean attrReference = myResourceValue.getPrefix() == '?';
    collectTargets(myFacet, myResourceValue, elements, attrReference);
    final List<ResolveResult> result = new ArrayList<>();
    if (elements.isEmpty() && myResourceValue.getResourceName() != null && !AndroidUtils.SYSTEM_RESOURCE_PACKAGE.equals(myResourceValue.getNamespace())) {
        // Dynamic items do not appear in the XML scanning file index; look for
        // these in the resource repositories.
        LocalResourceRepository resources = AppResourceRepository.getAppResources(myFacet.getModule(), true);
        ResourceType resourceType = myResourceValue.getType();
        if (resourceType != null && (resourceType != ResourceType.ATTR || attrReference)) {
            // If not, it could be some broken source, such as @android/test
            assert resources != null;
            List<ResourceItem> items = resources.getResourceItem(resourceType, myResourceValue.getResourceName());
            if (items != null && FolderTypeRelationship.getRelatedFolders(resourceType).contains(ResourceFolderType.VALUES)) {
                for (ResourceItem item : items) {
                    XmlTag tag = LocalResourceRepository.getItemTag(myFacet.getModule().getProject(), item);
                    if (tag != null) {
                        elements.add(tag);
                    } else if (item instanceof DynamicResourceValueItem) {
                        result.add(((DynamicResourceValueItem) item).createResolveResult());
                    }
                }
            }
        }
    }
    if (elements.size() > 1) {
        Collections.sort(elements, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
    }
    for (PsiElement target : elements) {
        result.add(new PsiElementResolveResult(target));
    }
    return result.toArray(new ResolveResult[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) DynamicResourceValueItem(com.android.tools.idea.res.DynamicResourceValueItem) ResourceType(com.android.resources.ResourceType) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 89 with ResourceType

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

the class AndroidResourceReferenceBase method collectTargets.

private void collectTargets(AndroidFacet facet, ResourceValue resValue, List<PsiElement> elements, boolean attrReference) {
    ResourceType resType = resValue.getType();
    if (resType == null) {
        return;
    }
    ResourceManager manager = facet.getResourceManager(resValue.getNamespace(), myElement);
    if (manager != null) {
        String resName = resValue.getResourceName();
        if (resName != null) {
            manager.collectLazyResourceElements(resType.getName(), resName, attrReference, myElement, elements);
        }
    }
}
Also used : ResourceType(com.android.resources.ResourceType) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager)

Example 90 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)

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