Search in sources :

Example 6 with CompatibilityRenderTarget

use of com.android.tools.idea.rendering.multi.CompatibilityRenderTarget in project android by JetBrains.

the class ConfiguredThemeEditorStyleTest method testGetParentNames.

public void testGetParentNames() {
    myFixture.copyFileToProject("themeEditor/attributeResolution/styles_base.xml", "res/values/styles.xml");
    myFixture.copyFileToProject("themeEditor/attributeResolution/styles-v17.xml", "res/values-v17/styles.xml");
    myFixture.copyFileToProject("themeEditor/attributeResolution/styles-v19.xml", "res/values-v19/styles.xml");
    VirtualFile file = myFixture.copyFileToProject("themeEditor/attributeResolution/styles-v20.xml", "res/values-v20/styles.xml");
    ConfigurationManager configurationManager = myFacet.getConfigurationManager();
    Configuration configuration = configurationManager.getConfiguration(file);
    ThemeResolver resolver = new ThemeResolver(configuration);
    ConfiguredThemeEditorStyle theme = resolver.getTheme("AppTheme");
    assertNotNull(theme);
    Collection<ConfiguredElement<String>> parents = theme.getParentNames();
    assertSize(2, parents);
    ImmutableList<String> parentNames = ImmutableList.of(Iterables.get(parents, 0).getElement(), Iterables.get(parents, 1).getElement());
    assertContainsElements(parentNames, "Base.V20", "Base.V17");
    // Set API 17 and try the same resolution
    //noinspection ConstantConditions
    configuration.setTarget(new CompatibilityRenderTarget(configurationManager.getHighestApiTarget(), 17, configurationManager.getHighestApiTarget()));
    parents = theme.getParentNames();
    assertSize(2, parents);
    parentNames = ImmutableList.of(Iterables.get(parents, 0).getElement(), Iterables.get(parents, 1).getElement());
    assertContainsElements(parentNames, "Base.V20", "Base.V17");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ConfiguredElement(com.android.tools.idea.editors.theme.datamodels.ConfiguredElement) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Example 7 with CompatibilityRenderTarget

use of com.android.tools.idea.rendering.multi.CompatibilityRenderTarget in project android by JetBrains.

the class AndroidTargetData method getLayoutLibrary.

@Nullable
public synchronized LayoutLibrary getLayoutLibrary(@NotNull Project project) throws RenderingException, IOException {
    if (myLayoutLibrary == null) {
        if (myTarget instanceof CompatibilityRenderTarget) {
            IAndroidTarget target = ((CompatibilityRenderTarget) myTarget).getRenderTarget();
            AndroidTargetData targetData = mySdkData.getTargetData(target);
            if (targetData != this) {
                myLayoutLibrary = targetData.getLayoutLibrary(project);
                return myLayoutLibrary;
            }
        }
        final AttributeDefinitionsImpl attrDefs = getAllAttrDefs(project);
        if (attrDefs == null) {
            return null;
        }
        myLayoutLibrary = LayoutLibraryLoader.load(myTarget, attrDefs.getEnumMap());
    }
    return myLayoutLibrary;
}
Also used : CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) AttributeDefinitionsImpl(org.jetbrains.android.dom.attrs.AttributeDefinitionsImpl) IAndroidTarget(com.android.sdklib.IAndroidTarget) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with CompatibilityRenderTarget

use of com.android.tools.idea.rendering.multi.CompatibilityRenderTarget in project android by JetBrains.

the class GraphicsLayoutRenderer method create.

@VisibleForTesting
@NotNull
static GraphicsLayoutRenderer create(@NotNull AndroidFacet facet, @NotNull AndroidPlatform platform, @NotNull Project project, @NotNull Configuration configuration, @NotNull ILayoutPullParser parser, @Nullable Color backgroundColor, @NotNull SessionParams.RenderingMode renderingMode, boolean useSecurityManager) throws InitializationException {
    AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(facet);
    LayoutLibrary layoutLib;
    try {
        IAndroidTarget latestTarget = configuration.getConfigurationManager().getHighestApiTarget();
        if (latestTarget == null) {
            throw new UnsupportedLayoutlibException("GraphicsLayoutRenderer requires at least layoutlib version " + MIN_LAYOUTLIB_API_VERSION);
        }
        layoutLib = platform.getSdkData().getTargetData(latestTarget).getLayoutLibrary(project);
        if (layoutLib == null) {
            throw new InitializationException("getLayoutLibrary() returned null");
        }
    } catch (RenderingException e) {
        throw new InitializationException(e);
    } catch (IOException e) {
        throw new InitializationException(e);
    }
    if (layoutLib.getApiLevel() < MIN_LAYOUTLIB_API_VERSION) {
        throw new UnsupportedLayoutlibException("GraphicsLayoutRenderer requires at least layoutlib version " + MIN_LAYOUTLIB_API_VERSION);
    }
    AppResourceRepository appResources = AppResourceRepository.getAppResources(facet, true);
    final Module module = facet.getModule();
    // Security token used to disable the security manager. Only objects that have a reference to it are allowed to disable it.
    Object credential = new Object();
    RenderLogger logger = new RenderLogger("theme_editor", module, credential);
    final ActionBarCallback actionBarCallback = new ActionBarCallback();
    // TODO: Remove LayoutlibCallback dependency.
    //noinspection ConstantConditions
    final LayoutlibCallbackImpl layoutlibCallback = new LayoutlibCallbackImpl(null, layoutLib, appResources, module, facet, logger, credential, null) {

        @Override
        public ActionBarCallback getActionBarCallback() {
            return actionBarCallback;
        }
    };
    // Load the local project R identifiers.
    boolean loadRResult = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

        @Override
        public Boolean compute() {
            // half way.
            if (module.isDisposed()) {
                return false;
            }
            layoutlibCallback.loadAndParseRClass();
            return true;
        }
    });
    if (!loadRResult) {
        throw new AlreadyDisposedException("Module was already disposed");
    }
    IAndroidTarget target = configuration.getTarget();
    if (target == null) {
        throw new InitializationException("Unable to get IAndroidTarget");
    }
    Device device = configuration.getDevice();
    assert device != null;
    HardwareConfigHelper hardwareConfigHelper = new HardwareConfigHelper(device);
    DynamicHardwareConfig hardwareConfig = new DynamicHardwareConfig(hardwareConfigHelper.getConfig());
    List<ResourceValue> resourceLookupChain = new ArrayList<ResourceValue>();
    ResourceResolver resourceResolver = ResourceResolver.copy(configuration.getResourceResolver());
    assert resourceResolver != null;
    // Create a resource resolver that will save the lookups on the passed List<>
    ResourceResolver recordingResourceResolver = resourceResolver.createRecorder(resourceLookupChain);
    final SessionParams params = new SessionParams(parser, renderingMode, module, hardwareConfig, recordingResourceResolver, layoutlibCallback, moduleInfo.getMinSdkVersion().getApiLevel(), moduleInfo.getTargetSdkVersion().getApiLevel(), logger, target instanceof CompatibilityRenderTarget ? target.getVersion().getApiLevel() : 0);
    params.setForceNoDecor();
    params.setAssetRepository(new AssetRepositoryImpl(facet));
    // The App Label needs to be not null
    params.setAppLabel("");
    if (backgroundColor != null) {
        params.setOverrideBgColor(backgroundColor.getRGB());
    }
    RenderSecurityManager mySecurityManager = useSecurityManager ? RenderSecurityManagerFactory.create(module, platform) : null;
    return new GraphicsLayoutRenderer(layoutLib, params, mySecurityManager, hardwareConfig, resourceLookupChain, credential);
}
Also used : LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) AssetRepositoryImpl(com.android.tools.idea.res.AssetRepositoryImpl) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) HardwareConfigHelper(com.android.ide.common.rendering.HardwareConfigHelper) Device(com.android.sdklib.devices.Device) RenderingException(com.android.tools.idea.layoutlib.RenderingException) IAndroidTarget(com.android.sdklib.IAndroidTarget) IOException(java.io.IOException) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) ResourceResolver(com.android.ide.common.resources.ResourceResolver) Module(com.intellij.openapi.module.Module) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with CompatibilityRenderTarget

use of com.android.tools.idea.rendering.multi.CompatibilityRenderTarget in project android by JetBrains.

the class MergedManifest method getDefaultTheme.

/**
   * Returns the default theme for this project, by looking at the manifest default
   * theme registration, target SDK, rendering target, etc.
   *
   * @param renderingTarget the rendering target use to render the theme, or null
   * @param screenSize      the screen size to obtain a default theme for, or null if unknown
   * @param device          the device to obtain a default theme for, or null if unknown
   * @return the theme to use for this project, never null
   */
@NotNull
public String getDefaultTheme(@Nullable IAndroidTarget renderingTarget, @Nullable ScreenSize screenSize, @Nullable Device device) {
    sync();
    if (myManifestTheme != null) {
        return myManifestTheme;
    }
    // For Android Wear and Android TV, the defaults differ
    if (device != null) {
        if (HardwareConfigHelper.isWear(device)) {
            return "@android:style/Theme.DeviceDefault.Light";
        } else if (HardwareConfigHelper.isTv(device)) {
            //noinspection SpellCheckingInspection
            return "@style/Theme.Leanback";
        }
    }
    // From manifest theme documentation:
    // "If that attribute is also not set, the default system theme is used."
    int targetSdk;
    AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assert facet != null;
    AndroidModuleInfo info = facet.getAndroidModuleInfo();
    targetSdk = info.getTargetSdkVersion().getApiLevel();
    int renderingTargetSdk = targetSdk;
    if (renderingTarget instanceof CompatibilityRenderTarget) {
        renderingTargetSdk = renderingTarget.getVersion().getApiLevel();
    //targetSdk = SdkVersionInfo.HIGHEST_KNOWN_API
    } else if (renderingTarget != null) {
        renderingTargetSdk = renderingTarget.getVersion().getApiLevel();
    }
    int apiLevel = Math.min(targetSdk, renderingTargetSdk);
    if (apiLevel >= 21) {
        //$NON-NLS-1$
        return ANDROID_STYLE_RESOURCE_PREFIX + "Theme.Material.Light";
    } else if (apiLevel >= 14 || apiLevel >= 11 && screenSize == ScreenSize.XLARGE) {
        //$NON-NLS-1$
        return ANDROID_STYLE_RESOURCE_PREFIX + "Theme.Holo";
    } else {
        //$NON-NLS-1$
        return ANDROID_STYLE_RESOURCE_PREFIX + "Theme";
    }
}
Also used : CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with CompatibilityRenderTarget

use of com.android.tools.idea.rendering.multi.CompatibilityRenderTarget 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

CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)12 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Configuration (com.android.tools.idea.configurations.Configuration)5 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)4 NotNull (org.jetbrains.annotations.NotNull)4 ResourceResolver (com.android.ide.common.resources.ResourceResolver)3 Module (com.intellij.openapi.module.Module)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Nullable (org.jetbrains.annotations.Nullable)3 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)2 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)2 Device (com.android.sdklib.devices.Device)2 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)2 IOException (java.io.IOException)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 HardwareConfigHelper (com.android.ide.common.rendering.HardwareConfigHelper)1 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)1 ResourceRepository (com.android.ide.common.resources.ResourceRepository)1 ResourceValueMap (com.android.ide.common.resources.ResourceValueMap)1 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)1