]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/App.java
App: make the Room database handle available
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / App.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger;
19
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;
25
26 import androidx.room.Room;
27
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;
34
35 import org.jetbrains.annotations.NotNull;
36
37 import java.net.Authenticator;
38 import java.net.MalformedURLException;
39 import java.net.PasswordAuthentication;
40 import java.net.URL;
41 import java.util.Locale;
42
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() {
50         if (instance == null)
51             throw new RuntimeException("Application not created yet");
52
53         return instance.getDB();
54     }
55     public static DB getRoomDB() {
56         if (instance == null)
57             throw new RuntimeException("Application not created yet");
58
59         return instance.getRoomDatabase();
60     }
61     public DB getRoomDatabase(){
62         return roomDatabase;
63     }
64     public static void prepareMonthNames() {
65         instance.prepareMonthNames(false);
66     }
67     public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
68         profileModel = model;
69     }
70     public static void resetAuthenticationData() {
71         profileModel = null;
72     }
73     private String getAuthURL() {
74         if (profileModel != null)
75             return profileModel.getUrl();
76         return Data.getProfile()
77                    .getUrl();
78     }
79     private String getAuthUserName() {
80         if (profileModel != null)
81             return profileModel.getAuthUserName();
82         return Data.getProfile()
83                    .getAuthUserName();
84     }
85     private String getAuthPassword() {
86         if (profileModel != null)
87             return profileModel.getAuthPassword();
88         return Data.getProfile()
89                    .getAuthPassword();
90     }
91     private boolean getAuthEnabled() {
92         if (profileModel != null)
93             return profileModel.getUseAuthentication();
94         return Data.getProfile()
95                    .isAuthEnabled();
96     }
97     @Override
98     public void onCreate() {
99         Logger.debug("flow", "App onCreate()");
100         instance = this;
101         super.onCreate();
102         roomDatabase = Room.databaseBuilder(this, DB.class, MobileLedgerDatabase.DB_NAME)
103                               .build();
104         Data.refreshCurrencyData(Locale.getDefault());
105         Authenticator.setDefault(new Authenticator() {
106             @Override
107             protected PasswordAuthentication getPasswordAuthentication() {
108                 if (getAuthEnabled()) {
109                     try {
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());
116                         else
117                             Log.w("http-auth",
118                                     String.format("Requesting host [%s] differs from expected [%s]",
119                                             requestingHost, expectedHost));
120                     }
121                     catch (MalformedURLException e) {
122                         e.printStackTrace();
123                     }
124                 }
125
126                 return super.getPasswordAuthentication();
127             }
128         });
129     }
130     private void prepareMonthNames(boolean force) {
131         if (force || monthNamesPrepared)
132             return;
133         Resources rm = getResources();
134         Globals.monthNames = rm.getStringArray(R.array.month_names);
135         monthNamesPrepared = true;
136     }
137     @Override
138     public void onTerminate() {
139         Logger.debug("flow", "App onTerminate()");
140         if (dbHelper != null)
141             dbHelper.close();
142         super.onTerminate();
143     }
144     @Override
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());
150     }
151     public SQLiteDatabase getDB() {
152         if (dbHelper == null)
153             initDb();
154
155         return dbHelper.getWritableDatabase();
156     }
157     private synchronized void initDb() {
158         if (dbHelper != null)
159             return;
160
161         dbHelper = new MobileLedgerDatabase(this);
162     }
163 }