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.
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.
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/>.
18 package net.ktnx.mobileledger;
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;
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;
32 import java.net.Authenticator;
33 import java.net.MalformedURLException;
34 import java.net.PasswordAuthentication;
36 import java.util.Locale;
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");
44 return instance.getDB();
47 public void onCreate() {
48 Logger.debug("flow", "App onCreate()");
52 Data.refreshCurrencyData(Locale.getDefault());
53 Authenticator.setDefault(new Authenticator() {
55 protected PasswordAuthentication getPasswordAuthentication() {
56 MobileLedgerProfile p = Data.profile.getValue();
57 if ((p != null) && p.isAuthEnabled()) {
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));
69 catch (MalformedURLException e) {
74 return super.getPasswordAuthentication();
78 private void updateMonthNames() {
79 Resources rm = getResources();
80 Globals.monthNames = rm.getStringArray(R.array.month_names);
83 public void onTerminate() {
84 Logger.debug("flow", "App onTerminate()");
85 if (dbHelper != null) dbHelper.close();
89 public void onConfigurationChanged(Configuration newConfig) {
90 super.onConfigurationChanged(newConfig);
92 Data.refreshCurrencyData(Locale.getDefault());
93 Data.locale.setValue(Locale.getDefault());
95 public SQLiteDatabase getDB() {
96 if (dbHelper == null) initDb();
98 final SQLiteDatabase db = dbHelper.getWritableDatabase();
99 db.execSQL("pragma case_sensitive_like=ON;");
103 private synchronized void initDb() {
104 if (dbHelper != null) return;
106 dbHelper = new MobileLedgerDatabase(this);