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