]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/App.java
move setting case_sensitive_like=ON to helper.onOpen
[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.model.MobileLedgerProfile;
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 MobileLedgerDatabase dbHelper;
43     private boolean monthNamesPrepared = false;
44     public static SQLiteDatabase getDatabase() {
45         if (instance == null) throw new RuntimeException("Application not created yet");
46
47         return instance.getDB();
48     }
49     @Override
50     public void onCreate() {
51         Logger.debug("flow", "App onCreate()");
52         instance = this;
53         super.onCreate();
54         Data.refreshCurrencyData(Locale.getDefault());
55         Authenticator.setDefault(new Authenticator() {
56             @Override
57             protected PasswordAuthentication getPasswordAuthentication() {
58                 MobileLedgerProfile p = Data.profile.getValue();
59                 if ((p != null) && p.isAuthEnabled()) {
60                     try {
61                         final URL url = new URL(p.getUrl());
62                         final String requestingHost = getRequestingHost();
63                         final String expectedHost = url.getHost();
64                         if (requestingHost.equalsIgnoreCase(expectedHost))
65                             return new PasswordAuthentication(p.getAuthUserName(),
66                                     p.getAuthPassword().toCharArray());
67                         else Log.w("http-auth",
68                                 String.format("Requesting host [%s] differs from expected [%s]",
69                                         requestingHost, expectedHost));
70                     }
71                     catch (MalformedURLException e) {
72                         e.printStackTrace();
73                     }
74                 }
75
76                 return super.getPasswordAuthentication();
77             }
78         });
79     }
80     public static void prepareMonthNames() {
81         instance.prepareMonthNames(false);
82     }
83     private void prepareMonthNames(boolean force) {
84         if (force || monthNamesPrepared)
85             return;
86         Resources rm = getResources();
87         Globals.monthNames = rm.getStringArray(R.array.month_names);
88         monthNamesPrepared = true;
89     }
90     @Override
91     public void onTerminate() {
92         Logger.debug("flow", "App onTerminate()");
93         if (dbHelper != null)
94             dbHelper.close();
95         super.onTerminate();
96     }
97     @Override
98     public void onConfigurationChanged(@NotNull Configuration newConfig) {
99         super.onConfigurationChanged(newConfig);
100         prepareMonthNames(true);
101         Data.refreshCurrencyData(Locale.getDefault());
102         Data.locale.setValue(Locale.getDefault());
103     }
104     public SQLiteDatabase getDB() {
105         if (dbHelper == null)
106             initDb();
107
108         final SQLiteDatabase db = dbHelper.getWritableDatabase();
109
110         return db;
111     }
112     private synchronized void initDb() {
113         if (dbHelper != null) return;
114
115         dbHelper = new MobileLedgerDatabase(this);
116     }
117 }