Search in sources :

Example 1 with XmlParser

use of com.android.tools.klint.client.api.XmlParser in project kotlin by JetBrains.

the class AppIndexingApiDetector method getActivitiesToCheck.

/**
     * Gets names of activities which needs app indexing. i.e. the activities have data tag in their
     * intent filters.
     * TODO: Cache the activities to speed up batch lint.
     *
     * @param context The context to check in.
     */
private static Set<String> getActivitiesToCheck(Context context) {
    Set<String> activitiesToCheck = Sets.newHashSet();
    List<File> manifestFiles = context.getProject().getManifestFiles();
    XmlParser xmlParser = context.getDriver().getClient().getXmlParser();
    if (xmlParser != null) {
        // TODO: Avoid visit all manifest files before enable this check by default.
        for (File manifest : manifestFiles) {
            XmlContext xmlContext = new XmlContext(context.getDriver(), context.getProject(), null, manifest, null, xmlParser);
            Document doc = xmlParser.parseXml(xmlContext);
            if (doc != null) {
                List<Element> children = LintUtils.getChildren(doc);
                for (Element child : children) {
                    if (child.getNodeName().equals(NODE_MANIFEST)) {
                        List<Element> apps = extractChildrenByName(child, NODE_APPLICATION);
                        for (Element app : apps) {
                            List<Element> acts = extractChildrenByName(app, NODE_ACTIVITY);
                            for (Element act : acts) {
                                List<Element> intents = extractChildrenByName(act, NODE_INTENT);
                                for (Element intent : intents) {
                                    List<Element> data = extractChildrenByName(intent, NODE_DATA);
                                    if (!data.isEmpty() && act.hasAttributeNS(ANDROID_URI, ATTRIBUTE_NAME)) {
                                        Attr attr = act.getAttributeNodeNS(ANDROID_URI, ATTRIBUTE_NAME);
                                        String activityName = attr.getValue();
                                        int dotIndex = activityName.indexOf('.');
                                        if (dotIndex <= 0) {
                                            String pkg = context.getMainProject().getPackage();
                                            if (pkg != null) {
                                                if (dotIndex == 0) {
                                                    activityName = pkg + activityName;
                                                } else {
                                                    activityName = pkg + '.' + activityName;
                                                }
                                            }
                                        }
                                        activitiesToCheck.add(activityName);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return activitiesToCheck;
}
Also used : XmlParser(com.android.tools.klint.client.api.XmlParser) UElement(org.jetbrains.uast.UElement) PsiElement(com.intellij.psi.PsiElement) Element(org.w3c.dom.Element) XmlContext(com.android.tools.klint.detector.api.XmlContext) Document(org.w3c.dom.Document) File(java.io.File) Attr(org.w3c.dom.Attr)

Aggregations

XmlParser (com.android.tools.klint.client.api.XmlParser)1 XmlContext (com.android.tools.klint.detector.api.XmlContext)1 PsiElement (com.intellij.psi.PsiElement)1 File (java.io.File)1 UElement (org.jetbrains.uast.UElement)1 Attr (org.w3c.dom.Attr)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1