Search in sources :

Example 36 with ResourceResolver

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

the class ConstraintUtilities method getDpValue.

/**
   * Return a dp value correctly resolved. This is only intended for generic
   * dimensions (number + unit). Do not use this if the string can contain
   * wrap_content or match_parent. See {@link #getLayoutDimensionDpValue(NlComponent, String)}.
   *
   * @param component the component we are looking at
   * @param value     the attribute value we want to parse
   * @return the value of the attribute in Dp, or zero if impossible to resolve
   */
public static int getDpValue(@NotNull NlComponent component, String value) {
    if (value != null) {
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        if (resourceResolver != null) {
            Integer px = ViewEditor.resolveDimensionPixelSize(resourceResolver, value, configuration);
            return px == null ? 0 : (int) (0.5f + px / (configuration.getDensity().getDpiValue() / 160.0f));
        }
    }
    return 0;
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) ResourceResolver(com.android.ide.common.resources.ResourceResolver)

Example 37 with ResourceResolver

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

the class ConstraintUtilities method resolveStringResource.

@NotNull
static String resolveStringResource(@NotNull NlComponent component, @NotNull String text) {
    Configuration configuration = component.getModel().getConfiguration();
    ResourceResolver resourceResolver = configuration.getResourceResolver();
    if (resourceResolver != null) {
        return resolveStringValue(resourceResolver, text);
    }
    return "";
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) ResourceResolver(com.android.ide.common.resources.ResourceResolver) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with ResourceResolver

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

the class SceneKeepDimensTest method setFakeResource.

private void setFakeResource(String dimension, String value) {
    Configuration configuration = Mockito.mock(Configuration.class);
    ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
    ResourceValue resourceValue = Mockito.mock(ResourceValue.class);
    when(configuration.getResourceResolver()).thenReturn(resolver);
    when(configuration.getDensity()).thenReturn(Density.MEDIUM);
    when(resolver.findResValue(dimension, false)).thenReturn(resourceValue);
    when(resolver.resolveResValue(resourceValue)).thenReturn(resourceValue);
    when(resourceValue.getValue()).thenReturn(value);
    ((SyncNlModel) myModel).setConfiguration(configuration);
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) SyncNlModel(com.android.tools.idea.uibuilder.SyncNlModel)

Example 39 with ResourceResolver

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

the class IncludeReferenceTest method testGetSet.

public void testGetSet() {
    VirtualFile included = myFixture.copyFileToProject("designer/included.xml", "res/layout/included.xml");
    assertNotNull(included);
    VirtualFile includer = myFixture.copyFileToProject("designer/included.xml", "res/layout/includer.xml");
    assertNotNull(includer);
    XmlFile psiFile = (XmlFile) PsiManager.getInstance(getProject()).findFile(included);
    assertNotNull(psiFile);
    assertEquals("@layout/includer", IncludeReference.getIncludingLayout(psiFile));
    ConfigurationManager manager = myFacet.getConfigurationManager();
    Configuration configuration = manager.getConfiguration(included);
    ResourceResolver resourceResolver = configuration.getResourceResolver();
    assertNotNull(resourceResolver);
    IncludeReference reference = IncludeReference.get(myModule, psiFile, resourceResolver);
    assertEquals("includer.xml", reference.getFromDisplayName());
    assertEquals("includer", reference.getFromResourceName());
    assertEquals("@layout/includer", reference.getFromResourceUrl());
    assertEquals(reference.getFromFile(), includer);
    assertEquals(reference.getToFile(), included);
    IncludeReference.setIncludingLayout(getProject(), psiFile, null);
    assertEquals(IncludeReference.NONE, IncludeReference.get(myModule, psiFile, resourceResolver));
    VirtualFile other = myFixture.copyFileToProject("xmlpull/designtime.xml", "res/layout-land/designtime.xml");
    assertNotNull(other);
    IncludeReference.setIncludingLayout(getProject(), psiFile, "@layout/designtime");
    assertEquals("@layout/designtime", IncludeReference.getIncludingLayout(psiFile));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) Configuration(com.android.tools.idea.configurations.Configuration) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager)

Example 40 with ResourceResolver

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

the class TextAppearanceEnumSupport method createFromResolvedValue.

@Override
@NotNull
protected ValueWithDisplayString createFromResolvedValue(@NotNull String resolvedValue, @Nullable String value, @Nullable String hint) {
    if (value != null && !value.startsWith(STYLE_RESOURCE_PREFIX) && !value.startsWith(ANDROID_STYLE_RESOURCE_PREFIX) && !value.startsWith(ATTR_REF_PREFIX)) {
        ResourceResolver resolver = myProperty.getResolver();
        ResourceValue resource = resolver.getStyle(value, true);
        if (resource == null) {
            resource = resolver.getStyle(value, false);
        }
        if (resource == null && !value.startsWith(TEXT_APPEARANCE)) {
            resource = resolver.getStyle(TEXT_APPEARANCE + "." + value, true);
        }
        if (resource == null && !value.startsWith(TEXT_APPEARANCE)) {
            resource = resolver.getStyle(TEXT_APPEARANCE + "." + value, false);
        }
        if (resource == null) {
            value = STYLE_RESOURCE_PREFIX + value;
        } else {
            value = (resource.isFramework() ? ANDROID_STYLE_RESOURCE_PREFIX : STYLE_RESOURCE_PREFIX) + resource.getName();
        }
    }
    String display = resolvedValue;
    Matcher matcher = TEXT_APPEARANCE_PATTERN.matcher(resolvedValue);
    if (matcher.matches()) {
        display = matcher.group(5);
        if (display == null) {
            display = TEXT_APPEARANCE;
        }
    } else {
        display = StringUtil.trimStart(display, ANDROID_STYLE_RESOURCE_PREFIX);
        display = StringUtil.trimStart(display, STYLE_RESOURCE_PREFIX);
    }
    return new ValueWithDisplayString(display, value, generateHint(display, value));
}
Also used : Matcher(java.util.regex.Matcher) 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