/** * Returns the entry related to a given package name. * * @param package_name The package name of the application to check. * @return An object containing all the neccessary information to extract a version and/or a * download url and changelog from a web page. */ public UpdateSourceEntry get_entry(String package_name) { for (UpdateSourceEntry use : _entries) { Matcher m = Pattern.compile(use.get_applicable_packages()).matcher(package_name); if (m.find()) { return use; } } return null; }
/** * Checks whether the UpdateSource is applicable for a given application. The package name of the * given app is checked against the different regular expressions available for the update source. * * @param package_name The package name of the application to check. * @return Whether the UpdateSource is valid for a given application. */ public boolean is_applicable(String package_name) { // Verify that the update source supports given application by matching the package name. for (UpdateSourceEntry pe : _entries) { Matcher m = Pattern.compile(pe.get_applicable_packages()).matcher(package_name); if (m.find()) { return true; } } return false; }
/** * Initializes the update sources by reading the ones available in sources.json. They are kept in * memory in order to avoid looking them up in the assets every time. * * @param ctx The context of the application. */ public static void initialize_update_sources(Context ctx) { if (_SOURCES != null) { return; } _SOURCES = new ArrayList<UpdateSource>(); Log.v(MainActivity.TAG, "Reading update sources..."); try { InputStream is = ctx.getAssets().open("sources.json"); StringBuilder buffer = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); String s; while ((s = br.readLine()) != null) { buffer.append(s); } JSONArray sources = new JSONArray(buffer.toString()); for (int i = 0; i < sources.length(); ++i) { String name = sources.getJSONObject(i).getString("name"); Log.v(MainActivity.TAG, "Reading " + name); String url = sources.getJSONObject(i).getString("url"); // Get the list of regular expressions and URLs for each supported package name. JSONObject packages = sources.getJSONObject(i).optJSONObject("packages"); if (packages == null || packages.length() == 0) { throw new JSONException("packages missing or empty for " + name); } ArrayList<UpdateSourceEntry> entries = new ArrayList<UpdateSourceEntry>(); Iterator<String> it = packages.keys(); while (it.hasNext()) { String applicable_packages = it.next(); JSONObject entry = packages.getJSONObject(applicable_packages); UpdateSourceEntry use = new UpdateSourceEntry(applicable_packages); use.set_version_regexp(entry.getString("version")); try { use.set_download_regexp(entry.getString("download_regexp")); } catch (JSONException ignored) { } try { use.set_download_url(entry.getString("download")); } catch (JSONException ignored) { } try { use.set_changelog_regexp(entry.getString("changelog")); } catch (JSONException ignored) { } entries.add(use); } UpdateSource us = new UpdateSource(name, url, entries); // Get autoselection conditions if available: JSONArray conditions = sources.getJSONObject(i).optJSONArray("autoselect_if"); if (conditions != null) { List<String> autoselect_conditions = new ArrayList<String>(); for (int j = 0; j < conditions.length(); ++j) { autoselect_conditions.add(conditions.getString(j)); } if (autoselect_conditions.size() > 0) { us.set_autoselect_conditions(autoselect_conditions); } } // Get optional request delay (in milliseconds) int delay = sources.getJSONObject(i).optInt("request_delay", 0); if (delay > 0) { us.set_request_delay(delay); } _SOURCES.add(us); } } catch (IOException e) { Log.e(MainActivity.TAG, "Could not open sources.json!", e); } catch (JSONException e) { Log.e(MainActivity.TAG, "sources.json seems to be malformed!", e); } }