X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FApp.java;h=a492e8a418ea1cfeb0c20e0d3e514d032ae55fd9;hb=9662c07481ca878f4e4ff23c2923737241a3f3b4;hp=d6b1ffaf7afc784ebd41ccb5a1227bf2e1bb47ac;hpb=0fb03702b44e99bef2a5674d96f025595aed4274;p=mobile-ledger-staging.git diff --git a/app/src/main/java/net/ktnx/mobileledger/App.java b/app/src/main/java/net/ktnx/mobileledger/App.java index d6b1ffaf..a492e8a4 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 © 2019 Damyan Ivanov. + * Copyright © 2020 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 @@ -24,11 +24,13 @@ 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; +import org.jetbrains.annotations.NotNull; + import java.net.Authenticator; import java.net.MalformedURLException; import java.net.PasswordAuthentication; @@ -37,34 +39,69 @@ 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() { - if (instance == null) throw new RuntimeException("Application not created yet"); + if (instance == null) + throw new RuntimeException("Application not created yet"); return instance.getDB(); } + 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()"); instance = this; super.onCreate(); - updateMonthNames(); Data.refreshCurrencyData(Locale.getDefault()); Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { - MobileLedgerProfile p = Data.profile.getValue(); - if ((p != null) && 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()); - else Log.w("http-auth", - String.format("Requesting host [%s] differs from expected [%s]", - requestingHost, expectedHost)); + return new PasswordAuthentication(getAuthUserName(), + getAuthPassword().toCharArray()); + else + Log.w("http-auth", + String.format("Requesting host [%s] differs from expected [%s]", + requestingHost, expectedHost)); } catch (MalformedURLException e) { e.printStackTrace(); @@ -75,33 +112,36 @@ public class App extends Application { } }); } - private void updateMonthNames() { + private void prepareMonthNames(boolean force) { + if (force || monthNamesPrepared) + return; Resources rm = getResources(); Globals.monthNames = rm.getStringArray(R.array.month_names); + monthNamesPrepared = true; } @Override public void onTerminate() { Logger.debug("flow", "App onTerminate()"); - if (dbHelper != null) dbHelper.close(); + if (dbHelper != null) + dbHelper.close(); super.onTerminate(); } @Override - public void onConfigurationChanged(Configuration newConfig) { + public void onConfigurationChanged(@NotNull Configuration newConfig) { super.onConfigurationChanged(newConfig); - updateMonthNames(); + prepareMonthNames(true); Data.refreshCurrencyData(Locale.getDefault()); Data.locale.setValue(Locale.getDefault()); } public SQLiteDatabase getDB() { - if (dbHelper == null) initDb(); - - final SQLiteDatabase db = dbHelper.getWritableDatabase(); - db.execSQL("pragma case_sensitive_like=ON;"); + if (dbHelper == null) + initDb(); - return db; + return dbHelper.getWritableDatabase(); } private synchronized void initDb() { - if (dbHelper != null) return; + if (dbHelper != null) + return; dbHelper = new MobileLedgerDatabase(this); }