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.
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 org.jetbrains.annotations.NotNull;
34 import java.net.Authenticator;
35 import java.net.MalformedURLException;
36 import java.net.PasswordAuthentication;
38 import java.util.Locale;
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");
47 return instance.getDB();
50 public void onCreate() {
51 Logger.debug("flow", "App onCreate()");
54 Data.refreshCurrencyData(Locale.getDefault());
55 Authenticator.setDefault(new Authenticator() {
57 protected PasswordAuthentication getPasswordAuthentication() {
58 MobileLedgerProfile p = Data.getProfile();
59 if (p.isAuthEnabled()) {
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));
71 catch (MalformedURLException e) {
76 return super.getPasswordAuthentication();
80 public static void prepareMonthNames() {
81 instance.prepareMonthNames(false);
83 private void prepareMonthNames(boolean force) {
84 if (force || monthNamesPrepared)
86 Resources rm = getResources();
87 Globals.monthNames = rm.getStringArray(R.array.month_names);
88 monthNamesPrepared = true;
91 public void onTerminate() {
92 Logger.debug("flow", "App onTerminate()");
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());
104 public SQLiteDatabase getDB() {
105 if (dbHelper == null)
108 final SQLiteDatabase db = dbHelper.getWritableDatabase();
112 private synchronized void initDb() {
113 if (dbHelper != null) return;
115 dbHelper = new MobileLedgerDatabase(this);