Search in sources :

Example 31 with ResourceResolver

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

the class ColorUtils method getContrastColorsWithDescription.

/**
   * @param styleAttributeName the name of a style attribute we want to check for contrast issues
   * @return all the colors the attribute needs to be checked against, each associated with the appropriate description
   */
@NotNull
public static ImmutableMap<String, Color> getContrastColorsWithDescription(@NotNull ThemeEditorContext context, @NotNull String styleAttributeName) {
    ImmutableMap.Builder<String, Color> contrastColorsBuilder = ImmutableMap.builder();
    ResourceResolver styleResourceResolver = context.getResourceResolver();
    assert styleResourceResolver != null;
    Project project = context.getProject();
    Set<ItemResourceValue> contrastItems = getContrastItems(context, styleAttributeName);
    for (ItemResourceValue contrastItem : contrastItems) {
        ResourceHelper.StateList stateList = ResourceHelper.resolveStateList(styleResourceResolver, contrastItem, project);
        if (stateList != null) {
            List<ResourceHelper.StateListState> disabledStates = stateList.getDisabledStates();
            for (ResourceHelper.StateListState stateListState : stateList.getStates()) {
                Color stateListColor = ResourceHelper.resolveColor(styleResourceResolver, styleResourceResolver.findResValue(stateListState.getValue(), false), project);
                if (stateListColor != null) {
                    try {
                        stateListColor = ResourceHelper.makeColorWithAlpha(styleResourceResolver, stateListColor, stateListState.getAlpha());
                    } catch (NumberFormatException e) {
                        // If the alpha value is not valid, Android uses 1.0, so nothing more needs to be done, we can use stateListColor directly
                        LOG.warn(String.format(ResourceHelper.ALPHA_FLOATING_ERROR_FORMAT, stateList.getDirName(), stateList.getFileName()));
                    }
                    String disabledPrefix = disabledStates.contains(stateListState) ? DISABLED_PREFIX : "";
                    contrastColorsBuilder.put(disabledPrefix + ThemeEditorUtils.generateWordEnumeration(stateListState.getAttributesNames(false)) + " <b>" + contrastItem.getName() + "</b>", stateListColor);
                }
            }
        } else {
            Color resolvedColor = ResourceHelper.resolveColor(styleResourceResolver, contrastItem, project);
            if (resolvedColor != null) {
                contrastColorsBuilder.put("<b>" + contrastItem.getName() + "</b>", resolvedColor);
            }
        }
    }
    return contrastColorsBuilder.build();
}
Also used : Project(com.intellij.openapi.project.Project) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ResourceHelper(com.android.tools.idea.res.ResourceHelper) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with ResourceResolver

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

the class ChooseResourceDialog method ensurePickersInitialized.

private void ensurePickersInitialized() {
    boolean allowDrawables = allowDrawables();
    boolean allowColors = allowColors();
    if (allowColors || allowDrawables) {
        if (myStateListPicker != null || myColorPicker != null) {
            return;
        }
        Configuration configuration = getConfiguration();
        ResourceResolver resolver = configuration.getResourceResolver();
        assert resolver != null;
        ResourceValue resValue = null;
        if (myInitialValue != null) {
            resValue = resolver.findResValue(myInitialValue, myInitialValueIsFramework);
        }
        final ResourceType stateListType;
        final ResourceFolderType stateListFolderType;
        if (allowDrawables) {
            stateListType = ResourceType.DRAWABLE;
            stateListFolderType = ResourceFolderType.DRAWABLE;
        } else {
            stateListType = ResourceType.COLOR;
            stateListFolderType = ResourceFolderType.COLOR;
        }
        initializeStateListPicker(configuration, resolver, resValue, stateListType, stateListFolderType);
        initializeColorPicker(myInitialValue, myResourceNameVisibility, resolver, resValue);
    }
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) ResourceFolderType(com.android.resources.ResourceFolderType) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 33 with ResourceResolver

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

the class StateListPicker method updateIcon.

private void updateIcon(@NotNull StateComponent component) {
    component.showAlphaError(false);
    ResourceResolver resourceResolver = myConfiguration.getResourceResolver();
    assert resourceResolver != null;
    component.setValueIcon(getSwatchIcon(component.getResourceValue(), resourceResolver, myRenderTask));
    String alphaValue = component.getAlphaValue();
    if (!StringUtil.isEmpty(alphaValue)) {
        try {
            float alpha = Float.parseFloat(ResourceHelper.resolveStringValue(resourceResolver, alphaValue));
            Font iconFont = JBUI.Fonts.smallFont().asBold();
            component.getAlphaComponent().setSwatchIcon(new SwatchComponent.TextIcon(String.format("%.2f", alpha), iconFont));
        } catch (NumberFormatException e) {
            component.showAlphaError(true);
            component.getAlphaComponent().setSwatchIcon(SwatchComponent.WARNING_ICON);
        }
    } else {
        Font iconFont = JBUI.Fonts.smallFont().asBold();
        component.getAlphaComponent().setSwatchIcon(new SwatchComponent.TextIcon("1.00", iconFont));
    }
}
Also used : ResourceResolver(com.android.ide.common.resources.ResourceResolver) SwatchComponent(com.android.tools.swing.ui.SwatchComponent)

Example 34 with ResourceResolver

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

the class ResourceResolverCacheTest method test.

public void test() throws Exception {
    VirtualFile file1 = myFixture.copyFileToProject("render/layout1.xml", "res/layout/layout1.xml");
    VirtualFile file2 = myFixture.copyFileToProject("render/layout2.xml", "res/layout/layout2.xml");
    VirtualFile file3 = myFixture.copyFileToProject("javadoc/strings/strings.xml", "res/values/strings.xml");
    assertNotNull(file1);
    assertNotNull(file2);
    assertNotNull(file3);
    AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assertNotNull(facet);
    Project project = getProject();
    PsiFile psiFile1 = PsiManager.getInstance(project).findFile(file1);
    assertNotNull(psiFile1);
    PsiFile psiFile2 = PsiManager.getInstance(project).findFile(file2);
    assertNotNull(psiFile2);
    final PsiFile psiFile3 = PsiManager.getInstance(project).findFile(file3);
    assertNotNull(psiFile3);
    ConfigurationManager configurationManager = facet.getConfigurationManager();
    assertNotNull(configurationManager);
    final Configuration configuration1 = configurationManager.getConfiguration(file1);
    Configuration configuration2 = configurationManager.getConfiguration(file2);
    assertNotNull(configuration1.getTheme());
    assertEquals(configuration2.getTheme(), configuration1.getTheme());
    ResourceResolver resolver1 = configuration1.getResourceResolver();
    ResourceResolver resolver2 = configuration2.getResourceResolver();
    assertSame(resolver1, resolver2);
    assertSame(resolver1, configuration1.getResourceResolver());
    configuration1.setTheme("Theme.Light");
    final ResourceResolver resolver1b = configuration1.getResourceResolver();
    assertNotSame(resolver1b, resolver1);
    assertNotSame(resolver1b, resolver2);
    assertSame(resolver1b, configuration1.getResourceResolver());
    configuration2.setTheme("Theme.Light");
    assertSame(resolver1b, configuration2.getResourceResolver());
    // Test project resource changes, should invalidate
    final LocalResourceRepository resources = myFacet.getModuleResources(true);
    assertNotNull(resources);
    final long generation = resources.getModificationCount();
    assertEquals("Cancel", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue());
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            //noinspection ConstantConditions
            XmlTagValue value = ((XmlFile) psiFile3).getRootTag().getSubTags()[1].getValue();
            assertEquals("Cancel", value.getTrimmedText());
            value.setText("\"FooBar\"");
        }
    });
    assertTrue(resources.isScanPending(psiFile3));
    ApplicationManager.getApplication().invokeLater(new Runnable() {

        @Override
        public void run() {
            assertTrue(generation < resources.getModificationCount());
            assertNotSame(resolver1b, configuration1.getResourceResolver());
            assertEquals("FooBar", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue());
        }
    });
    ResourceResolverCache cache = configuration1.getConfigurationManager().getResolverCache();
    assertSame(cache, configuration2.getConfigurationManager().getResolverCache());
    ResourceRepository frameworkResources = cache.getFrameworkResources(configuration1.getFullConfig(), configuration1.getTarget());
    assertTrue(frameworkResources instanceof FrameworkResourceLoader.IdeFrameworkResources);
    assertTrue(((FrameworkResourceLoader.IdeFrameworkResources) frameworkResources).getSkippedLocales());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlTagValue(com.intellij.psi.xml.XmlTagValue) XmlFile(com.intellij.psi.xml.XmlFile) FrameworkResourceLoader(org.jetbrains.android.sdk.FrameworkResourceLoader) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceResolver(com.android.ide.common.resources.ResourceResolver) PsiFile(com.intellij.psi.PsiFile) ResourceRepository(com.android.ide.common.resources.ResourceRepository) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository)

Example 35 with ResourceResolver

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

the class NlPreviewImagePanel method getBackgroundColor.

@NotNull
private Color getBackgroundColor() {
    Configuration configuration = myDesignSurface != null ? myDesignSurface.getConfiguration() : null;
    ResourceResolver resolver = configuration != null ? configuration.getResourceResolver() : null;
    if (resolver == null) {
        return UIUtil.getPanelBackground();
    }
    ResourceValue windowBackground = resolver.findItemInTheme("colorBackground", true);
    Color background = ResourceHelper.resolveColor(resolver, windowBackground, myDesignSurface.getProject());
    return background != null ? background : UIUtil.getPanelBackground();
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ResourceResolver (com.android.ide.common.resources.ResourceResolver)43 Configuration (com.android.tools.idea.configurations.Configuration)16 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)13 NotNull (org.jetbrains.annotations.NotNull)11 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)8 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)6 Nullable (org.jetbrains.annotations.Nullable)6 SessionParams (com.android.ide.common.rendering.api.SessionParams)5 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)5 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)5 Module (com.intellij.openapi.module.Module)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 IAndroidTarget (com.android.sdklib.IAndroidTarget)4 Project (com.intellij.openapi.project.Project)4 ResourceType (com.android.resources.ResourceType)3 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)3 XmlFile (com.intellij.psi.xml.XmlFile)3 ResourceRepository (com.android.ide.common.resources.ResourceRepository)2 Device (com.android.sdklib.devices.Device)2 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)2