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