X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FApp.java;h=724627971769db475e7007c799ed1ae81987edf6;hp=051a17c308540ac2b15dc2cd15e997207361b746;hb=HEAD;hpb=90383a155ec16a9f13b1e6ac94a118033e09b3aa diff --git a/app/src/main/java/net/ktnx/mobileledger/App.java b/app/src/main/java/net/ktnx/mobileledger/App.java index 051a17c3..a9a416e7 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 © 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 @@ -21,60 +21,120 @@ import android.app.Application; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; -import android.database.sqlite.SQLiteDatabase; -import android.preference.PreferenceManager; +import android.util.Log; import net.ktnx.mobileledger.model.Data; +import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel; +import net.ktnx.mobileledger.utils.Colors; import net.ktnx.mobileledger.utils.Globals; -import net.ktnx.mobileledger.utils.MobileLedgerDatabase; +import net.ktnx.mobileledger.utils.Logger; -import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS; +import org.jetbrains.annotations.NotNull; + +import java.net.Authenticator; +import java.net.MalformedURLException; +import java.net.PasswordAuthentication; +import java.net.URL; +import java.util.Locale; public class App extends Application { + public static final String PREF_NAME = "MoLe"; + public static final String PREF_THEME_HUE = "theme-hue"; + public static final String PREF_PROFILE_ID = "profile-id"; public static App instance; - private MobileLedgerDatabase dbHelper; + private static ProfileDetailModel profileModel; + private boolean monthNamesPrepared = false; + public static void prepareMonthNames() { + instance.prepareMonthNames(false); + } + public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) { + profileModel = model; + } + public static void resetAuthenticationData() { + profileModel = null; + } + public static void storeStartupProfileAndTheme(long currentProfileId, int currentTheme) { + SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE); + SharedPreferences.Editor editor = prefs.edit(); + editor.putLong(PREF_PROFILE_ID, currentProfileId); + editor.putInt(PREF_THEME_HUE, currentTheme); + editor.apply(); + } + public static long getStartupProfile() { + SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE); + return prefs.getLong(PREF_PROFILE_ID, -1); + } + public static int getStartupTheme() { + SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE); + return prefs.getInt(PREF_THEME_HUE, Colors.DEFAULT_HUE_DEG); + } + 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() + .getAuthUser(); + } + 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() + .useAuthentication(); + } @Override public void onCreate() { + Logger.debug("flow", "App onCreate()"); instance = this; super.onCreate(); - updateMonthNames(); - SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this); - Data.optShowOnlyStarred.set(p.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false)); - SharedPreferences.OnSharedPreferenceChangeListener handler = - (preference, value) -> Data.optShowOnlyStarred - .set(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false)); - p.registerOnSharedPreferenceChangeListener(handler); + Data.refreshCurrencyData(Locale.getDefault()); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + if (getAuthEnabled()) { + try { + final URL url = new URL(getAuthURL()); + final String requestingHost = getRequestingHost(); + final String expectedHost = url.getHost(); + if (requestingHost.equalsIgnoreCase(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(); + } + } + + return super.getPasswordAuthentication(); + } + }); } - 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() { - if (dbHelper != null) dbHelper.close(); - super.onTerminate(); - } - @Override - public void onConfigurationChanged(Configuration newConfig) { + public void onConfigurationChanged(@NotNull Configuration newConfig) { super.onConfigurationChanged(newConfig); - updateMonthNames(); - } - public static SQLiteDatabase getDatabase() { - if (instance == null) throw new RuntimeException("Application not created yet"); - - return instance.getDB(); - } - public SQLiteDatabase getDB() { - if (dbHelper == null) initDb(); - - final SQLiteDatabase db = dbHelper.getWritableDatabase(); - db.execSQL("pragma case_sensitive_like=ON;"); - - return db; - } - private synchronized void initDb() { - if (dbHelper != null) return; - - dbHelper = new MobileLedgerDatabase(this); + prepareMonthNames(true); + Data.refreshCurrencyData(Locale.getDefault()); + Data.locale.setValue(Locale.getDefault()); } }