Search in sources :

Example 1 with DEFAULT_DENSITY_VALUE

use of com.android.tools.build.bundletool.model.utils.ResourcesUtils.DEFAULT_DENSITY_VALUE 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

Configuration (com.android.aapt.ConfigurationOuterClass.Configuration)1 ConfigValue (com.android.aapt.Resources.ConfigValue)1 Entry (com.android.aapt.Resources.Entry)1 Package (com.android.aapt.Resources.Package)1 ResourceTable (com.android.aapt.Resources.ResourceTable)1 Type (com.android.aapt.Resources.Type)1 ScreenDensity (com.android.bundle.Targeting.ScreenDensity)1 DensityAlias (com.android.bundle.Targeting.ScreenDensity.DensityAlias)1 ScreenDensityTargeting (com.android.bundle.Targeting.ScreenDensityTargeting)1 ManifestMutator.withSplitsRequired (com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired)1 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)1 ResourceId (com.android.tools.build.bundletool.model.ResourceId)1 ResourceTableEntry (com.android.tools.build.bundletool.model.ResourceTableEntry)1 ScreenDensitySelector (com.android.tools.build.bundletool.model.targeting.ScreenDensitySelector)1 CollectorUtils.groupingByDeterministic (com.android.tools.build.bundletool.model.utils.CollectorUtils.groupingByDeterministic)1 ResourcesUtils (com.android.tools.build.bundletool.model.utils.ResourcesUtils)1 DEFAULT_DENSITY_VALUE (com.android.tools.build.bundletool.model.utils.ResourcesUtils.DEFAULT_DENSITY_VALUE)1 MIPMAP_TYPE (com.android.tools.build.bundletool.model.utils.ResourcesUtils.MIPMAP_TYPE)1 ResourcesUtils.getLowestDensity (com.android.tools.build.bundletool.model.utils.ResourcesUtils.getLowestDensity)1 Version (com.android.tools.build.bundletool.model.version.Version)1