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);
}
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);
}
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;
}
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");
}
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"));
}
Aggregations