use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.
the class ScreenDensitySelectorTest method theMidResourceSplit_matchesGivenConfigs.
@Test
public void theMidResourceSplit_matchesGivenConfigs() {
// The range for XHDPI(320 dpi) when XXHDPI(480 dpi) and HDPI(240dpi) are present is [264;363]
// device dpi.
// 264dpi device should match 241dpi resource (1 dpi more than HDPI).
// 363dpi device should match 475dpi resource (5 dpi less than XXHDPI).
DensityAlias desiredDensity = DensityAlias.XHDPI;
ImmutableSet<DensityAlias> alternatives = ImmutableSet.of(DensityAlias.HDPI, DensityAlias.XXHDPI);
ImmutableList<ConfigValue> densityConfigs = ImmutableList.of(onlyConfig(HDPI), onlyConfig(forDpi(241)), onlyConfig(XHDPI), onlyConfig(forDpi(475)), onlyConfig(XXHDPI));
assertThat(new ScreenDensitySelector().selectAllMatchingConfigValues(densityConfigs, desiredDensity, alternatives, DEFAULT_BUNDLE_VERSION)).containsExactlyElementsIn(ImmutableList.of(onlyConfig(forDpi(241)), onlyConfig(XHDPI), onlyConfig(forDpi(475))));
}
use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.
the class ScreenDensitySelectorTest method sortedOrder_deviceMatchesExistingDpi_notGreaterThan.
@Test
public void sortedOrder_deviceMatchesExistingDpi_notGreaterThan() {
DensityAlias desiredDensity = DensityAlias.HDPI;
ImmutableList<ConfigValue> densityConfigs = ImmutableList.of(onlyConfig(LDPI), onlyConfig(MDPI), onlyConfig(TVDPI), onlyConfig(HDPI));
assertThat(new ScreenDensitySelector().selectBestConfigValue(densityConfigs, desiredDensity, DEFAULT_BUNDLE_VERSION)).isEqualTo(onlyConfig(HDPI));
assertThat(new ScreenDensitySelector().selectBestDensity(toDensities(densityConfigs), toDpi(desiredDensity))).isEqualTo(HDPI_VALUE);
}
use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.
the class ScreenDensitySelectorTest method theLowestResourceSplit_matchesAllConfigsUpTo.
@Test
public void theLowestResourceSplit_matchesAllConfigsUpTo() {
// The cut-off for MDPI(160 dpi) when XXHDPI (480 dpi) and XXXHDPI are present is 219 dpi
// device.
// 219dpi device still prefers 240 dpi resource over 160dpi.
DensityAlias desiredDensity = DensityAlias.MDPI;
ImmutableSet<DensityAlias> alternatives = ImmutableSet.of(DensityAlias.XXHDPI, DensityAlias.XXXHDPI);
ImmutableList<ConfigValue> densityConfigs = ImmutableList.of(onlyConfig(LDPI), onlyConfig(MDPI), onlyConfig(HDPI), onlyConfig(XXXHDPI));
assertThat(new ScreenDensitySelector().selectAllMatchingConfigValues(densityConfigs, desiredDensity, alternatives, DEFAULT_BUNDLE_VERSION)).containsExactlyElementsIn(ImmutableList.of(onlyConfig(LDPI), onlyConfig(MDPI), onlyConfig(HDPI)));
}
use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.
the class ScreenDensitySelectorTest method desiredDensityBetweenBoth_higherMatches_reversed.
@Test
public void desiredDensityBetweenBoth_higherMatches_reversed() {
// mdpi = 160, hdpi = 240, xhdpi = 320
// left side = ((2 * l) - requested-dpi) * h = ((2* 160) - 240) * 320 = 80 * 320 = 25600
// right side = requested-dpi * requested-dpi = 240 * 240 = 57600
// left side < right side, so higher dpi resource wins. (see ResourceTypes.cpp)
DensityAlias desiredDensity = DensityAlias.MDPI;
ImmutableList<ConfigValue> densityConfigs = ImmutableList.of(onlyConfig(LDPI), onlyConfig(HDPI));
assertThat(new ScreenDensitySelector().selectBestConfigValue(densityConfigs, desiredDensity, DEFAULT_BUNDLE_VERSION)).isEqualTo(onlyConfig(HDPI));
assertThat(new ScreenDensitySelector().selectBestDensity(toDensities(densityConfigs), toDpi(desiredDensity))).isEqualTo(HDPI_VALUE);
}
use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.
the class ScreenDensityResourcesSplitter method filterEntryForDensity.
/**
* Only leaves the density specific config values optimized for a given density.
*
* <p>As any other resource qualifiers can be requested when delivering resources, the algorithm
* chooses the best match only within group of resources differing by density only.
*
* @param tableEntry the entry to be updated
* @param targetDensity the desired density to match
* @return the entry with the best matching density config values.
*/
private Entry filterEntryForDensity(ResourceTableEntry tableEntry, DensityAlias targetDensity) {
Entry initialEntry = tableEntry.getEntry();
// Groups together configs that only differ on density.
ImmutableMap<Configuration, ? extends List<ConfigValue>> configValuesByConfiguration = initialEntry.getConfigValueList().stream().filter(configValue -> RESOURCES_WITH_NO_ALTERNATIVES_IN_MASTER_SPLIT.enabledForVersion(bundleVersion) || configValue.getConfig().getDensity() != DEFAULT_DENSITY_VALUE).collect(groupingByDeterministic(configValue -> clearDensity(configValue.getConfig())));
// the master split.
if (RESOURCES_WITH_NO_ALTERNATIVES_IN_MASTER_SPLIT.enabledForVersion(bundleVersion)) {
configValuesByConfiguration = ImmutableMap.copyOf(Maps.filterValues(configValuesByConfiguration, configValues -> configValues.size() > 1));
}
ImmutableList<List<ConfigValue>> densityGroups = ImmutableList.copyOf(configValuesByConfiguration.values());
// We want to pin specific configs to the master, instead of putting them into a density split.
Predicate<ConfigValue> pinConfigToMaster;
if (pinWholeResourceToMaster.test(tableEntry.getResourceId())) {
pinConfigToMaster = anyConfig -> true;
} else if (pinLowestBucketToMaster(tableEntry)) {
ImmutableSet<ConfigValue> lowDensityConfigsPinnedToMaster = pickBestDensityForEachGroup(densityGroups, getLowestDensity(densityBuckets)).collect(toImmutableSet());
pinConfigToMaster = lowDensityConfigsPinnedToMaster::contains;
} else {
pinConfigToMaster = anyConfig -> false;
}
ImmutableList<ConfigValue> valuesToKeep = pickBestDensityForEachGroup(densityGroups, targetDensity).filter(config -> !pinConfigToMaster.test(config)).collect(toImmutableList());
return initialEntry.toBuilder().clearConfigValue().addAllConfigValue(valuesToKeep).build();
}
Aggregations