]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/App.java
fix detection of continuations in SQL revision scripts
[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 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;
31
32 import org.jetbrains.annotations.NotNull;
33
34 import java.net.Authenticator;
35 import java.net.MalformedURLException;
36 import java.net.PasswordAuthentication;
37 import java.net.URL;
38 import java.util.Locale;
39
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() {
46         if (instance == null)
47             throw new RuntimeException("Application not created yet");
48
49         return instance.getDB();
50     }
51     public static void prepareMonthNames() {
52         instance.prepareMonthNames(false);
53     }
54     public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
55         profileModel = model;
56     }
57     public static void resetAuthenticationData() {
58         profileModel = null;
59     }
60     private String getAuthURL() {
61         if (profileModel != null)
62             return profileModel.getUrl();
63         return Data.getProfile()
64                    .getUrl();
65     }
66     private String getAuthUserName() {
67         if (profileModel != null)
68             return profileModel.getAuthUserName();
69         return Data.getProfile()
70                    .getAuthUserName();
71     }
72     private String getAuthPassword() {
73         if (profileModel != null)
74             return profileModel.getAuthPassword();
75         return Data.getProfile()
76                    .getAuthPassword();
77     }
78     private boolean getAuthEnabled() {
79         if (profileModel != null)
80             return profileModel.getUseAuthentication();
81         return Data.getProfile()
82                    .isAuthEnabled();
83     }
84     @Override
85     public void onCreate() {
86         Logger.debug("flow", "App onCreate()");
87         instance = this;
88         super.onCreate();
89         Data.refreshCurrencyData(Locale.getDefault());
90         Authenticator.setDefault(new Authenticator() {
91             @Override
92             protected PasswordAuthentication getPasswordAuthentication() {
93                 if (getAuthEnabled()) {
94                     try {
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());
101                         else
102                             Log.w("http-auth",
103                                     String.format("Requesting host [%s] differs from expected [%s]",
104                                             requestingHost, expectedHost));
105                     }
106                     catch (MalformedURLException e) {
107                         e.printStackTrace();
108                     }
109                 }
110
111                 return super.getPasswordAuthentication();
112             }
113         });
114     }
115     private void prepareMonthNames(boolean force) {
116         if (force || monthNamesPrepared)
117             return;
118         Resources rm = getResources();
119         Globals.monthNames = rm.getStringArray(R.array.month_names);
120         monthNamesPrepared = true;
121     }
122     @Override
123     public void onTerminate() {
124         Logger.debug("flow", "App onTerminate()");
125         if (dbHelper != null)
126             dbHelper.close();
127         super.onTerminate();
128     }
129     @Override
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());
135     }
136     public SQLiteDatabase getDB() {
137         if (dbHelper == null)
138             initDb();
139
140         return dbHelper.getWritableDatabase();
141     }
142     private synchronized void initDb() {
143         if (dbHelper != null)
144             return;
145
146         dbHelper = new MobileLedgerDatabase(this);
147     }
148 }