X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfileDetailModel.java;h=5de553481684c8a78c12a3edd2a8e182b4e0acad;hb=bb789332571609eeb1bef6e39b7ad359227d1045;hp=92d1505fb0baa159bd38cf2e24b9f003d2ddfa4a;hpb=667ce42731c95a98926657fea359b56209f9348e;p=mobile-ledger.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 92d1505f..5de55348 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 @@ -24,7 +24,7 @@ import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModel; -import net.ktnx.mobileledger.async.SendTransactionTask; +import net.ktnx.mobileledger.json.API; import net.ktnx.mobileledger.model.Currency; import net.ktnx.mobileledger.model.HledgerVersion; import net.ktnx.mobileledger.model.MobileLedgerProfile; @@ -53,14 +53,14 @@ 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() { @@ -136,14 +136,14 @@ 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); } HledgerVersion getDetectedVersion() { return detectedVersion.getValue(); } @@ -219,6 +219,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) { @@ -250,7 +253,7 @@ 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(""); @@ -284,29 +287,28 @@ public class ProfileDetailModel extends ViewModel { 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; } - @Override - public void run() { + private HledgerVersion detectVersion() { + HttpURLConnection http = null; try { - HttpURLConnection http = NetworkUtil.prepareConnection(model.getUrl(), "version", + http = NetworkUtil.prepareConnection(model.getUrl(), "version", model.getUseAuthentication()); switch (http.getResponseCode()) { case 200: break; case 404: - model.detectedVersion.postValue(new HledgerVersion(true)); - return; + return new HledgerVersion(true); 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; + return null; } InputStream stream = http.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); @@ -318,17 +320,42 @@ public class ProfileDetailModel extends ViewModel { 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)); + 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; + } + } + @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); } } }