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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
package cgeo.geocaching;
import cgeo.geocaching.maps.google.googleMapFactory;
import cgeo.geocaching.maps.interfaces.MapFactory;
import cgeo.geocaching.maps.mapsforge.mfMapFactory;
import org.apache.commons.lang3.StringUtils;
import org.mapsforge.android.maps.MapDatabase;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.Environment;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* General c:geo preferences/settings set by the user
*/
public class cgSettings {
private static final String KEY_WEB_DEVICE_CODE = "webDeviceCode";
private static final String KEY_WEBDEVICE_NAME = "webDeviceName";
private static final String KEY_MAP_LIVE = "maplive";
private static final String KEY_MAP_SOURCE = "mapsource";
private static final String KEY_USE_TWITTER = "twitter";
private static final String KEY_SHOW_ADDRESS = "showaddress";
private static final String KEY_SHOW_CAPTCHA = "showcaptcha";
private static final String KEY_MAP_TRAIL = "maptrail";
private static final String KEY_LAST_MAP_ZOOM = "mapzoom";
private static final String KEY_LIVE_LIST = "livelist";
private static final String KEY_METRIC_UNITS = "units";
private static final String KEY_SKIN = "skin";
private static final String KEY_LAST_USED_LIST = "lastlist";
private static final String KEY_CACHE_TYPE = "cachetype";
private static final String KEY_INITIALIZED = "initialized";
private static final String KEY_TWITTER_TOKEN_SECRET = "tokensecret";
private static final String KEY_TWITTER_TOKEN_PUBLIC = "tokenpublic";
private static final String KEY_VERSION = "version";
private static final String KEY_LOAD_DESCRIPTION = "autoloaddesc";
private static final String KEY_USE_ENGLISH = "useenglish";
private static final String KEY_AS_BROWSER = "asbrowser";
private static final String KEY_USE_COMPASS = "usecompass";
private static final String KEY_AUTO_VISIT_TRACKABLES = "trackautovisit";
private static final String KEY_AUTO_INSERT_SIGNATURE = "sigautoinsert";
private static final String KEY_ALTITUDE_CORRECTION = "altcorrection";
private static final String KEY_USE_GOOGLE_NAVIGATION = "usegnav";
private static final String KEY_STORE_LOG_IMAGES = "logimages";
private static final String KEY_EXCLUDE_DISABLED = "excludedisabled";
private static final String KEY_EXCLUDE_OWN = "excludemine";
private static final String KEY_MAPFILE = "mfmapfile";
private static final String KEY_SIGNATURE = "signature";
private static final String KEY_GCVOTE_PASSWORD = "pass-vote";
private static final String KEY_PASSWORD = "password";
private static final String KEY_USERNAME = "username";
private static final String KEY_COORD_INPUT_FORMAT = "coordinputformat";
private static final String KEY_LOG_OFFLINE = "log_offline";
private static final String KEY_LOAD_DIRECTION_IMG = "loaddirectionimg";
private static final String KEY_GC_CUSTOM_DATE = "gccustomdate";
private interface PrefRunnable {
void edit(final Editor edit);
}
public enum mapSourceEnum {
googleMap,
googleSat,
mapsforgeMapnik,
mapsforgeOsmarender,
mapsforgeCycle,
mapsforgeOffline;
static mapSourceEnum fromInt(int id) {
mapSourceEnum[] values = mapSourceEnum.values();
if (id >= 0 && id < values.length) {
return values[id];
} else {
return googleMap;
}
}
public boolean isGoogleMapSource() {
if (googleMap == this || googleSat == this) {
return true;
}
return false;
}
}
public enum coordInputFormatEnum {
Plain,
Deg,
Min,
Sec;
static coordInputFormatEnum fromInt(int id) {
coordInputFormatEnum[] values = coordInputFormatEnum.values();
if (id >= 0 && id < values.length) {
return values[id];
} else {
return Min;
}
}
}
// constants
public final static int unitsMetric = 1;
public final static int unitsImperial = 2;
public final static String cache = ".cgeo";
// twitter api keys
public final static String keyConsumerPublic = "RFafPiNi3xRhcS1TPE3wTw";
public final static String keyConsumerSecret = "7iDJprNPI9hzRwWhpzycSr9SPZMFrdVdsxD2OauI9k";
// version
public int version = 0;
// skin
public int skin = 0;
// settings
public int helper = 0;
public int initialized = 0;
public int autoLoadDesc = 0;
public int units = unitsMetric;
public int livelist = 1;
public int mapzoom = 14;
public int maplive = 1;
public int maptrail = 1;
public boolean useEnglish = false;
public boolean showCaptcha = false;
public int excludeMine = 0;
public int excludeDisabled = 0;
public int storeOfflineMaps = 0;
public boolean storelogimages = false;
public int asBrowser = 1;
public int useCompass = 1;
public int useGNavigation = 1;
public int showAddress = 1;
public int publicLoc = 0;
public int twitter = 0;
public int altCorrection = 0;
public String cacheType = null;
public String tokenPublic = null;
public String tokenSecret = null;
public String webDeviceName = null;
public String webDeviceCode = null;
public boolean trackableAutovisit = false;
public boolean signatureAutoinsert = false;
// usable values
public static final String tag = "cgeo";
/** Name of the preferences file */
public static final String preferences = "cgeo.pref";
// private variables
private Context context = null;
private SharedPreferences prefs = null;
private String username = null;
private String password = null;
private coordInputFormatEnum coordInput = coordInputFormatEnum.Plain;
// maps
public MapFactory mapFactory = null;
public mapSourceEnum mapSourceUsed = mapSourceEnum.googleMap;
public mapSourceEnum mapSource = mapSourceEnum.googleMap;
private String mapFile = null;
private boolean mapFileValid = false;
public cgSettings(Context contextIn, SharedPreferences prefsIn) {
context = contextIn;
prefs = prefsIn;
load();
}
public void load() {
version = prefs.getInt(KEY_VERSION, 0);
initialized = prefs.getInt(KEY_INITIALIZED, 0);
helper = prefs.getInt("helper", 0);
skin = prefs.getInt(KEY_SKIN, 0);
autoLoadDesc = prefs.getInt(KEY_LOAD_DESCRIPTION, 0);
units = prefs.getInt(KEY_METRIC_UNITS, 1);
livelist = prefs.getInt(KEY_LIVE_LIST, 1);
maplive = prefs.getInt(KEY_MAP_LIVE, 1);
mapzoom = prefs.getInt(KEY_LAST_MAP_ZOOM, 14);
maptrail = prefs.getInt(KEY_MAP_TRAIL, 1);
useEnglish = prefs.getBoolean(KEY_USE_ENGLISH, false);
showCaptcha = prefs.getBoolean(KEY_SHOW_CAPTCHA, false);
excludeMine = prefs.getInt(KEY_EXCLUDE_OWN, 0);
excludeDisabled = prefs.getInt(KEY_EXCLUDE_DISABLED, 0);
storeOfflineMaps = prefs.getInt("offlinemaps", 1);
storelogimages = prefs.getBoolean(KEY_STORE_LOG_IMAGES, false);
asBrowser = prefs.getInt(KEY_AS_BROWSER, 1);
useCompass = prefs.getInt(KEY_USE_COMPASS, 1);
useGNavigation = prefs.getInt(KEY_USE_GOOGLE_NAVIGATION, 1);
showAddress = prefs.getInt(KEY_SHOW_ADDRESS, 1);
publicLoc = prefs.getInt("publicloc", 0);
twitter = prefs.getInt(KEY_USE_TWITTER, 0);
altCorrection = prefs.getInt(KEY_ALTITUDE_CORRECTION, 0);
cacheType = prefs.getString(KEY_CACHE_TYPE, null);
tokenPublic = prefs.getString(KEY_TWITTER_TOKEN_PUBLIC, null);
tokenSecret = prefs.getString(KEY_TWITTER_TOKEN_SECRET, null);
mapFile = prefs.getString(KEY_MAPFILE, null);
mapFileValid = checkMapfile(mapFile);
mapSource = mapSourceEnum.fromInt(prefs.getInt(KEY_MAP_SOURCE, 0));
webDeviceName = prefs.getString(KEY_WEBDEVICE_NAME, null);
webDeviceCode = prefs.getString(KEY_WEB_DEVICE_CODE, null);
trackableAutovisit = prefs.getBoolean(KEY_AUTO_VISIT_TRACKABLES, false);
signatureAutoinsert = prefs.getBoolean(KEY_AUTO_INSERT_SIGNATURE, false);
coordInput = coordInputFormatEnum.fromInt(prefs.getInt(KEY_COORD_INPUT_FORMAT, 0));
setLanguage(useEnglish);
}
public void setSkin(int skinIn) {
if (skin == 1) {
skin = 1;
} else {
skin = 0;
}
}
public void setLanguage(boolean useEnglish) {
Locale locale = Locale.getDefault();
if (useEnglish) {
locale = new Locale("en");
}
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
public void reloadTwitterTokens() {
tokenPublic = prefs.getString(KEY_TWITTER_TOKEN_PUBLIC, null);
tokenSecret = prefs.getString(KEY_TWITTER_TOKEN_SECRET, null);
}
public void reloadCacheType() {
cacheType = prefs.getString(KEY_CACHE_TYPE, null);
}
public static String getStorage() {
return getStorageSpecific(null)[0];
}
public static String getStorageSec() {
return getStorageSpecific(null)[1];
}
public static String[] getStorageSpecific(Boolean hidden) {
String external = Environment.getExternalStorageDirectory() + "/" + cache + "/";
String data = Environment.getDataDirectory() + "/data/cgeo.geocaching/" + cache + "/";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return new String[] { external, data };
} else {
return new String[] { data, external };
}
}
public boolean isLogin() {
final String preUsername = prefs.getString(KEY_USERNAME, null);
final String prePassword = prefs.getString(KEY_PASSWORD, null);
if (StringUtils.isBlank(preUsername) || StringUtils.isBlank(prePassword)) {
return false;
} else {
return true;
}
}
public Map<String, String> getLogin() {
final Map<String, String> login = new HashMap<String, String>();
if (username == null || password == null) {
final String preUsername = prefs.getString(KEY_USERNAME, null);
final String prePassword = prefs.getString(KEY_PASSWORD, null);
if (initialized == 0 && (preUsername == null || prePassword == null)) {
Intent initIntent = new Intent(context, cgeoinit.class);
context.startActivity(initIntent);
final SharedPreferences.Editor prefsEdit = prefs.edit();
prefsEdit.putInt(KEY_INITIALIZED, 1);
prefsEdit.commit();
initialized = 1;
return null;
} else if (initialized == 1 && (preUsername == null || prePassword == null)) {
return null;
}
login.put(KEY_USERNAME, preUsername);
login.put(KEY_PASSWORD, prePassword);
username = preUsername;
password = prePassword;
} else {
login.put(KEY_USERNAME, username);
login.put(KEY_PASSWORD, password);
}
return login;
}
public String getUsername() {
if (username == null) {
return prefs.getString(KEY_USERNAME, null);
} else {
return username;
}
}
public boolean setLogin(final String username, final String password) {
this.username = username;
this.password = password;
return editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
// erase username and password
edit.remove(KEY_USERNAME);
edit.remove(KEY_PASSWORD);
} else {
// save username and password
edit.putString(KEY_USERNAME, username);
edit.putString(KEY_PASSWORD, password);
}
}
});
}
public boolean isGCvoteLogin() {
final String preUsername = prefs.getString(KEY_USERNAME, null);
final String prePassword = prefs.getString(KEY_GCVOTE_PASSWORD, null);
if (StringUtils.isBlank(preUsername) || StringUtils.isBlank(prePassword)) {
return false;
} else {
return true;
}
}
public boolean setGCvoteLogin(final String password) {
return editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
if (StringUtils.isBlank(password)) {
// erase password
edit.remove(KEY_GCVOTE_PASSWORD);
} else {
// save password
edit.putString(KEY_GCVOTE_PASSWORD, password);
}
}
});
}
public Map<String, String> getGCvoteLogin() {
final Map<String, String> login = new HashMap<String, String>();
if (username == null || password == null) {
final String preUsername = prefs.getString(KEY_USERNAME, null);
final String prePassword = prefs.getString(KEY_GCVOTE_PASSWORD, null);
if (preUsername == null || prePassword == null) {
return null;
}
login.put(KEY_USERNAME, preUsername);
login.put(KEY_PASSWORD, prePassword);
username = preUsername;
} else {
login.put(KEY_USERNAME, username);
login.put(KEY_PASSWORD, password);
}
return login;
}
public boolean setSignature(final String signature) {
return editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
if (StringUtils.isBlank(signature)) {
// erase signature
edit.remove(KEY_SIGNATURE);
} else {
// save signature
edit.putString(KEY_SIGNATURE, signature);
}
}
});
}
public String getSignature() {
return prefs.getString(KEY_SIGNATURE, null);
}
public boolean setAltCorrection(final int altitude) {
altCorrection = altitude;
return editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putInt(KEY_ALTITUDE_CORRECTION, altitude);
}
});
}
public String setCacheType(final String cacheTypeIn) {
editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putString(KEY_CACHE_TYPE, cacheTypeIn);
}
});
cacheType = cacheTypeIn;
return cacheType;
}
public void liveMapEnable() {
if (editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putInt(KEY_MAP_LIVE, 1);
}
})) {
maplive = 1;
}
}
public void liveMapDisable() {
if (editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putInt(KEY_MAP_LIVE, 0);
}
})) {
maplive = 0;
}
}
public int getLastList() {
int listId = prefs.getInt(KEY_LAST_USED_LIST, -1);
return listId;
}
public void saveLastList(final int listId) {
editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putInt(KEY_LAST_USED_LIST, listId);
}
});
}
public void setWebNameCode(final String name, final String code) {
if (editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putString(KEY_WEBDEVICE_NAME, name);
edit.putString(KEY_WEB_DEVICE_CODE, code);
}
})) {
webDeviceCode = code;
webDeviceName = name;
}
}
public MapFactory getMapFactory() {
if (mapSource.isGoogleMapSource()) {
if (!mapSourceUsed.isGoogleMapSource() || mapFactory == null) {
mapFactory = new googleMapFactory();
mapSourceUsed = mapSource;
}
} else if (!mapSource.isGoogleMapSource()) {
if (mapSourceUsed.isGoogleMapSource() || mapFactory == null) {
mapFactory = new mfMapFactory();
mapSourceUsed = mapSource;
}
}
return mapFactory;
}
public String getMapFile() {
return mapFile;
}
public boolean setMapFile(final String mapFileIn) {
boolean commitResult = editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putString(KEY_MAPFILE, mapFileIn);
}
});
mapFile = mapFileIn;
mapFileValid = checkMapfile(mapFile);
return commitResult;
}
public boolean hasValidMapFile() {
return mapFileValid;
}
private static boolean checkMapfile(String mapFileIn) {
if (null == mapFileIn) {
return false;
}
return MapDatabase.isValidMapFile(mapFileIn);
}
public coordInputFormatEnum getCoordInputFormat() {
return coordInput;
}
public boolean setCoordInputFormat(coordInputFormatEnum format) {
coordInput = format;
boolean commitResult = editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putInt(KEY_COORD_INPUT_FORMAT, coordInput.ordinal());
}
});
return commitResult;
}
/**
* edit some settings without knowing how to get the settings editor or how to commit
*
* @param runnable
* @return commit result
*/
private boolean editSettings(final PrefRunnable runnable) {
final SharedPreferences.Editor prefsEdit = prefs.edit();
runnable.edit(prefsEdit);
return prefsEdit.commit();
}
void setLogOffline(final boolean offline) {
editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putBoolean(KEY_LOG_OFFLINE, offline);
}
});
}
public boolean getLogOffline() {
return prefs.getBoolean(KEY_LOG_OFFLINE, false);
}
void setLoadDirImg(final boolean value) {
editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putBoolean(KEY_LOAD_DIRECTION_IMG, value);
}
});
}
public boolean getLoadDirImg() {
return prefs.getBoolean(KEY_LOAD_DIRECTION_IMG, true);
}
void setGcCustomDate(final String format) {
editSettings(new PrefRunnable() {
@Override
public void edit(Editor edit) {
edit.putString(KEY_GC_CUSTOM_DATE, format);
}
});
}
public String getGcCustomDate() {
return prefs.getString(KEY_GC_CUSTOM_DATE, null);
}
}
|