2 * Copyright © 2021 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.SharedPreferences;
22 import android.content.res.Configuration;
23 import android.content.res.Resources;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.util.Log;
27 import net.ktnx.mobileledger.db.DB;
28 import net.ktnx.mobileledger.model.Data;
29 import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
30 import net.ktnx.mobileledger.utils.Colors;
31 import net.ktnx.mobileledger.utils.Globals;
32 import net.ktnx.mobileledger.utils.Logger;
33 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
35 import org.jetbrains.annotations.NotNull;
37 import java.net.Authenticator;
38 import java.net.MalformedURLException;
39 import java.net.PasswordAuthentication;
41 import java.util.Locale;
43 public class App extends Application {
44 public static final String PREF_NAME = "MoLe";
45 public static final String PREF_THEME_HUE = "theme-hue";
46 public static final String PREF_PROFILE_ID = "profile-id";
47 public static App instance;
48 private static ProfileDetailModel profileModel;
49 private MobileLedgerDatabase dbHelper;
50 private boolean monthNamesPrepared = false;
51 public static SQLiteDatabase getDatabase() {
53 throw new RuntimeException("Application not created yet");
55 return instance.getDB();
57 public static void prepareMonthNames() {
58 instance.prepareMonthNames(false);
60 public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
63 public static void resetAuthenticationData() {
66 public static void storeStartupProfileAndTheme(long currentProfileId, int currentTheme) {
67 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
68 SharedPreferences.Editor editor = prefs.edit();
69 editor.putLong(PREF_PROFILE_ID, currentProfileId);
70 editor.putInt(PREF_THEME_HUE, currentTheme);
73 public static long getStartupProfile() {
74 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
75 return prefs.getLong(PREF_PROFILE_ID, -1);
77 public static int getStartupTheme() {
78 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
79 return prefs.getInt(PREF_THEME_HUE, Colors.DEFAULT_HUE_DEG);
81 private String getAuthURL() {
82 if (profileModel != null)
83 return profileModel.getUrl();
84 return Data.getProfile()
87 private String getAuthUserName() {
88 if (profileModel != null)
89 return profileModel.getAuthUserName();
90 return Data.getProfile()
93 private String getAuthPassword() {
94 if (profileModel != null)
95 return profileModel.getAuthPassword();
96 return Data.getProfile()
99 private boolean getAuthEnabled() {
100 if (profileModel != null)
101 return profileModel.getUseAuthentication();
102 return Data.getProfile()
103 .useAuthentication();
106 public void onCreate() {
107 Logger.debug("flow", "App onCreate()");
110 Data.refreshCurrencyData(Locale.getDefault());
111 Authenticator.setDefault(new Authenticator() {
113 protected PasswordAuthentication getPasswordAuthentication() {
114 if (getAuthEnabled()) {
116 final URL url = new URL(getAuthURL());
117 final String requestingHost = getRequestingHost();
118 final String expectedHost = url.getHost();
119 if (requestingHost.equalsIgnoreCase(expectedHost))
120 return new PasswordAuthentication(getAuthUserName(),
121 getAuthPassword().toCharArray());
124 String.format("Requesting host [%s] differs from expected [%s]",
125 requestingHost, expectedHost));
127 catch (MalformedURLException e) {
132 return super.getPasswordAuthentication();
136 private void prepareMonthNames(boolean force) {
137 if (force || monthNamesPrepared)
139 Resources rm = getResources();
140 Globals.monthNames = rm.getStringArray(R.array.month_names);
141 monthNamesPrepared = true;
144 public void onTerminate() {
145 Logger.debug("flow", "App onTerminate()");
146 if (dbHelper != null)
151 public void onConfigurationChanged(@NotNull Configuration newConfig) {
152 super.onConfigurationChanged(newConfig);
153 prepareMonthNames(true);
154 Data.refreshCurrencyData(Locale.getDefault());
155 Data.locale.setValue(Locale.getDefault());
157 public SQLiteDatabase getDB() {
158 if (dbHelper == null)
161 return dbHelper.getWritableDatabase();
163 private synchronized void initDb() {
164 if (dbHelper != null)
167 // Let Room do any possible migrations
168 // this method may be removed when all DB access is made via Room
170 .compileStatement("select count(*) from profiles");
171 dbHelper = new MobileLedgerDatabase(this);