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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
|
package cgeo.geocaching;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.geopoint.Geopoint;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import android.app.Activity;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
public class cgeoapplication extends Application {
private cgData storage = null;
private String action = null;
private Geopoint lastCoords = null;
private cgGeo geo = null;
private boolean geoInUse = false;
private cgDirection dir = null;
private boolean dirInUse = false;
final private Map<String, cgCache> cachesCache = new HashMap<String, cgCache>(); // caching caches into memory
public boolean firstRun = true; // c:geo is just launched
public boolean showLoginToast = true; //login toast shown just once.
public boolean warnedLanguage = false; // user was warned about different language settings on geocaching.com
private boolean databaseCleaned = false; // database was cleaned
private static cgeoapplication instance;
public cgeoapplication() {
setInstance(this);
getStorage();
}
private static void setInstance(final cgeoapplication app) {
instance = app;
}
public static cgeoapplication getInstance() {
return instance;
}
@Override
public void onLowMemory() {
Log.i(Settings.tag, "Cleaning applications cache.");
cachesCache.clear();
}
@Override
public void onTerminate() {
Log.d(Settings.tag, "Terminating c:geo...");
if (geo != null) {
geo.closeGeo();
geo = null;
}
if (dir != null) {
dir.closeDir();
dir = null;
}
if (storage != null) {
storage.clean();
storage.closeDb();
storage = null;
}
super.onTerminate();
}
public String backupDatabase() {
return storage.backupDatabase();
}
public static File isRestoreFile() {
return cgData.isRestoreFile();
}
/**
* restore the database in a new thread, showing a progress window
*
* @param fromActivity
* calling activity
*/
public void restoreDatabase(final Activity fromActivity) {
final Resources res = this.getResources();
final ProgressDialog dialog = ProgressDialog.show(fromActivity, res.getString(R.string.init_backup_restore), res.getString(R.string.init_restore_running), true, false);
final AtomicBoolean atomic = new AtomicBoolean(false);
Thread restoreThread = new Thread() {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
dialog.dismiss();
boolean restored = atomic.get();
String message = restored ? res.getString(R.string.init_restore_success) : res.getString(R.string.init_restore_failed);
ActivityMixin.helpDialog(fromActivity, res.getString(R.string.init_backup_restore), message);
}
};
@Override
public void run() {
atomic.set(storage.restoreDatabase());
handler.sendMessage(handler.obtainMessage());
}
};
restoreThread.start();
}
public void cleanGeo() {
if (geo != null) {
geo.closeGeo();
geo = null;
}
}
public void cleanDir() {
if (dir != null) {
dir.closeDir();
dir = null;
}
}
public boolean storageStatus() {
return storage.status();
}
public cgGeo startGeo(Context context, cgUpdateLoc geoUpdate, int time, int distance) {
if (geo == null) {
geo = new cgGeo(context, this, geoUpdate, time, distance);
geo.initGeo();
Log.i(Settings.tag, "Location service started");
}
geo.replaceUpdate(geoUpdate);
geoInUse = true;
return geo;
}
public cgGeo removeGeo() {
if (geo != null) {
geo.replaceUpdate(null);
}
geoInUse = false;
(new removeGeoThread()).start();
return null;
}
private class removeGeoThread extends Thread {
@Override
public void run() {
try {
sleep(2500);
} catch (Exception e) {
// nothing
}
if (!geoInUse && geo != null) {
geo.closeGeo();
geo = null;
Log.i(Settings.tag, "Location service stopped");
}
}
}
public cgDirection startDir(Context context, cgUpdateDir dirUpdate) {
if (dir == null) {
dir = new cgDirection(context, dirUpdate);
dir.initDir();
Log.i(Settings.tag, "Direction service started");
}
dir.replaceUpdate(dirUpdate);
dirInUse = true;
return dir;
}
public cgDirection removeDir() {
if (dir != null) {
dir.replaceUpdate(null);
}
dirInUse = false;
(new removeDirThread()).start();
return null;
}
private class removeDirThread extends Thread {
@Override
public void run() {
try {
sleep(2500);
} catch (Exception e) {
// nothing
}
if (!dirInUse && dir != null) {
dir.closeDir();
dir = null;
Log.i(Settings.tag, "Direction service stopped");
}
}
}
public void cleanDatabase(boolean more) {
if (databaseCleaned) {
return;
}
getStorage().clean(more);
databaseCleaned = true;
}
public boolean isThere(String geocode, String guid, boolean detailed, boolean checkTime) {
return getStorage().isThere(geocode, guid, detailed, checkTime);
}
public boolean isOffline(String geocode, String guid) {
return getStorage().isOffline(geocode, guid);
}
public String getGeocode(String guid) {
return getStorage().getGeocodeForGuid(guid);
}
public String getCacheid(String geocode) {
return getStorage().getCacheidForGeocode(geocode);
}
public static StatusCode getError(final cgSearch search) {
if (search == null) {
return null;
}
return search.error;
}
public static boolean setError(final cgSearch search, final StatusCode error) {
if (search == null) {
return false;
}
search.error = error;
return true;
}
public static String getUrl(final cgSearch search) {
if (search == null) {
return null;
}
return search.url;
}
public static boolean setUrl(final cgSearch search, String url) {
if (search == null) {
return false;
}
search.url = url;
return true;
}
public static String[] getViewstates(final cgSearch search) {
if (search == null) {
return null;
}
return search.viewstates;
}
public static boolean setViewstates(final cgSearch search, String[] viewstates) {
if (cgBase.isEmpty(viewstates) || search == null) {
return false;
}
search.viewstates = viewstates;
return true;
}
public static Integer getTotal(final cgSearch search) {
if (search == null) {
return null;
}
return search.totalCnt;
}
public static Integer getCount(final cgSearch search) {
if (search == null) {
return 0;
}
return search.getCount();
}
public boolean hasUnsavedCaches(final cgSearch search) {
if (search == null) {
return false;
}
for (final String geocode : search.getGeocodes()) {
if (!isOffline(geocode, null)) {
return true;
}
}
return false;
}
public cgCache getCacheByGeocode(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
return getCacheByGeocode(geocode, cgCache.LOADWAYPOINTS);
}
public cgCache getCacheByGeocode(final String geocode, final int loadFlags) {
if (cachesCache.containsKey(geocode)) {
return cachesCache.get(geocode);
}
final cgCache cache = getStorage().loadCache(geocode, null, loadFlags);
if (cache != null && cache.getDetailed() && loadFlags == cgCache.LOADALL) {
putCacheInCache(cache);
}
return cache;
}
public cgTrackable getTrackableByGeocode(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
cgTrackable trackable = null;
trackable = storage.loadTrackable(geocode);
return trackable;
}
public void removeCacheFromCache(String geocode) {
if (geocode != null && cachesCache.containsKey(geocode)) {
cachesCache.remove(geocode);
}
}
public void putCacheInCache(cgCache cache) {
if (cache == null || cache.getGeocode() == null) {
return;
}
if (cachesCache.containsKey(cache.getGeocode())) {
cachesCache.remove(cache.getGeocode());
}
cachesCache.put(cache.getGeocode(), cache);
}
public String[] geocodesInCache() {
return getStorage().allDetailedThere();
}
public cgWaypoint getWaypointById(Integer id) {
if (id == null || id == 0) {
return null;
}
return getStorage().loadWaypoint(id);
}
public List<Object> getBounds(String geocode) {
if (geocode == null) {
return null;
}
List<String> geocodeList = new ArrayList<String>();
geocodeList.add(geocode);
return getBounds(geocodeList);
}
public List<Object> getBounds(final cgSearch search) {
if (search == null) {
return null;
}
if (storage == null) {
storage = new cgData(this);
}
final List<String> geocodeList = search.getGeocodes();
return getBounds(geocodeList);
}
public List<Object> getBounds(final List<String> geocodes) {
if (CollectionUtils.isEmpty(geocodes)) {
return null;
}
return getStorage().getBounds(geocodes.toArray());
}
public cgCache getCache(final cgSearch search) {
if (search == null) {
return null;
}
final List<String> geocodeList = search.getGeocodes();
return getCacheByGeocode(geocodeList.get(0), cgCache.LOADALL);
}
/**
* @param search
* @param loadWaypoints
* only load waypoints for map usage. All other callers should set this to <code>false</code>
* @return
*/
public List<cgCache> getCaches(final cgSearch search, final boolean loadWaypoints) {
return getCaches(search, null, null, null, null, (loadWaypoints ? cgCache.LOADWAYPOINTS : 0) | cgCache.LOADOFFLINELOG);
}
public List<cgCache> getCaches(final cgSearch search, Long centerLat, Long centerLon, Long spanLat, Long spanLon) {
return getCaches(search, centerLat, centerLon, spanLat, spanLon, cgCache.LOADWAYPOINTS | cgCache.LOADOFFLINELOG);
}
public List<cgCache> getCaches(final cgSearch search, final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final int loadFlags) {
if (search == null) {
List<cgCache> cachesOut = new ArrayList<cgCache>();
final List<cgCache> cachesPre = storage.loadCaches(null, null, centerLat, centerLon, spanLat, spanLon, loadFlags);
if (cachesPre != null) {
cachesOut.addAll(cachesPre);
}
return cachesOut;
}
List<cgCache> cachesOut = new ArrayList<cgCache>();
final List<String> geocodeList = search.getGeocodes();
// The list of geocodes is sufficient. more parameters generate an overly complex select.
final List<cgCache> cachesPre = getStorage().loadCaches(geocodeList.toArray(), null, null, null, null, null, loadFlags);
if (cachesPre != null) {
cachesOut.addAll(cachesPre);
}
return cachesOut;
}
public cgSearch getBatchOfStoredCaches(final boolean detailedOnly, final Geopoint coords, final CacheType cachetype, final int list) {
final List<String> geocodes = getStorage().loadBatchOfStoredGeocodes(detailedOnly, coords, cachetype, list);
return new cgSearch(geocodes);
}
public List<cgDestination> getHistoryOfSearchedLocations() {
return getStorage().loadHistoryOfSearchedLocations();
}
public cgSearch getHistoryOfCaches(final boolean detailedOnly, final CacheType cachetype) {
final List<String> geocodes = getStorage().loadBatchOfHistoricGeocodes(detailedOnly, cachetype);
return new cgSearch(geocodes);
}
public cgSearch getCachedInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cachetype) {
final List<String> geocodes = getStorage().getCachedInViewport(centerLat, centerLon, spanLat, spanLon, cachetype);
return new cgSearch(geocodes);
}
public cgSearch getStoredInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cachetype) {
final List<String> geocodes = getStorage().getStoredInViewport(centerLat, centerLon, spanLat, spanLon, cachetype);
return new cgSearch(geocodes);
}
public cgSearch getOfflineAll(String cachetype) {
final List<String> geocodes = getStorage().getOfflineAll(cachetype);
return new cgSearch(geocodes);
}
public int getAllStoredCachesCount(final boolean detailedOnly, final CacheType cacheType, final Integer list) {
return getStorage().getAllStoredCachesCount(detailedOnly, cacheType, list);
}
private cgData getStorage() {
if (storage == null) {
storage = new cgData(this);
}
return storage;
}
public int getAllHistoricCachesCount() {
return getStorage().getAllHistoricCachesCount();
}
public void markStored(String geocode, int listId) {
getStorage().markStored(geocode, listId);
}
public boolean markDropped(String geocode) {
return getStorage().markDropped(geocode);
}
public boolean markFound(String geocode) {
return getStorage().markFound(geocode);
}
public boolean clearSearchedDestinations() {
return getStorage().clearSearchedDestinations();
}
public boolean saveSearchedDestination(cgDestination destination) {
return getStorage().saveSearchedDestination(destination);
}
public boolean saveWaypoints(String geocode, List<cgWaypoint> waypoints, boolean drop) {
return getStorage().saveWaypoints(geocode, waypoints, drop);
}
public boolean saveOwnWaypoint(int id, String geocode, cgWaypoint waypoint) {
return getStorage().saveOwnWaypoint(id, geocode, waypoint);
}
public boolean deleteWaypoint(int id) {
return getStorage().deleteWaypoint(id);
}
public boolean saveTrackable(cgTrackable trackable) {
final List<cgTrackable> list = new ArrayList<cgTrackable>();
list.add(trackable);
return getStorage().saveInventory("---", list);
}
public static void addGeocode(final cgSearch search, final String geocode) {
if (search == null || StringUtils.isBlank(geocode)) {
return;
}
search.addGeocode(geocode);
}
public void addSearch(final List<cgCache> cacheList, final int reason) {
if (CollectionUtils.isEmpty(cacheList)) {
return;
}
if (storage == null) {
storage = new cgData(this);
}
for (final cgCache cache : cacheList) {
cache.setReason(reason);
storeWithMerge(cache, false);
}
}
public boolean addCacheToSearch(cgSearch search, cgCache cache) {
if (search == null || cache == null) {
return false;
}
final boolean status = storeWithMerge(cache, cache.getReason() >= 1);
if (status) {
search.addGeocode(cache.getGeocode());
}
return status;
}
/**
* Checks if Cache is already in Database and if so does a merge.
*
* @param cache
* the cache to be saved
* @param override
* override the check and persist the new state.
* @return true if the cache has been saved correctly
*/
private boolean storeWithMerge(final cgCache cache, final boolean override) {
if (!override) {
final cgCache oldCache = storage.loadCache(cache.getGeocode(), cache.getGuid(), cgCache.LOADALL);
cache.gatherMissingFrom(oldCache);
}
return storage.saveCache(cache);
}
public void dropStored(int listId) {
getStorage().dropStored(listId);
}
public List<cgTrackable> loadInventory(String geocode) {
return storage.loadInventory(geocode);
}
public Map<Integer, Integer> loadLogCounts(String geocode) {
return storage.loadLogCounts(geocode);
}
public List<cgImage> loadSpoilers(String geocode) {
return storage.loadSpoilers(geocode);
}
public cgWaypoint loadWaypoint(int id) {
return storage.loadWaypoint(id);
}
/**
* set the current action to be reported to Go4Cache (if enabled in settings)<br>
* this might be either
* <ul>
* <li>geocode</li>
* <li>name of a cache</li>
* <li>action like twittering</li>
* </ul>
*
* @param action
*/
public void setAction(String action) {
this.action = action;
}
public String getAction() {
return StringUtils.defaultString(action);
}
public boolean addLog(String geocode, cgLog log) {
if (StringUtils.isBlank(geocode)) {
return false;
}
if (log == null) {
return false;
}
List<cgLog> list = new ArrayList<cgLog>();
list.add(log);
return storage.saveLogs(geocode, list, false);
}
public void setLastLoc(final Geopoint coords) {
lastCoords = coords;
}
public Geopoint getLastCoords() {
return lastCoords;
}
public boolean saveLogOffline(String geocode, Date date, int logtype, String log) {
return storage.saveLogOffline(geocode, date, logtype, log);
}
public cgLog loadLogOffline(String geocode) {
return storage.loadLogOffline(geocode);
}
public void clearLogOffline(String geocode) {
storage.clearLogOffline(geocode);
}
public void saveVisitDate(String geocode) {
storage.saveVisitDate(geocode);
}
public void clearVisitDate(String geocode) {
storage.clearVisitDate(geocode);
}
public List<cgList> getLists() {
return storage.getLists(getResources());
}
public cgList getList(int id) {
return storage.getList(id, getResources());
}
public int createList(String title) {
return storage.createList(title);
}
public int renameList(final int listId, final String title) {
return storage.renameList(listId, title);
}
public boolean removeList(int id) {
return storage.removeList(id);
}
public boolean removeSearchedDestinations(cgDestination destination) {
return storage.removeSearchedDestination(destination);
}
public void moveToList(String geocode, int listId) {
storage.moveToList(geocode, listId);
}
public String getCacheDescription(String geocode) {
return storage.getCacheDescription(geocode);
}
}
|