+ mProfile.setTheme(themeId.getValue());
+ mProfile.setFutureDates(futureDates.getValue()
+ .toInt());
+ mProfile.setApiVersion(apiVersion.getValue()
+ .toInt());
+ HledgerVersion version = detectedVersion.getValue();
+ mProfile.setDetectedVersionPre_1_19(version != null && version.isPre_1_20_1());
+ mProfile.setDetectedVersionMajor(version != null ? version.getMajor() : -1);
+ mProfile.setDetectedVersionMinor(version != null ? version.getMinor() : -1);
+ }
+ synchronized public void triggerVersionDetection() {
+ if (versionDetectionThread != null)
+ versionDetectionThread.interrupt();
+
+ versionDetectionThread = new VersionDetectionThread(this);
+ versionDetectionThread.start();
+ }
+ public LiveData<Long> getProfileId() {
+ return profileId;
+ }
+ static class VersionDetectionThread extends Thread {
+ static final int TARGET_PROCESS_DURATION = 1000;
+ private final Pattern versionPattern =
+ Pattern.compile("^\"(\\d+)\\.(\\d+)(?:\\.(\\d+))?\"$");
+ private final ProfileDetailModel model;
+ public VersionDetectionThread(ProfileDetailModel model) {
+ this.model = model;
+ }
+ private HledgerVersion detectVersion() {
+ App.setAuthenticationDataFromProfileModel(model);
+ HttpURLConnection http;
+ try {
+ http = NetworkUtil.prepareConnection(model.getUrl(), "version",
+ model.getUseAuthentication());
+ switch (http.getResponseCode()) {
+ case 200:
+ break;
+ case 404:
+ return new HledgerVersion(true);
+ default:
+ Logger.debug("profile", String.format(Locale.US,
+ "HTTP error detecting hledger-web version: [%d] %s",
+ http.getResponseCode(), http.getResponseMessage()));
+ return null;
+ }
+ 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 String patchText = m.group(3);
+ final boolean hasPatch = patchText != null;
+ int patch = hasPatch ? Integer.parseInt(patchText) : 0;
+
+ return hasPatch ? new HledgerVersion(major, minor, patch)
+ : new HledgerVersion(major, minor);
+ }
+ else {
+ Logger.debug("profile",
+ String.format("Unrecognised version string '%s'", version));
+ return null;
+ }
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ return null;
+ }
+ finally {
+ App.resetAuthenticationData();
+ }
+ }
+ @Override
+ public void run() {
+ model.detectingHledgerVersion.postValue(true);
+ try {
+ long startTime = System.currentTimeMillis();
+
+ final HledgerVersion version = detectVersion();
+
+ long elapsed = System.currentTimeMillis() - startTime;
+ Logger.debug("profile", "Detection duration " + elapsed);
+ if (elapsed < TARGET_PROCESS_DURATION) {
+ try {
+ Thread.sleep(TARGET_PROCESS_DURATION - elapsed);
+ }
+ catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ model.detectedVersion.postValue(version);
+ }
+ finally {
+ model.detectingHledgerVersion.postValue(false);
+ }
+ }