2 * Copyright © 2024 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 final String PREF_SHOW_ZERO_BALANCE_ACCOUNTS = "show-zero-balance-accounts";
45 public static App instance;
46 private static ProfileDetailModel profileModel;
47 private boolean monthNamesPrepared = false;
48 public static void prepareMonthNames() {
49 instance.prepareMonthNames(false);
51 public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
54 public static void resetAuthenticationData() {
57 public static void storeStartupProfileAndTheme(long currentProfileId, int currentTheme) {
58 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
59 SharedPreferences.Editor editor = prefs.edit();
60 editor.putLong(PREF_PROFILE_ID, currentProfileId);
61 editor.putInt(PREF_THEME_HUE, currentTheme);
64 public static long getStartupProfile() {
65 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
66 return prefs.getLong(PREF_PROFILE_ID, -1);
68 public static int getStartupTheme() {
69 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
70 return prefs.getInt(PREF_THEME_HUE, Colors.DEFAULT_HUE_DEG);
72 public static boolean getShowZeroBalanceAccounts() {
73 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
74 return prefs.getBoolean(PREF_SHOW_ZERO_BALANCE_ACCOUNTS, true);
76 public static void storeShowZeroBalanceAccounts(boolean value) {
77 SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
78 SharedPreferences.Editor editor = prefs.edit();
79 editor.putBoolean(PREF_SHOW_ZERO_BALANCE_ACCOUNTS, value);
82 private String getAuthURL() {
83 if (profileModel != null)
84 return profileModel.getUrl();
85 return Data.getProfile()
88 private String getAuthUserName() {
89 if (profileModel != null)
90 return profileModel.getAuthUserName();
91 return Data.getProfile()
94 private String getAuthPassword() {
95 if (profileModel != null)
96 return profileModel.getAuthPassword();
97 return Data.getProfile()
100 private boolean getAuthEnabled() {
101 if (profileModel != null)
102 return profileModel.getUseAuthentication();
103 return Data.getProfile()
104 .useAuthentication();
107 public void onCreate() {
108 Logger.debug("flow", "App onCreate()");
111 Data.refreshCurrencyData(Locale.getDefault());
112 Authenticator.setDefault(new Authenticator() {
114 protected PasswordAuthentication getPasswordAuthentication() {
115 if (getAuthEnabled()) {
117 final URL url = new URL(getAuthURL());
118 final String requestingHost = getRequestingHost();
119 final String expectedHost = url.getHost();
120 if (requestingHost.equalsIgnoreCase(expectedHost))
121 return new PasswordAuthentication(getAuthUserName(),
122 getAuthPassword().toCharArray());
125 String.format("Requesting host [%s] differs from expected [%s]",
126 requestingHost, expectedHost));
128 catch (MalformedURLException e) {
133 return super.getPasswordAuthentication();
137 private void prepareMonthNames(boolean force) {
138 if (force || monthNamesPrepared)
140 Resources rm = getResources();
141 Globals.monthNames = rm.getStringArray(R.array.month_names);
142 monthNamesPrepared = true;
145 public void onConfigurationChanged(@NotNull Configuration newConfig) {
146 super.onConfigurationChanged(newConfig);
147 prepareMonthNames(true);
148 Data.refreshCurrencyData(Locale.getDefault());
149 Data.locale.setValue(Locale.getDefault());