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() {
46 throw new RuntimeException("Application not created yet");
48 return instance.getDB();
50 public static void prepareMonthNames() {
51 instance.prepareMonthNames(false);
54 public void onCreate() {
55 Logger.debug("flow", "App onCreate()");
58 Data.refreshCurrencyData(Locale.getDefault());
59 Authenticator.setDefault(new Authenticator() {
61 protected PasswordAuthentication getPasswordAuthentication() {
62 MobileLedgerProfile p = Data.getProfile();
63 if (p.isAuthEnabled()) {
65 final URL url = new URL(p.getUrl());
66 final String requestingHost = getRequestingHost();
67 final String expectedHost = url.getHost();
68 if (requestingHost.equalsIgnoreCase(expectedHost))
69 return new PasswordAuthentication(p.getAuthUserName(),
74 String.format("Requesting host [%s] differs from expected [%s]",
75 requestingHost, expectedHost));
77 catch (MalformedURLException e) {
82 return super.getPasswordAuthentication();
86 private void prepareMonthNames(boolean force) {
87 if (force || monthNamesPrepared)
89 Resources rm = getResources();
90 Globals.monthNames = rm.getStringArray(R.array.month_names);
91 monthNamesPrepared = true;
94 public void onTerminate() {
95 Logger.debug("flow", "App onTerminate()");
101 public void onConfigurationChanged(@NotNull Configuration newConfig) {
102 super.onConfigurationChanged(newConfig);
103 prepareMonthNames(true);
104 Data.refreshCurrencyData(Locale.getDefault());
105 Data.locale.setValue(Locale.getDefault());
107 public SQLiteDatabase getDB() {
108 if (dbHelper == null)
111 return dbHelper.getWritableDatabase();
113 private synchronized void initDb() {
114 if (dbHelper != null)
117 dbHelper = new MobileLedgerDatabase(this);