Java Code Examples for com.android.tools.lint.detector.api.LintUtils#getChildren()

The following examples show how to use com.android.tools.lint.detector.api.LintUtils#getChildren() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourcePrefixDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@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);
            }
        }
    }
}
 
Example 2
Source File: SecurityDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: SecurityDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
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."); //$NON-NLS-1$
                }
            }
        }
    }

    return false;
}
 
Example 4
Source File: ScrollViewChildDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@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);
        }
    }
}
 
Example 5
Source File: ManifestDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 6
Source File: SecurityDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: ButtonDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/** 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;
}
 
Example 8
Source File: UseCompoundDrawableDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void visitElement(@NonNull XmlContext context, @NonNull 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)))) {
            // If the layout has a background, ignore since it would disappear from
            // the TextView
            if (element.hasAttributeNS(ANDROID_URI, ATTR_BACKGROUND)) {
                return;
            }

            // Certain scale types cannot be done with compound drawables
            String scaleType = first.getTagName().equals(IMAGE_VIEW)
                    ? first.getAttributeNS(ANDROID_URI, ATTR_SCALE_TYPE)
                    : second.getAttributeNS(ANDROID_URI, ATTR_SCALE_TYPE);
            if (scaleType != null && !scaleType.isEmpty()) {
                // For now, ignore if any scale type is set
                return;
            }

            context.report(ISSUE, element, context.getLocation(element),
                    "This tag and its children can be replaced by one `<TextView/>` and " +
                            "a compound drawable");
        }
    }
}
 
Example 9
Source File: MainActivityDetector.java    From android-custom-lint-rules with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the XML node is an activity with a launcher intent.
 *
 * @param activityNode The node to check.
 * @return <code>true</code> if the node is an activity with a launcher intent.
 */
private boolean isMainActivity(Node activityNode) {
    if (TAG_ACTIVITY.equals(activityNode.getNodeName())) {
        // Loop through all <intent-filter> tags
        for (Element activityChild : LintUtils.getChildren(activityNode)) {
            if (TAG_INTENT_FILTER.equals(activityChild.getNodeName())) {
                // Check for these children nodes:
                //
                // <category android:name="android.intent.category.LAUNCHER" />
                // <action android:name="android.intent.action.MAIN" />
                boolean hasLauncherCategory = false;
                boolean hasMainAction = false;

                for (Element intentFilterChild : LintUtils.getChildren(activityChild)) {
                    // Check for category tag)
                    if (NODE_CATEGORY.equals(intentFilterChild.getNodeName())
                            && CATEGORY_NAME_LAUNCHER.equals(
                            intentFilterChild.getAttributeNS(ANDROID_URI, ATTR_NAME))) {
                        hasLauncherCategory = true;
                    }
                    // Check for action tag
                    if (NODE_ACTION.equals(intentFilterChild.getNodeName())
                            && ACTION_NAME_MAIN.equals(
                            intentFilterChild.getAttributeNS(ANDROID_URI, ATTR_NAME))) {
                        hasMainAction = true;
                    }
                }

                if (hasLauncherCategory && hasMainAction) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 10
Source File: SecurityDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private static void checkProvider(XmlContext context, Element element) {
    String exportValue = element.getAttributeNS(ANDROID_URI, ATTR_EXPORTED);
    // Content providers are exported by default
    boolean exported = true;
    if (exportValue != null && !exportValue.isEmpty()) {
        exported = Boolean.valueOf(exportValue);
    }

    if (exported) {
        // Just check for some use of permissions. Other Lint checks can check the saneness
        // of the permissions. We'll accept the permission, readPermission, or writePermission
        // attributes on the provider element, or a path-permission element.
        String permission = element.getAttributeNS(ANDROID_URI, ATTR_READ_PERMISSION);
        if (permission == null || permission.isEmpty()) {
            permission = element.getAttributeNS(ANDROID_URI, ATTR_WRITE_PERMISSION);
            if (permission == null || permission.isEmpty()) {
                permission = element.getAttributeNS(ANDROID_URI, ATTR_PERMISSION);
                if (permission == null || permission.isEmpty()) {
                    // No permission attributes? Check for path-permission.

                    // TODO: Add a Lint check to ensure the path-permission is good, similar to
                    // the grant-uri-permission check.
                    boolean hasPermission = false;
                    for (Element child : LintUtils.getChildren(element)) {
                        String tag = child.getTagName();
                        if (tag.equals(TAG_PATH_PERMISSION)) {
                            hasPermission = true;
                            break;
                        }
                    }

                    if (!hasPermission) {
                        context.report(EXPORTED_PROVIDER, element,
                                context.getLocation(element),
                                "Exported content providers can provide access to " +
                                        "potentially sensitive data");
                    }
                }
            }
        }
    }
}
 
Example 11
Source File: ButtonDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * We've found a resource reference to some label we're interested in ("OK",
 * "Cancel", "Back", ...). Record the corresponding name such that in the
 * next pass through the layouts we can check the context (for OK/Cancel the
 * button order etc).
 */
private void foundResource(XmlContext context, String name, Element element) {
    if (!LintUtils.isEnglishResource(context, true)) {
        return;
    }

    if (!context.getProject().getReportIssues()) {
        // If this is a library project not being analyzed, ignore it
        return;
    }

    if (mApplicableResources == null) {
        mApplicableResources = new HashSet<String>();
    }

    mApplicableResources.add(STRING_PREFIX + name);

    // ALSO record all the other string resources in this file to pick up other
    // labels. If you define "OK" in one resource file and "Cancel" in another
    // this won't work, but that's probably not common and has lower overhead.
    Node parentNode = element.getParentNode();

    List<Element> items = LintUtils.getChildren(parentNode);
    if (mKeyToLabel == null) {
        mKeyToLabel = new HashMap<String, String>(items.size());
    }
    for (Element item : items) {
        String itemName = item.getAttribute(ATTR_NAME);
        NodeList childNodes = item.getChildNodes();
        for (int i = 0, n = childNodes.getLength(); i < n; i++) {
            Node child = childNodes.item(i);
            if (child.getNodeType() == Node.TEXT_NODE) {
                String text = stripLabel(child.getNodeValue());
                if (!text.isEmpty()) {
                    mKeyToLabel.put(itemName, text);
                    break;
                }
            }
        }
    }
}
 
Example 12
Source File: LauncherActivityDetector.java    From lewis with Apache License 2.0 4 votes vote down vote up
/**
 * Returns true if the XML node is an activity with a launcher intent.
 *
 * @param node is the node to check.
 * @return true if the node is an activity with a launcher intent, false if not.
 */
private boolean isMainActivity(XmlContext context, Node node) {

    if (TAG_APPLICATION.equals(node.getNodeName())) {
        mApplicationTagLocation = context.getLocation(node);
    }

    if (TAG_ACTIVITY.equals(node.getNodeName())) {

        mHasActivity = true;

        for (Element activityChild : LintUtils.getChildren(node)) {
            if (TAG_INTENT_FILTER.equals(activityChild.getNodeName())) {

                boolean hasLauncherCategory = false;
                boolean hasMainAction = false;

                for (Element intentFilterChild : LintUtils.getChildren(activityChild)) {
                    // Check for category tag)
                    if (NODE_CATEGORY.equals(intentFilterChild.getNodeName())
                            && Constants.CATEGORY_NAME_LAUNCHER.equals(
                            intentFilterChild.getAttributeNS(ANDROID_URI, ATTR_NAME))) {
                        hasLauncherCategory = true;
                    }
                    // Check for action tag
                    if (NODE_ACTION.equals(intentFilterChild.getNodeName())
                            && Constants.ACTION_NAME_MAIN.equals(
                            intentFilterChild.getAttributeNS(ANDROID_URI, ATTR_NAME))) {
                        hasMainAction = true;
                    }
                }

                if (hasLauncherCategory && hasMainAction) {
                    if (mHasLauncherActivity) {
                        context.report(ISSUE_MORE_THAN_ONE_LAUNCHER, context.getLocation(node),
                                "Expecting " + ANDROID_MANIFEST_XML + " to have only one activity with a launcher intent.");
                    }

                    // if it is a library
                    if (context.getProject() == context.getMainProject() && context.getMainProject().isLibrary()) {
                        context.report(ISSUE_LAUNCHER_ACTIVITY_IN_LIBRARY, context.getLocation(node),
                                "Expecting " + ANDROID_MANIFEST_XML + " not to have an activity with a launcher intent.");
                    }

                    return true;
                }
            }
        }
    }
    return false;
}