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 =
68 instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
69 SharedPreferences.Editor editor = prefs.edit();
70 editor.putLong(PREF_PROFILE_ID,
72 editor.putInt(PREF_THEME_HUE, currentTheme);
75 public static long getStartupProfile() {
76 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
77 return prefs.getLong(PREF_PROFILE_ID, -1);
79 public static int getStartupTheme() {
80 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
81 return prefs.getInt(PREF_THEME_HUE, Colors.DEFAULT_HUE_DEG);
83 private String getAuthURL() {
84 if (profileModel != null)
85 return profileModel.getUrl();
86 return Data.getProfile()
89 private String getAuthUserName() {
90 if (profileModel != null)
91 return profileModel.getAuthUserName();
92 return Data.getProfile()
95 private String getAuthPassword() {
96 if (profileModel != null)
97 return profileModel.getAuthPassword();
98 return Data.getProfile()
101 private boolean getAuthEnabled() {
102 if (profileModel != null)
103 return profileModel.getUseAuthentication();
104 return Data.getProfile()
108 public void onCreate() {
109 Logger.debug("flow", "App onCreate()");
112 Data.refreshCurrencyData(Locale.getDefault());
113 Authenticator.setDefault(new Authenticator() {
115 protected PasswordAuthentication getPasswordAuthentication() {
116 if (getAuthEnabled()) {
118 final URL url = new URL(getAuthURL());
119 final String requestingHost = getRequestingHost();
120 final String expectedHost = url.getHost();
121 if (requestingHost.equalsIgnoreCase(expectedHost))
122 return new PasswordAuthentication(getAuthUserName(),
123 getAuthPassword().toCharArray());
126 String.format("Requesting host [%s] differs from expected [%s]",
127 requestingHost, expectedHost));
129 catch (MalformedURLException e) {
134 return super.getPasswordAuthentication();
138 private void prepareMonthNames(boolean force) {
139 if (force || monthNamesPrepared)
141 Resources rm = getResources();
142 Globals.monthNames = rm.getStringArray(R.array.month_names);
143 monthNamesPrepared = true;
146 public void onTerminate() {
147 Logger.debug("flow", "App onTerminate()");
148 if (dbHelper != null)
153 public void onConfigurationChanged(@NotNull Configuration newConfig) {
154 super.onConfigurationChanged(newConfig);
155 prepareMonthNames(true);
156 Data.refreshCurrencyData(Locale.getDefault());
157 Data.locale.setValue(Locale.getDefault());
159 public SQLiteDatabase getDB() {
160 if (dbHelper == null)
163 return dbHelper.getWritableDatabase();
165 private synchronized void initDb() {
166 if (dbHelper != null)
169 // Let Room do any possible migrations
170 // this method may be removed when all DB access is made via Room
172 .compileStatement("select count(*) from profiles");
173 dbHelper = new MobileLedgerDatabase(this);