move getMinSdkVersion() from LocalRepoManager to Utils
This will probably be useful elsewhere.
This commit is contained in:
parent
d24f94476e
commit
c1a6f545cf
@ -19,6 +19,9 @@
|
|||||||
package org.fdroid.fdroid;
|
package org.fdroid.fdroid;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
|
import android.content.res.AssetManager;
|
||||||
|
import android.content.res.XmlResourceParser;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
@ -28,6 +31,8 @@ import android.util.Log;
|
|||||||
import com.nostra13.universalimageloader.utils.StorageUtils;
|
import com.nostra13.universalimageloader.utils.StorageUtils;
|
||||||
|
|
||||||
import org.fdroid.fdroid.data.Repo;
|
import org.fdroid.fdroid.data.Repo;
|
||||||
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
@ -201,6 +206,34 @@ public final class Utils {
|
|||||||
return androidVersionNames[sdkLevel];
|
return androidVersionNames[sdkLevel];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* PackageManager doesn't give us minSdkVersion, so we have to parse it */
|
||||||
|
public static int getMinSdkVersion(Context context, String packageName) {
|
||||||
|
try {
|
||||||
|
AssetManager am = context.createPackageContext(packageName, 0).getAssets();
|
||||||
|
XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
|
||||||
|
int eventType = xml.getEventType();
|
||||||
|
while (eventType != XmlPullParser.END_DOCUMENT) {
|
||||||
|
if (eventType == XmlPullParser.START_TAG) {
|
||||||
|
if (xml.getName().equals("uses-sdk")) {
|
||||||
|
for (int j = 0; j < xml.getAttributeCount(); j++) {
|
||||||
|
if (xml.getAttributeName(j).equals("minSdkVersion")) {
|
||||||
|
return Integer.parseInt(xml.getAttributeValue(j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eventType = xml.nextToken();
|
||||||
|
}
|
||||||
|
} catch (NameNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (XmlPullParserException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return 8; // some kind of hopeful default
|
||||||
|
}
|
||||||
|
|
||||||
public static int countSubstringOccurrence(File file, String substring) throws IOException {
|
public static int countSubstringOccurrence(File file, String substring) throws IOException {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
FileReader input = null;
|
FileReader input = null;
|
||||||
|
@ -188,6 +188,7 @@ public class LocalRepoManager {
|
|||||||
|
|
||||||
@TargetApi(9)
|
@TargetApi(9)
|
||||||
public App addApp(Context context, String packageName) {
|
public App addApp(Context context, String packageName) {
|
||||||
|
// TODO this should become a constructor, i.e. public App(PackageManager pm, String id)
|
||||||
ApplicationInfo appInfo;
|
ApplicationInfo appInfo;
|
||||||
PackageInfo packageInfo;
|
PackageInfo packageInfo;
|
||||||
try {
|
try {
|
||||||
@ -223,7 +224,7 @@ public class LocalRepoManager {
|
|||||||
apk.hashType = "sha256";
|
apk.hashType = "sha256";
|
||||||
apk.hash = Utils.getBinaryHash(apkFile, apk.hashType);
|
apk.hash = Utils.getBinaryHash(apkFile, apk.hashType);
|
||||||
apk.added = app.added;
|
apk.added = app.added;
|
||||||
apk.minSdkVersion = getMinSdkVersion(context, packageName);
|
apk.minSdkVersion = Utils.getMinSdkVersion(context, packageName);
|
||||||
apk.id = app.id;
|
apk.id = app.id;
|
||||||
apk.installedFile = apkFile;
|
apk.installedFile = apkFile;
|
||||||
if (packageInfo.requestedPermissions == null)
|
if (packageInfo.requestedPermissions == null)
|
||||||
@ -311,34 +312,6 @@ public class LocalRepoManager {
|
|||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* PackageManager doesn't give us minSdkVersion, so we have to parse it */
|
|
||||||
public int getMinSdkVersion(Context context, String packageName) {
|
|
||||||
try {
|
|
||||||
AssetManager am = context.createPackageContext(packageName, 0).getAssets();
|
|
||||||
XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
|
|
||||||
int eventType = xml.getEventType();
|
|
||||||
while (eventType != XmlPullParser.END_DOCUMENT) {
|
|
||||||
if (eventType == XmlPullParser.START_TAG) {
|
|
||||||
if (xml.getName().equals("uses-sdk")) {
|
|
||||||
for (int j = 0; j < xml.getAttributeCount(); j++) {
|
|
||||||
if (xml.getAttributeName(j).equals("minSdkVersion")) {
|
|
||||||
return Integer.parseInt(xml.getAttributeValue(j));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
eventType = xml.nextToken();
|
|
||||||
}
|
|
||||||
} catch (NameNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (XmlPullParserException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return 8; // some kind of hopeful default
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeApp(String packageName) {
|
public void removeApp(String packageName) {
|
||||||
apps.remove(packageName);
|
apps.remove(packageName);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user