]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/App.java
asynchronous profile initialisation
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / App.java
1 /*
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.
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.SharedPreferences;
22 import android.content.res.Configuration;
23 import android.content.res.Resources;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.util.Log;
26
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;
34
35 import org.jetbrains.annotations.NotNull;
36
37 import java.net.Authenticator;
38 import java.net.MalformedURLException;
39 import java.net.PasswordAuthentication;
40 import java.net.URL;
41 import java.util.Locale;
42
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() {
52         if (instance == null)
53             throw new RuntimeException("Application not created yet");
54
55         return instance.getDB();
56     }
57     public static void prepareMonthNames() {
58         instance.prepareMonthNames(false);
59     }
60     public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
61         profileModel = model;
62     }
63     public static void resetAuthenticationData() {
64         profileModel = null;
65     }
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,
71                 currentProfileId);
72         editor.putInt(PREF_THEME_HUE, currentTheme);
73         editor.apply();
74     }
75     public static long getStartupProfile() {
76         SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
77         return prefs.getLong(PREF_PROFILE_ID, -1);
78     }
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);
82     }
83     private String getAuthURL() {
84         if (profileModel != null)
85             return profileModel.getUrl();
86         return Data.getProfile()
87                    .getUrl();
88     }
89     private String getAuthUserName() {
90         if (profileModel != null)
91             return profileModel.getAuthUserName();
92         return Data.getProfile()
93                    .getAuthUserName();
94     }
95     private String getAuthPassword() {
96         if (profileModel != null)
97             return profileModel.getAuthPassword();
98         return Data.getProfile()
99                    .getAuthPassword();
100     }
101     private boolean getAuthEnabled() {
102         if (profileModel != null)
103             return profileModel.getUseAuthentication();
104         return Data.getProfile()
105                    .isAuthEnabled();
106     }
107     @Override
108     public void onCreate() {
109         Logger.debug("flow", "App onCreate()");
110         instance = this;
111         super.onCreate();
112         Data.refreshCurrencyData(Locale.getDefault());
113         Authenticator.setDefault(new Authenticator() {
114             @Override
115             protected PasswordAuthentication getPasswordAuthentication() {
116                 if (getAuthEnabled()) {
117                     try {
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());
124                         else
125                             Log.w("http-auth",
126                                     String.format("Requesting host [%s] differs from expected [%s]",
127                                             requestingHost, expectedHost));
128                     }
129                     catch (MalformedURLException e) {
130                         e.printStackTrace();
131                     }
132                 }
133
134                 return super.getPasswordAuthentication();
135             }
136         });
137     }
138     private void prepareMonthNames(boolean force) {
139         if (force || monthNamesPrepared)
140             return;
141         Resources rm = getResources();
142         Globals.monthNames = rm.getStringArray(R.array.month_names);
143         monthNamesPrepared = true;
144     }
145     @Override
146     public void onTerminate() {
147         Logger.debug("flow", "App onTerminate()");
148         if (dbHelper != null)
149             dbHelper.close();
150         super.onTerminate();
151     }
152     @Override
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());
158     }
159     public SQLiteDatabase getDB() {
160         if (dbHelper == null)
161             initDb();
162
163         return dbHelper.getWritableDatabase();
164     }
165     private synchronized void initDb() {
166         if (dbHelper != null)
167             return;
168
169         // Let Room do any possible migrations
170         // this method may be removed when all DB access is made via Room
171         DB.get()
172           .compileStatement("select count(*) from profiles");
173         dbHelper = new MobileLedgerDatabase(this);
174     }
175 }