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
|
package cgeo.geocaching;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LoadFlags.LoadFlag;
import cgeo.geocaching.enumerations.LoadFlags.RemoveFlag;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.utils.Log;
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 java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
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;
public boolean firstRun = true; // c:geo is just launched
public boolean showLoginToast = true; //login toast shown just once.
private boolean databaseCleaned = false; // database was cleaned
private static cgeoapplication instance = null;
public cgeoapplication() {
instance = this;
storage = new cgData(this);
}
public static cgeoapplication getInstance() {
return instance;
}
@Override
public void onLowMemory() {
Log.i(Settings.tag, "Cleaning applications cache.");
storage.removeAllFromCache();
}
@Override
public void onTerminate() {
Log.d(Settings.tag, "Terminating c:geo...");
cleanGeo();
cleanDir();
if (storage != null) {
storage.clean();
storage.closeDb();
storage = null;
storage = new cgData(this);
}
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(UpdateLocationCallback geoUpdate) {
if (geo == null) {
geo = new cgGeo();
Log.i(Settings.tag, "Location service started");
}
geo.replaceUpdate(geoUpdate);
geoInUse = true;
return geo;
}
public float getSpeedFromGeo() {
return geo != null ? geo.speedNow : 0f;
}
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) {
cleanGeo();
Log.i(Settings.tag, "Location service stopped");
}
}
}
public cgDirection startDir(Context context, UpdateDirectionCallback dirUpdate) {
if (dir == null) {
dir = new cgDirection(context, dirUpdate);
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) {
cleanDir();
Log.i(Settings.tag, "Direction service stopped");
}
}
}
public void cleanDatabase(boolean more) {
if (databaseCleaned) {
return;
}
storage.clean(more);
databaseCleaned = true;
}
/** {@link cgData#isThere(String, String, boolean, boolean)} */
public boolean isThere(String geocode, String guid, boolean detailed, boolean checkTime) {
return storage.isThere(geocode, guid, detailed, checkTime);
}
/** {@link cgData#isOffline(String, String)} */
public boolean isOffline(String geocode, String guid) {
return storage.isOffline(geocode, guid);
}
/** {@link cgData#getGeocodeForGuid(String)} */
public String getGeocode(String guid) {
return storage.getGeocodeForGuid(guid);
}
/** {@link cgData#getCacheidForGeocode(String)} */
public String getCacheid(String geocode) {
return storage.getCacheidForGeocode(geocode);
}
public boolean hasUnsavedCaches(final SearchResult search) {
if (search == null) {
return false;
}
for (final String geocode : search.getGeocodes()) {
if (!isOffline(geocode, null)) {
return true;
}
}
return false;
}
public cgTrackable getTrackableByGeocode(String geocode) {
if (StringUtils.isBlank(geocode)) {
return null;
}
cgTrackable trackable = null;
trackable = storage.loadTrackable(geocode);
return trackable;
}
/** {@link cgData#allDetailedThere()} */
public String[] geocodesInCache() {
return storage.allDetailedThere();
}
public List<Number> getBounds(String geocode) {
if (geocode == null) {
return null;
}
Set<String> geocodeList = new HashSet<String>();
geocodeList.add(geocode);
return getBounds(geocodeList);
}
/** {@link cgData#getBounds(Set)} */
public List<Number> getBounds(final Set<String> geocodes) {
return storage.getBounds(geocodes);
}
/** {@link cgData#loadBatchOfStoredGeocodes(boolean, Geopoint, CacheType, int)} */
public SearchResult getBatchOfStoredCaches(final boolean detailedOnly, final Geopoint coords, final CacheType cacheType, final int listId) {
final Set<String> geocodes = storage.loadBatchOfStoredGeocodes(detailedOnly, coords, cacheType, listId);
return new SearchResult(geocodes, getAllStoredCachesCount(true, cacheType, listId));
}
/** {@link cgData#loadHistoryOfSearchedLocations()} */
public List<cgDestination> getHistoryOfSearchedLocations() {
return storage.loadHistoryOfSearchedLocations();
}
public SearchResult getHistoryOfCaches(final boolean detailedOnly, final CacheType cacheType) {
final Set<String> geocodes = storage.loadBatchOfHistoricGeocodes(detailedOnly, cacheType);
return new SearchResult(geocodes, getAllHistoricCachesCount());
}
/** {@link cgData#loadCachedInViewport(long, long, long, long, CacheType)} */
public SearchResult getCachedInViewport(final long centerLat, final long centerLon, final long spanLat, final long spanLon, final CacheType cacheType) {
final Set<String> geocodes = storage.loadCachedInViewport(centerLat, centerLon, spanLat, spanLon, cacheType);
return new SearchResult(geocodes);
}
/** {@link cgData#loadStoredInViewport(long, long, long, long, CacheType)} */
public SearchResult getStoredInViewport(final long centerLat, final long centerLon, final long spanLat, final long spanLon, final CacheType cacheType) {
final Set<String> geocodes = storage.loadStoredInViewport(centerLat, centerLon, spanLat, spanLon, cacheType);
return new SearchResult(geocodes);
}
/** {@link cgData#getAllStoredCachesCount(boolean, CacheType, Integer)} */
public int getAllStoredCachesCount(final boolean detailedOnly, final CacheType cacheType, final Integer list) {
return storage.getAllStoredCachesCount(detailedOnly, cacheType, list);
}
/** {@link cgData#getAllHistoricCachesCount()} */
public int getAllHistoricCachesCount() {
return storage.getAllHistoricCachesCount();
}
/** {@link cgData#moveToList(String, int)} */
public void markStored(String geocode, int listId) {
storage.moveToList(geocode, listId);
}
/** {@link cgData#moveToList(String, int)} */
public void markDropped(String geocode) {
storage.moveToList(geocode, StoredList.TEMPORARY_LIST_ID);
}
/** {@link cgData#markFound(String)} */
public boolean markFound(String geocode) {
return storage.markFound(geocode);
}
/** {@link cgData#clearSearchedDestinations()} */
public boolean clearSearchedDestinations() {
return storage.clearSearchedDestinations();
}
/** {@link cgData#saveSearchedDestination(cgDestination)} */
public void saveSearchedDestination(cgDestination destination) {
storage.saveSearchedDestination(destination);
}
/** {@link cgData#saveWaypoints(String, List, boolean)} */
public boolean saveWaypoints(String geocode, List<cgWaypoint> waypoints, boolean drop) {
return storage.saveWaypoints(geocode, waypoints, drop);
}
public boolean saveOwnWaypoint(int id, String geocode, cgWaypoint waypoint) {
if (storage.saveOwnWaypoint(id, geocode, waypoint)) {
this.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE));
return true;
}
return false;
}
/** {@link cgData#deleteWaypoint(int)} */
public boolean deleteWaypoint(int id) {
return storage.deleteWaypoint(id);
}
public boolean saveTrackable(cgTrackable trackable) {
final List<cgTrackable> list = new ArrayList<cgTrackable>();
list.add(trackable);
return storage.saveInventory("---", list);
}
/** {@link cgData#dropList(int)} **/
public void dropList(int listId) {
storage.dropList(listId);
}
/** {@link cgData#loadLogCounts(String)} */
public Map<LogType, Integer> loadLogCounts(String geocode) {
return storage.loadLogCounts(geocode);
}
/** {@link cgData#loadWaypoint(int)} */
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 setLastCoords(final Geopoint coords) {
lastCoords = coords;
}
public Geopoint getLastCoords() {
return lastCoords;
}
/** {@link cgData#saveLogOffline(String, Date, LogType, String)} */
public boolean saveLogOffline(String geocode, Date date, LogType logtype, String log) {
return storage.saveLogOffline(geocode, date, logtype, log);
}
/** {@link cgData#loadLogOffline(String)} */
public cgLog loadLogOffline(String geocode) {
return storage.loadLogOffline(geocode);
}
/** {@link cgData#clearLogOffline(String)} */
public void clearLogOffline(String geocode) {
storage.clearLogOffline(geocode);
}
/** {@link cgData#setVisitDate(String, long)} */
public void saveVisitDate(String geocode) {
storage.setVisitDate(geocode, System.currentTimeMillis());
}
/** {@link cgData#setVisitDate(String, long)} */
public void clearVisitDate(String geocode) {
storage.setVisitDate(geocode, 0);
}
/** {@link cgData#getLists(Resources)} */
public List<StoredList> getLists() {
return storage.getLists(getResources());
}
/** {@link cgData#getList(int, Resources))} */
public StoredList getList(int id) {
return storage.getList(id, getResources());
}
/** {@link cgData#createList(String)} */
public int createList(String title) {
return storage.createList(title);
}
/** {@link cgData#renameList(int, String)} */
public int renameList(final int listId, final String title) {
return storage.renameList(listId, title);
}
/** {@link cgData#removeList(int)} */
public boolean removeList(int id) {
return storage.removeList(id);
}
/** {@link cgData#removeSearchedDestination(cgDestination)} */
public boolean removeSearchedDestinations(cgDestination destination) {
return storage.removeSearchedDestination(destination);
}
/** {@link cgData#moveToList(String, int)} */
public void moveToList(String geocode, int listId) {
storage.moveToList(geocode, listId);
}
/** {@link cgData#getCacheDescription(String)} */
public String getCacheDescription(String geocode) {
return storage.getCacheDescription(geocode);
}
/** {@link cgData#loadCaches} */
public cgCache loadCache(final String geocode, final EnumSet<LoadFlag> loadFlags) {
return storage.loadCache(geocode, loadFlags);
}
/** {@link cgData#loadCaches} */
public Set<cgCache> loadCaches(final Set<String> geocodes, final EnumSet<LoadFlag> loadFlags) {
return storage.loadCaches(geocodes, loadFlags);
}
/** {@link cgData#saveCache} */
public boolean saveCache(cgCache cache, EnumSet<LoadFlags.SaveFlag> saveFlags) {
return storage.saveCache(cache, saveFlags);
}
/** {@link cgData#removeCache} */
public void removeCache(String geocode, EnumSet<LoadFlags.RemoveFlag> removeFlags) {
storage.removeCache(geocode, removeFlags);
}
/** {@link cgData#removeCaches} */
public void removeCaches(final Set<String> geocodes, EnumSet<LoadFlags.RemoveFlag> removeFlags) {
storage.removeCaches(geocodes, removeFlags);
}
}
|