Search in sources :

Example 1 with ConfigurationManager

use of com.android.tools.idea.configurations.ConfigurationManager in project android by JetBrains.

the class ThemeEditorUtils method getModuleThemeQualifiedNamesList.

/**
   * Returns the list of the qualified names of all the user-defined themes available from a given module
   */
@NotNull
public static ImmutableList<String> getModuleThemeQualifiedNamesList(@NotNull Module module) {
    AndroidFacet facet = AndroidFacet.getInstance(module);
    assert facet != null;
    ConfigurationManager manager = facet.getConfigurationManager();
    // We create a new ResourceResolverCache instead of using cache from myConfiguration to optimize memory instead of time/speed,
    // because we are about to create a lot of instances of ResourceResolver here that won't be used outside of this method
    final ResourceResolverCache resolverCache = new ResourceResolverCache(manager);
    final IAndroidTarget target = manager.getTarget();
    final Map<ResourceValue, Boolean> cache = new HashMap<ResourceValue, Boolean>();
    final Set<String> themeNamesSet = Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER);
    ResourceFolderVisitor visitor = new ResourceFolderVisitor() {

        @Override
        public void visitResourceFolder(@NotNull LocalResourceRepository resources, String moduleName, @NotNull String variantName, boolean isSelected) {
            if (!isSelected) {
                return;
            }
            for (String simpleThemeName : resources.getItemsOfType(ResourceType.STYLE)) {
                String themeStyleResourceUrl = SdkConstants.STYLE_RESOURCE_PREFIX + simpleThemeName;
                List<ResourceItem> themeItems = resources.getResourceItem(ResourceType.STYLE, simpleThemeName);
                assert themeItems != null;
                for (ResourceItem themeItem : themeItems) {
                    ResourceResolver resolver = resolverCache.getResourceResolver(target, themeStyleResourceUrl, themeItem.getConfiguration());
                    ResourceValue themeItemResourceValue = themeItem.getResourceValue(false);
                    assert themeItemResourceValue != null;
                    if (resolver.isTheme(themeItemResourceValue, cache)) {
                        themeNamesSet.add(simpleThemeName);
                        break;
                    }
                }
            }
        }
    };
    acceptResourceResolverVisitor(facet, visitor);
    return ImmutableList.copyOf(themeNamesSet);
}
Also used : IAndroidTarget(com.android.sdklib.IAndroidTarget) NotNull(org.jetbrains.annotations.NotNull) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ResourceResolverCache(com.android.tools.idea.configurations.ResourceResolverCache) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ResourceItem(com.android.ide.common.res2.ResourceItem) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ConfigurationManager

use of com.android.tools.idea.configurations.ConfigurationManager in project android by JetBrains.

the class VariantItemListener method itemStateChanged.

@Override
public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() != ItemEvent.SELECTED) {
        return;
    }
    VariantsComboItem item = (VariantsComboItem) e.getItem();
    Configuration oldConfiguration = myContext.getConfiguration();
    ConfigurationManager manager = oldConfiguration.getConfigurationManager();
    Configuration newConfiguration = Configuration.create(manager, null, null, item.getRestrictedConfiguration());
    // Target and locale are global so we need to set them in the configuration manager when updated
    VersionQualifier newVersionQualifier = item.getRestrictedConfiguration().getVersionQualifier();
    if (newVersionQualifier != null) {
        IAndroidTarget realTarget = manager.getHighestApiTarget() != null ? manager.getHighestApiTarget() : manager.getTarget();
        assert realTarget != null;
        manager.setTarget(new CompatibilityRenderTarget(realTarget, newVersionQualifier.getVersion(), null));
    } else {
        manager.setTarget(null);
    }
    LocaleQualifier newLocaleQualifier = item.getRestrictedConfiguration().getLocaleQualifier();
    manager.setLocale(newLocaleQualifier != null ? Locale.create(newLocaleQualifier) : Locale.ANY);
    oldConfiguration.setDevice(null, false);
    Configuration.copyCompatible(newConfiguration, oldConfiguration);
    oldConfiguration.updated(ConfigurationListener.MASK_FOLDERCONFIG);
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) IAndroidTarget(com.android.sdklib.IAndroidTarget) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier)

Example 3 with ConfigurationManager

use of com.android.tools.idea.configurations.ConfigurationManager in project android by JetBrains.

the class FmGetApplicationThemeMethod method exec.

@Override
public Object exec(List arguments) throws TemplateModelException {
    String modulePath = (String) myParamMap.get(TemplateMetadata.ATTR_PROJECT_OUT);
    if (modulePath == null) {
        return null;
    }
    Module module = FmUtil.findModule(modulePath);
    if (module == null) {
        return null;
    }
    ThemeHelper helper = new ThemeHelper(module);
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        return null;
    }
    VirtualFile projectFile = module.getProject().getProjectFile();
    if (projectFile == null) {
        return null;
    }
    ConfigurationManager manager = facet.getConfigurationManager();
    Configuration configuration = manager.getConfiguration(projectFile);
    String themeName = helper.getAppThemeName();
    if (themeName == null) {
        return null;
    }
    Map<String, Object> map = Maps.newHashMap();
    map.put("name", themeName);
    map.put("isAppCompat", helper.isAppCompatTheme(themeName));
    Boolean hasActionBar = ThemeHelper.hasActionBar(configuration, themeName);
    addDerivedTheme(map, themeName, "NoActionBar", hasActionBar == Boolean.FALSE, helper, configuration);
    addDerivedTheme(map, themeName, "AppBarOverlay", false, helper, configuration);
    addDerivedTheme(map, themeName, "PopupOverlay", false, helper, configuration);
    return map;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ThemeHelper(com.android.tools.idea.npw.ThemeHelper) Configuration(com.android.tools.idea.configurations.Configuration) Module(com.intellij.openapi.module.Module) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 4 with ConfigurationManager

use of com.android.tools.idea.configurations.ConfigurationManager 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 5 with ConfigurationManager

use of com.android.tools.idea.configurations.ConfigurationManager in project android by JetBrains.

the class ThemeResolverTest method testLocalThemes.

public void testLocalThemes() throws IOException {
    VirtualFile myLayout = myFixture.copyFileToProject("themeEditor/layout.xml", "res/layout/layout.xml");
    VirtualFile myStyleFile = myFixture.copyFileToProject("themeEditor/styles.xml", "res/values/styles.xml");
    ConfigurationManager configurationManager = myFacet.getConfigurationManager();
    Configuration configuration = configurationManager.getConfiguration(myLayout);
    ThemeResolver themeResolver = new ThemeResolver(configuration);
    // We don't have any libraries so this will only include the project theme
    assertEquals(1, themeResolver.getLocalThemes().size());
    // No library themes
    assertEquals(0, themeResolver.getExternalLibraryThemes().size());
    assertNull("The theme is an app theme and shouldn't be returned for the android namespace", themeResolver.getTheme("android:Theme.MyTheme"));
    ConfiguredThemeEditorStyle theme = themeResolver.getTheme("Theme.MyTheme");
    assertEquals("Theme.MyTheme", theme.getName());
    assertEquals("Theme", theme.getParent().getName());
    assertEquals(1, theme.getConfiguredValues().size());
    ConfiguredElement<ItemResourceValue> value = Iterables.get(theme.getConfiguredValues(), 0);
    assertEquals("windowBackground", value.getElement().getName());
    assertEquals("@drawable/pic", value.getElement().getValue());
    // Modify a value.
    theme.setValue("android:windowBackground", "@drawable/other");
    FileDocumentManager.getInstance().saveAllDocuments();
    assertFalse(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/pic"));
    assertTrue(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/other"));
    // Add a value.
    theme.setValue("android:windowBackground2", "@drawable/second_background");
    FileDocumentManager.getInstance().saveAllDocuments();
    assertTrue(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/other"));
    assertTrue(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/second_background"));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Configuration(com.android.tools.idea.configurations.Configuration) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Aggregations

ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)20 Configuration (com.android.tools.idea.configurations.Configuration)14 VirtualFile (com.intellij.openapi.vfs.VirtualFile)14 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)9 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)8 IAndroidTarget (com.android.sdklib.IAndroidTarget)6 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)5 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)4 NotNull (org.jetbrains.annotations.NotNull)4 Module (com.intellij.openapi.module.Module)3 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)2 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 ResourceItem (com.android.ide.common.res2.ResourceItem)1 ResourceItem (com.android.ide.common.resources.ResourceItem)1 ResourceRepository (com.android.ide.common.resources.ResourceRepository)1 Configurable (com.android.ide.common.resources.configuration.Configurable)1 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)1 Device (com.android.sdklib.devices.Device)1