private void checkForMediaSearchIntentFilter(Element element) { if (!mMediaSearchIntentFilterFound) { for (Element filterChild : LintUtils.getChildren(element)) { if (NODE_ACTION.equals(filterChild.getTagName())) { String actionValue = filterChild.getAttributeNS(ANDROID_URI, ATTR_NAME); if (ACTION_MEDIA_PLAY_FROM_SEARCH.equals(actionValue)) { mMediaSearchIntentFilterFound = true; break; } } } } }
public static boolean getExported(Element element) { // Used to check whether an activity, service or broadcast receiver is exported. String exportValue = element.getAttributeNS(ANDROID_URI, ATTR_EXPORTED); if (exportValue != null && !exportValue.isEmpty()) { return Boolean.valueOf(exportValue); } else { for (Element child : LintUtils.getChildren(element)) { if (child.getTagName().equals(TAG_INTENT_FILTER)) { return true; } } } return false; }
public static boolean getExported(Element element) { // Used to check whether an activity, service or broadcast receiver is exported. String exportValue = element.getAttributeNS(ANDROID_URI, ATTR_EXPORTED); if (exportValue != null && !exportValue.isEmpty()) { return Boolean.valueOf(exportValue); } else { for (Element child : LintUtils.getChildren(element)) { if (child.getTagName().equals(TAG_INTENT_FILTER)) { return true; } } } return false; }
/** Is the given target id the id of a {@code <Button>} within this RelativeLayout? */ private static boolean isButtonId(Element parent, String targetId) { for (Element child : LintUtils.getChildren(parent)) { String id = child.getAttributeNS(ANDROID_URI, ATTR_ID); if (LintUtils.idReferencesMatch(id, targetId)) { return child.getTagName().equals(BUTTON); } } return false; } }
private List<PsiAnnotation> createAnnotations(Element itemElement) { List<Element> children = getChildren(itemElement); List<PsiAnnotation> result = Lists.newArrayListWithExpectedSize(children.size()); for (Element annotationElement : children) { PsiAnnotation annotation = createAnnotation(annotationElement); result.add(annotation); } return result; }
static List<Element> findPermissionElements(Collection<String> permissions, Document document) { Node manifestElement = document.getDocumentElement(); if (manifestElement == null) { return Collections.emptyList(); } List<Element> nodes = new ArrayList<>(permissions.size()); for (Element child : LintUtils.getChildren(manifestElement)) { if (TAG_USES_PERMISSION.equals(child.getTagName()) && permissions.contains(child.getAttributeNS(ANDROID_URI, ATTR_NAME))) { nodes.add(child); } } return nodes; }
private static boolean isStandardReceiver(Element element) { // Play Services also the following receiver which we'll consider standard // in the sense that it doesn't require a separate permission String name = element.getAttributeNS(ANDROID_URI, ATTR_NAME); if ("com.google.android.gms.tagmanager.InstallReferrerReceiver".equals(name)) { return true; } // Checks whether a broadcast receiver receives a standard Android action for (Element child : LintUtils.getChildren(element)) { if (child.getTagName().equals(TAG_INTENT_FILTER)) { for (Element innerChild : LintUtils.getChildren(child)) { if (innerChild.getTagName().equals(NODE_ACTION)) { String categoryString = innerChild.getAttributeNS(ANDROID_URI, ATTR_NAME); return categoryString.startsWith("android."); } } } } return false; }
/** Is the given target id the id of a {@code <Button>} within this RelativeLayout? */ private static boolean isButtonId(Element parent, String targetId) { for (Element child : LintUtils.getChildren(parent)) { String id = child.getAttributeNS(ANDROID_URI, ATTR_ID); if (LintUtils.idReferencesMatch(id, targetId)) { return child.getTagName().equals(BUTTON); } } return false; } }
private static boolean hasLeanbackIntentFilter(@NonNull Node activityNode) { // Visit every intent filter for (Element activityChild : LintUtils.getChildren(activityNode)) { if (NODE_INTENT.equals(activityChild.getNodeName())) { for (Element intentFilterChild : LintUtils.getChildren(activityChild)) { // Check to see if the category is the leanback launcher String attrName = intentFilterChild.getAttributeNS(ANDROID_URI, ATTR_NAME); if (NODE_CATEGORY.equals(intentFilterChild.getNodeName()) && CATEGORY_LEANBACK_LAUNCHER.equals(attrName)) { return true; } } } } return false; }
private static boolean isWearableBindListener(@NonNull Element element) { // Checks whether a service has an Android Wear bind listener for (Element child : LintUtils.getChildren(element)) { if (child.getTagName().equals(TAG_INTENT_FILTER)) { for (Element innerChild : LintUtils.getChildren(child)) { if (innerChild.getTagName().equals(NODE_ACTION)) { String name = innerChild.getAttributeNS(ANDROID_URI, ATTR_NAME); if ("com.google.android.gms.wearable.BIND_LISTENER".equals(name)) { return true; } } } } } return false; }
private void checkServiceForBrowserServiceIntentFilter(Element element) { if (TAG_SERVICE.equals(element.getTagName()) && !mMediaIntentFilterFound) { for (Element child : LintUtils.getChildren(element)) { String tagName = child.getTagName(); if (TAG_INTENT_FILTER.equals(tagName)) { for (Element filterChild : LintUtils.getChildren(child)) { if (NODE_ACTION.equals(filterChild.getTagName())) { String actionValue = filterChild.getAttributeNS(ANDROID_URI, ATTR_NAME); if (ACTION_MEDIA_BROWSER_SERVICE.equals(actionValue)) { mMediaIntentFilterFound = true; return; } } } } } } }
@SuppressWarnings("SpellCheckingInspection") private static boolean isLaunchableActivity(@NonNull Element element) { if (!TAG_ACTIVITY.equals(element.getTagName())) { return false; } for (Element child : LintUtils.getChildren(element)) { if (child.getTagName().equals(TAG_INTENT_FILTER)) { for (Element innerChild : LintUtils.getChildren(child)) { if (innerChild.getTagName().equals("category")) { String categoryString = innerChild.getAttributeNS(ANDROID_URI, ATTR_NAME); return "android.intent.category.LAUNCHER".equals(categoryString); } } } } return false; }
@SuppressWarnings("SpellCheckingInspection") private static boolean isLaunchableActivity(@NonNull Element element) { if (!TAG_ACTIVITY.equals(element.getTagName())) { return false; } for (Element child : LintUtils.getChildren(element)) { if (child.getTagName().equals(TAG_INTENT_FILTER)) { for (Element innerChild : LintUtils.getChildren(child)) { if (innerChild.getTagName().equals("category")) { //$NON-NLS-1$ String categoryString = innerChild.getAttributeNS(ANDROID_URI, ATTR_NAME); return "android.intent.category.LAUNCHER".equals(categoryString); } } } } return false; }
private static List<Element> extractChildrenByName(@NonNull Element node, @NonNull String name) { List<Element> result = Lists.newArrayList(); List<Element> children = LintUtils.getChildren(node); for (Element child : children) { if (child.getNodeName().equals(name)) { result.add(child); } } return result; } }
/** * Assumes that the node is a direct child of the given Node. */ private static Node getElementWithTagName(@NonNull String tagName, @NonNull Node node) { for (Element child : LintUtils.getChildren(node)) { if (tagName.equals(child.getTagName())) { return child; } } return null; }
@Override public void visitElement(Context context, Element element) { List<Element> children = LintUtils.getChildren(element); boolean isHorizontal = HORIZONTAL_SCROLL_VIEW.equals(element.getTagName()); String attributeName = isHorizontal ? ATTR_LAYOUT_WIDTH : ATTR_LAYOUT_HEIGHT; for (Element child : children) { Attr sizeNode = child.getAttributeNodeNS(ANDROID_URI, attributeName); String value = sizeNode != null ? sizeNode.getValue() : null; if (VALUE_FILL_PARENT.equals(value) || VALUE_MATCH_PARENT.equals(value)) { String msg = String.format("This %1$s should use android:%2$s=\"wrap_content\"", child.getTagName(), attributeName); context.client.report(context, ISSUE, context.getLocation(sizeNode), msg, null); } } } }
@Override public void visitElement(Context context, Element element) { int childCount = LintUtils.getChildCount(element); if (childCount == 2) { List<Element> children = LintUtils.getChildren(element); Element first = children.get(0); Element second = children.get(1); if ((first.getTagName().equals(IMAGE_VIEW) && second.getTagName().equals(TEXT_VIEW) && !first.hasAttributeNS(ANDROID_URI, ATTR_LAYOUT_WEIGHT)) || ((second.getTagName().equals(IMAGE_VIEW) && first.getTagName().equals(TEXT_VIEW) && !second.hasAttributeNS(ANDROID_URI, ATTR_LAYOUT_WEIGHT)))) { context.client.report(context, ISSUE, context.getLocation(element), "This tag and its children can be replaced by one <TextView/> and " + "a compound drawable", null); } } } }
@Override public void visitElement(@NonNull XmlContext context, @NonNull Element element) { List<Element> children = LintUtils.getChildren(element); boolean isHorizontal = HORIZONTAL_SCROLL_VIEW.equals(element.getTagName()); String attributeName = isHorizontal ? ATTR_LAYOUT_WIDTH : ATTR_LAYOUT_HEIGHT; for (Element child : children) { Attr sizeNode = child.getAttributeNodeNS(ANDROID_URI, attributeName); if (sizeNode == null) { return; } String value = sizeNode.getValue(); if (VALUE_FILL_PARENT.equals(value) || VALUE_MATCH_PARENT.equals(value)) { String msg = String.format("This %1$s should use `android:%2$s=\"wrap_content\"`", child.getTagName(), attributeName); context.report(ISSUE, sizeNode, context.getLocation(sizeNode), msg); } } } }
@Override public void visitElement(@NonNull XmlContext context, @NonNull Element element) { List<Element> children = LintUtils.getChildren(element); boolean isHorizontal = HORIZONTAL_SCROLL_VIEW.equals(element.getTagName()); String attributeName = isHorizontal ? ATTR_LAYOUT_WIDTH : ATTR_LAYOUT_HEIGHT; for (Element child : children) { Attr sizeNode = child.getAttributeNodeNS(ANDROID_URI, attributeName); if (sizeNode == null) { return; } String value = sizeNode.getValue(); if (VALUE_FILL_PARENT.equals(value) || VALUE_MATCH_PARENT.equals(value)) { String msg = String.format("This %1$s should use `android:%2$s=\"wrap_content\"`", child.getTagName(), attributeName); context.report(ISSUE, sizeNode, context.getLocation(sizeNode), msg); } } } }
@Override public void visitElement(@NonNull XmlContext context, @NonNull Element element) { if (mPrefix == null || context.getResourceFolderType() != ResourceFolderType.VALUES) { return; } for (Element item : LintUtils.getChildren(element)) { Attr nameAttribute = item.getAttributeNode(ATTR_NAME); if (nameAttribute != null) { String name = nameAttribute.getValue(); if (!name.startsWith(mPrefix)) { String message = getErrorMessage(name); context.report(ISSUE, nameAttribute, context.getLocation(nameAttribute), message); } } } }