Search in sources :

Example 21 with FolderConfiguration

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

the class ThemeEditorStyle method findToBeCopied.

/**
   * Finds best to be copied {@link FolderConfiguration}s
   * e.g if style is defined in "port-v8", "port-v18", "port-v22", "night-v20" and desiredApi = 21,
   * then result is {"port-v18", "night-v20"}
   *
   * @param desiredApi new api level of {@link FolderConfiguration}s after being copied
   * @return Collection of FolderConfigurations which are going to be copied to version desiredApi
   */
@NotNull
private ImmutableCollection<FolderConfiguration> findToBeCopied(int desiredApi) {
    // Keeps closest VersionQualifier to 'desiredApi'
    // e.g. desiredApi = 21, "en-port", "en-port-v18", "en-port-v19", "en-port-v22" then
    // bestVersionCopyFrom will contain {"en-port" -> v19}, as it is closest one to v21
    final HashMap<FolderConfiguration, VersionQualifier> bestVersionCopyFrom = Maps.newHashMap();
    for (ResourceItem styleItem : getStyleResourceItems()) {
        FolderConfiguration configuration = FolderConfiguration.copyOf(styleItem.getConfiguration());
        int styleItemVersion = ThemeEditorUtils.getVersionFromConfiguration(configuration);
        // We want to get the best from port-v19 port-v20 port-v23. so we need to remove the version qualifier to compare them
        configuration.setVersionQualifier(null);
        if (styleItemVersion > desiredApi) {
            // Thus, we don't need to copy it, we are going to just modify it.
            continue;
        }
        // If 'version' is closer to 'desiredApi' than we have found
        if (!bestVersionCopyFrom.containsKey(configuration) || bestVersionCopyFrom.get(configuration).getVersion() < styleItemVersion) {
            bestVersionCopyFrom.put(configuration, new VersionQualifier(styleItemVersion));
        }
    }
    ImmutableList.Builder<FolderConfiguration> toBeCopied = ImmutableList.builder();
    for (FolderConfiguration key : bestVersionCopyFrom.keySet()) {
        FolderConfiguration configuration = FolderConfiguration.copyOf(key);
        VersionQualifier version = bestVersionCopyFrom.get(key);
        if (version.getVersion() != -1) {
            configuration.setVersionQualifier(version);
        }
        // Version can't be bigger as we have filtered above
        if (version.getVersion() < desiredApi) {
            toBeCopied.add(configuration);
        }
    }
    return toBeCopied.build();
}
Also used : VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) ImmutableList(com.google.common.collect.ImmutableList) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration 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 23 with FolderConfiguration

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

the class AndroidLintNewApiInspection method getApiDetectorFixes.

public static AndroidLintQuickFix[] getApiDetectorFixes(@NotNull Issue issue, @NotNull PsiElement startElement, @SuppressWarnings("UnusedParameters") @NotNull PsiElement endElement, @NotNull String message) {
    // TODO: Return one for each parent context (declaration, method, class, outer class(es)
    int api = ApiDetector.getRequiredVersion(issue, message, RAW);
    if (api != -1) {
        List<AndroidLintQuickFix> list = Lists.newArrayList();
        PsiFile file = startElement.getContainingFile();
        boolean isXml = false;
        if (file instanceof XmlFile) {
            isXml = true;
            ResourceFolderType folderType = ResourceHelper.getFolderType(file);
            if (folderType != null) {
                FolderConfiguration config = ResourceHelper.getFolderConfiguration(file);
                if (config != null) {
                    config.setVersionQualifier(new VersionQualifier(api));
                    String folder = config.getFolderName(folderType);
                    list.add(OverrideResourceAction.createFix(folder));
                }
            }
        }
        list.add(new AddTargetVersionCheckQuickFix(api));
        list.add(new AddTargetApiQuickFix(api, false, startElement));
        ApplicationManager.getApplication().assertReadAccessAllowed();
        Project project = startElement.getProject();
        if (!isXml && JavaPsiFacade.getInstance(project).findClass(REQUIRES_API_ANNOTATION, GlobalSearchScope.allScope(project)) != null) {
            list.add(new AddTargetApiQuickFix(api, true, startElement));
        }
        return list.toArray(new AndroidLintQuickFix[list.size()]);
    }
    return AndroidLintQuickFix.EMPTY_ARRAY;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Project(com.intellij.openapi.project.Project) ResourceFolderType(com.android.resources.ResourceFolderType) PsiFile(com.intellij.psi.PsiFile)

Example 24 with FolderConfiguration

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

the class ResourceDrawablePanel method select.

public void select(@NotNull ResourceChooserItem item) {
    myItem = item;
    List<String> qualifiers = item.getQualifiers();
    //noinspection unchecked
    myQualifierCombo.setModel(new DefaultComboBoxModel(ArrayUtil.toStringArray(qualifiers)));
    // Select the current item's qualifiers!
    //noinspection UnnecessaryLocalVariable
    String currentQualifier = DEFAULT_FOLDER_NAME;
    ResourceResolver resourceResolver = myDialog.getConfiguration().getResourceResolver();
    if (resourceResolver != null) {
        ResourceValue resourceValue = resourceResolver.resolveResValue(item.getResourceValue());
        if (resourceValue != null) {
            String value = resourceValue.getValue();
            if (value != null) {
                File rendered = new File(value);
                if (rendered.exists()) {
                    File folder = rendered.getParentFile();
                    if (folder != null) {
                        String folderName = folder.getName();
                        FolderConfiguration folderConfig = FolderConfiguration.getConfigForFolder(folderName);
                        if (folderConfig != null) {
                            int index = folderName.indexOf('-');
                            if (index != -1) {
                                currentQualifier = folderName.substring(index + 1);
                            }
                        }
                    }
                }
            }
        }
    }
    try {
        myIgnoreSelection = true;
        myQualifierCombo.setSelectedItem(currentQualifier);
    } finally {
        myIgnoreSelection = false;
    }
    show(item);
}
Also used : ResourceResolver(com.android.ide.common.resources.ResourceResolver) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) File(java.io.File)

Example 25 with FolderConfiguration

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

the class CreateResourceFileDialogBase method setupDeviceConfigurationPanel.

protected DeviceConfiguratorPanel setupDeviceConfigurationPanel(final JTextField directoryNameTextField, final TemplateKindCombo resourceTypeCombo, final JBLabel errorLabel) {
    return new DeviceConfiguratorPanel() {

        @Override
        public void applyEditors() {
            try {
                doApplyEditors();
                final FolderConfiguration config = this.getConfiguration();
                final CreateTypedResourceFileAction selectedAction = getSelectedAction(resourceTypeCombo);
                errorLabel.setText("");
                directoryNameTextField.setText("");
                if (selectedAction != null) {
                    final ResourceFolderType resFolderType = selectedAction.getResourceFolderType();
                    directoryNameTextField.setText(config.getFolderName(resFolderType));
                }
            } catch (InvalidOptionValueException e) {
                errorLabel.setText(new HtmlBuilder().openHtmlBody().coloredText(JBColor.RED, e.getMessage()).closeHtmlBody().getHtml());
                directoryNameTextField.setText("");
            }
            updateOkAction();
        }
    };
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) HtmlBuilder(com.android.utils.HtmlBuilder) InvalidOptionValueException(org.jetbrains.android.uipreview.InvalidOptionValueException) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) DeviceConfiguratorPanel(org.jetbrains.android.uipreview.DeviceConfiguratorPanel)

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