VectorDrawableRenderer implements ResourcePreprocessor { @Override public Collection<File> getFilesToBeGenerated(File inputXmlFile) { Collection<File> filesToBeGenerated = Lists.newArrayList(); FolderConfiguration originalConfiguration = getFolderConfiguration(inputXmlFile); DensityQualifier densityQualifier = originalConfiguration.getDensityQualifier(); boolean validDensityQualifier = ResourceQualifier.isValid(densityQualifier); if (validDensityQualifier && densityQualifier.getValue() == Density.NODPI) { filesToBeGenerated.add(new File( getDirectory(originalConfiguration), inputXmlFile.getName())); } else if (validDensityQualifier && densityQualifier.getValue() != Density.ANYDPI) { filesToBeGenerated.add(new File( getDirectory(originalConfiguration), inputXmlFile.getName().replace(".xml", ".png"))); originalConfiguration.setVersionQualifier( new VersionQualifier(MIN_SDK_WITH_VECTOR_SUPPORT)); filesToBeGenerated.add(new File( getDirectory(originalConfiguration), inputXmlFile.getName())); } else { for (Density density : mDensities) { FolderConfiguration newConfiguration = FolderConfiguration.copyOf(originalConfiguration); newConfiguration.setDensityQualifier(new DensityQualifier(density)); filesToBeGenerated.add(new File( getDirectory(newConfiguration), inputXmlFile.getName().replace(".xml", ".png"))); } originalConfiguration.setDensityQualifier(new DensityQualifier(Density.ANYDPI)); originalConfiguration.setVersionQualifier( new VersionQualifier(MIN_SDK_WITH_VECTOR_SUPPORT)); filesToBeGenerated.add( new File(getDirectory(originalConfiguration), inputXmlFile.getName())); } return filesToBeGenerated; } VectorDrawableRenderer(
            int minSdk,
            File outputDir,
            Collection<Density> densities,
            ILogger logger); @Override boolean needsPreprocessing(File resourceFile); @Override Collection<File> getFilesToBeGenerated(File inputXmlFile); @Override void generateFile(File toBeGenerated, File original); static final int MIN_SDK_WITH_VECTOR_SUPPORT; }
@Test public void commonCase() throws Exception { File drawable = new File(mRes, "drawable"); File input = new File(drawable, "icon.xml"); Files.createParentDirs(input); Files.write("<vector></vector>", input, defaultCharset()); Collection<File> result = mRenderer.getFilesToBeGenerated(input); Assert.assertEquals( ImmutableSet.of( FileUtils.join(mOutput, "drawable-hdpi", "icon.png"), FileUtils.join(mOutput, "drawable-mdpi", "icon.png"), FileUtils.join(mOutput, "drawable-ldpi", "icon.png"), FileUtils.join(mOutput, "drawable-anydpi-v21", "icon.xml")), ImmutableSet.copyOf(result)); }
@Test public void noDensities() throws Exception { mRenderer = new VectorDrawableRenderer( 19, mOutput, Collections.<Density>emptySet(), new NullLogger()); File drawable = new File(mRes, "drawable"); File input = new File(drawable, "icon.xml"); Files.createParentDirs(input); Files.write("<vector></vector>", input, defaultCharset()); Collection<File> result = mRenderer.getFilesToBeGenerated(input); Assert.assertEquals( ImmutableSet.of( FileUtils.join(mOutput, "drawable-anydpi-v21", "icon.xml")), ImmutableSet.copyOf(result)); }
@Test public void languageQualifier() throws Exception { File drawable = new File(mRes, "drawable-fr"); File input = new File(drawable, "icon.xml"); Files.createParentDirs(input); Files.write("<vector></vector>", input, defaultCharset()); Collection<File> result = mRenderer.getFilesToBeGenerated(input); Assert.assertEquals( ImmutableSet.of( FileUtils.join(mOutput, "drawable-fr-hdpi", "icon.png"), FileUtils.join(mOutput, "drawable-fr-mdpi", "icon.png"), FileUtils.join(mOutput, "drawable-fr-ldpi", "icon.png"), FileUtils.join(mOutput, "drawable-fr-anydpi-v21", "icon.xml")), ImmutableSet.copyOf(result)); }
@Test public void versionQualifier() throws Exception { File drawable = new File(mRes, "drawable-v16"); File input = new File(drawable, "icon.xml"); Files.createParentDirs(input); Files.write("<vector></vector>", input, defaultCharset()); Collection<File> result = mRenderer.getFilesToBeGenerated(input); Assert.assertEquals( ImmutableSet.of( FileUtils.join(mOutput, "drawable-hdpi-v16", "icon.png"), FileUtils.join(mOutput, "drawable-mdpi-v16", "icon.png"), FileUtils.join(mOutput, "drawable-ldpi-v16", "icon.png"), FileUtils.join(mOutput, "drawable-anydpi-v21", "icon.xml")), ImmutableSet.copyOf(result)); }
@Test public void densityQualifier() throws Exception { File drawable = new File(mRes, "drawable-hdpi"); File input = new File(drawable, "icon.xml"); Files.createParentDirs(input); Files.write("<vector></vector>", input, defaultCharset()); Collection<File> result = mRenderer.getFilesToBeGenerated(input); Assert.assertEquals( ImmutableSet.of( FileUtils.join(mOutput, "drawable-hdpi", "icon.png"), FileUtils.join(mOutput, "drawable-hdpi-v21", "icon.xml")), ImmutableSet.copyOf(result)); }
@Test public void anyDpi() throws Exception { File drawable = new File(mRes, "drawable-anydpi"); File input = new File(drawable, "icon.xml"); Files.createParentDirs(input); Files.write("<vector></vector>", input, defaultCharset()); Collection<File> result = mRenderer.getFilesToBeGenerated(input); Assert.assertEquals( ImmutableSet.of( FileUtils.join(mOutput, "drawable-ldpi", "icon.png"), FileUtils.join(mOutput, "drawable-mdpi", "icon.png"), FileUtils.join(mOutput, "drawable-hdpi", "icon.png"), FileUtils.join(mOutput, "drawable-anydpi-v21", "icon.xml")), ImmutableSet.copyOf(result)); }
@Test public void anyDpi_version() throws Exception { File drawable = new File(mRes, "drawable-anydpi-v16"); File input = new File(drawable, "icon.xml"); Files.createParentDirs(input); Files.write("<vector></vector>", input, defaultCharset()); Collection<File> result = mRenderer.getFilesToBeGenerated(input); Assert.assertEquals( ImmutableSet.of( FileUtils.join(mOutput, "drawable-ldpi-v16", "icon.png"), FileUtils.join(mOutput, "drawable-mdpi-v16", "icon.png"), FileUtils.join(mOutput, "drawable-hdpi-v16", "icon.png"), FileUtils.join(mOutput, "drawable-anydpi-v21", "icon.xml")), ImmutableSet.copyOf(result)); }
@Test public void noDpi() throws Exception { File drawable = new File(mRes, "drawable-nodpi"); File input = new File(drawable, "icon.xml"); Files.createParentDirs(input); Files.write("<vector></vector>", input, defaultCharset()); Collection<File> result = mRenderer.getFilesToBeGenerated(input); Assert.assertEquals( ImmutableSet.of( FileUtils.join(mOutput, "drawable-nodpi", "icon.xml")), ImmutableSet.copyOf(result)); }
@Test public void noDpi_version() throws Exception { File drawable = new File(mRes, "drawable-nodpi-v16"); File input = new File(drawable, "icon.xml"); Files.createParentDirs(input); Files.write("<vector></vector>", input, defaultCharset()); Collection<File> result = mRenderer.getFilesToBeGenerated(input); Assert.assertEquals( ImmutableSet.of( FileUtils.join(mOutput, "drawable-nodpi-v16", "icon.xml")), ImmutableSet.copyOf(result)); }