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.util.Log;
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;
32 import org.jetbrains.annotations.NotNull;
34 import java.net.Authenticator;
35 import java.net.MalformedURLException;
36 import java.net.PasswordAuthentication;
38 import java.util.Locale;
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);
50 public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
53 public static void resetAuthenticationData() {
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);
63 public static long getStartupProfile() {
64 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
65 return prefs.getLong(PREF_PROFILE_ID, -1);
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);
71 private String getAuthURL() {
72 if (profileModel != null)
73 return profileModel.getUrl();
74 return Data.getProfile()
77 private String getAuthUserName() {
78 if (profileModel != null)
79 return profileModel.getAuthUserName();
80 return Data.getProfile()
83 private String getAuthPassword() {
84 if (profileModel != null)
85 return profileModel.getAuthPassword();
86 return Data.getProfile()
89 private boolean getAuthEnabled() {
90 if (profileModel != null)
91 return profileModel.getUseAuthentication();
92 return Data.getProfile()
96 public void onCreate() {
97 Logger.debug("flow", "App onCreate()");
100 Data.refreshCurrencyData(Locale.getDefault());
101 Authenticator.setDefault(new Authenticator() {
103 protected PasswordAuthentication getPasswordAuthentication() {
104 if (getAuthEnabled()) {
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());
114 String.format("Requesting host [%s] differs from expected [%s]",
115 requestingHost, expectedHost));
117 catch (MalformedURLException e) {
122 return super.getPasswordAuthentication();
126 private void prepareMonthNames(boolean force) {
127 if (force || monthNamesPrepared)
129 Resources rm = getResources();
130 Globals.monthNames = rm.getStringArray(R.array.month_names);
131 monthNamesPrepared = true;
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());