Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
choose best density for resource
Browse files Browse the repository at this point in the history
  • Loading branch information
dongliu committed Oct 12, 2018
1 parent 19b1093 commit f23aa00
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/main/java/net/dongliu/apk/parser/struct/ResourceValue.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package net.dongliu.apk.parser.struct;

import net.dongliu.apk.parser.struct.resource.*;
import net.dongliu.apk.parser.utils.Locales;
import net.dongliu.apk.parser.struct.resource.ResourceEntry;
import net.dongliu.apk.parser.struct.resource.ResourceTable;
import net.dongliu.apk.parser.struct.resource.Type;
import net.dongliu.apk.parser.struct.resource.TypeSpec;

import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -119,6 +116,11 @@ public String toStringValue(ResourceTable resourceTable, Locale locale) {
return null;
}
}

@Override
public String toString() {
return value + ":" + stringPool.get(value);
}
}

/**
Expand Down Expand Up @@ -147,18 +149,21 @@ public String toStringValue(ResourceTable resourceTable, Locale locale) {
// read from type resource
ResourceEntry selected = null;
TypeSpec typeSpec = null;
int currentLevel = -1;
int currentLocalMatchLevel = -1;
int currentDensityLevel = -1;
for (ResourceTable.Resource resource : resources) {
Type type = resource.getType();
typeSpec = resource.getTypeSpec();
ResourceEntry resourceEntry = resource.getResourceEntry();
int level = Locales.match(locale, type.getLocale());
if (level == 2) {
int localMatchLevel = Locales.match(locale, type.getLocale());
int densityLevel = densityLevel(type.getDensity());
if (localMatchLevel > currentLocalMatchLevel) {
selected = resourceEntry;
break;
} else if (level > currentLevel) {
currentLocalMatchLevel = localMatchLevel;
currentDensityLevel = densityLevel;
} else if (densityLevel > currentDensityLevel) {
selected = resourceEntry;
currentLevel = level;
currentDensityLevel = densityLevel;
}
}
String result;
Expand All @@ -176,6 +181,15 @@ public long getReferenceResourceId() {
return value & 0xFFFFFFFFL;
}

private static int densityLevel(int density) {
if (density == Densities.ANY || density == Densities.NONE) {
return -1;
}
if (density == Densities.DEFAULT) {
return Densities.DEFAULT;
}
return density;
}
}

private static class NullResourceValue extends ResourceValue {
Expand Down

0 comments on commit f23aa00

Please sign in to comment.