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 androidx.room.Room;
28 import net.ktnx.mobileledger.db.DB;
29 import net.ktnx.mobileledger.model.Data;
30 import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
31 import net.ktnx.mobileledger.utils.Globals;
32 import net.ktnx.mobileledger.utils.Logger;
33 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
35 import org.jetbrains.annotations.NotNull;
37 import java.net.Authenticator;
38 import java.net.MalformedURLException;
39 import java.net.PasswordAuthentication;
41 import java.util.Locale;
43 public class App extends Application {
44 public static App instance;
45 private static ProfileDetailModel profileModel;
46 private MobileLedgerDatabase dbHelper;
47 private boolean monthNamesPrepared = false;
48 private DB roomDatabase;
49 public static SQLiteDatabase getDatabase() {
51 throw new RuntimeException("Application not created yet");
53 return instance.getDB();
55 public static DB getRoomDB() {
57 throw new RuntimeException("Application not created yet");
59 return instance.getRoomDatabase();
61 public DB getRoomDatabase(){
64 public static void prepareMonthNames() {
65 instance.prepareMonthNames(false);
67 public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
70 public static void resetAuthenticationData() {
73 private String getAuthURL() {
74 if (profileModel != null)
75 return profileModel.getUrl();
76 return Data.getProfile()
79 private String getAuthUserName() {
80 if (profileModel != null)
81 return profileModel.getAuthUserName();
82 return Data.getProfile()
85 private String getAuthPassword() {
86 if (profileModel != null)
87 return profileModel.getAuthPassword();
88 return Data.getProfile()
91 private boolean getAuthEnabled() {
92 if (profileModel != null)
93 return profileModel.getUseAuthentication();
94 return Data.getProfile()
98 public void onCreate() {
99 Logger.debug("flow", "App onCreate()");
102 roomDatabase = Room.databaseBuilder(this, DB.class, MobileLedgerDatabase.DB_NAME)
104 Data.refreshCurrencyData(Locale.getDefault());
105 Authenticator.setDefault(new Authenticator() {
107 protected PasswordAuthentication getPasswordAuthentication() {
108 if (getAuthEnabled()) {
110 final URL url = new URL(getAuthURL());
111 final String requestingHost = getRequestingHost();
112 final String expectedHost = url.getHost();
113 if (requestingHost.equalsIgnoreCase(expectedHost))
114 return new PasswordAuthentication(getAuthUserName(),
115 getAuthPassword().toCharArray());
118 String.format("Requesting host [%s] differs from expected [%s]",
119 requestingHost, expectedHost));
121 catch (MalformedURLException e) {
126 return super.getPasswordAuthentication();
130 private void prepareMonthNames(boolean force) {
131 if (force || monthNamesPrepared)
133 Resources rm = getResources();
134 Globals.monthNames = rm.getStringArray(R.array.month_names);
135 monthNamesPrepared = true;
138 public void onTerminate() {
139 Logger.debug("flow", "App onTerminate()");
140 if (dbHelper != null)
145 public void onConfigurationChanged(@NotNull Configuration newConfig) {
146 super.onConfigurationChanged(newConfig);
147 prepareMonthNames(true);
148 Data.refreshCurrencyData(Locale.getDefault());
149 Data.locale.setValue(Locale.getDefault());
151 public SQLiteDatabase getDB() {
152 if (dbHelper == null)
155 return dbHelper.getWritableDatabase();
157 private synchronized void initDb() {
158 if (dbHelper != null)
161 dbHelper = new MobileLedgerDatabase(this);