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