Also populate maxSdkVersion when parsing a package

There is no reason why this should fail, so make the default be 0.
This commit is contained in:
Daniel Martí 2015-05-14 17:03:17 +02:00
parent 70a93b01db
commit 09444b0181
2 changed files with 13 additions and 3 deletions

View File

@ -219,7 +219,8 @@ public final class Utils {
}
/* PackageManager doesn't give us minSdkVersion, so we have to parse it */
public static int getMinSdkVersion(Context context, String packageName) {
private static int getMinMaxSdkVersion(Context context, String packageName,
String attrName) {
try {
AssetManager am = context.createPackageContext(packageName, 0).getAssets();
XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
@ -227,7 +228,7 @@ public final class Utils {
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG && xml.getName().equals("uses-sdk")) {
for (int j = 0; j < xml.getAttributeCount(); j++) {
if (xml.getAttributeName(j).equals("minSdkVersion")) {
if (xml.getAttributeName(j).equals(attrName)) {
return Integer.parseInt(xml.getAttributeValue(j));
}
}
@ -237,7 +238,15 @@ public final class Utils {
} catch (PackageManager.NameNotFoundException | IOException | XmlPullParserException e) {
e.printStackTrace();
}
return 8; // some kind of hopeful default
return 0;
}
public static int getMinSdkVersion(Context context, String packageName) {
return getMinMaxSdkVersion(context, packageName, "minSdkVersion");
}
public static int getMaxSdkVersion(Context context, String packageName) {
return getMinMaxSdkVersion(context, packageName, "maxSdkVersion");
}
public static int countSubstringOccurrence(File file, String substring) throws IOException {

View File

@ -257,6 +257,7 @@ public class App extends ValueObject implements Comparable<App> {
apk.hash = Utils.getBinaryHash(apkFile, apk.hashType);
apk.added = this.added;
apk.minSdkVersion = Utils.getMinSdkVersion(context, packageName);
apk.maxSdkVersion = Utils.getMaxSdkVersion(context, packageName);
apk.id = this.id;
apk.installedFile = apkFile;
apk.permissions = Utils.CommaSeparatedList.make(packageInfo.requestedPermissions);