]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/App.java
Room-based profile management
[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 = 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);
71         editor.apply();
72     }
73     public static long getStartupProfile() {
74         SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
75         return prefs.getLong(PREF_PROFILE_ID, -1);
76     }
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);
80     }
81     private String getAuthURL() {
82         if (profileModel != null)
83             return profileModel.getUrl();
84         return Data.getProfile()
85                    .getUrl();
86     }
87     private String getAuthUserName() {
88         if (profileModel != null)
89             return profileModel.getAuthUserName();
90         return Data.getProfile()
91                    .getAuthUser();
92     }
93     private String getAuthPassword() {
94         if (profileModel != null)
95             return profileModel.getAuthPassword();
96         return Data.getProfile()
97                    .getAuthPassword();
98     }
99     private boolean getAuthEnabled() {
100         if (profileModel != null)
101             return profileModel.getUseAuthentication();
102         return Data.getProfile()
103                    .useAuthentication();
104     }
105     @Override
106     public void onCreate() {
107         Logger.debug("flow", "App onCreate()");
108         instance = this;
109         super.onCreate();
110         Data.refreshCurrencyData(Locale.getDefault());
111         Authenticator.setDefault(new Authenticator() {
112             @Override
113             protected PasswordAuthentication getPasswordAuthentication() {
114                 if (getAuthEnabled()) {
115                     try {
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());
122                         else
123                             Log.w("http-auth",
124                                     String.format("Requesting host [%s] differs from expected [%s]",
125                                             requestingHost, expectedHost));
126                     }
127                     catch (MalformedURLException e) {
128                         e.printStackTrace();
129                     }
130                 }
131
132                 return super.getPasswordAuthentication();
133             }
134         });
135     }
136     private void prepareMonthNames(boolean force) {
137         if (force || monthNamesPrepared)
138             return;
139         Resources rm = getResources();
140         Globals.monthNames = rm.getStringArray(R.array.month_names);
141         monthNamesPrepared = true;
142     }
143     @Override
144     public void onTerminate() {
145         Logger.debug("flow", "App onTerminate()");
146         if (dbHelper != null)
147             dbHelper.close();
148         super.onTerminate();
149     }
150     @Override
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());
156     }
157     public SQLiteDatabase getDB() {
158         if (dbHelper == null)
159             initDb();
160
161         return dbHelper.getWritableDatabase();
162     }
163     private synchronized void initDb() {
164         if (dbHelper != null)
165             return;
166
167         // Let Room do any possible migrations
168         // this method may be removed when all DB access is made via Room
169         DB.get()
170           .compileStatement("select count(*) from profiles");
171         dbHelper = new MobileLedgerDatabase(this);
172     }
173 }