Search in sources :

Example 21 with ResourceResolver

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

the class ConstraintComponentUtilities 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 22 with ResourceResolver

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

the class Main method getSessionParams.

/**
     * Uses Theme.Material and Target sdk version as 22.
     */
private SessionParams getSessionParams(LayoutPullParser layoutParser, ConfigGenerator configGenerator, LayoutLibTestCallback layoutLibCallback, String themeName, boolean isProjectTheme, RenderingMode renderingMode, int targetSdk) {
    FolderConfiguration config = configGenerator.getFolderConfig();
    ResourceResolver resourceResolver = ResourceResolver.create(sProjectResources.getConfiguredResources(config), sFrameworkRepo.getConfiguredResources(config), themeName, isProjectTheme);
    SessionParams sessionParams = new SessionParams(layoutParser, renderingMode, null, /*used for caching*/
    configGenerator.getHardwareConfig(), resourceResolver, layoutLibCallback, 0, targetSdk, getLayoutLog());
    sessionParams.setFlag(RenderParamsFlags.FLAG_DO_NOT_RENDER_ON_CREATE, true);
    return sessionParams;
}
Also used : SessionParams(com.android.ide.common.rendering.api.SessionParams) ResourceResolver(com.android.ide.common.resources.ResourceResolver) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 23 with ResourceResolver

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

the class Main method getSessionParams.

/**
     * Uses Theme.Material and Target sdk version as 22.
     */
private SessionParams getSessionParams(LayoutPullParser layoutParser, ConfigGenerator configGenerator, LayoutLibTestCallback layoutLibCallback, String themeName, boolean isProjectTheme, RenderingMode renderingMode, int targetSdk) {
    FolderConfiguration config = configGenerator.getFolderConfig();
    ResourceResolver resourceResolver = ResourceResolver.create(sProjectResources.getConfiguredResources(config), sFrameworkRepo.getConfiguredResources(config), themeName, isProjectTheme);
    SessionParams sessionParams = new SessionParams(layoutParser, renderingMode, null, /*used for caching*/
    configGenerator.getHardwareConfig(), resourceResolver, layoutLibCallback, 0, targetSdk, getLayoutLog());
    sessionParams.setFlag(RenderParamsFlags.FLAG_DO_NOT_RENDER_ON_CREATE, true);
    return sessionParams;
}
Also used : SessionParams(com.android.ide.common.rendering.api.SessionParams) ResourceResolver(com.android.ide.common.resources.ResourceResolver) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 24 with ResourceResolver

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

the class RenderErrorContributor method reportMissingSizeAttributes.

private void reportMissingSizeAttributes(@NotNull final RenderLogger logger, @NotNull RenderTask renderTask) {
    Module module = logger.getModule();
    if (module == null) {
        return;
    }
    Project project = module.getProject();
    if (logger.isMissingSize()) {
        HtmlBuilder builder = new HtmlBuilder();
        // Emit hyperlink about missing attributes; the action will operate on all of them
        builder.addBold("NOTE: One or more layouts are missing the layout_width or layout_height attributes. " + "These are required in most layouts.").newline();
        final ResourceResolver resourceResolver = renderTask.getResourceResolver();
        XmlFile psiFile = renderTask.getPsiFile();
        if (psiFile == null) {
            LOG.error("PsiFile is missing in RenderTask used in RenderErrorPanel!");
            return;
        }
        AddMissingAttributesFix fix = new AddMissingAttributesFix(project, psiFile, resourceResolver);
        List<XmlTag> missing = fix.findViewsMissingSizes();
        // See whether we should offer match_parent instead of fill_parent
        AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(module);
        final String fill = moduleInfo == null || moduleInfo.getBuildSdkVersion() == null || moduleInfo.getBuildSdkVersion().getApiLevel() >= 8 ? VALUE_MATCH_PARENT : VALUE_FILL_PARENT;
        for (final XmlTag tag : missing) {
            ApplicationManager.getApplication().runReadAction(() -> {
                boolean missingWidth = !AddMissingAttributesFix.definesWidth(tag, resourceResolver);
                boolean missingHeight = !AddMissingAttributesFix.definesHeight(tag, resourceResolver);
                assert missingWidth || missingHeight;
                String id = tag.getAttributeValue(ATTR_ID);
                if (id == null || id.length() == 0) {
                    id = '<' + tag.getName() + '>';
                } else {
                    id = '"' + stripIdPrefix(id) + '"';
                }
                if (missingWidth) {
                    reportMissingSize(builder, logger, fill, tag, id, ATTR_LAYOUT_WIDTH);
                }
                if (missingHeight) {
                    reportMissingSize(builder, logger, fill, tag, id, ATTR_LAYOUT_HEIGHT);
                }
            });
        }
        builder.newline().add("Or: ").addLink("Automatically add all missing attributes", myLinkManager.createCommandLink(fix)).newline().newline().newline();
        addIssue().setSeverity(HighlightSeverity.ERROR).setSummary("One or more layouts are missing the layout_width or layout_height attributes").setHtmlContent(builder).build();
    }
}
Also used : AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) HtmlBuilder(com.android.utils.HtmlBuilder) ResourceResolver(com.android.ide.common.resources.ResourceResolver) Module(com.intellij.openapi.module.Module) XmlTag(com.intellij.psi.xml.XmlTag)

Example 25 with ResourceResolver

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

the class ResourceResolverCache method getResourceResolver.

@NotNull
public ResourceResolver getResourceResolver(@Nullable IAndroidTarget target, @NotNull String themeStyle, @NotNull final FolderConfiguration fullConfiguration) {
    // Are caches up to date?
    final LocalResourceRepository resources = AppResourceRepository.getAppResources(myManager.getModule(), true);
    assert resources != null;
    if (myCachedGeneration != resources.getModificationCount()) {
        myResolverMap.clear();
        myAppResourceMap.clear();
    }
    // Store the modification count as soon as possible. This ensures that if there is any modification of resources while the
    // resolver is being created, it will be cleared subsequently.
    myCachedGeneration = resources.getModificationCount();
    // When looking up the configured project and framework resources, the theme doesn't matter, so we look up only
    // by the configuration qualifiers; for example, here's a sample key:
    // -ldltr-sw384dp-w384dp-h640dp-normal-notlong-port-notnight-xhdpi-finger-keyssoft-nokeys-navhidden-nonav-1280x768-v17
    // Note that the target version is already baked in via the -v qualifier.
    //
    // However, the resource resolver also depends on the theme, so we use a more specific key for the resolver map than
    // for the configured resource maps, by prepending the theme name:
    // @style/MyTheme-ldltr-sw384dp-w384dp-h640dp-normal-notlong-port-notnight-xhdpi-finger-keyssoft-nokeys-navhidden-nonav-1280x768-v17
    String configurationKey = fullConfiguration.getUniqueKey();
    String resolverKey = themeStyle + configurationKey;
    ResourceResolver resolver = myResolverMap.get(resolverKey);
    if (resolver == null) {
        Map<ResourceType, ResourceValueMap> configuredAppRes;
        Map<ResourceType, ResourceValueMap> frameworkResources;
        // Framework resources
        if (target == null) {
            target = myManager.getTarget();
        }
        if (target == null) {
            frameworkResources = Collections.emptyMap();
        } else {
            ResourceRepository frameworkRes = getFrameworkResources(fullConfiguration, target);
            if (frameworkRes == null) {
                frameworkResources = Collections.emptyMap();
            } else {
                // get the framework resource values based on the current config
                frameworkResources = myFrameworkResourceMap.get(configurationKey);
                if (frameworkResources == null) {
                    frameworkResources = frameworkRes.getConfiguredResources(fullConfiguration);
                    // assets replaced the look for the same theme; that doesn't happen to the same extend for Holo)
                    if (target instanceof CompatibilityRenderTarget && target.getVersion().getApiLevel() == 8) {
                        IAndroidTarget realTarget = ((CompatibilityRenderTarget) target).getRealTarget();
                        if (realTarget != null) {
                            replaceDrawableBitmaps(frameworkResources, target, realTarget);
                        }
                    }
                    myFrameworkResourceMap.put(configurationKey, frameworkResources);
                }
            }
        }
        // App resources
        configuredAppRes = myAppResourceMap.get(configurationKey);
        if (configuredAppRes == null) {
            // get the project resource values based on the current config
            Application application = ApplicationManager.getApplication();
            configuredAppRes = application.runReadAction(new Computable<Map<ResourceType, ResourceValueMap>>() {

                @Override
                public Map<ResourceType, ResourceValueMap> compute() {
                    return resources.getConfiguredResources(fullConfiguration);
                }
            });
            myAppResourceMap.put(configurationKey, configuredAppRes);
        }
        // Resource Resolver
        assert themeStyle.startsWith(PREFIX_RESOURCE_REF) : themeStyle;
        boolean isProjectTheme = ResourceHelper.isProjectStyle(themeStyle);
        String themeName = ResourceHelper.styleToTheme(themeStyle);
        resolver = ResourceResolver.create(configuredAppRes, frameworkResources, themeName, isProjectTheme);
        if (target instanceof CompatibilityRenderTarget) {
            int apiLevel = target.getVersion().getFeatureLevel();
            if (apiLevel >= 21) {
                resolver.setDeviceDefaults("Material");
            } else if (apiLevel >= 14) {
                resolver.setDeviceDefaults("Holo");
            } else {
                resolver.setDeviceDefaults(ResourceResolver.LEGACY_THEME);
            }
        }
        myResolverMap.put(resolverKey, resolver);
    }
    return resolver;
}
Also used : ResourceType(com.android.resources.ResourceType) IAndroidTarget(com.android.sdklib.IAndroidTarget) ResourceValueMap(com.android.ide.common.resources.ResourceValueMap) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceResolver(com.android.ide.common.resources.ResourceResolver) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceRepository(com.android.ide.common.resources.ResourceRepository) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) Application(com.intellij.openapi.application.Application) Computable(com.intellij.openapi.util.Computable) 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