Search in sources :

Example 6 with DensityAlias

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))));
}
Also used : ConfigValue(com.android.aapt.Resources.ConfigValue) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Test(org.junit.Test)

Example 7 with DensityAlias

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);
}
Also used : ConfigValue(com.android.aapt.Resources.ConfigValue) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Test(org.junit.Test)

Example 8 with DensityAlias

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)));
}
Also used : ConfigValue(com.android.aapt.Resources.ConfigValue) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Test(org.junit.Test)

Example 9 with DensityAlias

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);
}
Also used : ConfigValue(com.android.aapt.Resources.ConfigValue) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Test(org.junit.Test)

Example 10 with DensityAlias

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();
}
Also used : DEFAULT_DENSITY_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.DEFAULT_DENSITY_VALUE) ScreenDensitySelector(com.android.tools.build.bundletool.model.targeting.ScreenDensitySelector) ImmutableCollection(com.google.common.collect.ImmutableCollection) CollectorUtils.groupingByDeterministic(com.android.tools.build.bundletool.model.utils.CollectorUtils.groupingByDeterministic) ArrayList(java.util.ArrayList) ManifestMutator.withSplitsRequired(com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired) Type(com.android.aapt.Resources.Type) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ResourceId(com.android.tools.build.bundletool.model.ResourceId) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) ImmutableList(com.google.common.collect.ImmutableList) ResourcesUtils.getLowestDensity(com.android.tools.build.bundletool.model.utils.ResourcesUtils.getLowestDensity) Package(com.android.aapt.Resources.Package) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Version(com.android.tools.build.bundletool.model.version.Version) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ResourceTable(com.android.aapt.Resources.ResourceTable) MIPMAP_TYPE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.MIPMAP_TYPE) ImmutableSet(com.google.common.collect.ImmutableSet) ConfigValue(com.android.aapt.Resources.ConfigValue) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) Entry(com.android.aapt.Resources.Entry) Maps(com.google.common.collect.Maps) ScreenDensity(com.android.bundle.Targeting.ScreenDensity) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ScreenDensityTargeting(com.android.bundle.Targeting.ScreenDensityTargeting) List(java.util.List) Stream(java.util.stream.Stream) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ResourcesUtils(com.android.tools.build.bundletool.model.utils.ResourcesUtils) Optional(java.util.Optional) RESOURCES_WITH_NO_ALTERNATIVES_IN_MASTER_SPLIT(com.android.tools.build.bundletool.model.version.VersionGuardedFeature.RESOURCES_WITH_NO_ALTERNATIVES_IN_MASTER_SPLIT) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) Entry(com.android.aapt.Resources.Entry) ConfigValue(com.android.aapt.Resources.ConfigValue) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List)

Aggregations

DensityAlias (com.android.bundle.Targeting.ScreenDensity.DensityAlias)19 Test (org.junit.Test)17 ConfigValue (com.android.aapt.Resources.ConfigValue)12 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)10 BundleModule (com.android.tools.build.bundletool.model.BundleModule)8 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)8 ResourceTable (com.android.aapt.Resources.ResourceTable)5 ResourceTableBuilder (com.android.tools.build.bundletool.testing.ResourceTableBuilder)5 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)4 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 Configuration (com.android.aapt.ConfigurationOuterClass.Configuration)3 ScreenDensity (com.android.bundle.Targeting.ScreenDensity)3 ManifestMutator.withSplitsRequired (com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired)3 ResourceId (com.android.tools.build.bundletool.model.ResourceId)3 DEFAULT_DENSITY_VALUE (com.android.tools.build.bundletool.model.utils.ResourcesUtils.DEFAULT_DENSITY_VALUE)3 Version (com.android.tools.build.bundletool.model.version.Version)3 ImmutableCollection (com.google.common.collect.ImmutableCollection)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)3