Search in sources :

Example 6 with AbstractResourceRepository

use of com.android.ide.common.res2.AbstractResourceRepository in project kotlin by JetBrains.

the class AppIndexingApiDetector method replaceUrlWithValue.

private static String replaceUrlWithValue(@NonNull XmlContext context, @NonNull String str) {
    Project project = context.getProject();
    LintClient client = context.getClient();
    if (!client.supportsProjectResources()) {
        return str;
    }
    ResourceUrl style = ResourceUrl.parse(str);
    if (style == null || style.type != ResourceType.STRING || style.framework) {
        return str;
    }
    AbstractResourceRepository resources = client.getProjectResources(project, true);
    if (resources == null) {
        return str;
    }
    List<ResourceItem> items = resources.getResourceItem(ResourceType.STRING, style.name);
    if (items == null || items.isEmpty()) {
        return str;
    }
    ResourceValue resourceValue = items.get(0).getResourceValue(false);
    if (resourceValue == null) {
        return str;
    }
    return resourceValue.getValue() == null ? str : resourceValue.getValue();
}
Also used : Project(com.android.tools.klint.detector.api.Project) AbstractResourceRepository(com.android.ide.common.res2.AbstractResourceRepository) LintClient(com.android.tools.klint.client.api.LintClient) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) ResourceUrl(com.android.ide.common.resources.ResourceUrl)

Example 7 with AbstractResourceRepository

use of com.android.ide.common.res2.AbstractResourceRepository in project kotlin by JetBrains.

the class ViewTypeDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
    LintClient client = context.getClient();
    if (mIgnore == Boolean.TRUE) {
        return;
    } else if (mIgnore == null) {
        mIgnore = !context.getScope().contains(Scope.ALL_RESOURCE_FILES) && !client.supportsProjectResources();
        if (mIgnore) {
            return;
        }
    }
    assert method.getName().equals("findViewById");
    UElement node = LintUtils.skipParentheses(call);
    while (node != null && node.getUastParent() instanceof UParenthesizedExpression) {
        node = node.getUastParent();
    }
    if (node.getUastParent() instanceof UBinaryExpressionWithType) {
        UBinaryExpressionWithType cast = (UBinaryExpressionWithType) node.getUastParent();
        PsiType type = cast.getType();
        String castType = null;
        if (type instanceof PsiClassType) {
            castType = type.getCanonicalText();
        }
        if (castType == null) {
            return;
        }
        List<UExpression> args = call.getValueArguments();
        if (args.size() == 1) {
            UExpression first = args.get(0);
            ResourceUrl resourceUrl = ResourceEvaluator.getResource(context, first);
            if (resourceUrl != null && resourceUrl.type == ResourceType.ID && !resourceUrl.framework) {
                String id = resourceUrl.name;
                if (client.supportsProjectResources()) {
                    AbstractResourceRepository resources = client.getProjectResources(context.getMainProject(), true);
                    if (resources == null) {
                        return;
                    }
                    List<ResourceItem> items = resources.getResourceItem(ResourceType.ID, id);
                    if (items != null && !items.isEmpty()) {
                        Set<String> compatible = Sets.newHashSet();
                        for (ResourceItem item : items) {
                            Collection<String> tags = getViewTags(context, item);
                            if (tags != null) {
                                compatible.addAll(tags);
                            }
                        }
                        if (!compatible.isEmpty()) {
                            ArrayList<String> layoutTypes = Lists.newArrayList(compatible);
                            checkCompatible(context, castType, null, layoutTypes, cast);
                        }
                    }
                } else {
                    Object types = mIdToViewTag.get(id);
                    if (types instanceof String) {
                        String layoutType = (String) types;
                        checkCompatible(context, castType, layoutType, null, cast);
                    } else if (types instanceof List<?>) {
                        @SuppressWarnings("unchecked") List<String> layoutTypes = (List<String>) types;
                        checkCompatible(context, castType, null, layoutTypes, cast);
                    }
                }
            }
        }
    }
}
Also used : LintClient(com.android.tools.klint.client.api.LintClient) PsiClassType(com.intellij.psi.PsiClassType) AbstractResourceRepository(com.android.ide.common.res2.AbstractResourceRepository) ResourceItem(com.android.ide.common.res2.ResourceItem) ResourceUrl(com.android.ide.common.resources.ResourceUrl) PsiType(com.intellij.psi.PsiType)

Example 8 with AbstractResourceRepository

use of com.android.ide.common.res2.AbstractResourceRepository in project kotlin by JetBrains.

the class LintUtils method getInheritedStyles.

@Nullable
public static List<StyleResourceValue> getInheritedStyles(@NonNull Project project, @NonNull LintClient client, @NonNull String styleUrl) {
    if (!client.supportsProjectResources()) {
        return null;
    }
    AbstractResourceRepository resources = client.getProjectResources(project, true);
    if (resources == null) {
        return null;
    }
    ResourceUrl style = ResourceUrl.parse(styleUrl);
    if (style == null || style.framework) {
        return null;
    }
    List<StyleResourceValue> result = null;
    Queue<ResourceValue> queue = new ArrayDeque<ResourceValue>();
    queue.add(new ResourceValue(style.type, style.name, false));
    Set<String> seen = Sets.newHashSet();
    int count = 0;
    while (count < 30 && !queue.isEmpty()) {
        ResourceValue front = queue.remove();
        String name = front.getName();
        seen.add(name);
        List<ResourceItem> items = resources.getResourceItem(front.getResourceType(), name);
        if (items != null) {
            for (ResourceItem item : items) {
                ResourceValue rv = item.getResourceValue(false);
                if (rv instanceof StyleResourceValue) {
                    StyleResourceValue srv = (StyleResourceValue) rv;
                    if (result == null) {
                        result = Lists.newArrayList();
                    }
                    result.add(srv);
                    String parent = srv.getParentStyle();
                    if (parent != null && !parent.startsWith(ANDROID_PREFIX)) {
                        ResourceUrl p = ResourceUrl.parse(parent);
                        if (p != null && !p.framework && !seen.contains(p.name)) {
                            seen.add(p.name);
                            queue.add(new ResourceValue(ResourceType.STYLE, p.name, false));
                        }
                    }
                    int index = name.lastIndexOf('.');
                    if (index > 0) {
                        String parentName = name.substring(0, index);
                        if (!seen.contains(parentName)) {
                            seen.add(parentName);
                            queue.add(new ResourceValue(ResourceType.STYLE, parentName, false));
                        }
                    }
                }
            }
        }
        count++;
    }
    return result;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) AbstractResourceRepository(com.android.ide.common.res2.AbstractResourceRepository) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) ResourceUrl(com.android.ide.common.resources.ResourceUrl) Nullable(com.android.annotations.Nullable)

Aggregations

AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)8 ResourceItem (com.android.ide.common.res2.ResourceItem)6 ResourceUrl (com.android.ide.common.resources.ResourceUrl)5 Nullable (com.android.annotations.Nullable)4 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)4 LintClient (com.android.tools.klint.client.api.LintClient)4 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)2 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)2 Project (com.android.tools.klint.detector.api.Project)2 PsiClassType (com.intellij.psi.PsiClassType)2 PsiType (com.intellij.psi.PsiType)2 ResourceFile (com.android.ide.common.res2.ResourceFile)1 LocalResourceRepository (com.android.tools.idea.rendering.LocalResourceRepository)1 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)1 Location (com.android.tools.klint.detector.api.Location)1 Handle (com.android.tools.klint.detector.api.Location.Handle)1 Pair (com.android.utils.Pair)1 ImmutableList (com.google.common.collect.ImmutableList)1 PsiElement (com.intellij.psi.PsiElement)1 PsiParameterList (com.intellij.psi.PsiParameterList)1