use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class ConfigurationManager method createSimilar.
/**
* Similar to {@link #getConfiguration(VirtualFile)}, but creates a configuration
* for a file known to be new, and crucially, bases the configuration on the existing configuration
* for a known file. This is intended for when you fork a layout, and you expect the forked layout
* to have a configuration that is (as much as possible) similar to the configuration of the
* forked file. For example, if you create a landscape version of a layout, it will preserve the
* screen size, locale, theme and render target of the existing layout.
*
* @param file the file to create a configuration for
* @param baseFile the other file to base the configuration on
* @return the new configuration
*/
@NotNull
public Configuration createSimilar(@NotNull VirtualFile file, @NotNull VirtualFile baseFile) {
ConfigurationStateManager stateManager = getStateManager();
ConfigurationFileState fileState = stateManager.getConfigurationState(baseFile);
FolderConfiguration config = FolderConfiguration.getConfigForFolder(file.getParent().getName());
if (config == null) {
config = new FolderConfiguration();
}
Configuration configuration = Configuration.create(this, file, fileState, config);
Configuration baseConfig = myCache.get(file);
if (baseConfig != null) {
configuration.setEffectiveDevice(baseConfig.getDevice(), baseConfig.getDeviceState());
}
LocalResourceRepository resources = AppResourceRepository.getAppResources(myModule, true);
ConfigurationMatcher matcher = new ConfigurationMatcher(configuration, resources, file);
matcher.adaptConfigSelection(true);
myCache.put(file, configuration);
return configuration;
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class StringResourceDataTest method setUpData.
private void setUpData() {
Collection<VirtualFile> resourceDirectories = Collections.singletonList(resourceDirectory);
ClassField field = Mockito.mock(ClassField.class);
Mockito.when(field.getType()).thenReturn("string");
Mockito.when(field.getName()).thenReturn("dynamic_key1");
Mockito.when(field.getValue()).thenReturn("L\\'Étranger");
Map<String, ClassField> values = Collections.singletonMap("dynamic_key1", field);
LocalResourceRepository otherDelegate = DynamicResourceValueRepository.createForTest(myFacet, values);
Disposer.register(myFacet, otherDelegate);
Collection<LocalResourceRepository> otherDelegates = Collections.singletonList(otherDelegate);
data = StringResourceParser.parse(myFacet, ModuleResourceRepository.createForTest(myFacet, resourceDirectories, otherDelegates));
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class DataBindingUtil method getViewClassNameFromLayoutReferenceTag.
private static String getViewClassNameFromLayoutReferenceTag(XmlTag tag, AndroidFacet facet) {
String layout = tag.getAttributeValue(SdkConstants.ATTR_LAYOUT);
if (layout == null) {
return null;
}
LocalResourceRepository moduleResources = facet.getModuleResources(false);
if (moduleResources == null) {
return null;
}
ResourceUrl resourceUrl = ResourceUrl.parse(layout);
if (resourceUrl == null || resourceUrl.type != ResourceType.LAYOUT) {
return null;
}
DataBindingInfo info = moduleResources.getDataBindingInfoForLayout(resourceUrl.name);
if (info == null) {
return null;
}
return info.getQualifiedName();
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class ThemeEditorUtilsTest method testCopyThemeVersionOverride.
/**
* Tests copyTheme method for following cases:
* 1. copyTheme(21, "values-en-night")
* 2. copyTheme(21, "values-v19")
*/
public void testCopyThemeVersionOverride() {
myFixture.copyFileToProject("themeEditor/styles_1.xml", "res/values-en-night/styles.xml");
myFixture.copyFileToProject("themeEditor/styles_1.xml", "res/values-v19/styles.xml");
LocalResourceRepository repository = AppResourceRepository.getAppResources(myModule, true);
assertNotNull(repository);
final List<ResourceItem> styleItems = repository.getResourceItem(ResourceType.STYLE, "AppTheme");
assertNotNull(styleItems);
assertEquals(2, styleItems.size());
new WriteCommandAction.Simple(myModule.getProject(), "Copy a theme") {
@Override
protected void run() throws Throwable {
for (ResourceItem styleItem : styleItems) {
XmlTag styleTag = LocalResourceRepository.getItemTag(getProject(), styleItem);
assertNotNull(styleTag);
ThemeEditorUtils.copyTheme(21, styleTag);
}
}
}.execute();
myFixture.checkResultByFile("res/values-en-night-v21/styles.xml", "themeEditor/styles_1.xml", true);
myFixture.checkResultByFile("res/values-v21/styles.xml", "themeEditor/styles_1.xml", true);
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class StringsWriteUtils method getStringResourceItem.
@Nullable
private static ResourceItem getStringResourceItem(@NotNull AndroidFacet facet, @NotNull String key, @Nullable Locale locale) {
LocalResourceRepository repository = facet.getModuleResources(true);
// Ensure that items *just* created are processed by the resource repository
repository.sync();
List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
if (items == null) {
return null;
}
for (ResourceItem item : items) {
FolderConfiguration config = item.getConfiguration();
LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
if (qualifier == null) {
if (locale == null) {
return item;
} else {
continue;
}
}
Locale l = Locale.create(qualifier);
if (l.equals(locale)) {
return item;
}
}
return null;
}
Aggregations