Search in sources :

Example 61 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class NestedConfigurationTest method testListener.

public void testListener() throws Exception {
    final AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assertNotNull(facet);
    ConfigurationManager manager = facet.getConfigurationManager();
    assertNotNull(manager);
    assertSame(manager, facet.getConfigurationManager());
    Configuration parent = Configuration.create(manager, null, new FolderConfiguration());
    assertNotNull(parent);
    NestedConfiguration configuration = NestedConfiguration.create(parent);
    assertNotNull(configuration);
    ConfigurationListener listener = new ConfigurationListener() {

        @Override
        public boolean changed(int flags) {
            myFlags = flags;
            myChangeCount++;
            return true;
        }
    };
    configuration.addListener(listener);
    myChangeCount = 0;
    myFlags = 0;
    configuration.setOverrideLocale(true);
    configuration.setLocale(Locale.create("en-rUS"));
    assertEquals(1, myChangeCount);
    assertEquals(CFG_LOCALE, myFlags);
    // No firing when only seeing overridden changes
    myFlags = 0;
    parent.setLocale(Locale.create("es"));
    assertEquals(1, myChangeCount);
    assertEquals(0, myFlags);
    myFlags = 0;
    parent.setLocale(Locale.create("de"));
    parent.setUiMode(UiMode.APPLIANCE);
    assertEquals(2, myChangeCount);
    assertEquals(CFG_UI_MODE, myFlags);
}
Also used : ConfigurationListener(com.android.tools.idea.configurations.ConfigurationListener) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 62 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ConfiguredThemeEditorStyleTest method testHasItem.

public void testHasItem() {
    VirtualFile myFile = myFixture.copyFileToProject("themeEditor/styles_1.xml", "res/values/styles.xml");
    myFixture.copyFileToProject("themeEditor/attrs.xml", "res/values/attrs.xml");
    Configuration configuration = myFacet.getConfigurationManager().getConfiguration(myFile);
    ThemeResolver themeResolver = new ThemeResolver(configuration);
    ConfiguredThemeEditorStyle theme = themeResolver.getTheme("AppTheme");
    assertNotNull(theme);
    ConfiguredThemeEditorStyle parent = theme.getParent();
    assertNotNull(parent);
    FolderConfiguration defaultConfig = new FolderConfiguration();
    ConfiguredElement<ItemResourceValue> hasItem = ConfiguredElement.create(defaultConfig, new ItemResourceValue("myColor", false, "?android:attr/colorBackground", false, null));
    ConfiguredElement<ItemResourceValue> hasNotItem = ConfiguredElement.create(defaultConfig, new ItemResourceValue("myHasNot", false, "?android:attr/colorBackground", false, null));
    ConfiguredElement<ItemResourceValue> hasInParent = ConfiguredElement.create(defaultConfig, new ItemResourceValue("editTextStyle", true, "?android:attr/colorBackground", true, null));
    assertTrue(theme.hasItem(new EditedStyleItem(hasItem, theme)));
    assertFalse(theme.hasItem(new EditedStyleItem(hasNotItem, theme)));
    assertTrue(theme.getParent().hasItem(new EditedStyleItem(hasInParent, parent)));
    assertFalse(theme.hasItem(new EditedStyleItem(hasInParent, parent)));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)

Example 63 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android_frameworks_base by DirtyUnicorns.

the class ConfigGenerator method getFolderConfig.

public FolderConfiguration getFolderConfig() {
    FolderConfiguration config = new FolderConfiguration();
    config.createDefault();
    config.setDensityQualifier(new DensityQualifier(mDensity));
    config.setNavigationMethodQualifier(new NavigationMethodQualifier(mNavigation));
    if (mScreenWidth > mScreenHeight) {
        config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenWidth, mScreenHeight));
    } else {
        config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenHeight, mScreenWidth));
    }
    config.setScreenRatioQualifier(new ScreenRatioQualifier(mRatio));
    config.setScreenSizeQualifier(new ScreenSizeQualifier(mSize));
    config.setTextInputMethodQualifier(new TextInputMethodQualifier(mKeyboard));
    config.setTouchTypeQualifier(new TouchScreenQualifier(mTouchScreen));
    config.setKeyboardStateQualifier(new KeyboardStateQualifier(mKeyboardState));
    config.setScreenOrientationQualifier(new ScreenOrientationQualifier(mOrientation));
    config.updateScreenWidthAndHeight();
    // some default qualifiers.
    config.setUiModeQualifier(new UiModeQualifier(UiMode.NORMAL));
    config.setNightModeQualifier(new NightModeQualifier(NightMode.NOTNIGHT));
    config.setCountryCodeQualifier(new CountryCodeQualifier());
    config.setLayoutDirectionQualifier(new LayoutDirectionQualifier());
    config.setNetworkCodeQualifier(new NetworkCodeQualifier());
    config.setLocaleQualifier(new LocaleQualifier());
    config.setVersionQualifier(new VersionQualifier());
    return config;
}
Also used : VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) NavigationMethodQualifier(com.android.ide.common.resources.configuration.NavigationMethodQualifier) ScreenSizeQualifier(com.android.ide.common.resources.configuration.ScreenSizeQualifier) KeyboardStateQualifier(com.android.ide.common.resources.configuration.KeyboardStateQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) NightModeQualifier(com.android.ide.common.resources.configuration.NightModeQualifier) ScreenOrientationQualifier(com.android.ide.common.resources.configuration.ScreenOrientationQualifier) UiModeQualifier(com.android.ide.common.resources.configuration.UiModeQualifier) TouchScreenQualifier(com.android.ide.common.resources.configuration.TouchScreenQualifier) LayoutDirectionQualifier(com.android.ide.common.resources.configuration.LayoutDirectionQualifier) ScreenDimensionQualifier(com.android.ide.common.resources.configuration.ScreenDimensionQualifier) ScreenRatioQualifier(com.android.ide.common.resources.configuration.ScreenRatioQualifier) TextInputMethodQualifier(com.android.ide.common.resources.configuration.TextInputMethodQualifier) DensityQualifier(com.android.ide.common.resources.configuration.DensityQualifier) NetworkCodeQualifier(com.android.ide.common.resources.configuration.NetworkCodeQualifier) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) CountryCodeQualifier(com.android.ide.common.resources.configuration.CountryCodeQualifier)

Example 64 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ModuleResourceRepositoryTest method testOverlays.

public void testOverlays() {
    myFixture.copyFileToProject(LAYOUT, "res/layout/layout1.xml");
    myFixture.copyFileToProject(LAYOUT_OVERLAY, "res2/layout/layout1.xml");
    myFixture.copyFileToProject(LAYOUT_IDS_1, "res2/layout/layout_ids1.xml");
    myFixture.copyFileToProject(LAYOUT_IDS_2, "res2/layout/layout_ids2.xml");
    VirtualFile res1 = myFixture.copyFileToProject(VALUES, "res/values/values.xml").getParent().getParent();
    VirtualFile res2 = myFixture.copyFileToProject(VALUES_OVERLAY1, "res2/values/values.xml").getParent().getParent();
    VirtualFile res3 = myFixture.copyFileToProject(VALUES_OVERLAY2, "res3/values/nameDoesNotMatter.xml").getParent().getParent();
    myFixture.copyFileToProject(VALUES_OVERLAY2_NO, "res3/values-no/values.xml");
    assertNotSame(res1, res2);
    assertNotSame(res1, res3);
    assertNotSame(res2, res3);
    ModuleResourceRepository resources = ModuleResourceRepository.createForTest(myFacet, Arrays.asList(res1, res2, res3));
    // Check that values are handled correctly. First a plain value (not overridden anywhere).
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    // Check that an overridden key (overridden in just one flavor) is picked up
    // Overridden in res2
    assertStringIs(resources, "title_crossfade", "Complex Crossfade");
    // Overridden in res3
    assertStringIs(resources, "title_zoom", "Zoom!");
    // Make sure that new/unique strings from flavors are available
    // Overridden in res2
    assertStringIs(resources, "unique_string", "Unique");
    // Overridden in res3
    assertStringIs(resources, "another_unique_string", "Another Unique", false);
    // Check that an overridden key (overridden in multiple flavors) picks the last one
    // res3 (not unique because we have a values-no item too)
    assertStringIs(resources, "app_name", "Very Different App Name", false);
    // Layouts: Should only be offered id's from the overriding layout (plus those defined in values.xml)
    // from values.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
    // from res2 layout1.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "noteArea"));
    // Layout masking does not currently work. I'm not 100% certain what the intended behavior is
    // here (e.g. res1's layout1 contains @+id/button1, res2's layout1 does not; should @+id/button1 be visible?)
    //assertFalse(resources.hasResourceItem(ResourceType.ID, "btn_title_refresh")); // masked in res1 by res2's layout replacement
    // Check that localized lookup (qualifier matching works)
    List<ResourceItem> stringList = resources.getResourceItem(ResourceType.STRING, "another_unique_string");
    assertNotNull(stringList);
    assertSize(2, stringList);
    FolderConfiguration valueConfig = FolderConfiguration.getConfigForFolder("values-no");
    assertNotNull(valueConfig);
    ResourceValue stringValue = resources.getConfiguredResources(ResourceType.STRING, valueConfig).get("another_unique_string");
    assertNotNull(stringValue);
    assertEquals("En Annen", stringValue.getValue());
    // Change flavor order and make sure things are updated and work correctly
    resources.updateRoots(Arrays.asList(res1, res3, res2));
    // Should now be picking app_name from res2 rather than res3 since it's now last
    // res2
    assertStringIs(resources, "app_name", "Different App Name", false);
    // Sanity check other merging
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    // Overridden in res2
    assertStringIs(resources, "title_crossfade", "Complex Crossfade");
    // Overridden in res3
    assertStringIs(resources, "title_zoom", "Zoom!");
    // Overridden in res2
    assertStringIs(resources, "unique_string", "Unique");
    // Overridden in res3
    assertStringIs(resources, "another_unique_string", "Another Unique", false);
    // Hide a resource root (res2)
    resources.updateRoots(Arrays.asList(res1, res3));
    // No longer aliasing the main layout
    // res1 layout1.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "btn_title_refresh"));
    // from res1 layout1.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "noteArea"));
    // from values.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
    // No longer overridden in res2
    assertStringIs(resources, "title_crossfade", "Simple Crossfade");
    // Finally ensure that we can switch roots repeatedly (had some earlier bugs related to root unregistration)
    resources.updateRoots(Arrays.asList(res1, res3, res2));
    resources.updateRoots(Arrays.asList(res1));
    resources.updateRoots(Arrays.asList(res1, res3, res2));
    resources.updateRoots(Arrays.asList(res1));
    resources.updateRoots(Arrays.asList(res1, res3, res2));
    resources.updateRoots(Arrays.asList(res2));
    resources.updateRoots(Arrays.asList(res1));
    resources.updateRoots(Arrays.asList(res1, res2, res3));
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    // Make sure I get all the resource ids (there can be multiple; these are not replaced via overlays)
    List<ResourceItem> ids = resources.getResourceItem(ResourceType.ID, "my_id");
    assertNotNull(ids);
    assertSize(2, ids);
    Collections.sort(ids, new Comparator<ResourceItem>() {

        @SuppressWarnings("ConstantConditions")
        @Override
        public int compare(ResourceItem item1, ResourceItem item2) {
            return item1.getSource().getFile().getName().compareTo(item2.getSource().getFile().getName());
        }
    });
    //noinspection ConstantConditions
    assertEquals("layout_ids1.xml", ids.get(0).getSource().getFile().getName());
    //noinspection ConstantConditions
    assertEquals("layout_ids2.xml", ids.get(1).getSource().getFile().getName());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 65 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ResourceFolderRepositoryTest method testSerializeDensityBasedResourceValues.

public void testSerializeDensityBasedResourceValues() throws Exception {
    myFixture.copyFileToProject(STRINGS, "res/values/strings.xml");
    myFixture.copyFileToProject(DRAWABLE_ID_SCAN, "res/drawable-hdpi/drawable_foo.xml");
    myFixture.copyFileToProject(DRAWABLE_ID_SCAN, "res/drawable-xhdpi/drawable_foo.xml");
    myFixture.copyFileToProject(DRAWABLE_ID_SCAN, "res/drawable-fr/drawable_foo.xml");
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    FolderConfiguration config = FolderConfiguration.getConfigForFolder("drawable-xhdpi");
    assertNotNull(config);
    // For drawable xml, the ResourceValue#getValue is the file path.
    ResourceValue value = resources.getConfiguredValue(ResourceType.DRAWABLE, "drawable_foo", config);
    assertNotNull(value);
    String valueString = value.getValue();
    assertNotNull(valueString);
    assertTrue(valueString.endsWith("drawable_foo.xml"));
    assertTrue(valueString.contains("drawable-xhdpi"));
    DensityBasedResourceValue densityValue = (DensityBasedResourceValue) value;
    assertEquals(Density.XHIGH, densityValue.getResourceDensity());
    resources.saveStateToFile();
    ResourceFolderRegistry.reset();
    final ResourceFolderRepository fromBlob = createRepository();
    assertNotNull(fromBlob);
    // We don't count files that we explicitly skip against freshness.
    assertTrue(fromBlob.hasFreshFileCache());
    assertNotSame(resources, fromBlob);
    assertTrue(fromBlob.equalFilesItems(resources));
    value = fromBlob.getConfiguredValue(ResourceType.DRAWABLE, "drawable_foo", config);
    assertNotNull(value);
    valueString = value.getValue();
    assertNotNull(valueString);
    assertTrue(valueString.endsWith("drawable_foo.xml"));
    assertTrue(valueString.contains("drawable-xhdpi"));
    // Make sure that the resource value is still of type DensityBasedResourceValue.
    densityValue = (DensityBasedResourceValue) value;
    assertEquals(Density.XHIGH, densityValue.getResourceDensity());
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Aggregations

FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 NotNull (org.jetbrains.annotations.NotNull)14 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)12 Nullable (org.jetbrains.annotations.Nullable)12 ResourceItem (com.android.ide.common.res2.ResourceItem)10 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)10 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)9 ResourceFolderType (com.android.resources.ResourceFolderType)9 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)7 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Project (com.intellij.openapi.project.Project)7 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)6 ResourceResolver (com.android.ide.common.resources.ResourceResolver)6 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)6 ScreenOrientationQualifier (com.android.ide.common.resources.configuration.ScreenOrientationQualifier)6 ScreenSizeQualifier (com.android.ide.common.resources.configuration.ScreenSizeQualifier)6 SessionParams (com.android.ide.common.rendering.api.SessionParams)5 CountryCodeQualifier (com.android.ide.common.resources.configuration.CountryCodeQualifier)5