X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfileDetailModel.java;h=92d3ab508dd98930c8fd2fa1a4aec07ef2328312;hb=9662c07481ca878f4e4ff23c2923737241a3f3b4;hp=57c992180d265bbda33e61655429d8a9140e7e45;hpb=c434526b687162a4ebf4c268e521404e2b58fcc7;p=mobile-ledger-staging.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java index 57c99218..92d3ab50 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java @@ -17,16 +17,32 @@ package net.ktnx.mobileledger.ui.profiles; +import android.text.TextUtils; + import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModel; -import net.ktnx.mobileledger.async.SendTransactionTask; +import net.ktnx.mobileledger.App; +import net.ktnx.mobileledger.json.API; import net.ktnx.mobileledger.model.Currency; +import net.ktnx.mobileledger.model.HledgerVersion; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.utils.Colors; +import net.ktnx.mobileledger.utils.Logger; import net.ktnx.mobileledger.utils.Misc; +import net.ktnx.mobileledger.utils.NetworkUtil; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.util.Locale; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class ProfileDetailModel extends ViewModel { private static final String HTTPS_URL_START = "https://"; @@ -38,14 +54,16 @@ public class ProfileDetailModel extends ViewModel { private final MutableLiveData showCommodityByDefault = new MutableLiveData<>(false); private final MutableLiveData showCommentsByDefault = new MutableLiveData<>(true); private final MutableLiveData useAuthentication = new MutableLiveData<>(false); - private final MutableLiveData apiVersion = - new MutableLiveData<>(SendTransactionTask.API.auto); + private final MutableLiveData apiVersion = new MutableLiveData<>(API.auto); private final MutableLiveData url = new MutableLiveData<>(null); private final MutableLiveData authUserName = new MutableLiveData<>(null); private final MutableLiveData authPassword = new MutableLiveData<>(null); private final MutableLiveData preferredAccountsFilter = new MutableLiveData<>(null); private final MutableLiveData themeId = new MutableLiveData<>(-1); + private final MutableLiveData detectedVersion = new MutableLiveData<>(null); + private final MutableLiveData detectingHledgerVersion = new MutableLiveData<>(false); public int initialThemeHue = Colors.DEFAULT_HUE_DEG; + private VersionDetectionThread versionDetectionThread; public ProfileDetailModel() { } String getProfileName() { @@ -109,7 +127,7 @@ public class ProfileDetailModel extends ViewModel { void observeShowCommodityByDefault(LifecycleOwner lfo, Observer o) { showCommodityByDefault.observe(lfo, o); } - Boolean getUseAuthentication() { + public Boolean getUseAuthentication() { return useAuthentication.getValue(); } void setUseAuthentication(boolean newValue) { @@ -119,17 +137,25 @@ public class ProfileDetailModel extends ViewModel { void observeUseAuthentication(LifecycleOwner lfo, Observer o) { useAuthentication.observe(lfo, o); } - SendTransactionTask.API getApiVersion() { + API getApiVersion() { return apiVersion.getValue(); } - void setApiVersion(SendTransactionTask.API newValue) { + void setApiVersion(API newValue) { if (newValue != apiVersion.getValue()) apiVersion.setValue(newValue); } - void observeApiVersion(LifecycleOwner lfo, Observer o) { + void observeApiVersion(LifecycleOwner lfo, Observer o) { apiVersion.observe(lfo, o); } - String getUrl() { + HledgerVersion getDetectedVersion() { return detectedVersion.getValue(); } + void setDetectedVersion(HledgerVersion newValue) { + if (!Objects.equals(detectedVersion.getValue(), newValue)) + detectedVersion.setValue(newValue); + } + void observeDetectedVersion(LifecycleOwner lfo, Observer o) { + detectedVersion.observe(lfo, o); + } + public String getUrl() { return url.getValue(); } void setUrl(String newValue) { @@ -143,7 +169,7 @@ public class ProfileDetailModel extends ViewModel { void observeUrl(LifecycleOwner lfo, Observer o) { url.observe(lfo, o); } - String getAuthUserName() { + public String getAuthUserName() { return authUserName.getValue(); } void setAuthUserName(String newValue) { @@ -157,7 +183,7 @@ public class ProfileDetailModel extends ViewModel { void observeUserName(LifecycleOwner lfo, Observer o) { authUserName.observe(lfo, o); } - String getAuthPassword() { + public String getAuthPassword() { return authPassword.getValue(); } void setAuthPassword(String newValue) { @@ -194,6 +220,9 @@ public class ProfileDetailModel extends ViewModel { void observeThemeId(LifecycleOwner lfo, Observer o) { themeId.observe(lfo, o); } + void observeDetectingHledgerVersion(LifecycleOwner lfo, Observer o) { + detectingHledgerVersion.observe(lfo, o); + } void setValuesFromProfile(MobileLedgerProfile mProfile, int newProfileHue) { final int profileThemeId; if (mProfile != null) { @@ -203,7 +232,7 @@ public class ProfileDetailModel extends ViewModel { showCommodityByDefault.setValue(mProfile.getShowCommodityByDefault()); { String comm = mProfile.getDefaultCommodity(); - if (Misc.isEmptyOrNull(comm)) + if (TextUtils.isEmpty(comm)) setDefaultCommodity(null); else setDefaultCommodity(new Currency(-1, comm)); @@ -216,6 +245,7 @@ public class ProfileDetailModel extends ViewModel { authPassword.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : ""); preferredAccountsFilter.setValue(mProfile.getPreferredAccountsFilter()); themeId.setValue(mProfile.getThemeHue()); + detectedVersion.setValue(mProfile.getDetectedVersion()); } else { profileName.setValue(null); @@ -224,15 +254,14 @@ public class ProfileDetailModel extends ViewModel { showCommentsByDefault.setValue(true); showCommodityByDefault.setValue(false); setFutureDates(MobileLedgerProfile.FutureDates.None); - setApiVersion(SendTransactionTask.API.auto); + setApiVersion(API.auto); useAuthentication.setValue(false); authUserName.setValue(""); authPassword.setValue(""); preferredAccountsFilter.setValue(null); themeId.setValue(newProfileHue); + detectedVersion.setValue(null); } - - } void updateProfile(MobileLedgerProfile mProfile) { mProfile.setName(profileName.getValue()); @@ -249,5 +278,90 @@ public class ProfileDetailModel extends ViewModel { mProfile.setThemeHue(themeId.getValue()); mProfile.setFutureDates(futureDates.getValue()); mProfile.setApiVersion(apiVersion.getValue()); + mProfile.setDetectedVersion(detectedVersion.getValue()); + } + synchronized public void triggerVersionDetection() { + if (versionDetectionThread != null) + versionDetectionThread.interrupt(); + + versionDetectionThread = new VersionDetectionThread(this); + versionDetectionThread.start(); + } + 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 = null; + 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 boolean hasPatch = m.groupCount() >= 3; + int patch = hasPatch ? Integer.parseInt(Objects.requireNonNull(m.group(3))) : 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); + } + } } }