Search in sources :

Example 41 with ResourceType

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

the class ResourceTypeClassBase method buildResourceFields.

@NotNull
static PsiField[] buildResourceFields(@NotNull ResourceManager manager, boolean nonFinal, @NotNull String resClassName, @NotNull final PsiClass context) {
    ResourceType resourceType = ResourceType.getEnum(resClassName);
    if (resourceType == null) {
        return PsiField.EMPTY_ARRAY;
    }
    final Map<String, PsiType> fieldNames = new HashMap<String, PsiType>();
    final boolean styleable = ResourceType.STYLEABLE == resourceType;
    final PsiType basicType = styleable ? PsiType.INT.createArrayType() : PsiType.INT;
    for (String resName : manager.getResourceNames(resourceType)) {
        fieldNames.put(resName, basicType);
    }
    if (styleable) {
        for (ResourceEntry entry : manager.getValueResourceEntries(ResourceType.ATTR)) {
            final String resName = entry.getName();
            final String resContext = entry.getContext();
            if (resContext.length() > 0) {
                fieldNames.put(resContext + '_' + resName, PsiType.INT);
            }
        }
    }
    final PsiField[] result = new PsiField[fieldNames.size()];
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.getProject());
    int idIterator = resourceType.ordinal() * 100000;
    int i = 0;
    for (Map.Entry<String, PsiType> entry : fieldNames.entrySet()) {
        final String fieldName = AndroidResourceUtil.getFieldNameByResourceName(entry.getKey());
        final PsiType type = entry.getValue();
        final int id = -(idIterator++);
        final AndroidLightField field = new AndroidLightField(fieldName, context, type, !nonFinal, nonFinal ? null : id);
        field.setInitializer(factory.createExpressionFromText(Integer.toString(id), field));
        result[i++] = field;
    }
    return result;
}
Also used : HashMap(com.intellij.util.containers.HashMap) ResourceType(com.android.resources.ResourceType) ResourceEntry(org.jetbrains.android.util.ResourceEntry) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with ResourceType

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

the class ResourceManager method findValueResourceInfos.

@NotNull
public List<ValueResourceInfoImpl> findValueResourceInfos(@NotNull String resourceType, @NotNull final String resourceName, final boolean distinguishDelimetersInName, boolean searchAttrs) {
    final ResourceType type = resourceType.startsWith("+") ? ResourceType.ID : ResourceType.getEnum(resourceType);
    if (type == null || !AndroidResourceUtil.VALUE_RESOURCE_TYPES.contains(type) && (type != ResourceType.ATTR || !searchAttrs)) {
        return Collections.emptyList();
    }
    final GlobalSearchScope scope = GlobalSearchScope.allScope(myProject);
    final List<ValueResourceInfoImpl> result = new ArrayList<ValueResourceInfoImpl>();
    final Set<VirtualFile> valueResourceFiles = getAllValueResourceFiles();
    FileBasedIndex.getInstance().processValues(AndroidValueResourcesIndex.INDEX_ID, AndroidValueResourcesIndex.createTypeNameMarkerKey(resourceType, resourceName), null, new FileBasedIndex.ValueProcessor<ImmutableSet<AndroidValueResourcesIndex.MyResourceInfo>>() {

        @Override
        public boolean process(@NotNull VirtualFile file, ImmutableSet<AndroidValueResourcesIndex.MyResourceInfo> infos) {
            for (AndroidValueResourcesIndex.MyResourceInfo info : infos) {
                final String name = info.getResourceEntry().getName();
                if (AndroidUtils.equal(resourceName, name, distinguishDelimetersInName)) {
                    if (valueResourceFiles.contains(file)) {
                        result.add(new ValueResourceInfoImpl(info.getResourceEntry().getName(), type, file, myProject, info.getOffset()));
                    }
                }
            }
            return true;
        }
    }, scope);
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceType(com.android.resources.ResourceType) ImmutableSet(com.google.common.collect.ImmutableSet) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) AndroidValueResourcesIndex(org.jetbrains.android.AndroidValueResourcesIndex) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) NotNull(org.jetbrains.annotations.NotNull)

Example 43 with ResourceType

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

the class PrivateResourceDetector method visitElement.

@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    if (TAG_RESOURCES.equals(element.getTagName())) {
        for (Element item : LintUtils.getChildren(element)) {
            Attr nameAttribute = item.getAttributeNode(ATTR_NAME);
            if (nameAttribute != null) {
                String name = getResourceFieldName(nameAttribute.getValue());
                String type = item.getTagName();
                if (type.equals(TAG_ITEM)) {
                    type = item.getAttribute(ATTR_TYPE);
                    if (type == null || type.isEmpty()) {
                        type = RESOURCE_CLZ_ID;
                    }
                } else if (type.equals("declare-styleable")) {
                    //$NON-NLS-1$
                    type = RESOURCE_CLR_STYLEABLE;
                } else if (type.contains("array")) {
                    //$NON-NLS-1$
                    // <string-array> etc
                    type = RESOURCE_CLZ_ARRAY;
                }
                ResourceType t = ResourceType.getEnum(type);
                if (t != null && isPrivate(context, t, name) && !VALUE_TRUE.equals(item.getAttributeNS(TOOLS_URI, ATTR_OVERRIDE))) {
                    String message = createOverrideErrorMessage(context, t, name);
                    Location location = context.getValueLocation(nameAttribute);
                    context.report(ISSUE, nameAttribute, location, message);
                }
            }
        }
    } else {
        assert TAG_STYLE.equals(element.getTagName()) || TAG_ARRAY.equals(element.getTagName()) || TAG_PLURALS.equals(element.getTagName()) || TAG_INTEGER_ARRAY.equals(element.getTagName()) || TAG_STRING_ARRAY.equals(element.getTagName());
        for (Element item : LintUtils.getChildren(element)) {
            checkChildRefs(context, item);
        }
    }
}
Also used : UElement(org.jetbrains.uast.UElement) Element(org.w3c.dom.Element) ResourceType(com.android.resources.ResourceType) Attr(org.w3c.dom.Attr) Location(com.android.tools.klint.detector.api.Location)

Example 44 with ResourceType

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

the class PrivateResourceDetector method beforeCheckFile.

@Override
public void beforeCheckFile(@NonNull Context context) {
    File file = context.file;
    boolean isXmlFile = LintUtils.isXmlFile(file);
    if (!isXmlFile && !LintUtils.isBitmapFile(file)) {
        return;
    }
    String parentName = file.getParentFile().getName();
    int dash = parentName.indexOf('-');
    if (dash != -1 || FD_RES_VALUES.equals(parentName)) {
        return;
    }
    ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
    if (folderType == null) {
        return;
    }
    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
    if (types.isEmpty()) {
        return;
    }
    ResourceType type = types.get(0);
    String resourceName = getResourceFieldName(getBaseName(file.getName()));
    if (isPrivate(context, type, resourceName)) {
        String message = createOverrideErrorMessage(context, type, resourceName);
        Location location = Location.create(file);
        context.report(ISSUE, location, message);
    }
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) ResourceType(com.android.resources.ResourceType) File(java.io.File) Location(com.android.tools.klint.detector.api.Location)

Example 45 with ResourceType

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

the class ChooseResourceDialog method createNewResourcePopupMenu.

private ActionPopupMenu createNewResourcePopupMenu() {
    ActionManager actionManager = ActionManager.getInstance();
    DefaultActionGroup actionGroup = new DefaultActionGroup();
    ResourcePanel panel = getSelectedPanel();
    ResourceType resourceType = panel.getType();
    ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(resourceType);
    if (folderType != null) {
        AnAction newFileAction = createNewResourceFileAction();
        newFileAction.getTemplatePresentation().setText("New " + folderType.getName() + " File...");
        newFileAction.getTemplatePresentation().putClientProperty(FOLDER_TYPE_KEY, folderType);
        actionGroup.add(newFileAction);
    }
    if (AndroidResourceUtil.VALUE_RESOURCE_TYPES.contains(resourceType)) {
        String title = "New " + resourceType + " Value...";
        if (resourceType == ResourceType.LAYOUT) {
            title = "New Layout Alias";
        }
        AnAction newValueAction = createNewResourceValueAction();
        newValueAction.getTemplatePresentation().setText(title);
        newValueAction.getTemplatePresentation().putClientProperty(TYPE_KEY, resourceType);
        actionGroup.add(newValueAction);
    }
    if (myTag != null && ResourceType.STYLE.equals(resourceType)) {
        final boolean enabled = AndroidBaseLayoutRefactoringAction.getLayoutViewElement(myTag) != null && AndroidExtractStyleAction.doIsEnabled(myTag);
        AnAction extractStyleAction = createExtractStyleAction();
        extractStyleAction.getTemplatePresentation().setEnabled(enabled);
        actionGroup.add(extractStyleAction);
    }
    if (GraphicalResourceRendererEditor.COLORS_AND_DRAWABLES.contains(resourceType)) {
        AnAction newReferenceAction = createNewResourceReferenceAction();
        newReferenceAction.getTemplatePresentation().setText("New " + resourceType + " Reference...");
        actionGroup.add(newReferenceAction);
    }
    return actionManager.createActionPopupMenu(ActionPlaces.UNKNOWN, actionGroup);
}
Also used : CreateXmlResourcePanel(org.jetbrains.android.actions.CreateXmlResourcePanel) ResourceFolderType(com.android.resources.ResourceFolderType) ResourceType(com.android.resources.ResourceType)

Aggregations

ResourceType (com.android.resources.ResourceType)137 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)31 NotNull (org.jetbrains.annotations.NotNull)16 Field (java.lang.reflect.Field)13 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)12 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 Map (java.util.Map)10 Nullable (org.jetbrains.annotations.Nullable)10 Nullable (com.android.annotations.Nullable)8 ResourceFolderType (com.android.resources.ResourceFolderType)8 File (java.io.File)8 EnumMap (java.util.EnumMap)7 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)7 Context (android.content.Context)6 View (android.view.View)6 AbsListView (android.widget.AbsListView)6 AdapterView (android.widget.AdapterView)6 ExpandableListView (android.widget.ExpandableListView)6 FrameLayout (android.widget.FrameLayout)6