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);
}
}
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;
}
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);
}
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);
}
}
}
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);
}
}
Aggregations