replaced json with xml

This commit is contained in:
Dimitri Rusin 2018-07-12 20:03:31 +00:00
parent 03507804b6
commit 51135e5684

View File

@ -43,13 +43,9 @@ import org.fdroid.fdroid.data.Schema.InstalledAppTable;
import org.fdroid.fdroid.data.Schema.PackageTable; import org.fdroid.fdroid.data.Schema.PackageTable;
import org.fdroid.fdroid.data.Schema.RepoTable; import org.fdroid.fdroid.data.Schema.RepoTable;
import java.util.ArrayList; import java.util.*;
import java.util.List; import java.io.*;
import org.xmlpull.v1.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import org.json.*;
/** /**
* This is basically a singleton used to represent the database at the core * This is basically a singleton used to represent the database at the core
@ -261,10 +257,8 @@ public class DBHelper extends SQLiteOpenHelper {
db.execSQL(CREATE_TABLE_APK_ANTI_FEATURE_JOIN); db.execSQL(CREATE_TABLE_APK_ANTI_FEATURE_JOIN);
ensureIndexes(db); ensureIndexes(db);
Log.debugLog(TAG, "TEST PACKAGE NAME: " + context.getPackage());
// Always, load the internal defaults afterwards. This way, F-Droid native repos can never be deleted // Always, load the internal defaults afterwards. This way, F-Droid native repos can never be deleted
List<String> default_repos = loadDefaultReposInternal(); List<String> defaultRepos = loadDefaultReposInternal();
// Insert all the repos into the database // Insert all the repos into the database
if (defaultRepos.size() % REPO_XML_ARG_COUNT != 0) { if (defaultRepos.size() % REPO_XML_ARG_COUNT != 0) {
@ -273,65 +267,66 @@ public class DBHelper extends SQLiteOpenHelper {
} }
// Load a couple external default repos from one of { "/oem", "/odm", "/vendor", "/system" } // Load a couple external default repos from one of { "/oem", "/odm", "/vendor", "/system" }
int minPriority = (default_repos.size() / REPO_XML_ARG_COUNT) + 1; defaultRepos.addAll(loadDefaultReposExternal());
default_repos.addAll(loadDefaultReposExternal(minPriority));
for (int i = 0; i < defaultRepos.size; i += REPO_XML_ARG_COUNT) { Utils.debugLog(TAG, "defaultRepos.size(): " + defaultRepos.size());
for (int i = 0; i < defaultRepos.size(); i += REPO_XML_ARG_COUNT) {
insertRepo( insertRepo(
db, db,
defaultRepos[i], // name defaultRepos.get(i), // name
defaultRepos[i + 1], // address defaultRepos.get(i + 1), // address
defaultRepos[i + 2], // description defaultRepos.get(i + 2), // description
defaultRepos[i + 3], // version defaultRepos.get(i + 3), // version
defaultRepos[i + 4], // enabled defaultRepos.get(i + 4), // enabled
defaultRepos[i + 5], // priority defaultRepos.get(i + 5), // priority
defaultRepos[i + 6], // pushRequests defaultRepos.get(i + 6), // pushRequests
defaultRepos[i + 7] // pubkey defaultRepos.get(i + 7) // pubkey
); );
} }
} }
private void loadDefaultReposInternal() { private List<String> loadDefaultReposInternal() {
return new LinkedList<String>(context.getResources().getStringArray(R.array.default_repos)); return new LinkedList<>(Arrays.asList(context.getResources().getStringArray(R.array.default_repos)));
} }
/* /*
* Look for external repositories under { "/oem", "/odm", "/vendor", "/system" } in this order and only load the first one. * Look for external repositories under { "/oem", "/odm", "/vendor", "/system" } in this order and only load the first one.
* If ROOT is one of those paths and 'packageName' is the name of the package * If ROOT is one of those paths and 'packageName' is the name of the package
* that contains this class, then we look under ROOT/etc/'packageName'/default_repos.xml * that contains this class, then we look under 'root'/etc/'packageName'/default_repos.xml
*/ */
private loadDefaultReposExternal(int minPriority) { private List<String> loadDefaultReposExternal() {
final String packageName = context.getPackage(); final String packageName = context.getPackageName();
for (String root : { "/oem", "/odm", "/vendor", "/system" }) { for (String root : Arrays.asList("/oem", "/odm", "/vendor", "/system")) {
String defaultReposPath = root + "/etc/" + packageName + "/default_repos.xml"; String defaultReposPath = root + "/etc/" + packageName + "/default_repos.xml";
try { try {
File defaultReposFile = new File(defaultReposPath); File defaultReposFile = new File(defaultReposPath);
// Only try loading external repos once. Even if this one fails, we will not try the other roots // Only try loading external repos once. Even if this one fails, we will not try the other roots
if (defaultReposFile.exists() && defaultReposFile.isFile()) { if (defaultReposFile.exists() && defaultReposFile.isFile()) {
return parseXmlRepos(defaultReposFile, minPriority); return parseXmlRepos(defaultReposFile);
} }
} catch (Exception e) { } catch (Exception e) {
Utils.debugLog(TAG, "Error loading " + defaultReposPath); Utils.debugLog(TAG, "Error loading " + defaultReposPath);
Utils.debugLog(TAG, "Exception: " + e.getMessage()); Utils.debugLog(TAG, "Exception: " + e.getMessage());
return; break;
} }
} }
return new LinkedList<>();
} }
private List<String> parseXmlRepos(File defaultReposFile, int minPriority) { private List<String> parseXmlRepos(File defaultReposFile) throws IOException, XmlPullParserException {
List<String> default_repos = LinkedList<>(); List<String> defaultRepos = new LinkedList<>();
try { InputStream xmlInputStream = null;
InputStream xmlInputStream = new FileInputStream(defaultReposFile); xmlInputStream = new FileInputStream(defaultReposFile);
XmlPullParserFactoryfactory = XmlPullParserFactory.newInstance(); XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser(); XmlPullParser parser = factory.newPullParser();
parser.setInput(is, "UTF-8"); parser.setInput(xmlInputStream, "UTF-8");
// Type of piece of xml file, can be END_DOCUMENT, TEXT, END_TAG, START_TAG, ... We only care about TEXT. // Type of piece of xml file, can be END_DOCUMENT, TEXT, END_TAG, START_TAG, ...
int eventType = parser.getEventType(); int eventType = parser.getEventType();
static final int PRIORITY_INDEX = 5; boolean isItem = false;
int indexItem = 0;
/* Walk through all TEXT pieces of the xml file and put them into our list of default repos. /* Walk through all TEXT pieces of the xml file and put them into our list of default repos.
* This could be improved by structuring the content for example into a list of lists where each contained list represents a repository, but * This could be improved by structuring the content for example into a list of lists where each contained list represents a repository, but
* we do it the traditional way. * we do it the traditional way.
@ -339,103 +334,26 @@ public class DBHelper extends SQLiteOpenHelper {
while (eventType != XmlPullParser.END_DOCUMENT) { while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName(); String tagname = parser.getName();
switch (eventType) { switch (eventType) {
/*
* This checks whether the current repository does not violate the priority constraint: Its priority is higher or equal to minPrioriry.
* minPriority is at least the number of internal repos plus one. This way, the external repos can never be shown before the internal ones in the UI.
*
* Very ugly though. We count the number of items read in the xml file, because the items are not named but just rowed one after another.
* We also skip one event here, namely the text event after the START_TAG event.
*/
case XmlPullParser.START_TAG: case XmlPullParser.START_TAG:
if (indexItem == PRIORITY_INDEX) { if (tagname.equals("item")) {
parser.next(); isItem = true;
int priority = Integer.parseInt(parser.getText());
// If some repository violates the priority constraint, immediately dismiss this whole external set of repos
if (priority > minPriority) {
return new LinkedList<>();
} }
} break;
indexItem++; case XmlPullParser.END_TAG:
if (indexItem > 7) { indexItem -= 7; } isItem = false;
break; break;
case XmlPullParser.TEXT: case XmlPullParser.TEXT:
default_repos.add(parser.getText()); // This is a piece of real information in the xml file if (isItem) {
defaultRepos.add(new String(parser.getText())); // This is a piece of real information in the xml file
}
break; break;
} }
eventType = parser.next(); eventType = parser.next();
} }
xmlInputStream.close();
} catch (XmlPullParserException e) { return defaultRepos;
Utils.debugLog(TAG, "Error loading " + defaultReposFile.getAbsolutePath());
Utils.debugLog(TAG, "Exception: " + e.getMessage());
return;
} catch (IOException e) {
Utils.debugLog(TAG, "Error loading " + defaultReposFile.getAbsolutePath());
Utils.debugLog(TAG, "Exception: " + e.getMessage());
return;
} }
return default_repos;
}
/*
* The following two functions might be useful in the future to add repositories in json format as proposed in
* https://gitlab.com/fdroid/fdroidclient/merge_requests/705
*
private String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder stringBuilder = new StringBuilder();
try {
String ls = System.getProperty("line.separator");
String line = null;
while((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
} finally {
reader.close();
}
return stringBuilder.toString();
}
private void loadOemDefaultRepos(SQLiteDatabase db) {
Utils.debugLog(TAG, "Loading external repos: Opening " + OEM_DEFAULT_REPOS_PATH);
JSONArray reposArray;
try {
String jsonContents = readFile(OEM_DEFAULT_REPOS_PATH);
reposArray = new JSONArray(jsonContents);
} catch (Exception e) {
Utils.debugLog(TAG, "Error loading " + OEM_DEFAULT_REPOS_PATH);
Utils.debugLog(TAG, "Exception: " + e.getMessage());
return;
}
Utils.debugLog(TAG, OEM_DEFAULT_REPOS_PATH + " successfully opened.");
try {
for (int i = 0; i < reposArray.length(); i++) {
JSONObject repo = reposArray.getJSONObject(i);
insertRepo(
db,
repo.getString("name"),
repo.getString("address"),
repo.getString("description"),
repo.getString("version"),
repo.getString("enabled"),
repo.getString("priority"),
repo.getString("pushRequests"),
repo.getString("pubkey")
);
}
} catch (Exception e) {
Utils.debugLog(TAG, "Error loading at least one external json repository from " + OEM_DEFAULT_REPOS_PATH);
Utils.debugLog(TAG, "Exception: " + e.getMessage());
return;
}
Utils.debugLog(TAG, "All external repositories successfully added!");
}
*/
@Override @Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {