Search in sources :

Example 11 with ResourceUrl

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

the class GradleInstantRunContext method getManifestResourcesHash.

@VisibleForTesting
static HashCode getManifestResourcesHash(@NotNull AndroidFacet facet) {
    Document manifest = MergedManifest.get(facet).getDocument();
    if (manifest == null || manifest.getDocumentElement() == null) {
        return HashCode.fromInt(0);
    }
    final Hasher hasher = Hashing.goodFastHash(32).newHasher();
    SortedSet<ResourceUrl> appResourceReferences = getAppResourceReferences(manifest.getDocumentElement());
    AppResourceRepository appResources = AppResourceRepository.getAppResources(facet, true);
    // read action needed when reading the values for app resources
    ApplicationManager.getApplication().runReadAction(() -> {
        hashResources(appResourceReferences, appResources, hasher);
    });
    return hasher.hash();
}
Also used : Hasher(com.google.common.hash.Hasher) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceUrl(com.android.ide.common.resources.ResourceUrl) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 12 with ResourceUrl

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

the class ResourceHelper method createStateListState.

/**
   * Try to parse a state in the "item" tag. Only handles those items that have
   * either "android:color" or "android:drawable" attributes in "item" tag.
   *
   * @return {@link StateListState} representing the state in tag, null if parse is unsuccessful
   */
@Nullable
private static StateListState createStateListState(XmlTag tag, boolean isFramework) {
    String stateValue = null;
    String alphaValue = null;
    Map<String, Boolean> stateAttributes = new HashMap<>();
    XmlAttribute[] attributes = tag.getAttributes();
    for (XmlAttribute attr : attributes) {
        String name = attr.getLocalName();
        String value = attr.getValue();
        if (value == null) {
            continue;
        }
        if (ATTR_COLOR.equals(name) || ATTR_DRAWABLE.equals(name)) {
            ResourceUrl url = ResourceUrl.parse(value, isFramework);
            stateValue = url != null ? url.toString() : value;
        } else if ("alpha".equals(name)) {
            ResourceUrl url = ResourceUrl.parse(value, isFramework);
            alphaValue = url != null ? url.toString() : value;
        } else if (name.startsWith(STATE_NAME_PREFIX)) {
            stateAttributes.put(name, Boolean.valueOf(value));
        }
    }
    if (stateValue == null) {
        return null;
    }
    return new StateListState(stateValue, stateAttributes, alphaValue);
}
Also used : ResourceUrl(com.android.ide.common.resources.ResourceUrl) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with ResourceUrl

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

the class DataBindingUtil method getViewClassNameFromLayoutReferenceTag.

private static String getViewClassNameFromLayoutReferenceTag(XmlTag tag, AndroidFacet facet) {
    String layout = tag.getAttributeValue(SdkConstants.ATTR_LAYOUT);
    if (layout == null) {
        return null;
    }
    LocalResourceRepository moduleResources = facet.getModuleResources(false);
    if (moduleResources == null) {
        return null;
    }
    ResourceUrl resourceUrl = ResourceUrl.parse(layout);
    if (resourceUrl == null || resourceUrl.type != ResourceType.LAYOUT) {
        return null;
    }
    DataBindingInfo info = moduleResources.getDataBindingInfoForLayout(resourceUrl.name);
    if (info == null) {
        return null;
    }
    return info.getQualifiedName();
}
Also used : LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo) ResourceUrl(com.android.ide.common.resources.ResourceUrl)

Example 14 with ResourceUrl

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

the class ResolutionUtils method getOriginalApiLevel.

/**
   * Returns the Api level at which was defined the attribute or value with the name passed as argument.
   * Returns -1 if the name argument is null or not the name of a framework attribute or resource,
   * or if it is the name of a framework attribute or resource defined in API 1, or if no Lint client found.
   */
public static int getOriginalApiLevel(@Nullable String name, @NotNull Project project) {
    if (name == null) {
        return -1;
    }
    ApiLookup apiLookup = LintIdeClient.getApiLookup(project);
    if (apiLookup == null) {
        // There is no Lint API database for this project
        LOG.warn("Could not find Lint client for project " + project.getName());
        return -1;
    }
    ResourceUrl resUrl = ResourceUrl.parse(name);
    if (resUrl == null) {
        // It is an attribute
        if (!name.startsWith(ANDROID_NS_NAME_PREFIX)) {
            // not an android attribute
            return -1;
        }
        return apiLookup.getFieldVersion("android/R$attr", name.substring(ANDROID_NS_NAME_PREFIX_LEN));
    } else {
        if (!resUrl.framework) {
            // not an android value
            return -1;
        }
        return apiLookup.getFieldVersion("android/R$" + resUrl.type, AndroidResourceUtil.getFieldNameByResourceName(resUrl.name));
    }
}
Also used : ApiLookup(com.android.tools.lint.checks.ApiLookup) ResourceUrl(com.android.ide.common.resources.ResourceUrl)

Example 15 with ResourceUrl

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

the class GutterIconCache method replaceResourceReferences.

private static void replaceResourceReferences(@NonNull Node node, @NonNull ResourceResolver resolver) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        NamedNodeMap attributes = element.getAttributes();
        attributes: for (int i = 0, n = attributes.getLength(); i < n; i++) {
            Node attribute = attributes.item(i);
            String value = attribute.getNodeValue();
            if (!(value.startsWith(PREFIX_RESOURCE_REF) || value.startsWith(PREFIX_THEME_REF))) {
                continue;
            }
            for (int j = 0; j < 10; j++) {
                ResourceUrl resolvedUrl = ResourceUrl.parse(value);
                if (resolvedUrl == null) {
                    continue attributes;
                }
                ResourceValue resourceValue;
                if (resolvedUrl.theme) {
                    resourceValue = resolver.findItemInTheme(resolvedUrl.name, resolvedUrl.framework);
                } else {
                    resourceValue = resolver.findResValue(resolvedUrl.toString(), resolvedUrl.framework);
                }
                if (resourceValue == null) {
                    continue attributes;
                }
                value = resourceValue.getValue();
                if (value == null) {
                    continue attributes;
                }
                if (!(value.startsWith(PREFIX_RESOURCE_REF) || value.startsWith(PREFIX_THEME_REF))) {
                    // Found leaf value
                    attribute.setNodeValue(value);
                    break;
                }
            }
        }
    }
    node = node.getFirstChild();
    while (node != null) {
        replaceResourceReferences(node, resolver);
        node = node.getNextSibling();
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceUrl(com.android.ide.common.resources.ResourceUrl)

Aggregations

ResourceUrl (com.android.ide.common.resources.ResourceUrl)24 PsiElement (com.intellij.psi.PsiElement)7 Nullable (com.android.annotations.Nullable)6 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)5 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)5 ResourceItem (com.android.ide.common.res2.ResourceItem)5 ResourceType (com.android.resources.ResourceType)4 PsiMethod (com.intellij.psi.PsiMethod)4 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)3 LintClient (com.android.tools.klint.client.api.LintClient)3 UExpression (org.jetbrains.uast.UExpression)3 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)2 Module (com.intellij.openapi.module.Module)2 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)2 PsiClass (com.intellij.psi.PsiClass)2 PsiConditionalExpression (com.intellij.psi.PsiConditionalExpression)2 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)2 PsiExpression (com.intellij.psi.PsiExpression)2 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)2 PsiField (com.intellij.psi.PsiField)2