From 9662c07481ca878f4e4ff23c2923737241a3f3b4 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Mon, 28 Dec 2020 17:22:02 +0200 Subject: [PATCH] fix server version detection when the first profile is being created the Authenticator always used the current profile, which is null when there are no profiles yet --- .../main/java/net/ktnx/mobileledger/App.java | 43 ++++++++++++++++--- .../ui/profiles/ProfileDetailModel.java | 13 ++++-- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/App.java b/app/src/main/java/net/ktnx/mobileledger/App.java index 72462797..a492e8a4 100644 --- a/app/src/main/java/net/ktnx/mobileledger/App.java +++ b/app/src/main/java/net/ktnx/mobileledger/App.java @@ -24,7 +24,7 @@ import android.database.sqlite.SQLiteDatabase; import android.util.Log; import net.ktnx.mobileledger.model.Data; -import net.ktnx.mobileledger.model.MobileLedgerProfile; +import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel; import net.ktnx.mobileledger.utils.Globals; import net.ktnx.mobileledger.utils.Logger; import net.ktnx.mobileledger.utils.MobileLedgerDatabase; @@ -39,6 +39,7 @@ import java.util.Locale; public class App extends Application { public static App instance; + private static ProfileDetailModel profileModel; private MobileLedgerDatabase dbHelper; private boolean monthNamesPrepared = false; public static SQLiteDatabase getDatabase() { @@ -50,6 +51,36 @@ public class App extends Application { public static void prepareMonthNames() { instance.prepareMonthNames(false); } + public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) { + profileModel = model; + } + public static void resetAuthenticationData() { + profileModel = null; + } + private String getAuthURL() { + if (profileModel != null) + return profileModel.getUrl(); + return Data.getProfile() + .getUrl(); + } + private String getAuthUserName() { + if (profileModel != null) + return profileModel.getAuthUserName(); + return Data.getProfile() + .getAuthUserName(); + } + private String getAuthPassword() { + if (profileModel != null) + return profileModel.getAuthPassword(); + return Data.getProfile() + .getAuthPassword(); + } + private boolean getAuthEnabled() { + if (profileModel != null) + return profileModel.getUseAuthentication(); + return Data.getProfile() + .isAuthEnabled(); + } @Override public void onCreate() { Logger.debug("flow", "App onCreate()"); @@ -59,16 +90,14 @@ public class App extends Application { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { - MobileLedgerProfile p = Data.getProfile(); - if (p.isAuthEnabled()) { + if (getAuthEnabled()) { try { - final URL url = new URL(p.getUrl()); + final URL url = new URL(getAuthURL()); final String requestingHost = getRequestingHost(); final String expectedHost = url.getHost(); if (requestingHost.equalsIgnoreCase(expectedHost)) - return new PasswordAuthentication(p.getAuthUserName(), - p.getAuthPassword() - .toCharArray()); + return new PasswordAuthentication(getAuthUserName(), + getAuthPassword().toCharArray()); else Log.w("http-auth", String.format("Requesting host [%s] differs from expected [%s]", 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 5de55348..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 @@ -24,6 +24,7 @@ import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModel; +import net.ktnx.mobileledger.App; import net.ktnx.mobileledger.json.API; import net.ktnx.mobileledger.model.Currency; import net.ktnx.mobileledger.model.HledgerVersion; @@ -126,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) { @@ -154,7 +155,7 @@ public class ProfileDetailModel extends ViewModel { void observeDetectedVersion(LifecycleOwner lfo, Observer o) { detectedVersion.observe(lfo, o); } - String getUrl() { + public String getUrl() { return url.getValue(); } void setUrl(String newValue) { @@ -168,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) { @@ -182,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) { @@ -295,6 +296,7 @@ public class ProfileDetailModel extends ViewModel { this.model = model; } private HledgerVersion detectVersion() { + App.setAuthenticationDataFromProfileModel(model); HttpURLConnection http = null; try { http = NetworkUtil.prepareConnection(model.getUrl(), "version", @@ -333,6 +335,9 @@ public class ProfileDetailModel extends ViewModel { e.printStackTrace(); return null; } + finally { + App.resetAuthenticationData(); + } } @Override public void run() { -- 2.39.5