Search in sources :

Example 6 with ResourceFile

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

the class ThemeEditorStyle method getParentName.

/**
   * @param configuration FolderConfiguration of the style to lookup
   * @return parent this style with a FolderConfiguration configuration
   */
@Nullable
public /*if there is no of this style*/
String getParentName(@NotNull FolderConfiguration configuration) {
    if (isFramework()) {
        IAndroidTarget target = myManager.getHighestApiTarget();
        assert target != null;
        com.android.ide.common.resources.ResourceItem styleItem = myManager.getResolverCache().getFrameworkResources(new FolderConfiguration(), target).getResourceItem(ResourceType.STYLE, getName());
        for (ResourceFile file : styleItem.getSourceFileList()) {
            if (file.getConfiguration().equals(configuration)) {
                StyleResourceValue style = (StyleResourceValue) file.getValue(ResourceType.STYLE, getName());
                return ResolutionUtils.getParentQualifiedName(style);
            }
        }
        throw new IllegalArgumentException("bad folder config " + configuration);
    }
    for (final ResourceItem styleItem : getStyleResourceItems()) {
        if (configuration.equals(styleItem.getConfiguration())) {
            StyleResourceValue style = (StyleResourceValue) styleItem.getResourceValue(false);
            assert style != null;
            return ResolutionUtils.getParentQualifiedName(style);
        }
    }
    throw new IllegalArgumentException("bad folder config " + configuration);
}
Also used : ResourceFile(com.android.ide.common.resources.ResourceFile) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) IAndroidTarget(com.android.sdklib.IAndroidTarget) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with ResourceFile

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

the class ThemeEditorStyle method getValues.

/**
   * @param configuration FolderConfiguration of the style to lookup
   * @return all values defined in this style with a FolderConfiguration configuration
   */
@NotNull
public Collection<ItemResourceValue> getValues(@NotNull FolderConfiguration configuration) {
    if (isFramework()) {
        IAndroidTarget target = myManager.getHighestApiTarget();
        assert target != null;
        com.android.ide.common.resources.ResourceItem styleItem = myManager.getResolverCache().getFrameworkResources(new FolderConfiguration(), target).getResourceItem(ResourceType.STYLE, getName());
        for (ResourceFile file : styleItem.getSourceFileList()) {
            if (file.getConfiguration().equals(configuration)) {
                StyleResourceValue style = (StyleResourceValue) file.getValue(ResourceType.STYLE, getName());
                return style.getValues();
            }
        }
        throw new IllegalArgumentException("bad folder config " + configuration);
    }
    for (final ResourceItem styleItem : getStyleResourceItems()) {
        if (configuration.equals(styleItem.getConfiguration())) {
            StyleResourceValue style = (StyleResourceValue) styleItem.getResourceValue(false);
            if (style == null) {
                // style might be null if the value fails to parse
                continue;
            }
            return style.getValues();
        }
    }
    throw new IllegalArgumentException("bad folder config " + configuration);
}
Also used : ResourceFile(com.android.ide.common.resources.ResourceFile) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) IAndroidTarget(com.android.sdklib.IAndroidTarget) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ResourceFile

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

the class StringsWriteUtils method createItem.

/**
   * Creates a string resource in the specified locale.
   *
   * @return the resource item that was created, null if it wasn't created or could not be read back
   */
@Nullable
public static ResourceItem createItem(@NotNull final AndroidFacet facet, @NotNull VirtualFile resFolder, @Nullable final Locale locale, @NotNull final String name, @NotNull final String value, final boolean translatable) {
    Project project = facet.getModule().getProject();
    XmlFile resourceFile = getStringResourceFile(project, resFolder, locale);
    if (resourceFile == null) {
        return null;
    }
    final XmlTag root = resourceFile.getRootTag();
    if (root == null) {
        return null;
    }
    new WriteCommandAction.Simple(project, "Creating string " + name, resourceFile) {

        @Override
        public void run() {
            XmlTag child = root.createChildTag(ResourceType.STRING.getName(), root.getNamespace(), escapeResourceStringAsXml(value), false);
            child.setAttribute(SdkConstants.ATTR_NAME, name);
            // XmlTagImpl handles a null value by deleting the attribute, which is our desired behavior
            //noinspection ConstantConditions
            child.setAttribute(SdkConstants.ATTR_TRANSLATABLE, translatable ? null : SdkConstants.VALUE_FALSE);
            root.addSubTag(child, false);
        }
    }.execute();
    if (ApplicationManager.getApplication().isReadAccessAllowed()) {
        return getStringResourceItem(facet, name, locale);
    } else {
        return ApplicationManager.getApplication().runReadAction(new Computable<ResourceItem>() {

            @Override
            public ResourceItem compute() {
                return getStringResourceItem(facet, name, locale);
            }
        });
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with ResourceFile

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

the class AndroidResourceRenameResourceProcessor method appendUnhandledReferences.

/** Writes into the given {@link HtmlBuilder} a set of references
   * that are defined in a library (and may or may not also be defined locally) */
private static void appendUnhandledReferences(@NotNull Project project, @NotNull AndroidFacet facet, @NotNull List<ResourceItem> all, @NotNull List<ResourceItem> local, @NotNull HtmlBuilder builder) {
    File root = VfsUtilCore.virtualToIoFile(project.getBaseDir());
    Collection<AndroidLibrary> libraries = null;
    // Write a set of descriptions to library references. Put them in a list first such that we can
    // sort the (to for example make test output stable.)
    List<String> descriptions = Lists.newArrayList();
    for (ResourceItem item : all) {
        if (!local.contains(item)) {
            ResourceFile source = item.getSource();
            if (libraries == null) {
                libraries = AppResourceRepository.findAarLibraries(facet);
            }
            if (source != null) {
                File sourceFile = source.getFile();
                // TODO: Look up the corresponding AAR artifact, and then use library.getRequestedCoordinates() or
                // library.getResolvedCoordinates() here and append the coordinate. However, until b.android.com/77341
                // is fixed this doesn't work.
                /*
          // Attempt to find the corresponding AAR artifact
          AndroidLibrary library = null;
          for (AndroidLibrary l : libraries) {
            File res = l.getResFolder();
            if (res.exists() && FileUtil.isAncestor(res, sourceFile, true)) {
              library = l;
              break;
            }
          }
          */
                // Look for exploded-aar and strip off the prefix path to it
                File localRoot = root;
                File prev = sourceFile;
                File current = sourceFile.getParentFile();
                while (current != null) {
                    String name = current.getName();
                    if (EXPLODED_AAR.equals(name)) {
                        localRoot = prev;
                        break;
                    }
                    prev = current;
                    current = current.getParentFile();
                }
                if (FileUtil.isAncestor(localRoot, sourceFile, true)) {
                    descriptions.add(FileUtil.getRelativePath(localRoot, sourceFile));
                } else {
                    descriptions.add(sourceFile.getPath());
                }
            }
        }
    }
    Collections.sort(descriptions);
    builder.newline().newline();
    builder.add("Unhandled references:");
    builder.newline();
    int count = 0;
    for (String s : descriptions) {
        builder.add(s).newline();
        count++;
        if (count == 10) {
            builder.add("...").newline();
            builder.add("(Additional results truncated)");
            break;
        }
    }
}
Also used : ResourceFile(com.android.ide.common.res2.ResourceFile) AndroidLibrary(com.android.builder.model.AndroidLibrary) ResourceItem(com.android.ide.common.res2.ResourceItem) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFile(com.android.ide.common.res2.ResourceFile) File(java.io.File)

Example 10 with ResourceFile

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

the class ModuleResourceRepositoryTest method assertItemIsInDir.

// Unit test support methods
static void assertItemIsInDir(VirtualFile dir, ResourceItem item) {
    ResourceFile resourceFile = item.getSource();
    assertNotNull(resourceFile);
    VirtualFile parent = VfsUtil.findFileByIoFile(resourceFile.getFile(), false);
    assertNotNull(parent);
    assertEquals(dir, parent.getParent().getParent());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFile(com.android.ide.common.res2.ResourceFile)

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