]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/App.java
whitespace
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / App.java
1 /*
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.
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 org.jetbrains.annotations.NotNull;
33
34 import java.net.Authenticator;
35 import java.net.MalformedURLException;
36 import java.net.PasswordAuthentication;
37 import java.net.URL;
38 import java.util.Locale;
39
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)
46             throw new RuntimeException("Application not created yet");
47
48         return instance.getDB();
49     }
50     public static void prepareMonthNames() {
51         instance.prepareMonthNames(false);
52     }
53     @Override
54     public void onCreate() {
55         Logger.debug("flow", "App onCreate()");
56         instance = this;
57         super.onCreate();
58         Data.refreshCurrencyData(Locale.getDefault());
59         Authenticator.setDefault(new Authenticator() {
60             @Override
61             protected PasswordAuthentication getPasswordAuthentication() {
62                 MobileLedgerProfile p = Data.getProfile();
63                 if (p.isAuthEnabled()) {
64                     try {
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(),
70                                     p.getAuthPassword()
71                                      .toCharArray());
72                         else
73                             Log.w("http-auth",
74                                     String.format("Requesting host [%s] differs from expected [%s]",
75                                             requestingHost, expectedHost));
76                     }
77                     catch (MalformedURLException e) {
78                         e.printStackTrace();
79                     }
80                 }
81
82                 return super.getPasswordAuthentication();
83             }
84         });
85     }
86     private void prepareMonthNames(boolean force) {
87         if (force || monthNamesPrepared)
88             return;
89         Resources rm = getResources();
90         Globals.monthNames = rm.getStringArray(R.array.month_names);
91         monthNamesPrepared = true;
92     }
93     @Override
94     public void onTerminate() {
95         Logger.debug("flow", "App onTerminate()");
96         if (dbHelper != null)
97             dbHelper.close();
98         super.onTerminate();
99     }
100     @Override
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());
106     }
107     public SQLiteDatabase getDB() {
108         if (dbHelper == null)
109             initDb();
110
111         return dbHelper.getWritableDatabase();
112     }
113     private synchronized void initDb() {
114         if (dbHelper != null)
115             return;
116
117         dbHelper = new MobileLedgerDatabase(this);
118     }
119 }