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