Search in sources :

Example 41 with ResourceFolderType

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

the class ChooseResourceDialog method ensurePickersInitialized.

private void ensurePickersInitialized() {
    boolean allowDrawables = allowDrawables();
    boolean allowColors = allowColors();
    if (allowColors || allowDrawables) {
        if (myStateListPicker != null || myColorPicker != null) {
            return;
        }
        Configuration configuration = getConfiguration();
        ResourceResolver resolver = configuration.getResourceResolver();
        assert resolver != null;
        ResourceValue resValue = null;
        if (myInitialValue != null) {
            resValue = resolver.findResValue(myInitialValue, myInitialValueIsFramework);
        }
        final ResourceType stateListType;
        final ResourceFolderType stateListFolderType;
        if (allowDrawables) {
            stateListType = ResourceType.DRAWABLE;
            stateListFolderType = ResourceFolderType.DRAWABLE;
        } else {
            stateListType = ResourceType.COLOR;
            stateListFolderType = ResourceFolderType.COLOR;
        }
        initializeStateListPicker(configuration, resolver, resValue, stateListType, stateListFolderType);
        initializeColorPicker(myInitialValue, myResourceNameVisibility, resolver, resValue);
    }
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) ResourceFolderType(com.android.resources.ResourceFolderType) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 42 with ResourceFolderType

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

the class ChooseResourceDialog method createNewResourceFileAction.

@NotNull
private AnAction createNewResourceFileAction() {
    return new AnAction() {

        @Override
        public void actionPerformed(AnActionEvent e) {
            ResourceFolderType type = (ResourceFolderType) getTemplatePresentation().getClientProperty(FOLDER_TYPE_KEY);
            createNewResourceFile(type);
        }
    };
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) NotNull(org.jetbrains.annotations.NotNull)

Example 43 with ResourceFolderType

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

the class IncludeTagCreator method createNewIncludedLayout.

/**
   * Create a new layout that will be included as a child of the mockup component
   */
private String createNewIncludedLayout() {
    AndroidFacet facet = getMockup().getComponent().getModel().getFacet();
    ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(ResourceType.LAYOUT);
    XmlFile newFile = CreateResourceFileAction.createFileResource(facet, folderType, null, null, null, true, null, null, null, false);
    if (newFile == null) {
        return null;
    }
    XmlTag rootTag = newFile.getRootTag();
    if (rootTag == null) {
        return null;
    }
    NlModel nlModel = NlModel.create(getScreenView().getSurface(), newFile.getProject(), facet, newFile);
    ModelListener listener = new ModelListener() {

        @Override
        public void modelChanged(@NotNull NlModel model) {
        }

        @Override
        public void modelRendered(@NotNull NlModel model) {
            model.removeListener(this);
            if (model.getComponents().isEmpty()) {
                return;
            }
            NlComponent component = model.getComponents().get(0);
            final AttributesTransaction transaction = component.startAttributeTransaction();
            addShowInAttribute(transaction);
            addSizeAttributes(transaction, getAndroidBounds());
            addMockupAttributes(transaction, getSelectionBounds());
            WriteCommandAction.runWriteCommandAction(model.getProject(), (Computable) transaction::commit);
        }

        @Override
        public void modelChangedOnLayout(@NotNull NlModel model, boolean animate) {
        // Do nothing
        }
    };
    nlModel.addListener(listener);
    nlModel.requestRender();
    return getResourceName(newFile);
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ResourceFolderType(com.android.resources.ResourceFolderType) ModelListener(com.android.tools.idea.uibuilder.model.ModelListener) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) XmlTag(com.intellij.psi.xml.XmlTag)

Example 44 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 45 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