-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathSettingsStore.java
235 lines (177 loc) · 6.25 KB
/
SettingsStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.medicmobile.webapp.mobile;
import android.content.*;
import android.net.Uri;
import java.util.*;
import java.util.regex.*;
import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG;
import static org.medicmobile.webapp.mobile.BuildConfig.TTL_LAST_URL;
import static org.medicmobile.webapp.mobile.SimpleJsonClient2.redactUrl;
import static org.medicmobile.webapp.mobile.MedicLog.trace;
@SuppressWarnings("PMD.ShortMethodName")
public abstract class SettingsStore {
private final SharedPreferences prefs;
SettingsStore(SharedPreferences prefs) {
this.prefs = prefs;
}
public abstract String getAppUrl();
public String getUrlToLoad(Uri url) {
return url != null ? url.toString() : getAppUrl();
}
public boolean isRootUrl(String url) {
if (url == null) {
return false;
}
return getAppUrl().equals(AppUrlVerifier.clean(url));
}
public boolean allowCustomHosts() { return false; }
public abstract boolean hasWebappSettings();
public abstract boolean allowsConfiguration();
public abstract void update(SharedPreferences.Editor ed, WebappSettings s);
void updateWith(WebappSettings s) throws SettingsException {
s.validate();
SharedPreferences.Editor ed = prefs.edit();
update(ed, s);
if (!ed.commit()) {
throw new SettingsException("Failed to save to SharedPreferences.");
}
}
String get(String key) {
return prefs.getString(key, null);
}
/**
* Return last visited URL in the app, within TTL_LAST_URL milliseconds.
*/
String getLastUrl() {
long lastUrlTimeMillis = prefs.getLong("last-url-time-ms", 0);
long lastUrlTimeMillisFromNow = System.currentTimeMillis() - lastUrlTimeMillis;
if (lastUrlTimeMillisFromNow > TTL_LAST_URL) {
return null;
}
String lastUrl = prefs.getString("last-url", null);
trace(this, "SettingsStore() :: getting last-url: %s", lastUrl);
return lastUrl;
}
/**
* Set last visited URL in the app.
*/
void setLastUrl(String lastUrl) throws SettingsException {
trace(this, "SettingsStore() :: setting last-url: %s", lastUrl);
SharedPreferences.Editor ed = prefs.edit();
ed.putString("last-url", lastUrl);
ed.putLong("last-url-time-ms", System.currentTimeMillis());
if (!ed.commit()) {
throw new SettingsException("Failed to save 'last-url' to SharedPreferences.");
}
}
static SettingsStore in(Context ctx) {
trace(SettingsStore.class, "Loading settings for context %s...", ctx);
SharedPreferences prefs = ctx.getSharedPreferences(
SettingsStore.class.getName(),
Context.MODE_PRIVATE);
String appHost = ctx.getResources().getString(R.string.app_host);
String scheme = ctx.getResources().getString(R.string.scheme);
if(appHost.length() > 0) {
return new BrandedSettingsStore(prefs, scheme + "://" + appHost);
}
Boolean allowCustomHosts = ctx.getResources().getBoolean(R.bool.allowCustomHosts);
return new UnbrandedSettingsStore(prefs, allowCustomHosts);
}
}
@SuppressWarnings("PMD.CallSuperInConstructor")
class BrandedSettingsStore extends SettingsStore {
private final String apiUrl;
BrandedSettingsStore(SharedPreferences prefs, String apiUrl) {
super(prefs);
this.apiUrl = apiUrl;
}
public String getAppUrl() { return apiUrl; }
public boolean hasWebappSettings() { return true; }
public boolean allowsConfiguration() { return false; }
public void update(SharedPreferences.Editor ed, WebappSettings s) { /* nothing to save */ }
}
@SuppressWarnings("PMD.CallSuperInConstructor")
class UnbrandedSettingsStore extends SettingsStore {
private final Boolean allowCustomHosts;
UnbrandedSettingsStore(SharedPreferences prefs, Boolean allowCustomHosts) {
super(prefs);
this.allowCustomHosts = allowCustomHosts;
}
//> ACCESSORS
public String getAppUrl() { return get("app-url"); }
public boolean allowsConfiguration() { return true; }
public boolean allowCustomHosts() { return this.allowCustomHosts; }
public boolean hasWebappSettings() {
WebappSettings s = new WebappSettings(getAppUrl());
try {
s.validate();
return true;
} catch(IllegalSettingsException ex) {
return false;
}
}
public void update(SharedPreferences.Editor ed, WebappSettings s) {
ed.putString("app-url", s.appUrl);
}
}
class WebappSettings {
public static final Pattern URL_PATTERN = Pattern.compile("http[s]?://([^/:]*)(:\\d*)?(.*)");
public final String appUrl;
public WebappSettings(String appUrl) {
trace(this, "WebappSettings() :: appUrl: %s", redactUrl(appUrl));
this.appUrl = appUrl;
}
public void validate() throws IllegalSettingsException {
List<IllegalSetting> errors = new LinkedList<>();
if (!isSet(appUrl)) {
errors.add(new IllegalSetting(R.id.txtAppUrl, R.string.errRequired));
} else if (!URL_PATTERN.matcher(appUrl).matches()) {
errors.add(new IllegalSetting(R.id.txtAppUrl, R.string.errInvalidUrl));
}
if (!errors.isEmpty()) {
throw new IllegalSettingsException(errors);
}
}
public void update(SharedPreferences.Editor ed, WebappSettings s) {
ed.putString("app-url", s.appUrl);
}
private boolean isSet(String val) {
return val != null && val.length() > 0;
}
}
class IllegalSetting {
public final int componentId;
public final int errorStringId;
public IllegalSetting(int componentId, int errorStringId) {
this.componentId = componentId;
this.errorStringId = errorStringId;
}
}
class SettingsException extends Exception {
// See: https://docs.pmd-code.org/pmd-doc-7.1.0/pmd_rules_java_errorprone.html#missingserialversionuid
public static final long serialVersionUID = -1008287132276329302L;
public SettingsException(String message) {
super(message);
}
}
class IllegalSettingsException extends SettingsException {
// See: https://docs.pmd-code.org/pmd-doc-7.1.0/pmd_rules_java_errorprone.html#missingserialversionuid
public static final long serialVersionUID = -6920159047512297868L;
public final List<IllegalSetting> errors;
public IllegalSettingsException(List<IllegalSetting> errors) {
super(createMessage(errors));
this.errors = errors;
}
private static String createMessage(List<IllegalSetting> errors) {
if (DEBUG) {
StringBuilder bob = new StringBuilder();
for (IllegalSetting e : errors) {
if (bob.length() > 0) {
bob.append("; ");
}
bob.append(String.format("component[%s]: error[%s]", e.componentId, e.errorStringId));
}
return bob.toString();
}
return null;
}
}