X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FApp.java;h=2fc6d65c276dafd0b41131791317f7f3238af843;hb=9ea5a330029c99e0eecf55aaa94d8689fa64fc92;hp=724627971769db475e7007c799ed1ae81987edf6;hpb=c27aa72c2c641bcd568692b4a20b125605cfb3b5;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/App.java b/app/src/main/java/net/ktnx/mobileledger/App.java index 72462797..2fc6d65c 100644 --- a/app/src/main/java/net/ktnx/mobileledger/App.java +++ b/app/src/main/java/net/ktnx/mobileledger/App.java @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -23,8 +23,9 @@ import android.content.res.Resources; import android.database.sqlite.SQLiteDatabase; import android.util.Log; +import net.ktnx.mobileledger.db.DB; 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 +40,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 +52,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 +91,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]", @@ -114,6 +144,10 @@ public class App extends Application { if (dbHelper != null) return; + // Let Room do any possible migrations + // this method may be removed when all DB access is made via Room + DB.get() + .compileStatement("select count(*) from profiles"); dbHelper = new MobileLedgerDatabase(this); } }