Search in sources :

Example 51 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class AndroidThemePreviewToolWindowManager method getBestConfiguration.

@Nullable
private static /*if there is no available configuration that would select the passed file*/
Configuration getBestConfiguration(@Nullable PsiFile psiFile) {
    Module module = psiFile != null ? ModuleUtilCore.findModuleForPsiElement(psiFile) : null;
    if (module == null) {
        return null;
    }
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        return null;
    }
    VirtualFile virtualFile = psiFile.getVirtualFile();
    ConfigurationManager manager = facet.getConfigurationManager();
    List<VirtualFile> variations = ResourceHelper.getResourceVariations(virtualFile, false);
    if (variations.isEmpty()) {
        return manager.getConfiguration(virtualFile);
    }
    // There is more than one resource folder available so make sure we select a configuration that only matches the current file.
    Collection<FolderConfiguration> incompatible = Collections2.transform(variations, new Function<VirtualFile, FolderConfiguration>() {

        @Nullable
        @Override
        public FolderConfiguration apply(VirtualFile input) {
            assert input != null;
            return ResourceHelper.getFolderConfiguration(input);
        }
    });
    FolderConfiguration selectedFileFolderConfiguration = ResourceHelper.getFolderConfiguration(psiFile);
    if (selectedFileFolderConfiguration == null) {
        // This folder probably has invalid qualifiers or they are in the wrong order
        return null;
    }
    RestrictedConfiguration restrictedConfiguration = RestrictedConfiguration.restrict(selectedFileFolderConfiguration, incompatible);
    if (restrictedConfiguration == null) {
        // Unable to create a configuration that only matches this file
        return null;
    }
    FolderConfiguration restricted = restrictedConfiguration.getAny();
    Configuration newConfiguration = Configuration.create(manager, virtualFile, null, restricted);
    VersionQualifier newVersionQualifier = restricted.getVersionQualifier();
    if (newVersionQualifier != null) {
        IAndroidTarget realTarget = manager.getHighestApiTarget() != null ? manager.getHighestApiTarget() : manager.getTarget();
        assert realTarget != null;
        newConfiguration.setTarget(new CompatibilityRenderTarget(realTarget, newVersionQualifier.getVersion(), null));
    }
    return newConfiguration;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Configuration(com.android.tools.idea.configurations.Configuration) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IAndroidTarget(com.android.sdklib.IAndroidTarget) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) Module(com.intellij.openapi.module.Module) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class StringsWriteUtils method getStringResourceFile.

@Nullable
private static XmlFile getStringResourceFile(@NotNull Project project, @NotNull final VirtualFile resFolder, @Nullable Locale locale) {
    FolderConfiguration configuration = new FolderConfiguration();
    if (locale != null) {
        configuration.setLocaleQualifier(locale.qualifier);
    }
    PsiManager manager = PsiManager.getInstance(project);
    final String valuesFolderName = configuration.getFolderName(ResourceFolderType.VALUES);
    VirtualFile valuesFolder = resFolder.findChild(valuesFolderName);
    if (valuesFolder == null) {
        valuesFolder = new WriteCommandAction<VirtualFile>(project, "Creating directory " + valuesFolderName, manager.findFile(resFolder)) {

            @Override
            public void run(@NotNull Result<VirtualFile> result) {
                try {
                    result.setResult(resFolder.createChildDirectory(this, valuesFolderName));
                } catch (IOException ex) {
                    // Immediately after this, we handle the case where the result is null
                    //noinspection ConstantConditions
                    result.setResult(null);
                }
            }
        }.execute().getResultObject();
        if (valuesFolder == null) {
            return null;
        }
    }
    String resourceFileName = AndroidResourceUtil.getDefaultResourceFileName(ResourceType.STRING);
    if (resourceFileName == null) {
        return null;
    }
    VirtualFile resourceVirtualFile = valuesFolder.findChild(resourceFileName);
    XmlFile resourceFile;
    if (resourceVirtualFile == null) {
        PsiDirectory valuesDir = manager.findDirectory(valuesFolder);
        if (valuesDir == null) {
            return null;
        }
        try {
            resourceFile = AndroidResourceUtil.createFileResource(resourceFileName, valuesDir, "", ResourceType.STRING.getName(), true);
        } catch (Exception ex) {
            return null;
        }
    } else {
        PsiFile resourcePsiFile = manager.findFile(resourceVirtualFile);
        if (!(resourcePsiFile instanceof XmlFile)) {
            return null;
        }
        resourceFile = (XmlFile) resourcePsiFile;
    }
    return resourceFile;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IOException(java.io.IOException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 53 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ThemeEditorUtils method getMinFolderApi.

/**
   * Returns the smallest api level of the folders in folderNames.
   * Returns Integer.MAX_VALUE if folderNames is empty.
   */
public static int getMinFolderApi(@NotNull List<String> folderNames, @NotNull Module module) {
    int minFolderApi = Integer.MAX_VALUE;
    int minModuleApi = getMinApiLevel(module);
    for (String folderName : folderNames) {
        FolderConfiguration folderConfig = FolderConfiguration.getConfigForFolder(folderName);
        if (folderConfig != null) {
            VersionQualifier version = folderConfig.getVersionQualifier();
            int folderApi = version != null ? version.getVersion() : minModuleApi;
            minFolderApi = Math.min(minFolderApi, folderApi);
        }
    }
    return minFolderApi;
}
Also used : VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 54 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ResourceFolderRepository method scanResFolder.

private void scanResFolder(@NotNull VirtualFile resDir) {
    for (VirtualFile subDir : resDir.getChildren()) {
        if (subDir.isValid() && subDir.isDirectory()) {
            String name = subDir.getName();
            ResourceFolderType folderType = getFolderType(name);
            if (folderType != null) {
                FolderConfiguration folderConfiguration = FolderConfiguration.getConfigForFolder(name);
                if (folderConfiguration == null) {
                    continue;
                }
                String qualifiers = getQualifiers(name);
                if (folderType == VALUES) {
                    scanValueResFolder(subDir, qualifiers, folderConfiguration);
                } else {
                    scanFileResourceFolder(subDir, folderType, qualifiers, folderConfiguration);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFolderType(com.android.resources.ResourceFolderType) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 55 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class PsiResourceItem method getConfiguration.

@Override
public FolderConfiguration getConfiguration() {
    PsiResourceFile source = (PsiResourceFile) super.getSource();
    // Temporary safety workaround
    if (source == null) {
        if (myFile != null) {
            PsiDirectory parent = myFile.getParent();
            if (parent != null) {
                String name = parent.getName();
                FolderConfiguration configuration = FolderConfiguration.getConfigForFolder(name);
                if (configuration != null) {
                    return configuration;
                }
            }
        }
        String qualifiers = getQualifiers();
        FolderConfiguration fromQualifiers = FolderConfiguration.getConfigForQualifierString(qualifiers);
        if (fromQualifiers == null) {
            return new FolderConfiguration();
        }
        return fromQualifiers;
    }
    return source.getFolderConfiguration();
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Aggregations

FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 NotNull (org.jetbrains.annotations.NotNull)14 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)12 Nullable (org.jetbrains.annotations.Nullable)12 ResourceItem (com.android.ide.common.res2.ResourceItem)10 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)10 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)9 ResourceFolderType (com.android.resources.ResourceFolderType)9 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)7 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Project (com.intellij.openapi.project.Project)7 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)6 ResourceResolver (com.android.ide.common.resources.ResourceResolver)6 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)6 ScreenOrientationQualifier (com.android.ide.common.resources.configuration.ScreenOrientationQualifier)6 ScreenSizeQualifier (com.android.ide.common.resources.configuration.ScreenSizeQualifier)6 SessionParams (com.android.ide.common.rendering.api.SessionParams)5 CountryCodeQualifier (com.android.ide.common.resources.configuration.CountryCodeQualifier)5