Search in sources :

Example 91 with ResourceType

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

the class ViewLoader method parseClass.

private static boolean parseClass(@NotNull Class<?> rClass, @NotNull TIntObjectHashMap<Pair<ResourceType, String>> id2res, @NotNull Map<IntArrayWrapper, String> styleableId2Res, @NotNull Map<ResourceType, TObjectIntHashMap<String>> res2id) throws ClassNotFoundException {
    try {
        final Class<?>[] nestedClasses;
        try {
            nestedClasses = rClass.getDeclaredClasses();
        } catch (LinkageError e) {
            final Throwable cause = e.getCause();
            LOG.debug(e);
            if (cause instanceof ClassNotFoundException) {
                throw (ClassNotFoundException) cause;
            }
            throw e;
        }
        for (Class<?> resClass : nestedClasses) {
            final ResourceType resType = ResourceType.getEnum(resClass.getSimpleName());
            if (resType == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("  '%s' is not a valid resource type", anonymizeClassName(resClass.getSimpleName())));
                }
                continue;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("  Defining resource type '%s'", anonymizeClassName(resClass.getSimpleName())));
            }
            final TObjectIntHashMap<String> resName2Id = new TObjectIntHashMap<>();
            res2id.put(resType, resName2Id);
            for (Field field : resClass.getDeclaredFields()) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    // May not be final in library projects
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' field is not static, skipping", field.getName()));
                    }
                    continue;
                }
                final Class<?> type = field.getType();
                if (type.isArray() && type.getComponentType() == int.class) {
                    styleableId2Res.put(new IntArrayWrapper((int[]) field.get(null)), field.getName());
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' defined as int[]", field.getName()));
                    }
                } else if (type == int.class) {
                    final Integer value = (Integer) field.get(null);
                    id2res.put(value, Pair.of(resType, field.getName()));
                    resName2Id.put(field.getName(), value);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' defined as int", field.getName()));
                    }
                } else {
                    LOG.error("Unknown field type in R class: " + type);
                }
            }
        }
    } catch (IllegalAccessException e) {
        LOG.info(e);
        return false;
    }
    return true;
}
Also used : ResourceType(com.android.resources.ResourceType) Field(java.lang.reflect.Field) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) PsiClass(com.intellij.psi.PsiClass) IntArrayWrapper(com.android.ide.common.resources.IntArrayWrapper)

Example 92 with ResourceType

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

the class InferSupportAnnotations method getResourceTypeConstraints.

@Nullable
private Constraints getResourceTypeConstraints(PsiModifierListOwner owner, boolean inHierarchy) {
    Constraints constraints = null;
    for (PsiAnnotation annotation : AnnotationUtil.getAllAnnotations(owner, inHierarchy, null)) {
        String qualifiedName = annotation.getQualifiedName();
        if (qualifiedName == null) {
            continue;
        }
        ResourceType type = null;
        if (qualifiedName.startsWith(SUPPORT_ANNOTATIONS_PREFIX) && qualifiedName.endsWith(RES_SUFFIX)) {
            String name = qualifiedName.substring(SUPPORT_ANNOTATIONS_PREFIX.length(), qualifiedName.length() - RES_SUFFIX.length());
            type = ResourceType.getEnum(name.toLowerCase(Locale.US));
        } else if (qualifiedName.equals(COLOR_INT_ANNOTATION)) {
            type = COLOR_INT_MARKER_TYPE;
        } else if (qualifiedName.equals(PX_ANNOTATION)) {
            type = DIMENSION_MARKER_TYPE;
        }
        if (type != null) {
            if (constraints == null) {
                constraints = new Constraints();
            }
            constraints.addResourceType(type);
        }
    }
    final SmartPsiElementPointer<PsiModifierListOwner> pointer = myPointerManager.createSmartPsiElementPointer(owner);
    Constraints existing = myConstraints.get(pointer);
    if (existing != null) {
        if (constraints != null) {
            constraints.merge(existing);
            return constraints;
        }
        return existing;
    }
    return constraints;
}
Also used : ResourceType(com.android.resources.ResourceType) Nullable(org.jetbrains.annotations.Nullable)

Example 93 with ResourceType

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

the class OverrideResourceAction method forkResourceValue.

/**
   * Create a variation (copy) of a given resource.
   * @param project Current project
   * @param tag Resource to be copied
   * @param file File containing the resource
   * @param dir Directory to contain the new resource, or null to ask the user
   * @param open if true, open the file containing the new resource
   */
public static void forkResourceValue(@NotNull Project project, @NotNull XmlTag tag, @NotNull PsiFile file, @Nullable PsiDirectory dir, boolean open) {
    PsiDirectory resFolder = findRes(file);
    if (resFolder == null) {
        // shouldn't happen; we checked in isAvailable
        return;
    }
    String name = tag.getAttributeValue(ATTR_NAME);
    ResourceType type = AndroidResourceUtil.getResourceForResourceTag(tag);
    if (name == null || type == null) {
        // shouldn't happen; we checked in isAvailable
        return;
    }
    if (dir == null) {
        dir = selectFolderDir(project, resFolder.getVirtualFile(), ResourceFolderType.VALUES);
    }
    if (dir != null) {
        String value = ResourceHelper.getTextContent(tag).trim();
        createValueResource(project, resFolder, file, dir, name, value, type, tag.getText(), open);
    }
}
Also used : ResourceType(com.android.resources.ResourceType)

Example 94 with ResourceType

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

the class ResourceFolderRepository method equalFilesItems.

// Not a super-robust comparator. Compares a subset of the repo state for testing.
// Returns false if a difference is found.
// Returns true if the repositories are roughly equivalent.
@VisibleForTesting
boolean equalFilesItems(ResourceFolderRepository other) {
    File myResourceDirFile = VfsUtilCore.virtualToIoFile(myResourceDir);
    File otherResourceDir = VfsUtilCore.virtualToIoFile(other.myResourceDir);
    if (!FileUtil.filesEqual(myResourceDirFile, otherResourceDir)) {
        return false;
    }
    if (myResourceFiles.size() != other.myResourceFiles.size()) {
        return false;
    }
    for (Map.Entry<VirtualFile, ResourceFile> fileEntry : myResourceFiles.entrySet()) {
        ResourceFile otherResFile = other.myResourceFiles.get(fileEntry.getKey());
        if (otherResFile == null) {
            return false;
        }
        if (!FileUtil.filesEqual(fileEntry.getValue().getFile(), otherResFile.getFile())) {
            return false;
        }
    }
    if (myItems.size() != other.myItems.size()) {
        return false;
    }
    for (Map.Entry<ResourceType, ListMultimap<String, ResourceItem>> entry : myItems.entrySet()) {
        ListMultimap<String, ResourceItem> ownEntries = entry.getValue();
        ListMultimap<String, ResourceItem> otherEntries = other.myItems.get(entry.getKey());
        if (otherEntries == null || otherEntries.size() != ownEntries.size()) {
            return false;
        }
        for (Map.Entry<String, ResourceItem> itemEntry : ownEntries.entries()) {
            List<ResourceItem> otherItemsList = otherEntries.get(itemEntry.getKey());
            if (otherItemsList == null) {
                return false;
            }
            final ResourceItem item = itemEntry.getValue();
            if (!ContainerUtil.exists(otherItemsList, new Condition<ResourceItem>() {

                @Override
                public boolean value(ResourceItem resourceItem) {
                    // Use #compareTo instead of #equals because #equals compares pointers of mSource.
                    if (resourceItem.compareTo(item) != 0) {
                        return false;
                    }
                    // Skip ID type resources, where the ResourceValues are not important and where blob writing doesn't preserve the value.
                    if (item.getType() != ResourceType.ID) {
                        ResourceValue resValue = item.getResourceValue(false);
                        ResourceValue otherResValue = resourceItem.getResourceValue(false);
                        if (resValue == null || otherResValue == null) {
                            if (resValue != otherResValue) {
                                return false;
                            }
                        } else {
                            String resValueStr = resValue.getValue();
                            String otherResValueStr = otherResValue.getValue();
                            if (resValueStr == null || otherResValueStr == null) {
                                if (resValueStr != otherResValueStr) {
                                    return false;
                                }
                            } else {
                                if (!resValueStr.equals(otherResValueStr)) {
                                    return false;
                                }
                            }
                        }
                    }
                    // We can only compareValueWith (compare equivalence of XML nodes) for VALUE items.
                    // For others, the XML node may be different before and after serialization.
                    ResourceFile source = item.getSource();
                    ResourceFile otherSource = resourceItem.getSource();
                    if (source != null && otherSource != null) {
                        ResourceFolderType ownFolderType = ResourceHelper.getFolderType(source);
                        ResourceFolderType otherFolderType = ResourceHelper.getFolderType(otherSource);
                        if (otherFolderType != ownFolderType) {
                            return false;
                        }
                        if (otherFolderType == VALUES) {
                            return resourceItem.compareValueWith(item);
                        }
                    }
                    return true;
                }
            })) {
                return false;
            }
        }
    }
    // Only compare the keys.
    return myDataBindingResourceFiles.keySet().equals(other.myDataBindingResourceFiles.keySet());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Condition(com.intellij.openapi.util.Condition) ResourceType(com.android.resources.ResourceType) ResourceFolderType(com.android.resources.ResourceFolderType) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) VisibleForTesting(com.android.annotations.VisibleForTesting)

Example 95 with ResourceType

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

the class ResourceFolderRepository method scanValueFileAsPsi.

private boolean scanValueFileAsPsi(String qualifiers, PsiFile file, FolderConfiguration folderConfiguration) {
    boolean added = false;
    FileType fileType = file.getFileType();
    if (fileType == StdFileTypes.XML) {
        XmlFile xmlFile = (XmlFile) file;
        assert xmlFile.isValid();
        XmlDocument document = xmlFile.getDocument();
        if (document != null) {
            XmlTag root = document.getRootTag();
            if (root == null) {
                return false;
            }
            if (!root.getName().equals(TAG_RESOURCES)) {
                return false;
            }
            // Not recursive, right?
            XmlTag[] subTags = root.getSubTags();
            List<ResourceItem> items = Lists.newArrayListWithExpectedSize(subTags.length);
            for (XmlTag tag : subTags) {
                String name = tag.getAttributeValue(ATTR_NAME);
                if (name != null) {
                    ResourceType type = AndroidResourceUtil.getType(tag);
                    if (type != null) {
                        ListMultimap<String, ResourceItem> map = getMap(type, true);
                        ResourceItem item = new PsiResourceItem(name, type, tag, file);
                        map.put(name, item);
                        items.add(item);
                        added = true;
                        if (type == ResourceType.DECLARE_STYLEABLE) {
                            // for declare styleables we also need to create attr items for its children
                            XmlTag[] attrs = tag.getSubTags();
                            if (attrs.length > 0) {
                                map = getMap(ResourceType.ATTR, true);
                                for (XmlTag child : attrs) {
                                    String attrName = child.getAttributeValue(ATTR_NAME);
                                    if (attrName != null && !attrName.startsWith(ANDROID_NS_NAME_PREFIX) && // it's just a reference to an existing attr
                                    (child.getAttribute(ATTR_FORMAT) != null || child.getSubTags().length > 0)) {
                                        ResourceItem attrItem = new PsiResourceItem(attrName, ResourceType.ATTR, child, file);
                                        items.add(attrItem);
                                        map.put(attrName, attrItem);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            PsiResourceFile resourceFile = new PsiResourceFile(file, items, qualifiers, ResourceFolderType.VALUES, folderConfiguration);
            myResourceFiles.put(file.getVirtualFile(), resourceFile);
        }
    }
    return added;
}
Also used : ResourceType(com.android.resources.ResourceType) FileType(com.intellij.openapi.fileTypes.FileType)

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