Search in sources :

Example 1 with ResourceFile

use of com.android.ide.common.res2.ResourceFile in project kotlin by JetBrains.

the class ViewTypeDetector method getViewTags.

@Nullable
protected Collection<String> getViewTags(@NonNull Context context, @NonNull ResourceItem item) {
    // Check view tag in this file. Can I do it cheaply? Try with
    // an XML pull parser. Or DOM if we have multiple resources looked
    // up?
    ResourceFile source = item.getSource();
    if (source != null) {
        File file = source.getFile();
        Multimap<String, String> map = getIdToTagsIn(context, file);
        if (map != null) {
            return map.get(item.getName());
        }
    }
    return null;
}
Also used : ResourceFile(com.android.ide.common.res2.ResourceFile) File(java.io.File) ResourceFile(com.android.ide.common.res2.ResourceFile) Nullable(com.android.annotations.Nullable)

Example 2 with ResourceFile

use of com.android.ide.common.res2.ResourceFile in project kotlin by JetBrains.

the class LayoutInflationDetector method hasLayoutParams.

private static boolean hasLayoutParams(@NonNull JavaContext context, String name) {
    LintClient client = context.getClient();
    if (!client.supportsProjectResources()) {
        // not certain
        return true;
    }
    Project project = context.getProject();
    AbstractResourceRepository resources = client.getProjectResources(project, true);
    if (resources == null) {
        // not certain
        return true;
    }
    List<ResourceItem> items = resources.getResourceItem(ResourceType.LAYOUT, name);
    if (items == null || items.isEmpty()) {
        return false;
    }
    for (ResourceItem item : items) {
        ResourceFile source = item.getSource();
        if (source == null) {
            // not certain
            return true;
        }
        File file = source.getFile();
        if (file.exists()) {
            try {
                String s = context.getClient().readFile(file);
                if (hasLayoutParams(new StringReader(s))) {
                    return true;
                }
            } catch (Exception e) {
                context.log(e, "Could not read/parse inflated layout");
                // not certain
                return true;
            }
        }
    }
    return false;
}
Also used : Project(com.android.tools.klint.detector.api.Project) ResourceFile(com.android.ide.common.res2.ResourceFile) AbstractResourceRepository(com.android.ide.common.res2.AbstractResourceRepository) LintClient(com.android.tools.klint.client.api.LintClient) StringReader(java.io.StringReader) ResourceItem(com.android.ide.common.res2.ResourceItem) File(java.io.File) ResourceFile(com.android.ide.common.res2.ResourceFile) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 3 with ResourceFile

use of com.android.ide.common.res2.ResourceFile 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 4 with ResourceFile

use of com.android.ide.common.res2.ResourceFile in project android by JetBrains.

the class LocalResourceRepository method getItemPsiFile.

/** Returns the {@link PsiFile} corresponding to the source of the given resource item, if possible */
@Nullable
public static PsiFile getItemPsiFile(@NonNull Project project, @NonNull ResourceItem item) {
    if (project.isDisposed()) {
        return null;
    }
    if (item instanceof PsiResourceItem) {
        PsiResourceItem psiResourceItem = (PsiResourceItem) item;
        return psiResourceItem.getPsiFile();
    }
    ResourceFile source = item.getSource();
    if (source == null) {
        // most likely a dynamically defined value
        return null;
    }
    if (source instanceof PsiResourceFile) {
        PsiResourceFile prf = (PsiResourceFile) source;
        return prf.getPsiFile();
    }
    File file = source.getFile();
    VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file);
    if (virtualFile != null) {
        PsiManager psiManager = PsiManager.getInstance(project);
        return psiManager.findFile(virtualFile);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFile(com.android.ide.common.res2.ResourceFile) PsiManager(com.intellij.psi.PsiManager) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) ResourceFile(com.android.ide.common.res2.ResourceFile) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ResourceFile

use of com.android.ide.common.res2.ResourceFile in project android by JetBrains.

the class ConfiguredThemeEditorStyle method getConfiguredValues.

/**
   * Returns all the style attributes and its values. For each attribute, multiple {@link ConfiguredElement} can be returned
   * representing the multiple values in different configurations for each item.
   * TODO: needs to be deleted, as we don't use this method except tests
   */
@NotNull
public ImmutableCollection<ConfiguredElement<ItemResourceValue>> getConfiguredValues() {
    // Get a list of all the items indexed by the item name. Each item contains a list of the
    // possible values in this theme in different configurations.
    //
    // If item1 has multiple values in different configurations, there will be an
    // item1 = {folderConfiguration1 -> value1, folderConfiguration2 -> value2}
    final ImmutableList.Builder<ConfiguredElement<ItemResourceValue>> itemResourceValues = ImmutableList.builder();
    if (isFramework()) {
        assert myConfiguration.getFrameworkResources() != null;
        com.android.ide.common.resources.ResourceItem styleItem = myConfiguration.getFrameworkResources().getResourceItem(ResourceType.STYLE, myStyleResourceValue.getName());
        // Go over all the files containing the resource.
        for (ResourceFile file : styleItem.getSourceFileList()) {
            ResourceValue styleResourceValue = file.getValue(ResourceType.STYLE, styleItem.getName());
            FolderConfiguration folderConfiguration = file.getConfiguration();
            if (styleResourceValue instanceof StyleResourceValue) {
                for (final ItemResourceValue value : ((StyleResourceValue) styleResourceValue).getValues()) {
                    itemResourceValues.add(ConfiguredElement.create(folderConfiguration, value));
                }
            }
        }
    } else {
        for (ResourceItem styleDefinition : getStyleResourceItems()) {
            ResourceValue styleResourceValue = styleDefinition.getResourceValue(isFramework());
            FolderConfiguration folderConfiguration = styleDefinition.getConfiguration();
            if (styleResourceValue instanceof StyleResourceValue) {
                for (final ItemResourceValue value : ((StyleResourceValue) styleResourceValue).getValues()) {
                    // We use the qualified name since apps and libraries can use the same attribute name twice with and without "android:"
                    itemResourceValues.add(ConfiguredElement.create(folderConfiguration, value));
                }
            }
        }
    }
    return itemResourceValues.build();
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceFile(com.android.ide.common.resources.ResourceFile) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ResourceFile (com.android.ide.common.res2.ResourceFile)8 ResourceItem (com.android.ide.common.res2.ResourceItem)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)4 File (java.io.File)4 Nullable (org.jetbrains.annotations.Nullable)4 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)3 ResourceFile (com.android.ide.common.resources.ResourceFile)3 IAndroidTarget (com.android.sdklib.IAndroidTarget)2 PsiFile (com.intellij.psi.PsiFile)2 XmlFile (com.intellij.psi.xml.XmlFile)2 IOException (java.io.IOException)2 NotNull (org.jetbrains.annotations.NotNull)2 NonNull (com.android.annotations.NonNull)1 Nullable (com.android.annotations.Nullable)1 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)1 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)1 ResourceFolderType (com.android.resources.ResourceFolderType)1