Disable incompatible APKs

This commit is contained in:
Henrik Tunedal 2011-03-04 18:08:52 +01:00
parent 08816e4bc8
commit 111a57fc47
2 changed files with 48 additions and 0 deletions

View File

@ -70,8 +70,10 @@ public class AppDetails extends ListActivity {
private class ApkListAdapter extends BaseAdapter {
private List<DB.Apk> items = new ArrayList<DB.Apk>();
private DB.Apk.CompatibilityChecker compatChecker;
public ApkListAdapter(Context context) {
compatChecker = DB.Apk.CompatibilityChecker.getChecker(context);
}
public void addItem(DB.Apk apk) {
@ -122,6 +124,12 @@ public class AppDetails extends ListActivity {
} else {
buildtype.setText("bin");
}
if (!compatChecker.isCompatible(apk)) {
View[] views = { v, version, status, size, buildtype };
for (View view : views) {
view.setEnabled(false);
}
}
return v;
}
}

View File

@ -32,6 +32,7 @@ import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;
@ -184,6 +185,45 @@ public class DB {
String path = apkName.replace(" ", "%20");
return server + "/" + path;
}
// Call isCompatible(apk) on an instance of this class to
// check if an APK is compatible with the user's device.
public static abstract class CompatibilityChecker {
// Because Build.VERSION.SDK_INT requires API level 5
protected final static int SDK_INT
= Integer.parseInt(Build.VERSION.SDK);
public static CompatibilityChecker getChecker(Context ctx) {
CompatibilityChecker checker;
if (SDK_INT >= 5)
checker = new EclairChecker(ctx);
else
checker = new BasicChecker();
Log.d("FDroid", "Compatibility checker for API level "
+ SDK_INT + ": " + checker.getClass().getName());
return checker;
}
public abstract boolean isCompatible(Apk apk);
}
private static class BasicChecker extends CompatibilityChecker {
public boolean isCompatible(Apk apk) {
return (apk.minSdkVersion <= SDK_INT);
}
}
private static class EclairChecker extends CompatibilityChecker {
private PackageManager pm;
public EclairChecker(Context ctx) {
pm = ctx.getPackageManager();
}
public boolean isCompatible(Apk apk) {
if (apk.minSdkVersion > SDK_INT)
return false;
for (String feat : apk.features) {
if (!pm.hasSystemFeature(feat))
return false;
}
return true;
}
}
}
// The TABLE_REPO table stores the details of the repositories in use.