]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/App.java
drop unused preference activity
[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 import java.util.Locale;
37
38 public class App extends Application {
39     public static App instance;
40     private MobileLedgerDatabase dbHelper;
41     public static SQLiteDatabase getDatabase() {
42         if (instance == null) throw new RuntimeException("Application not created yet");
43
44         return instance.getDB();
45     }
46     @Override
47     public void onCreate() {
48         Logger.debug("flow", "App onCreate()");
49         instance = this;
50         super.onCreate();
51         updateMonthNames();
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     private void updateMonthNames() {
79         Resources rm = getResources();
80         Globals.monthNames = rm.getStringArray(R.array.month_names);
81     }
82     @Override
83     public void onTerminate() {
84         Logger.debug("flow", "App onTerminate()");
85         if (dbHelper != null) dbHelper.close();
86         super.onTerminate();
87     }
88     @Override
89     public void onConfigurationChanged(Configuration newConfig) {
90         super.onConfigurationChanged(newConfig);
91         updateMonthNames();
92         Data.refreshCurrencyData(Locale.getDefault());
93         Data.locale.setValue(Locale.getDefault());
94     }
95     public SQLiteDatabase getDB() {
96         if (dbHelper == null) initDb();
97
98         final SQLiteDatabase db = dbHelper.getWritableDatabase();
99         db.execSQL("pragma case_sensitive_like=ON;");
100
101         return db;
102     }
103     private synchronized void initDb() {
104         if (dbHelper != null) return;
105
106         dbHelper = new MobileLedgerDatabase(this);
107     }
108 }