Search in sources :

Example 1 with ResourceValueMap

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

the class ResourceResolverCache method replaceDrawableBitmaps.

/**
   * Replaces drawable bitmaps with those from the real older target. This helps the simulated platform look more genuine,
   * since a lot of the look comes from the nine patch assets. For example, when used to simulate Froyo, the checkboxes
   * will look better than if we use the current classic theme assets, which look like gingerbread.
   */
private static void replaceDrawableBitmaps(@NotNull Map<ResourceType, ResourceValueMap> frameworkResources, @NotNull IAndroidTarget from, @NotNull IAndroidTarget realTarget) {
    // This is a bit hacky; we should be operating at the resource repository level rather than
    // for configured resources. However, we may not need this for very long.
    ResourceValueMap map = frameworkResources.get(ResourceType.DRAWABLE);
    String oldPrefix = from.getPath(IAndroidTarget.RESOURCES);
    String newPrefix = realTarget.getPath(IAndroidTarget.RESOURCES);
    if (map == null || map.isEmpty() || oldPrefix == null || newPrefix == null || oldPrefix.equals(newPrefix)) {
        return;
    }
    Collection<ResourceValue> values = map.values();
    Map<String, String> densityDirMap = Maps.newHashMap();
    // Leave XML drawable resources alone since they can reference nonexistent colors and other resources
    // not available in the real rendering platform
    final boolean ONLY_REPLACE_BITMAPS = true;
    Density[] densities = Density.values();
    for (ResourceValue value : values) {
        String v = value.getValue();
        //noinspection ConstantConditions,PointlessBooleanExpression
        if (v != null && (!ONLY_REPLACE_BITMAPS || v.endsWith(DOT_PNG))) {
            if (v.startsWith(oldPrefix)) {
                String relative = v.substring(oldPrefix.length());
                if (v.endsWith(DOT_PNG)) {
                    int index = relative.indexOf(File.separatorChar);
                    if (index == -1) {
                        index = relative.indexOf('/');
                    }
                    if (index == -1) {
                        continue;
                    }
                    String parent = relative.substring(0, index);
                    String replace = densityDirMap.get(parent);
                    if (replace == null) {
                        FolderConfiguration configuration = FolderConfiguration.getConfigForFolder(parent);
                        if (configuration != null) {
                            DensityQualifier densityQualifier = configuration.getDensityQualifier();
                            if (densityQualifier != null) {
                                Density density = densityQualifier.getValue();
                                if (!new File(newPrefix, parent).exists()) {
                                    String oldQualifier = SdkConstants.RES_QUALIFIER_SEP + density.getResourceValue();
                                    String matched = null;
                                    for (Density d : densities) {
                                        if (d.ordinal() <= density.ordinal()) {
                                            // No reason to check higher
                                            continue;
                                        }
                                        String newQualifier = SdkConstants.RES_QUALIFIER_SEP + d.getResourceValue();
                                        String newName = parent.replace(oldQualifier, newQualifier);
                                        File dir = new File(newPrefix, newName);
                                        if (dir.exists()) {
                                            matched = newName;
                                            break;
                                        }
                                    }
                                    if (matched == null) {
                                        continue;
                                    }
                                    replace = matched;
                                    // This isn't right; there may be some assets only in mdpi!
                                    densityDirMap.put(parent, replace);
                                }
                            }
                        }
                    }
                    relative = replace + relative.substring(index);
                }
                File newFile = new File(newPrefix, relative);
                if (newFile.exists()) {
                    value.setValue(newFile.getPath());
                }
            }
        }
    }
}
Also used : ResourceValue(com.android.ide.common.rendering.api.ResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Density(com.android.resources.Density) File(java.io.File) DensityQualifier(com.android.ide.common.resources.configuration.DensityQualifier) ResourceValueMap(com.android.ide.common.resources.ResourceValueMap)

Example 2 with ResourceValueMap

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

the class StyleFilter method getWidgetStyleNames.

@NotNull
private static List<String> getWidgetStyleNames(@NotNull Project project, @NotNull ResourceResolver resolver, @NotNull String tagName) {
    ViewHandlerManager manager = ViewHandlerManager.get(project);
    ViewHandler handler = manager.getHandler(tagName);
    if (handler == null) {
        return Collections.emptyList();
    }
    List<String> possibleNames = handler.getBaseStyles(tagName);
    ResourceValueMap frameworkStyles = resolver.getFrameworkResources().get(ResourceType.STYLE);
    ResourceValueMap projectStyles = resolver.getProjectResources().get(ResourceType.STYLE);
    // This is usually a small list
    List<String> names = new ArrayList<>(4);
    for (String styleName : possibleNames) {
        if (frameworkStyles.containsKey(styleName)) {
            names.add(SdkConstants.PREFIX_ANDROID + styleName);
        }
        if (projectStyles.containsKey(styleName)) {
            names.add(styleName);
        }
    }
    return names;
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ResourceValueMap(com.android.ide.common.resources.ResourceValueMap) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ResourceValueMap

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

the class EnumSupportFactoryTest method testStyle.

public void testStyle() {
    StyleResourceValue checkboxStyle = new StyleResourceValue(ResourceType.STYLE, "Widget.CompoundButton.CheckBox", true);
    ResourceValueMap frameworkResources = ResourceValueMap.create();
    frameworkResources.put(checkboxStyle.getName(), checkboxStyle);
    ResourceValueMap projectResources = ResourceValueMap.create();
    when(myProperty.getTagName()).thenReturn(CHECK_BOX);
    when(myResolver.getFrameworkResources()).thenReturn(Collections.singletonMap(ResourceType.STYLE, frameworkResources));
    when(myResolver.getProjectResources()).thenReturn(Collections.singletonMap(ResourceType.STYLE, projectResources));
    checkSupported(ATTR_STYLE, StyleEnumSupport.class);
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValueMap(com.android.ide.common.resources.ResourceValueMap)

Example 4 with ResourceValueMap

use of com.android.ide.common.resources.ResourceValueMap 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

ResourceValueMap (com.android.ide.common.resources.ResourceValueMap)4 NotNull (org.jetbrains.annotations.NotNull)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)1 ResourceRepository (com.android.ide.common.resources.ResourceRepository)1 ResourceResolver (com.android.ide.common.resources.ResourceResolver)1 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)1 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)1 Density (com.android.resources.Density)1 ResourceType (com.android.resources.ResourceType)1 IAndroidTarget (com.android.sdklib.IAndroidTarget)1 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)1 ViewHandler (com.android.tools.idea.uibuilder.api.ViewHandler)1 ViewHandlerManager (com.android.tools.idea.uibuilder.handlers.ViewHandlerManager)1 Application (com.intellij.openapi.application.Application)1 Computable (com.intellij.openapi.util.Computable)1 File (java.io.File)1