]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/App.java
bump androidx.constraintlayout library version
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / App.java
1 /*
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.
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 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);
50     }
51     public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
52         profileModel = model;
53     }
54     public static void resetAuthenticationData() {
55         profileModel = null;
56     }
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);
62         editor.apply();
63     }
64     public static long getStartupProfile() {
65         SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
66         return prefs.getLong(PREF_PROFILE_ID, -1);
67     }
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);
71     }
72     public static boolean getShowZeroBalanceAccounts() {
73         SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
74         return prefs.getBoolean(PREF_SHOW_ZERO_BALANCE_ACCOUNTS, true);
75     }
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);
80         editor.apply();
81     }
82     private String getAuthURL() {
83         if (profileModel != null)
84             return profileModel.getUrl();
85         return Data.getProfile()
86                    .getUrl();
87     }
88     private String getAuthUserName() {
89         if (profileModel != null)
90             return profileModel.getAuthUserName();
91         return Data.getProfile()
92                    .getAuthUser();
93     }
94     private String getAuthPassword() {
95         if (profileModel != null)
96             return profileModel.getAuthPassword();
97         return Data.getProfile()
98                    .getAuthPassword();
99     }
100     private boolean getAuthEnabled() {
101         if (profileModel != null)
102             return profileModel.getUseAuthentication();
103         return Data.getProfile()
104                    .useAuthentication();
105     }
106     @Override
107     public void onCreate() {
108         Logger.debug("flow", "App onCreate()");
109         instance = this;
110         super.onCreate();
111         Data.refreshCurrencyData(Locale.getDefault());
112         Authenticator.setDefault(new Authenticator() {
113             @Override
114             protected PasswordAuthentication getPasswordAuthentication() {
115                 if (getAuthEnabled()) {
116                     try {
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());
123                         else
124                             Log.w("http-auth",
125                                     String.format("Requesting host [%s] differs from expected [%s]",
126                                             requestingHost, expectedHost));
127                     }
128                     catch (MalformedURLException e) {
129                         e.printStackTrace();
130                     }
131                 }
132
133                 return super.getPasswordAuthentication();
134             }
135         });
136     }
137     private void prepareMonthNames(boolean force) {
138         if (force || monthNamesPrepared)
139             return;
140         Resources rm = getResources();
141         Globals.monthNames = rm.getStringArray(R.array.month_names);
142         monthNamesPrepared = true;
143     }
144     @Override
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());
150     }
151 }