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;
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");
43 return instance.getDB();
46 public void onCreate() {
47 Logger.debug("flow", "App onCreate()");
51 Authenticator.setDefault(new Authenticator() {
53 protected PasswordAuthentication getPasswordAuthentication() {
54 MobileLedgerProfile p = Data.profile.getValue();
55 if ((p != null) && p.isAuthEnabled()) {
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));
67 catch (MalformedURLException e) {
72 return super.getPasswordAuthentication();
76 private void updateMonthNames() {
77 Resources rm = getResources();
78 Globals.monthNames = rm.getStringArray(R.array.month_names);
81 public void onTerminate() {
82 Logger.debug("flow", "App onTerminate()");
83 if (dbHelper != null) dbHelper.close();
87 public void onConfigurationChanged(Configuration newConfig) {
88 super.onConfigurationChanged(newConfig);
91 public SQLiteDatabase getDB() {
92 if (dbHelper == null) initDb();
94 final SQLiteDatabase db = dbHelper.getWritableDatabase();
95 db.execSQL("pragma case_sensitive_like=ON;");
99 private synchronized void initDb() {
100 if (dbHelper != null) return;
102 dbHelper = new MobileLedgerDatabase(this);