+ mProfile.setDetectedVersion(detectedVersion.getValue());
+ }
+ synchronized public void triggerVersionDetection() {
+ if (versionDetectionThread != null)
+ versionDetectionThread.interrupt();
+
+ versionDetectionThread = new VersionDetectionThread(this);
+ versionDetectionThread.start();
+ }
+ static class VersionDetectionThread extends Thread {
+ private final Pattern versionPattern =
+ Pattern.compile("^\"(\\d+)\\.(\\d+)(?:\\.(\\d+))?\"$");
+ private final ProfileDetailModel model;
+ public VersionDetectionThread(ProfileDetailModel model) {
+ this.model = model;
+ }
+ @Override
+ public void run() {
+ try {
+ HttpURLConnection http = NetworkUtil.prepareConnection(model.getUrl(), "version",
+ model.getUseAuthentication());
+ switch (http.getResponseCode()) {
+ case 200:
+ break;
+ case 404:
+ model.detectedVersion.postValue(new HledgerVersion(true));
+ return;
+ default:
+ Logger.debug("profile", String.format(Locale.US,
+ "HTTP error detecting hledger-web version: [%d] %s",
+ http.getResponseCode(), http.getResponseMessage()));
+ model.detectedVersion.postValue(null);
+ return;
+ }
+ InputStream stream = http.getInputStream();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
+ String version = reader.readLine();
+ Matcher m = versionPattern.matcher(version);
+ if (m.matches()) {
+ int major = Integer.parseInt(Objects.requireNonNull(m.group(1)));
+ int minor = Integer.parseInt(Objects.requireNonNull(m.group(2)));
+ final boolean hasPatch = m.groupCount() >= 3;
+ int patch = hasPatch ? Integer.parseInt(Objects.requireNonNull(m.group(3))) : 0;
+
+ model.detectedVersion.postValue(
+ hasPatch ? new HledgerVersion(major, minor, patch)
+ : new HledgerVersion(major, minor));
+ }
+ else {
+ Logger.debug("profile",
+ String.format("Unrecognised version string '%s'", version));
+ }
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ }
+ }