2 * Copyright © 2020 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger;
20 import android.app.Application;
21 import android.content.res.Configuration;
22 import android.content.res.Resources;
23 import android.database.sqlite.SQLiteDatabase;
24 import android.util.Log;
26 import net.ktnx.mobileledger.model.Data;
27 import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
28 import net.ktnx.mobileledger.utils.Globals;
29 import net.ktnx.mobileledger.utils.Logger;
30 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
32 import org.jetbrains.annotations.NotNull;
34 import java.net.Authenticator;
35 import java.net.MalformedURLException;
36 import java.net.PasswordAuthentication;
38 import java.util.Locale;
40 public class App extends Application {
41 public static App instance;
42 private static ProfileDetailModel profileModel;
43 private MobileLedgerDatabase dbHelper;
44 private boolean monthNamesPrepared = false;
45 public static SQLiteDatabase getDatabase() {
47 throw new RuntimeException("Application not created yet");
49 return instance.getDB();
51 public static void prepareMonthNames() {
52 instance.prepareMonthNames(false);
54 public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
57 public static void resetAuthenticationData() {
60 private String getAuthURL() {
61 if (profileModel != null)
62 return profileModel.getUrl();
63 return Data.getProfile()
66 private String getAuthUserName() {
67 if (profileModel != null)
68 return profileModel.getAuthUserName();
69 return Data.getProfile()
72 private String getAuthPassword() {
73 if (profileModel != null)
74 return profileModel.getAuthPassword();
75 return Data.getProfile()
78 private boolean getAuthEnabled() {
79 if (profileModel != null)
80 return profileModel.getUseAuthentication();
81 return Data.getProfile()
85 public void onCreate() {
86 Logger.debug("flow", "App onCreate()");
89 Data.refreshCurrencyData(Locale.getDefault());
90 Authenticator.setDefault(new Authenticator() {
92 protected PasswordAuthentication getPasswordAuthentication() {
93 if (getAuthEnabled()) {
95 final URL url = new URL(getAuthURL());
96 final String requestingHost = getRequestingHost();
97 final String expectedHost = url.getHost();
98 if (requestingHost.equalsIgnoreCase(expectedHost))
99 return new PasswordAuthentication(getAuthUserName(),
100 getAuthPassword().toCharArray());
103 String.format("Requesting host [%s] differs from expected [%s]",
104 requestingHost, expectedHost));
106 catch (MalformedURLException e) {
111 return super.getPasswordAuthentication();
115 private void prepareMonthNames(boolean force) {
116 if (force || monthNamesPrepared)
118 Resources rm = getResources();
119 Globals.monthNames = rm.getStringArray(R.array.month_names);
120 monthNamesPrepared = true;
123 public void onTerminate() {
124 Logger.debug("flow", "App onTerminate()");
125 if (dbHelper != null)
130 public void onConfigurationChanged(@NotNull Configuration newConfig) {
131 super.onConfigurationChanged(newConfig);
132 prepareMonthNames(true);
133 Data.refreshCurrencyData(Locale.getDefault());
134 Data.locale.setValue(Locale.getDefault());
136 public SQLiteDatabase getDB() {
137 if (dbHelper == null)
140 return dbHelper.getWritableDatabase();
142 private synchronized void initDb() {
143 if (dbHelper != null)
146 dbHelper = new MobileLedgerDatabase(this);