Search in sources :

Example 26 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by ResurrectionRemix.

the class RenderDrawable method render.

public Result render() {
    checkLock();
    // get the drawable resource value
    DrawableParams params = getParams();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    ResourceValue drawableResource = params.getDrawable();
    // resolve it
    BridgeContext context = getContext();
    drawableResource = context.getRenderResources().resolveResValue(drawableResource);
    if (drawableResource == null) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    ResourceType resourceType = drawableResource.getResourceType();
    if (resourceType != ResourceType.DRAWABLE && resourceType != ResourceType.MIPMAP) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    Drawable d = ResourceHelper.getDrawable(drawableResource, context);
    final Boolean allStates = params.getFlag(RenderParamsFlags.FLAG_KEY_RENDER_ALL_DRAWABLE_STATES);
    if (allStates == Boolean.TRUE) {
        final List<BufferedImage> result;
        if (d instanceof StateListDrawable) {
            result = new ArrayList<BufferedImage>();
            final StateListDrawable stateList = (StateListDrawable) d;
            for (int i = 0; i < stateList.getStateCount(); i++) {
                final Drawable stateDrawable = stateList.getStateDrawable(i);
                result.add(renderImage(hardwareConfig, stateDrawable, context));
            }
        } else {
            result = Collections.singletonList(renderImage(hardwareConfig, d, context));
        }
        return Status.SUCCESS.createResult(result);
    } else {
        BufferedImage image = renderImage(hardwareConfig, d, context);
        return Status.SUCCESS.createResult(image);
    }
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) StateListDrawable(android.graphics.drawable.StateListDrawable) BufferedImage(java.awt.image.BufferedImage) DrawableParams(com.android.ide.common.rendering.api.DrawableParams)

Example 27 with ResourceType

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

the class ResourceEvaluator method getResourceTypes.

/**
     * Evaluates the given node and returns the resource types applicable to the
     * node, if any.
     *
     * @param element the element to compute the types for
     * @return the corresponding resource types
     */
@Nullable
public EnumSet<ResourceType> getResourceTypes(@Nullable PsiElement element) {
    if (element == null) {
        return null;
    }
    if (element instanceof PsiConditionalExpression) {
        PsiConditionalExpression expression = (PsiConditionalExpression) element;
        Object known = ConstantEvaluator.evaluate(null, expression.getCondition());
        if (known == Boolean.TRUE && expression.getThenExpression() != null) {
            return getResourceTypes(expression.getThenExpression());
        } else if (known == Boolean.FALSE && expression.getElseExpression() != null) {
            return getResourceTypes(expression.getElseExpression());
        } else {
            EnumSet<ResourceType> left = getResourceTypes(expression.getThenExpression());
            EnumSet<ResourceType> right = getResourceTypes(expression.getElseExpression());
            if (left == null) {
                return right;
            } else if (right == null) {
                return left;
            } else {
                EnumSet<ResourceType> copy = EnumSet.copyOf(left);
                copy.addAll(right);
                return copy;
            }
        }
    } else if (element instanceof PsiParenthesizedExpression) {
        PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression) element;
        return getResourceTypes(parenthesizedExpression.getExpression());
    } else if (element instanceof PsiMethodCallExpression && mAllowDereference) {
        PsiMethodCallExpression call = (PsiMethodCallExpression) element;
        PsiReferenceExpression expression = call.getMethodExpression();
        PsiMethod method = call.resolveMethod();
        if (method != null && method.getContainingClass() != null) {
            EnumSet<ResourceType> types = getTypesFromAnnotations(method);
            if (types != null) {
                return types;
            }
            String qualifiedName = method.getContainingClass().getQualifiedName();
            String name = expression.getReferenceName();
            if ((CLASS_RESOURCES.equals(qualifiedName) || CLASS_CONTEXT.equals(qualifiedName) || CLASS_FRAGMENT.equals(qualifiedName) || CLASS_V4_FRAGMENT.equals(qualifiedName) || CLS_TYPED_ARRAY.equals(qualifiedName)) && name != null && name.startsWith("get")) {
                PsiExpression[] args = call.getArgumentList().getExpressions();
                if (args.length > 0) {
                    types = getResourceTypes(args[0]);
                    if (types != null) {
                        return types;
                    }
                }
            }
        }
    } else if (element instanceof PsiReference) {
        ResourceUrl url = getResourceConstant(element);
        if (url != null) {
            return EnumSet.of(url.type);
        }
        PsiElement resolved = ((PsiReference) element).resolve();
        if (resolved instanceof PsiField) {
            url = getResourceConstant(resolved);
            if (url != null) {
                return EnumSet.of(url.type);
            }
            PsiField field = (PsiField) resolved;
            if (field.getInitializer() != null) {
                return getResourceTypes(field.getInitializer());
            }
            return null;
        } else if (resolved instanceof PsiParameter) {
            return getTypesFromAnnotations((PsiParameter) resolved);
        } else if (resolved instanceof PsiLocalVariable) {
            PsiLocalVariable variable = (PsiLocalVariable) resolved;
            PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class, false);
            if (statement != null) {
                PsiStatement prev = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class);
                String targetName = variable.getName();
                if (targetName == null) {
                    return null;
                }
                while (prev != null) {
                    if (prev instanceof PsiDeclarationStatement) {
                        PsiDeclarationStatement prevStatement = (PsiDeclarationStatement) prev;
                        for (PsiElement e : prevStatement.getDeclaredElements()) {
                            if (variable.equals(e)) {
                                return getResourceTypes(variable.getInitializer());
                            }
                        }
                    } else if (prev instanceof PsiExpressionStatement) {
                        PsiExpression expression = ((PsiExpressionStatement) prev).getExpression();
                        if (expression instanceof PsiAssignmentExpression) {
                            PsiAssignmentExpression assign = (PsiAssignmentExpression) expression;
                            PsiExpression lhs = assign.getLExpression();
                            if (lhs instanceof PsiReferenceExpression) {
                                PsiReferenceExpression reference = (PsiReferenceExpression) lhs;
                                if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) {
                                    return getResourceTypes(assign.getRExpression());
                                }
                            }
                        }
                    }
                    prev = PsiTreeUtil.getPrevSiblingOfType(prev, PsiStatement.class);
                }
            }
        }
    }
    return null;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiDeclarationStatement(com.intellij.psi.PsiDeclarationStatement) PsiExpression(com.intellij.psi.PsiExpression) PsiMethod(com.intellij.psi.PsiMethod) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) EnumSet(java.util.EnumSet) PsiReference(com.intellij.psi.PsiReference) ResourceType(com.android.resources.ResourceType) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) PsiConditionalExpression(com.intellij.psi.PsiConditionalExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) PsiParameter(com.intellij.psi.PsiParameter) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) PsiField(com.intellij.psi.PsiField) PsiParenthesizedExpression(com.intellij.psi.PsiParenthesizedExpression) ResourceUrl(com.android.ide.common.resources.ResourceUrl) PsiElement(com.intellij.psi.PsiElement) Nullable(com.android.annotations.Nullable)

Example 28 with ResourceType

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

the class UastLintUtils method toAndroidReference.

@Nullable
private static AndroidReference toAndroidReference(UQualifiedReferenceExpression expression) {
    List<String> path = UastUtils.asQualifiedPath(expression);
    String packageNameFromResolved = null;
    PsiClass containingClass = UastUtils.getContainingClass(expression.resolve());
    if (containingClass != null) {
        String containingClassFqName = containingClass.getQualifiedName();
        if (containingClassFqName != null) {
            int i = containingClassFqName.lastIndexOf(".R.");
            if (i >= 0) {
                packageNameFromResolved = containingClassFqName.substring(0, i);
            }
        }
    }
    if (path == null) {
        return null;
    }
    int size = path.size();
    if (size < 3) {
        return null;
    }
    String r = path.get(size - 3);
    if (!r.equals(SdkConstants.R_CLASS)) {
        return null;
    }
    String packageName = packageNameFromResolved != null ? packageNameFromResolved : Joiner.on('.').join(path.subList(0, size - 3));
    String type = path.get(size - 2);
    String name = path.get(size - 1);
    ResourceType resourceType = null;
    for (ResourceType value : ResourceType.values()) {
        if (value.getName().equals(type)) {
            resourceType = value;
            break;
        }
    }
    if (resourceType == null) {
        return null;
    }
    return new AndroidReference(expression, packageName, resourceType, name);
}
Also used : ResourceType(com.android.resources.ResourceType) Nullable(com.android.annotations.Nullable)

Example 29 with ResourceType

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

the class PrivateResourceDetector method visitElement.

@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    if (TAG_RESOURCES.equals(element.getTagName())) {
        for (Element item : LintUtils.getChildren(element)) {
            Attr nameAttribute = item.getAttributeNode(ATTR_NAME);
            if (nameAttribute != null) {
                String name = getResourceFieldName(nameAttribute.getValue());
                String type = item.getTagName();
                if (type.equals(TAG_ITEM)) {
                    type = item.getAttribute(ATTR_TYPE);
                    if (type == null || type.isEmpty()) {
                        type = RESOURCE_CLZ_ID;
                    }
                } else if (type.equals("declare-styleable")) {
                    //$NON-NLS-1$
                    type = RESOURCE_CLR_STYLEABLE;
                } else if (type.contains("array")) {
                    //$NON-NLS-1$
                    // <string-array> etc
                    type = RESOURCE_CLZ_ARRAY;
                }
                ResourceType t = ResourceType.getEnum(type);
                if (t != null && isPrivate(context, t, name) && !VALUE_TRUE.equals(item.getAttributeNS(TOOLS_URI, ATTR_OVERRIDE))) {
                    String message = createOverrideErrorMessage(context, t, name);
                    Location location = context.getValueLocation(nameAttribute);
                    context.report(ISSUE, nameAttribute, location, message);
                }
            }
        }
    } else {
        assert TAG_STYLE.equals(element.getTagName()) || TAG_ARRAY.equals(element.getTagName()) || TAG_PLURALS.equals(element.getTagName()) || TAG_INTEGER_ARRAY.equals(element.getTagName()) || TAG_STRING_ARRAY.equals(element.getTagName());
        for (Element item : LintUtils.getChildren(element)) {
            checkChildRefs(context, item);
        }
    }
}
Also used : UElement(org.jetbrains.uast.UElement) Element(org.w3c.dom.Element) ResourceType(com.android.resources.ResourceType) Attr(org.w3c.dom.Attr) Location(com.android.tools.klint.detector.api.Location)

Example 30 with ResourceType

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

the class PrivateResourceDetector method beforeCheckFile.

@Override
public void beforeCheckFile(@NonNull Context context) {
    File file = context.file;
    boolean isXmlFile = LintUtils.isXmlFile(file);
    if (!isXmlFile && !LintUtils.isBitmapFile(file)) {
        return;
    }
    String parentName = file.getParentFile().getName();
    int dash = parentName.indexOf('-');
    if (dash != -1 || FD_RES_VALUES.equals(parentName)) {
        return;
    }
    ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
    if (folderType == null) {
        return;
    }
    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
    if (types.isEmpty()) {
        return;
    }
    ResourceType type = types.get(0);
    String resourceName = getResourceFieldName(getBaseName(file.getName()));
    if (isPrivate(context, type, resourceName)) {
        String message = createOverrideErrorMessage(context, type, resourceName);
        Location location = Location.create(file);
        context.report(ISSUE, location, message);
    }
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) ResourceType(com.android.resources.ResourceType) File(java.io.File) Location(com.android.tools.klint.detector.api.Location)

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