Search in sources :

Example 21 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class AndroidXmlSchemaProvider method isXmlResourceFile.

private static boolean isXmlResourceFile(XmlFile file) {
    if (!AndroidResourceUtil.isInResourceSubdirectory(file, null)) {
        return false;
    }
    final PsiDirectory parent = file.getParent();
    if (parent == null) {
        return false;
    }
    final ResourceFolderType resType = ResourceFolderType.getFolderType(parent.getName());
    if (resType == null) {
        return false;
    }
    if (resType == ResourceFolderType.XML) {
        return XmlResourceDomFileDescription.isXmlResourceFile(file);
    }
    return resType != ResourceFolderType.RAW;
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) PsiDirectory(com.intellij.psi.PsiDirectory)

Example 22 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class AndroidResourceFileSafeDeleteProcessor method getAdditionalElementsToDelete.

@Nullable
@Override
public Collection<PsiElement> getAdditionalElementsToDelete(@NotNull PsiElement element, @NotNull Collection<PsiElement> allElementsToDelete, boolean askUser) {
    if (allElementsToDelete.size() > 1) {
        // todo: support this case (we should ask once)
        return Collections.emptyList();
    }
    final AndroidFacet facet = AndroidFacet.getInstance(element);
    assert facet != null;
    final PsiFile file = (PsiFile) element;
    final VirtualFile vFile = file.getVirtualFile();
    assert vFile != null;
    final VirtualFile dir = vFile.getParent();
    assert dir != null;
    ResourceFolderType folderType = ResourceFolderType.getFolderType(dir.getName());
    if (folderType == null) {
        return Collections.emptyList();
    }
    final String type = folderType.getName();
    final String name = vFile.getName();
    final List<PsiFile> resourceFiles = facet.getLocalResourceManager().findResourceFiles(type, AndroidCommonUtils.getResourceName(type, name), true, false);
    final List<PsiElement> result = new ArrayList<PsiElement>();
    for (PsiFile resourceFile : resourceFiles) {
        if (!resourceFile.getManager().areElementsEquivalent(file, resourceFile) && resourceFile.getName().equals(name)) {
            result.add(resourceFile);
        }
    }
    if (result.size() > 0 && askUser) {
        final int r = Messages.showDialog(element.getProject(), "Delete alternative resource files for other configurations?", "Delete", new String[] { Messages.YES_BUTTON, Messages.NO_BUTTON }, 1, Messages.getQuestionIcon());
        if (r != Messages.YES) {
            return Collections.emptyList();
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFolderType(com.android.resources.ResourceFolderType) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class ResourceQualifierSwitcher method collectQualifiers.

private static BidirectionalMap<String, VirtualFile> collectQualifiers(VirtualFile resDirectory, VirtualFile file) {
    BidirectionalMap<String, VirtualFile> result = new BidirectionalMap<String, VirtualFile>();
    ResourceFolderType type = ResourceHelper.getFolderType(file);
    for (VirtualFile dir : resDirectory.getChildren()) {
        ResourceFolderType otherType = ResourceFolderType.getFolderType(dir.getName());
        if (otherType == type) {
            VirtualFile fileWithQualifier = dir.findChild(file.getName());
            if (fileWithQualifier != null) {
                String childName = dir.getName();
                int dashPos = childName.indexOf('-');
                String qualifier = dashPos > 0 ? childName.substring(dashPos + 1) : "<default>";
                result.put(qualifier, fileWithQualifier);
            }
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFolderType(com.android.resources.ResourceFolderType) BidirectionalMap(com.intellij.util.containers.BidirectionalMap)

Example 24 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project kotlin by JetBrains.

the class ApiDetector method visitElement.

@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    if (mApiDatabase == null) {
        return;
    }
    String tag = element.getTagName();
    ResourceFolderType folderType = context.getResourceFolderType();
    if (folderType != ResourceFolderType.LAYOUT) {
        if (folderType == ResourceFolderType.DRAWABLE) {
            checkElement(context, element, TAG_VECTOR, 21, "1.4", UNSUPPORTED);
            checkElement(context, element, TAG_RIPPLE, 21, null, UNSUPPORTED);
            checkElement(context, element, TAG_ANIMATED_SELECTOR, 21, null, UNSUPPORTED);
            checkElement(context, element, TAG_ANIMATED_VECTOR, 21, null, UNSUPPORTED);
            checkElement(context, element, "drawable", 24, null, UNSUPPORTED);
            if ("layer-list".equals(tag)) {
                checkLevelList(context, element);
            } else if (tag.contains(".")) {
                checkElement(context, element, tag, 24, null, UNSUPPORTED);
            }
        }
        if (element.getParentNode().getNodeType() != Node.ELEMENT_NODE) {
            // Root node
            return;
        }
        NodeList childNodes = element.getChildNodes();
        for (int i = 0, n = childNodes.getLength(); i < n; i++) {
            Node textNode = childNodes.item(i);
            if (textNode.getNodeType() == Node.TEXT_NODE) {
                String text = textNode.getNodeValue();
                if (text.contains(ANDROID_PREFIX)) {
                    text = text.trim();
                    // Convert @android:type/foo into android/R$type and "foo"
                    int index = text.indexOf('/', ANDROID_PREFIX.length());
                    if (index != -1) {
                        String typeString = text.substring(ANDROID_PREFIX.length(), index);
                        if (ResourceType.getEnum(typeString) != null) {
                            String owner = "android/R$" + typeString;
                            String name = getResourceFieldName(text.substring(index + 1));
                            int api = mApiDatabase.getFieldVersion(owner, name);
                            int minSdk = getMinSdk(context);
                            if (api > minSdk && api > context.getFolderVersion() && api > getLocalMinSdk(element)) {
                                Location location = context.getLocation(textNode);
                                String message = String.format("`%1$s` requires API level %2$d (current min is %3$d)", text, api, minSdk);
                                context.report(UNSUPPORTED, element, location, message);
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (VIEW_TAG.equals(tag)) {
            tag = element.getAttribute(ATTR_CLASS);
            if (tag == null || tag.isEmpty()) {
                return;
            }
        } else {
            // TODO: Complain if <tag> is used at the root level!
            checkElement(context, element, TAG, 21, null, UNUSED);
        }
        // Check widgets to make sure they're available in this version of the SDK.
        if (tag.indexOf('.') != -1) {
            // Custom views aren't in the index
            return;
        }
        //$NON-NLS-1$
        String fqn = "android/widget/" + tag;
        if (tag.equals("TextureView")) {
            //$NON-NLS-1$
            //$NON-NLS-1$
            fqn = "android/view/TextureView";
        }
        // TODO: Consider other widgets outside of android.widget.*
        int api = mApiDatabase.getClassVersion(fqn);
        int minSdk = getMinSdk(context);
        if (api > minSdk && api > context.getFolderVersion() && api > getLocalMinSdk(element)) {
            Location location = context.getLocation(element);
            String message = String.format("View requires API level %1$d (current min is %2$d): `<%3$s>`", api, minSdk, tag);
            context.report(UNSUPPORTED, element, location, message);
        }
    }
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 25 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project kotlin by JetBrains.

the class OverdrawDetector method beforeCheckFile.

@Override
public void beforeCheckFile(@NonNull Context context) {
    if (endsWith(context.file.getName(), DOT_XML)) {
        // Drawable XML files should not be considered for overdraw, except for <bitmap>'s.
        // The bitmap elements are handled in the scanBitmap() method; it will clear
        // out anything added by this method.
        File parent = context.file.getParentFile();
        ResourceFolderType type = ResourceFolderType.getFolderType(parent.getName());
        if (type == ResourceFolderType.DRAWABLE) {
            if (mValidDrawables == null) {
                mValidDrawables = new ArrayList<String>();
            }
            String resource = getDrawableResource(context.file);
            mValidDrawables.add(resource);
        }
    }
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) File(java.io.File)

Aggregations

ResourceFolderType (com.android.resources.ResourceFolderType)46 VirtualFile (com.intellij.openapi.vfs.VirtualFile)12 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)10 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 NotNull (org.jetbrains.annotations.NotNull)10 ResourceType (com.android.resources.ResourceType)8 XmlFile (com.intellij.psi.xml.XmlFile)8 File (java.io.File)8 PsiFile (com.intellij.psi.PsiFile)7 Nullable (org.jetbrains.annotations.Nullable)7 PsiDirectory (com.intellij.psi.PsiDirectory)6 Module (com.intellij.openapi.module.Module)5 XmlTag (com.intellij.psi.xml.XmlTag)5 Configuration (com.android.tools.idea.configurations.Configuration)3 PsiElement (com.intellij.psi.PsiElement)3 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)2 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)2 HtmlBuilder (com.android.utils.HtmlBuilder)2 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 Project (com.intellij.openapi.project.Project)2