use of com.android.bundle.Targeting.ScreenDensity in project bundletool by google.
the class ScreenDensityMatcher method matchesTargeting.
@Override
public boolean matchesTargeting(ScreenDensityTargeting targeting) {
ImmutableList<ScreenDensity> allDensities = ImmutableList.<ScreenDensity>builder().addAll(targeting.getValueList()).addAll(targeting.getAlternativesList()).build();
if (allDensities.isEmpty()) {
return true;
}
int bestMatchingDensity = new ScreenDensitySelector().selectBestDensity(Iterables.transform(allDensities, ResourcesUtils::convertToDpi), getDeviceSpec().getScreenDensity());
return targeting.getValueList().stream().map(ResourcesUtils::convertToDpi).anyMatch(isEqual(bestMatchingDensity));
}
use of com.android.bundle.Targeting.ScreenDensity in project bundletool by google.
the class BuildApksManagerTest method explicitMdpiPreferredOverDefault_enabledSince_0_9_1.
@Test
@Theory
public void explicitMdpiPreferredOverDefault_enabledSince_0_9_1(@FromDataPoints("bundleFeatureEnabled") boolean bundleFeatureEnabled) throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.addFile("res/drawable/image.jpg").addFile("res/drawable-mdpi/image.jpg").setManifest(androidManifest("com.test.app")).setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addDrawableResourceForMultipleDensities("image", ImmutableMap.of(/* default */
0, "res/drawable/image.jpg", /* mdpi */
160, "res/drawable-mdpi/image.jpg")).build())).setBundleConfig(BundleConfigBuilder.create().setVersion(bundleFeatureEnabled ? "0.9.1" : "0.9.0").build()).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
ImmutableList<Variant> splitApkVariants = splitApkVariants(result);
ImmutableList<ApkDescription> mdpiSplits = apkDescriptions(splitApkVariants).stream().filter(apkDesc -> {
List<ScreenDensity> targetDensities = apkDesc.getTargeting().getScreenDensityTargeting().getValueList();
return targetDensities.stream().anyMatch(density -> density.getDensityAlias() == DensityAlias.MDPI) || targetDensities.stream().anyMatch(density -> density.getDensityDpi() == MDPI_VALUE);
}).collect(toImmutableList());
String fileToBePresent;
String fileToBeAbsent;
if (bundleFeatureEnabled) {
fileToBePresent = "res/drawable-mdpi/image.jpg";
fileToBeAbsent = "res/drawable/image.jpg";
} else {
fileToBeAbsent = "res/drawable-mdpi/image.jpg";
fileToBePresent = "res/drawable/image.jpg";
}
assertThat(mdpiSplits).isNotEmpty();
for (ApkDescription mdpiSplit : mdpiSplits) {
assertThat(filesInApk(mdpiSplit, apkSetFile)).contains(fileToBePresent);
assertThat(filesInApk(mdpiSplit, apkSetFile)).doesNotContain(fileToBeAbsent);
}
}
Aggregations