Search in sources :

Example 6 with ResourceRepository

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

the class Main method setUp.

/**
     * Initialize the bridge and the resource maps.
     */
@BeforeClass
public static void setUp() {
    File data_dir = new File(PLATFORM_DIR, "data");
    File res = new File(data_dir, "res");
    sFrameworkRepo = new FrameworkResources(new FolderWrapper(res));
    sFrameworkRepo.loadResources();
    sFrameworkRepo.loadPublicResources(getLogger());
    sProjectResources = new ResourceRepository(new FolderWrapper(TEST_RES_DIR + APP_TEST_RES), false) {

        @NonNull
        @Override
        protected ResourceItem createResourceItem(@NonNull String name) {
            return new ResourceItem(name);
        }
    };
    sProjectResources.loadResources();
    File fontLocation = new File(data_dir, "fonts");
    File buildProp = new File(PLATFORM_DIR, "build.prop");
    File attrs = new File(res, "values" + File.separator + "attrs.xml");
    sBridge = new Bridge();
    sBridge.init(ConfigGenerator.loadProperties(buildProp), fontLocation, ConfigGenerator.getEnumMap(attrs), getLayoutLog());
}
Also used : FrameworkResources(com.android.ide.common.resources.FrameworkResources) NonNull(android.annotation.NonNull) FolderWrapper(com.android.io.FolderWrapper) ResourceRepository(com.android.ide.common.resources.ResourceRepository) ResourceItem(com.android.ide.common.resources.ResourceItem) File(java.io.File) Bridge(com.android.layoutlib.bridge.Bridge) BeforeClass(org.junit.BeforeClass)

Example 7 with ResourceRepository

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

Example 8 with ResourceRepository

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

the class AndroidColorAnnotator method findResourceValue.

/** Looks up the resource item of the given type and name for the given configuration, if any */
@Nullable
private static ResourceValue findResourceValue(ResourceType type, String name, boolean isFramework, Module module, Configuration configuration) {
    if (isFramework) {
        ResourceRepository frameworkResources = configuration.getFrameworkResources();
        if (frameworkResources == null) {
            return null;
        }
        if (!frameworkResources.hasResourceItem(type, name)) {
            return null;
        }
        ResourceItem item = frameworkResources.getResourceItem(type, name);
        return item.getResourceValue(type, configuration.getFullConfig(), false);
    } else {
        LocalResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
        if (appResources == null) {
            return null;
        }
        if (!appResources.hasResourceItem(type, name)) {
            return null;
        }
        return appResources.getConfiguredValue(type, name, configuration.getFullConfig());
    }
}
Also used : LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceRepository(com.android.ide.common.resources.ResourceRepository) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.resources.ResourceItem) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with ResourceRepository

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

the class ResourceDrawablePanel method updateResolutionChain.

private void updateResolutionChain(@NotNull ResourceChooserItem item) {
    // Resource resolver
    myResolvedPanel.removeAll();
    ResourceValue resourceValue = item.getResourceValue();
    Configuration configuration = myDialog.getConfiguration();
    ResourceRepository frameworkResources = configuration.getFrameworkResources();
    if (frameworkResources != null) {
        AppResourceRepository appResources = AppResourceRepository.getAppResources(myDialog.geFacet(), true);
        ResourceItemResolver resolver = new ResourceItemResolver(configuration.getFullConfig(), frameworkResources, appResources, null);
        List<ResourceValue> lookupChain = Lists.newArrayList();
        lookupChain.add(resourceValue);
        resolver.setLookupChainList(lookupChain);
        resolver.resolveResValue(resourceValue);
        String prev = null;
        int indent = 0;
        if (lookupChain.size() >= 2) {
            for (ResourceValue element : lookupChain) {
                if (element == null) {
                    continue;
                }
                String value = element.getValue();
                if (value == null) {
                    continue;
                }
                String text = value;
                if (text.equals(prev)) {
                    continue;
                }
                // Strip paths
                if (!(text.startsWith(PREFIX_THEME_REF) || text.startsWith(PREFIX_RESOURCE_REF))) {
                    if (indent == 0) {
                        break;
                    }
                    int end = Math.max(text.lastIndexOf('/'), text.lastIndexOf('\\'));
                    if (end != -1) {
                        text = text.substring(end + 1);
                    }
                }
                if (indent > 0) {
                    // 21D2: Rightwards arrow
                    text = "⇒ " + text;
                }
                JBLabel label = new JBLabel(text);
                label.setBorder(IdeBorderFactory.createEmptyBorder(0, JBUI.scale(indent * 12), 0, 0));
                myResolvedPanel.add(label);
                indent++;
                prev = value;
            }
        }
    }
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) JBLabel(com.intellij.ui.components.JBLabel) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceItemResolver(com.android.ide.common.resources.ResourceItemResolver) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceRepository(com.android.ide.common.resources.ResourceRepository)

Example 10 with ResourceRepository

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

the class StateListPicker method doValidate.

/**
   * Returns a {@link ValidationInfo} in the case one of the state list state has a value that does not resolve to a valid resource,
   * or a value that is a private framework value. or if one of the state list component requires an API level higher than minApi.
   */
@Nullable
public /*if there is no error*/
ValidationInfo doValidate(int minApi) {
    IAndroidTarget target = myConfiguration.getRealTarget();
    assert target != null;
    final AndroidTargetData androidTargetData = AndroidTargetData.getTargetData(target, myModule);
    assert androidTargetData != null;
    ResourceRepository frameworkResources = myConfiguration.getFrameworkResources();
    assert frameworkResources != null;
    for (StateComponent component : myStateComponents) {
        ValidationInfo error = component.doValidate(minApi, androidTargetData);
        if (error != null) {
            return error;
        }
    }
    return null;
}
Also used : ValidationInfo(com.intellij.openapi.ui.ValidationInfo) IAndroidTarget(com.android.sdklib.IAndroidTarget) ResourceRepository(com.android.ide.common.resources.ResourceRepository) AndroidTargetData(org.jetbrains.android.sdk.AndroidTargetData) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ResourceRepository (com.android.ide.common.resources.ResourceRepository)11 ResourceItem (com.android.ide.common.resources.ResourceItem)7 NonNull (android.annotation.NonNull)5 FrameworkResources (com.android.ide.common.resources.FrameworkResources)5 FolderWrapper (com.android.io.FolderWrapper)5 Bridge (com.android.layoutlib.bridge.Bridge)5 File (java.io.File)5 BeforeClass (org.junit.BeforeClass)5 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)4 IAndroidTarget (com.android.sdklib.IAndroidTarget)3 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)3 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 ResourceItemResolver (com.android.ide.common.resources.ResourceItemResolver)1 ResourceValueMap (com.android.ide.common.resources.ResourceValueMap)1 Configurable (com.android.ide.common.resources.configuration.Configurable)1 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)1 ResourceType (com.android.resources.ResourceType)1