aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/DensitySpecificResourceFilter.java
diff options
context:
space:
mode:
authorGravatar Andrew Pellegrini <apell@google.com>2015-12-09 18:30:57 +0000
committerGravatar David Chen <dzc@google.com>2015-12-09 22:51:20 +0000
commit8aa4b8ef79641a1d39ade560b2ca461bfcaedc4f (patch)
treee6348fbdbacbf037d9e6948abdf14c09e8584ca3 /src/tools/android/java/com/google/devtools/build/android/DensitySpecificResourceFilter.java
parentcdd5b0ea601fb5636367d5e758b584362483212b (diff)
Fix all density specific resources being removed if multiple densities are specified.
-- MOS_MIGRATED_REVID=109807370
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/DensitySpecificResourceFilter.java')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/DensitySpecificResourceFilter.java55
1 files changed, 29 insertions, 26 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/DensitySpecificResourceFilter.java b/src/tools/android/java/com/google/devtools/build/android/DensitySpecificResourceFilter.java
index 1e191e28a5..a2856160c9 100644
--- a/src/tools/android/java/com/google/devtools/build/android/DensitySpecificResourceFilter.java
+++ b/src/tools/android/java/com/google/devtools/build/android/DensitySpecificResourceFilter.java
@@ -178,17 +178,15 @@ public class DensitySpecificResourceFilter {
Collection<ResourceInfo> qualifierResourceInfos = qualifierGroups.get(qualifiers);
if (qualifierResourceInfos.size() != 1) {
- for (final String density : densities) {
- List<ResourceInfo> sortedResourceInfos = Ordering.natural().onResultOf(
- new Function<ResourceInfo, Double>() {
- @Override
- public Double apply(ResourceInfo info) {
- return matchScore(info, density);
- }
- }).immutableSortedCopy(qualifierResourceInfos);
-
- resourceInfoToRemove.addAll(sortedResourceInfos.subList(1, sortedResourceInfos.size()));
- }
+ List<ResourceInfo> sortedResourceInfos = Ordering.natural().onResultOf(
+ new Function<ResourceInfo, Double>() {
+ @Override
+ public Double apply(ResourceInfo info) {
+ return matchScore(info, densities);
+ }
+ }).immutableSortedCopy(qualifierResourceInfos);
+
+ resourceInfoToRemove.addAll(sortedResourceInfos.subList(1, sortedResourceInfos.size()));
}
}
}
@@ -252,26 +250,31 @@ public class DensitySpecificResourceFilter {
return ImmutableList.copyOf(densityResourceInfos);
}
- private static double matchScore(ResourceInfo resource, String density) {
- if (resource.getDensity().equals(density)) {
- return -2;
+ private static double matchScore(ResourceInfo resource, List<String> densities) {
+ double score = 0;
+ for (String density : densities) {
+ score += computeAffinity(DENSITY_MAP.get(resource.getDensity()), DENSITY_MAP.get(density));
}
+ return score;
+ }
- double affinity =
- Math.log((double) (DENSITY_MAP.get(density)) / DENSITY_MAP.get(resource.getDensity()))
- / Math.log(2);
-
- if (affinity == -1) {
+ private static double computeAffinity(int resourceDensity, int density) {
+ if (resourceDensity == density) {
+ // Exact match is the best.
+ return -2;
+ } else if (resourceDensity == 2 * density) {
// It's very efficient to downsample an image that's exactly 2x the screen
- // density, so we prefer that over other non-perfect matches
+ // density, so we prefer that over other non-perfect matches.
+ return -1;
+ } else {
+ double affinity = Math.log((double) density / resourceDensity) / Math.log(2);
+
+ // We give a slight bump to images that have the same multiplier but are higher quality.
+ if (affinity < 0) {
+ affinity = Math.abs(affinity) - 0.01;
+ }
return affinity;
- } else if (affinity < 0) {
- // We give a slight bump to images that have the same multiplier but are
- // higher quality.
- affinity = Math.abs(affinity + 0.01);
}
-
- return affinity;
}
/** Filters the contents of a resource directory. */