2 * Copyright © 2021 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.db.DB;
27 import net.ktnx.mobileledger.model.Data;
28 import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
29 import net.ktnx.mobileledger.utils.Globals;
30 import net.ktnx.mobileledger.utils.Logger;
31 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
33 import org.jetbrains.annotations.NotNull;
35 import java.net.Authenticator;
36 import java.net.MalformedURLException;
37 import java.net.PasswordAuthentication;
39 import java.util.Locale;
41 public class App extends Application {
42 public static App instance;
43 private static ProfileDetailModel profileModel;
44 private MobileLedgerDatabase dbHelper;
45 private boolean monthNamesPrepared = false;
46 public static SQLiteDatabase getDatabase() {
48 throw new RuntimeException("Application not created yet");
50 return instance.getDB();
52 public static void prepareMonthNames() {
53 instance.prepareMonthNames(false);
55 public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
58 public static void resetAuthenticationData() {
61 private String getAuthURL() {
62 if (profileModel != null)
63 return profileModel.getUrl();
64 return Data.getProfile()
67 private String getAuthUserName() {
68 if (profileModel != null)
69 return profileModel.getAuthUserName();
70 return Data.getProfile()
73 private String getAuthPassword() {
74 if (profileModel != null)
75 return profileModel.getAuthPassword();
76 return Data.getProfile()
79 private boolean getAuthEnabled() {
80 if (profileModel != null)
81 return profileModel.getUseAuthentication();
82 return Data.getProfile()
86 public void onCreate() {
87 Logger.debug("flow", "App onCreate()");
90 Data.refreshCurrencyData(Locale.getDefault());
91 Authenticator.setDefault(new Authenticator() {
93 protected PasswordAuthentication getPasswordAuthentication() {
94 if (getAuthEnabled()) {
96 final URL url = new URL(getAuthURL());
97 final String requestingHost = getRequestingHost();
98 final String expectedHost = url.getHost();
99 if (requestingHost.equalsIgnoreCase(expectedHost))
100 return new PasswordAuthentication(getAuthUserName(),
101 getAuthPassword().toCharArray());
104 String.format("Requesting host [%s] differs from expected [%s]",
105 requestingHost, expectedHost));
107 catch (MalformedURLException e) {
112 return super.getPasswordAuthentication();
116 private void prepareMonthNames(boolean force) {
117 if (force || monthNamesPrepared)
119 Resources rm = getResources();
120 Globals.monthNames = rm.getStringArray(R.array.month_names);
121 monthNamesPrepared = true;
124 public void onTerminate() {
125 Logger.debug("flow", "App onTerminate()");
126 if (dbHelper != null)
131 public void onConfigurationChanged(@NotNull Configuration newConfig) {
132 super.onConfigurationChanged(newConfig);
133 prepareMonthNames(true);
134 Data.refreshCurrencyData(Locale.getDefault());
135 Data.locale.setValue(Locale.getDefault());
137 public SQLiteDatabase getDB() {
138 if (dbHelper == null)
141 return dbHelper.getWritableDatabase();
143 private synchronized void initDb() {
144 if (dbHelper != null)
147 // Let Room do any possible migrations
148 // this method may be removed when all DB access is made via Room
150 .compileStatement("select count(*) from profiles");
151 dbHelper = new MobileLedgerDatabase(this);