Search in sources :

Example 6 with LocalResourceRepository

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;
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with LocalResourceRepository

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));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ClassField(com.android.builder.model.ClassField)

Example 8 with LocalResourceRepository

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();
}
Also used : LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo) ResourceUrl(com.android.ide.common.resources.ResourceUrl)

Example 9 with LocalResourceRepository

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);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag)

Example 10 with LocalResourceRepository

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;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)25 NotNull (org.jetbrains.annotations.NotNull)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 Nullable (org.jetbrains.annotations.Nullable)6 ResourceItem (com.android.ide.common.res2.ResourceItem)5 Module (com.intellij.openapi.module.Module)5 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)5 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)4 DataBindingInfo (com.android.tools.idea.res.DataBindingInfo)4 ResourceRepository (com.android.ide.common.resources.ResourceRepository)3 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)3 Locale (com.android.tools.idea.rendering.Locale)3 XmlTag (com.intellij.psi.xml.XmlTag)3 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 ResourceType (com.android.resources.ResourceType)2 State (com.android.sdklib.devices.State)2 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 PsiFile (com.intellij.psi.PsiFile)2 ArrayList (java.util.ArrayList)2