Search in sources :

Example 1 with ResourceFolderType

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

the class LintDriver method checkIndividualResources.

/** Checks individual resources */
private void checkIndividualResources(@NonNull Project project, @Nullable Project main, @NonNull List<ResourceXmlDetector> xmlDetectors, @Nullable List<Detector> dirChecks, @Nullable List<Detector> binaryChecks, @NonNull List<File> files) {
    for (File file : files) {
        if (file.isDirectory()) {
            // Is it a resource folder?
            ResourceFolderType type = ResourceFolderType.getFolderType(file.getName());
            if (type != null && new File(file.getParentFile(), RES_FOLDER).exists()) {
                // Yes.
                checkResourceFolder(project, main, file, type, xmlDetectors, dirChecks, binaryChecks);
            } else if (file.getName().equals(RES_FOLDER)) {
                // Is it the res folder?
                // Yes
                checkResFolder(project, main, file, xmlDetectors, dirChecks, binaryChecks);
            } else {
                mClient.log(null, "Unexpected folder %1$s; should be project, " + "\"res\" folder or resource folder", file.getPath());
            }
        } else if (file.isFile() && LintUtils.isXmlFile(file)) {
            // Yes, find out its resource type
            String folderName = file.getParentFile().getName();
            ResourceFolderType type = ResourceFolderType.getFolderType(folderName);
            if (type != null) {
                ResourceVisitor visitor = getVisitor(type, xmlDetectors, binaryChecks);
                if (visitor != null) {
                    XmlContext context = new XmlContext(this, project, main, file, type, visitor.getParser());
                    fireEvent(EventType.SCANNING_FILE, context);
                    visitor.visitFile(context, file);
                }
            }
        } else if (binaryChecks != null && file.isFile() && LintUtils.isBitmapFile(file)) {
            // Yes, find out its resource type
            String folderName = file.getParentFile().getName();
            ResourceFolderType type = ResourceFolderType.getFolderType(folderName);
            if (type != null) {
                ResourceVisitor visitor = getVisitor(type, xmlDetectors, binaryChecks);
                if (visitor != null) {
                    ResourceContext context = new ResourceContext(this, project, main, file, type);
                    fireEvent(EventType.SCANNING_FILE, context);
                    visitor.visitBinaryResource(context);
                    if (mCanceled) {
                        return;
                    }
                }
            }
        }
    }
}
Also used : ResourceContext(com.android.tools.klint.detector.api.ResourceContext) ResourceFolderType(com.android.resources.ResourceFolderType) XmlContext(com.android.tools.klint.detector.api.XmlContext) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Example 2 with ResourceFolderType

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

the class PsiResourceItem method getSource.

@Nullable
@Override
public ResourceFile getSource() {
    ResourceFile source = super.getSource();
    // Temporary safety workaround
    if (source == null && myFile != null && myFile.getParent() != null) {
        PsiDirectory parent = myFile.getParent();
        if (parent != null) {
            String name = parent.getName();
            ResourceFolderType folderType = ResourceFolderType.getFolderType(name);
            FolderConfiguration configuration = FolderConfiguration.getConfigForFolder(name);
            int index = name.indexOf('-');
            String qualifiers = index == -1 ? "" : name.substring(index + 1);
            source = new PsiResourceFile(myFile, Collections.<ResourceItem>singletonList(this), qualifiers, folderType, configuration);
            setSource(source);
        }
    }
    return source;
}
Also used : ResourceFile(com.android.ide.common.res2.ResourceFile) ResourceFolderType(com.android.resources.ResourceFolderType) PsiDirectory(com.intellij.psi.PsiDirectory) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ResourceFolderType

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

the class AndroidResourceReference method handleElementRename.

@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    if (newElementName.startsWith(SdkConstants.NEW_ID_PREFIX)) {
        newElementName = AndroidResourceUtil.getResourceNameByReferenceText(newElementName);
    }
    ResourceValue value = myValue.getValue();
    assert value != null;
    final ResourceType resType = value.getType();
    if (resType != null && newElementName != null) {
        // todo: do not allow new value resource name to contain dot, because it is impossible to check if it file or value otherwise
        final String newResName;
        // Does renamed resource point to a file?
        ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(resType);
        if (folderType != null && newElementName.contains(".")) {
            // If it does, we need to chop off its extension when inserting the new value.
            newResName = AndroidCommonUtils.getResourceName(resType.getName(), newElementName);
        } else {
            newResName = newElementName;
        }
        // Note: We're using value.getResourceType(), not resType.getName() here, because we want the "+" in the new name
        myValue.setValue(ResourceValue.referenceTo(value.getPrefix(), value.getNamespace(), value.getResourceType(), newResName));
    }
    return myValue.getXmlTag();
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 4 with ResourceFolderType

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

the class AndroidResourceUtil method findResourceFieldsForValueResource.

@NotNull
public static PsiField[] findResourceFieldsForValueResource(XmlTag tag, boolean onlyInOwnPackages) {
    final AndroidFacet facet = AndroidFacet.getInstance(tag);
    if (facet == null) {
        return PsiField.EMPTY_ARRAY;
    }
    ResourceFolderType fileResType = ResourceHelper.getFolderType(tag.getContainingFile());
    final String resourceType = fileResType == ResourceFolderType.VALUES ? getResourceTypeByValueResourceTag(tag) : null;
    if (resourceType == null) {
        return PsiField.EMPTY_ARRAY;
    }
    String name = tag.getAttributeValue(ATTR_NAME);
    if (name == null) {
        return PsiField.EMPTY_ARRAY;
    }
    return findResourceFields(facet, resourceType, name, onlyInOwnPackages);
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ResourceFolderType

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

the class ThemeEditorUtils method copyTheme.

/**
   * Copies a theme to a values folder with api version apiLevel,
   * potentially creating the necessary folder or file.
   * @param apiLevel api level of the folder the theme is copied to
   * @param toBeCopied theme to be copied
   */
public static void copyTheme(int apiLevel, @NotNull final XmlTag toBeCopied) {
    ApplicationManager.getApplication().assertWriteAccessAllowed();
    PsiFile file = toBeCopied.getContainingFile();
    assert file instanceof XmlFile : file;
    ResourceFolderType folderType = ResourceHelper.getFolderType(file);
    assert folderType != null : file;
    FolderConfiguration config = ResourceHelper.getFolderConfiguration(file);
    assert config != null : file;
    VersionQualifier qualifier = new VersionQualifier(apiLevel);
    config.setVersionQualifier(qualifier);
    String folder = config.getFolderName(folderType);
    if (folderType != ResourceFolderType.VALUES) {
        OverrideResourceAction.forkResourceFile((XmlFile) file, folder, false);
    } else {
        XmlTag tag = OverrideResourceAction.getValueTag(PsiTreeUtil.getParentOfType(toBeCopied, XmlTag.class, false));
        if (tag != null) {
            PsiDirectory dir = null;
            PsiDirectory resFolder = file.getParent();
            if (resFolder != null) {
                resFolder = resFolder.getParent();
            }
            if (resFolder != null) {
                dir = resFolder.findSubdirectory(folder);
                if (dir == null) {
                    dir = resFolder.createSubdirectory(folder);
                }
            }
            OverrideResourceAction.forkResourceValue(toBeCopied.getProject(), tag, file, dir, false);
        }
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) ResourceFolderType(com.android.resources.ResourceFolderType) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) XmlTag(com.intellij.psi.xml.XmlTag)

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