]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/App.java
more pronounced day/month delimiters in the transaction list
[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.util.Log;
25
26 import net.ktnx.mobileledger.model.Data;
27 import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
28 import net.ktnx.mobileledger.utils.Colors;
29 import net.ktnx.mobileledger.utils.Globals;
30 import net.ktnx.mobileledger.utils.Logger;
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 final String PREF_NAME = "MoLe";
42     public static final String PREF_THEME_HUE = "theme-hue";
43     public static final String PREF_PROFILE_ID = "profile-id";
44     public static App instance;
45     private static ProfileDetailModel profileModel;
46     private boolean monthNamesPrepared = false;
47     public static void prepareMonthNames() {
48         instance.prepareMonthNames(false);
49     }
50     public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
51         profileModel = model;
52     }
53     public static void resetAuthenticationData() {
54         profileModel = null;
55     }
56     public static void storeStartupProfileAndTheme(long currentProfileId, int currentTheme) {
57         SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
58         SharedPreferences.Editor editor = prefs.edit();
59         editor.putLong(PREF_PROFILE_ID, currentProfileId);
60         editor.putInt(PREF_THEME_HUE, currentTheme);
61         editor.apply();
62     }
63     public static long getStartupProfile() {
64         SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
65         return prefs.getLong(PREF_PROFILE_ID, -1);
66     }
67     public static int getStartupTheme() {
68         SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
69         return prefs.getInt(PREF_THEME_HUE, Colors.DEFAULT_HUE_DEG);
70     }
71     private String getAuthURL() {
72         if (profileModel != null)
73             return profileModel.getUrl();
74         return Data.getProfile()
75                    .getUrl();
76     }
77     private String getAuthUserName() {
78         if (profileModel != null)
79             return profileModel.getAuthUserName();
80         return Data.getProfile()
81                    .getAuthUser();
82     }
83     private String getAuthPassword() {
84         if (profileModel != null)
85             return profileModel.getAuthPassword();
86         return Data.getProfile()
87                    .getAuthPassword();
88     }
89     private boolean getAuthEnabled() {
90         if (profileModel != null)
91             return profileModel.getUseAuthentication();
92         return Data.getProfile()
93                    .useAuthentication();
94     }
95     @Override
96     public void onCreate() {
97         Logger.debug("flow", "App onCreate()");
98         instance = this;
99         super.onCreate();
100         Data.refreshCurrencyData(Locale.getDefault());
101         Authenticator.setDefault(new Authenticator() {
102             @Override
103             protected PasswordAuthentication getPasswordAuthentication() {
104                 if (getAuthEnabled()) {
105                     try {
106                         final URL url = new URL(getAuthURL());
107                         final String requestingHost = getRequestingHost();
108                         final String expectedHost = url.getHost();
109                         if (requestingHost.equalsIgnoreCase(expectedHost))
110                             return new PasswordAuthentication(getAuthUserName(),
111                                     getAuthPassword().toCharArray());
112                         else
113                             Log.w("http-auth",
114                                     String.format("Requesting host [%s] differs from expected [%s]",
115                                             requestingHost, expectedHost));
116                     }
117                     catch (MalformedURLException e) {
118                         e.printStackTrace();
119                     }
120                 }
121
122                 return super.getPasswordAuthentication();
123             }
124         });
125     }
126     private void prepareMonthNames(boolean force) {
127         if (force || monthNamesPrepared)
128             return;
129         Resources rm = getResources();
130         Globals.monthNames = rm.getStringArray(R.array.month_names);
131         monthNamesPrepared = true;
132     }
133     @Override
134     public void onConfigurationChanged(@NotNull Configuration newConfig) {
135         super.onConfigurationChanged(newConfig);
136         prepareMonthNames(true);
137         Data.refreshCurrencyData(Locale.getDefault());
138         Data.locale.setValue(Locale.getDefault());
139     }
140 }