Java Code Examples for com.android.tools.lint.detector.api.XmlContext#isEnabled()

The following examples show how to use com.android.tools.lint.detector.api.XmlContext#isEnabled() . 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: ManifestDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private static void checkMipmapIcon(@NonNull XmlContext context, @NonNull Element element) {
    Attr attribute = element.getAttributeNodeNS(ANDROID_URI, ATTR_ICON);
    if (attribute == null) {
        return;
    }
    String icon = attribute.getValue();
    if (icon.startsWith(DRAWABLE_PREFIX)) {
        if (TAG_ACTIVITY.equals(element.getTagName()) && !isLaunchableActivity(element)) {
            return;
        }

        if (context.isEnabled(MIPMAP)
                // Only complain if this app is skipping some densities
                && context.getProject().getApplicableDensities() != null) {
            context.report(MIPMAP, element, context.getLocation(attribute),
                    "Should use `@mipmap` instead of `@drawable` for launcher icons");
        }
    }
}
 
Example 2
Source File: ManifestDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static void checkDocumentElement(XmlContext context, Element element) {
    Attr codeNode = element.getAttributeNodeNS(ANDROID_URI, ATTR_VERSION_CODE);
    if (codeNode != null && codeNode.getValue().startsWith(PREFIX_RESOURCE_REF)
            && context.isEnabled(ILLEGAL_REFERENCE)) {
        context.report(ILLEGAL_REFERENCE, element, context.getLocation(codeNode),
                "The `android:versionCode` cannot be a resource url, it must be "
                        + "a literal integer");
    } else if (codeNode == null && context.isEnabled(SET_VERSION)
            // Not required in Gradle projects; typically defined in build.gradle instead
            // and inserted at build time
            && !context.getMainProject().isGradleProject()) {
        context.report(SET_VERSION, element, context.getLocation(element),
                "Should set `android:versionCode` to specify the application version");
    }
    Attr nameNode = element.getAttributeNodeNS(ANDROID_URI, ATTR_VERSION_NAME);
    if (nameNode == null && context.isEnabled(SET_VERSION)
            // Not required in Gradle projects; typically defined in build.gradle instead
            // and inserted at build time
            && !context.getMainProject().isGradleProject()) {
        context.report(SET_VERSION, element, context.getLocation(element),
                "Should set `android:versionName` to specify the application version");
    }

    checkOverride(context, element, ATTR_VERSION_CODE);
    checkOverride(context, element, ATTR_VERSION_NAME);

    Attr pkgNode = element.getAttributeNode(ATTR_PACKAGE);
    if (pkgNode != null) {
        String pkg = pkgNode.getValue();
        if (pkg.contains("${") && context.getMainProject().isGradleProject()) {
            context.report(GRADLE_OVERRIDES, pkgNode, context.getLocation(pkgNode),
                    "Cannot use placeholder for the package in the manifest; "
                            + "set `applicationId` in `build.gradle` instead");
        }
    }
}
 
Example 3
Source File: TextViewDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    if (element.getTagName().equals(TEXT_VIEW)) {
        if (!element.hasAttributeNS(ANDROID_URI, ATTR_TEXT)
                && element.hasAttributeNS(ANDROID_URI, ATTR_ID)
                && !element.hasAttributeNS(ANDROID_URI, ATTR_TEXT_IS_SELECTABLE)
                && !element.hasAttributeNS(ANDROID_URI, ATTR_VISIBILITY)
                && !element.hasAttributeNS(ANDROID_URI, ATTR_ON_CLICK)
                && context.getMainProject().getTargetSdk() >= 11
                && context.isEnabled(SELECTABLE)) {
            context.report(SELECTABLE, element, context.getLocation(element),
                    "Consider making the text value selectable by specifying " +
                    "`android:textIsSelectable=\"true\"`");
        }
    }

    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        Attr attribute = (Attr) attributes.item(i);
        String name = attribute.getLocalName();
        if (name == null || name.isEmpty()) {
            // Attribute not in a namespace; we only care about the android: ones
            continue;
        }

        boolean isEditAttribute = false;
        switch (name.charAt(0)) {
            case 'a': {
                isEditAttribute = name.equals(ATTR_AUTO_TEXT);
                break;
            }
            case 'b': {
                isEditAttribute = name.equals(ATTR_BUFFER_TYPE) &&
                        attribute.getValue().equals(VALUE_EDITABLE);
                break;
            }
            case 'p': {
                isEditAttribute = name.equals(ATTR_PASSWORD)
                        || name.equals(ATTR_PHONE_NUMBER)
                        || name.equals(ATTR_PRIVATE_IME_OPTIONS);
                break;
            }
            case 'c': {
                isEditAttribute = name.equals(ATTR_CAPITALIZE)
                        || name.equals(ATTR_CURSOR_VISIBLE);
                break;
            }
            case 'd': {
                isEditAttribute = name.equals(ATTR_DIGITS);
                break;
            }
            case 'e': {
                if (name.equals(ATTR_EDITABLE)) {
                    isEditAttribute = attribute.getValue().equals(VALUE_TRUE);
                } else {
                    isEditAttribute = name.equals(ATTR_EDITOR_EXTRAS);
                }
                break;
            }
            case 'i': {
                if (name.equals(ATTR_INPUT_TYPE)) {
                    String value = attribute.getValue();
                    isEditAttribute = !value.isEmpty() && !value.equals(VALUE_NONE);
                } else {
                    isEditAttribute = name.equals(ATTR_INPUT_TYPE)
                            || name.equals(ATTR_IME_OPTIONS)
                            || name.equals(ATTR_IME_ACTION_LABEL)
                            || name.equals(ATTR_IME_ACTION_ID)
                            || name.equals(ATTR_INPUT_METHOD);
                }
                break;
            }
            case 'n': {
                isEditAttribute = name.equals(ATTR_NUMERIC);
                break;
            }
        }

        if (isEditAttribute && ANDROID_URI.equals(attribute.getNamespaceURI()) && context.isEnabled(ISSUE)) {
            Location location = context.getLocation(attribute);
            String message;
            String view = element.getTagName();
            if (view.equals(TEXT_VIEW)) {
                message = String.format(
                        "Attribute `%1$s` should not be used with `<TextView>`: " +
                        "Change element type to `<EditText>` ?", attribute.getName());
            } else {
                message = String.format(
                        "Attribute `%1$s` should not be used with `<%2$s>`: " +
                        "intended for editable text widgets",
                        attribute.getName(), view);
            }
            context.report(ISSUE, attribute, location, message);
        }
    }
}