Search in sources :

Example 1 with ResourceRepository

use of com.android.ide.common.res2.ResourceRepository in project android by JetBrains.

the class ThemeEditorStyle method getStyleResourceItems.

/**
   * Returns all the {@link ResourceItem} where this style is defined. This includes all the definitions in the
   * different resource folders.
   */
@NotNull
protected Collection<ResourceItem> getStyleResourceItems() {
    assert !isFramework();
    Collection<ResourceItem> resultItems;
    final Module module = myManager.getModule();
    if (isProjectStyle()) {
        AndroidFacet facet = AndroidFacet.getInstance(module);
        assert facet != null : module.getName() + " module doesn't have AndroidFacet";
        // We need to keep a Set of ResourceItems to override them. The key is the folder configuration + the name
        final HashMap<String, ResourceItem> resourceItems = Maps.newHashMap();
        ThemeEditorUtils.acceptResourceResolverVisitor(facet, new ThemeEditorUtils.ResourceFolderVisitor() {

            @Override
            public void visitResourceFolder(@NotNull LocalResourceRepository resources, String moduleName, @NotNull String variantName, boolean isSourceSelected) {
                if (!isSourceSelected) {
                    // TODO: Process all source sets
                    return;
                }
                List<ResourceItem> items = resources.getResourceItem(ResourceType.STYLE, myQualifiedName);
                if (items == null) {
                    return;
                }
                for (ResourceItem item : items) {
                    String key = item.getConfiguration().toShortDisplayString() + "/" + item.getName();
                    resourceItems.put(key, item);
                }
            }
        });
        resultItems = ImmutableList.copyOf(resourceItems.values());
    } else {
        LocalResourceRepository resourceRepository = AppResourceRepository.getAppResources(module, true);
        assert resourceRepository != null;
        List<ResourceItem> items = resourceRepository.getResourceItem(ResourceType.STYLE, myQualifiedName);
        if (items != null) {
            resultItems = items;
        } else {
            resultItems = Collections.emptyList();
        }
    }
    return resultItems;
}
Also used : AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ThemeEditorUtils(com.android.tools.idea.editors.theme.ThemeEditorUtils) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ResourceItem(com.android.ide.common.res2.ResourceItem) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ResourceRepository

use of com.android.ide.common.res2.ResourceRepository in project android by JetBrains.

the class ResourceClassGeneratorTest method test.

public void test() throws Exception {
    final ResourceRepository repository = TestResourceRepository.createRes2(false, new Object[] { "layout/layout1.xml", "<!--contents doesn't matter-->", "layout-land/layout1.xml", "<!--contents doesn't matter-->", "values/styles.xml", "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "    <style name=\"MyTheme.Dark\" parent=\"android:Theme.Light\">\n" + "        <item name=\"android:textColor\">#999999</item>\n" + "        <item name=\"foo\">?android:colorForeground</item>\n" + "    </style>\n" + "    <declare-styleable name=\"GridLayout_Layout\">\n" + "        <attr name=\"android:layout_width\" />\n" + "        <attr name=\"android:layout_height\" />\n" + "        <attr name=\"layout_columnSpan\" format=\"integer\" min=\"1\" />\n" + "        <attr name=\"layout_gravity\">\n" + "            <flag name=\"top\" value=\"0x30\" />\n" + "            <flag name=\"bottom\" value=\"0x50\" />\n" + "            <flag name=\"center_vertical\" value=\"0x10\" />\n" + "        </attr>\n" + "    </declare-styleable>\n" + "</resources>\n", "values/strings.xml", "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "    <item type=\"id\" name=\"action_bar_refresh\" />\n" + "    <item type=\"dimen\" name=\"dialog_min_width_major\">45%</item>\n" + "    <string name=\"show_all_apps\">All</string>\n" + "    <string name=\"menu_wallpaper\">Wallpaper</string>\n" + "</resources>\n", "values-es/strings.xml", "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "    <string name=\"show_all_apps\">Todo</string>\n" + "</resources>\n" });
    LocalResourceRepository resources = new LocalResourceRepository("test") {

        @NonNull
        @Override
        protected Map<ResourceType, ListMultimap<String, ResourceItem>> getMap() {
            return repository.getItems();
        }

        @Nullable
        @Override
        protected ListMultimap<String, ResourceItem> getMap(ResourceType type, boolean create) {
            return repository.getItems().get(type);
        }

        @NotNull
        @Override
        protected Set<VirtualFile> computeResourceDirs() {
            return ImmutableSet.of();
        }
    };
    AppResourceRepository appResources = new AppResourceRepository(myFacet, Collections.singletonList(resources), Collections.<FileResourceRepository>emptyList());
    ResourceClassGenerator generator = ResourceClassGenerator.create(appResources);
    assertNotNull(generator);
    String name = "my.test.pkg.R";
    Class<?> clz = generateClass(generator, name);
    assertNotNull(clz);
    assertEquals(name, clz.getName());
    assertTrue(Modifier.isPublic(clz.getModifiers()));
    assertTrue(Modifier.isFinal(clz.getModifiers()));
    assertFalse(Modifier.isInterface(clz.getModifiers()));
    Object r = clz.newInstance();
    assertNotNull(r);
    name = "my.test.pkg.R$string";
    clz = generateClass(generator, name);
    assertNotNull(clz);
    assertEquals(name, clz.getName());
    assertTrue(Modifier.isPublic(clz.getModifiers()));
    assertTrue(Modifier.isFinal(clz.getModifiers()));
    assertFalse(Modifier.isInterface(clz.getModifiers()));
    try {
        clz.getField("nonexistent");
        fail("Shouldn't find nonexistent fields");
    } catch (NoSuchFieldException e) {
    // pass
    }
    Field field1 = clz.getField("menu_wallpaper");
    Object value1 = field1.get(null);
    assertEquals(Integer.TYPE, field1.getType());
    assertNotNull(value1);
    assertEquals(2, clz.getFields().length);
    Field field2 = clz.getField("show_all_apps");
    assertNotNull(field2);
    assertEquals(Integer.TYPE, field2.getType());
    assertTrue(Modifier.isPublic(field2.getModifiers()));
    assertTrue(Modifier.isFinal(field2.getModifiers()));
    assertTrue(Modifier.isStatic(field2.getModifiers()));
    assertFalse(Modifier.isSynchronized(field2.getModifiers()));
    assertFalse(Modifier.isTransient(field2.getModifiers()));
    assertFalse(Modifier.isStrict(field2.getModifiers()));
    assertFalse(Modifier.isVolatile(field2.getModifiers()));
    r = clz.newInstance();
    assertNotNull(r);
    // Make sure the id's match what we've dynamically allocated in the resource repository
    @SuppressWarnings("deprecation") Pair<ResourceType, String> pair = appResources.resolveResourceId((Integer) clz.getField("menu_wallpaper").get(null));
    assertNotNull(pair);
    assertEquals(ResourceType.STRING, pair.getFirst());
    assertEquals("menu_wallpaper", pair.getSecond());
    assertEquals(clz.getField("menu_wallpaper").get(null), appResources.getResourceId(ResourceType.STRING, "menu_wallpaper"));
    assertEquals(clz.getField("show_all_apps").get(null), appResources.getResourceId(ResourceType.STRING, "show_all_apps"));
    // Test attr class!
    name = "my.test.pkg.R$attr";
    clz = generateClass(generator, name);
    assertNotNull(clz);
    assertEquals(name, clz.getName());
    assertTrue(Modifier.isPublic(clz.getModifiers()));
    assertTrue(Modifier.isFinal(clz.getModifiers()));
    assertFalse(Modifier.isInterface(clz.getModifiers()));
    assertEquals(2, clz.getFields().length);
    field1 = clz.getField("layout_gravity");
    assertNotNull(field1);
    Object gravityValue = field1.get(null);
    Object layoutColumnSpanValue = clz.getField("layout_columnSpan").get(null);
    // Test style class
    styleTest(generator);
    // Run the same test to check caching.
    styleTest(generator);
    // Test styleable class!
    styleableTest(generator, gravityValue, layoutColumnSpanValue);
    // Run the same test again to ensure that caching is working as expected.
    styleableTest(generator, gravityValue, layoutColumnSpanValue);
    name = "my.test.pkg.R$id";
    clz = generateClass(generator, name);
    assertNotNull(clz);
    r = clz.newInstance();
    assertNotNull(r);
    assertEquals(name, clz.getName());
    // getEnclosingClass() results in generating all R classes. So, this should be called at the end
    // so that tests for caching work as expected.
    Class<?> enclosingClass = clz.getEnclosingClass();
    assertNotNull(enclosingClass);
// TODO: Flag and enum values should also be created as id's by the ValueResourceParser
//assertNotNull(clz.getField("top"));
//assertNotNull(clz.getField("bottom"));
//assertNotNull(clz.getField("center_vertical"));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceType(com.android.resources.ResourceType) Field(java.lang.reflect.Field) TestResourceRepository(com.android.ide.common.resources.TestResourceRepository) ResourceRepository(com.android.ide.common.res2.ResourceRepository) ListMultimap(com.google.common.collect.ListMultimap) ResourceItem(com.android.ide.common.res2.ResourceItem)

Aggregations

ResourceItem (com.android.ide.common.res2.ResourceItem)2 ResourceRepository (com.android.ide.common.res2.ResourceRepository)1 TestResourceRepository (com.android.ide.common.resources.TestResourceRepository)1 ResourceType (com.android.resources.ResourceType)1 ThemeEditorUtils (com.android.tools.idea.editors.theme.ThemeEditorUtils)1 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)1 ImmutableList (com.google.common.collect.ImmutableList)1 ListMultimap (com.google.common.collect.ListMultimap)1 Module (com.intellij.openapi.module.Module)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 NotNull (org.jetbrains.annotations.NotNull)1