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;
public class App extends Application {
public static App instance;
+ private static ProfileDetailModel profileModel;
private MobileLedgerDatabase dbHelper;
private boolean monthNamesPrepared = false;
public static SQLiteDatabase getDatabase() {
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()");
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]",
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;
void observeShowCommodityByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
showCommodityByDefault.observe(lfo, o);
}
- Boolean getUseAuthentication() {
+ public Boolean getUseAuthentication() {
return useAuthentication.getValue();
}
void setUseAuthentication(boolean newValue) {
void observeDetectedVersion(LifecycleOwner lfo, Observer<HledgerVersion> o) {
detectedVersion.observe(lfo, o);
}
- String getUrl() {
+ public String getUrl() {
return url.getValue();
}
void setUrl(String newValue) {
void observeUrl(LifecycleOwner lfo, Observer<String> o) {
url.observe(lfo, o);
}
- String getAuthUserName() {
+ public String getAuthUserName() {
return authUserName.getValue();
}
void setAuthUserName(String newValue) {
void observeUserName(LifecycleOwner lfo, Observer<String> o) {
authUserName.observe(lfo, o);
}
- String getAuthPassword() {
+ public String getAuthPassword() {
return authPassword.getValue();
}
void setAuthPassword(String newValue) {
this.model = model;
}
private HledgerVersion detectVersion() {
+ App.setAuthenticationDataFromProfileModel(model);
HttpURLConnection http = null;
try {
http = NetworkUtil.prepareConnection(model.getUrl(), "version",
e.printStackTrace();
return null;
}
+ finally {
+ App.resetAuthenticationData();
+ }
}
@Override
public void run() {