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
|
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.geopoint.Viewport;
import cgeo.geocaching.utils.IObserver;
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.res.Resources;
import android.os.Handler;
import android.os.Message;
import java.io.File;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
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 volatile GeoDataProvider geo;
private volatile DirectionProvider dir;
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("Cleaning applications cache.");
storage.removeAllFromCache();
}
@Override
public void onTerminate() {
Log.d("Terminating c:geo...");
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 addGeoObserver(final IObserver<? super IGeoData> observer) {
currentGeoObject().addObserver(observer);
}
public void deleteGeoObserver(final IObserver<? super IGeoData> observer) {
currentGeoObject().deleteObserver(observer);
}
private GeoDataProvider currentGeoObject() {
if (geo == null) {
synchronized(this) {
if (geo == null) {
geo = new GeoDataProvider(this);
}
}
}
return geo;
}
public IGeoData currentGeo() {
return currentGeoObject().getMemory();
}
public void addDirectionObserver(final IObserver<? super Float> observer) {
currentDirObject().addObserver(observer);
}
public void deleteDirectionObserver(final IObserver<? super Float> observer) {
currentDirObject().deleteObserver(observer);
}
private DirectionProvider currentDirObject() {
if (dir == null) {
synchronized(this) {
if (dir == null) {
dir = new DirectionProvider(this);
}
}
}
return dir;
}
public boolean storageStatus() {
return storage.status();
}
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;
}
return storage.loadTrackable(geocode);
}
/** {@link cgData#allDetailedThere()} */
public String[] geocodesInCache() {
return storage.allDetailedThere();
}
public Viewport getBounds(String geocode) {
if (geocode == null) {
return null;
}
return getBounds(Collections.singleton(geocode));
}
/** {@link cgData#getBounds(Set)} */
public Viewport 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<Destination> 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(Viewport, CacheType)} */
public SearchResult getCachedInViewport(final Viewport viewport, final CacheType cacheType) {
final Set<String> geocodes = storage.loadCachedInViewport(viewport, cacheType);
return new SearchResult(geocodes);
}
/** {@link cgData#loadStoredInViewport(Viewport, CacheType)} */
public SearchResult getStoredInViewport(final Viewport viewport, final CacheType cacheType) {
final Set<String> geocodes = storage.loadStoredInViewport(viewport, cacheType);
return new SearchResult(geocodes);
}
/** {@link cgData#getAllStoredCachesCount(boolean, CacheType, int)} */
public int getAllStoredCachesCount(final boolean detailedOnly, final CacheType cacheType) {
return storage.getAllStoredCachesCount(detailedOnly, cacheType, 0);
}
/** {@link cgData#getAllStoredCachesCount(boolean, CacheType, int)} */
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(List<cgCache> caches, int listId) {
storage.moveToList(caches, listId);
}
/** {@link cgData#moveToList(String, int)} */
public void markDropped(List<cgCache> caches) {
storage.moveToList(caches, StoredList.TEMPORARY_LIST_ID);
}
/** {@link cgData#clearSearchedDestinations()} */
public boolean clearSearchedDestinations() {
return storage.clearSearchedDestinations();
}
/** {@link cgData#saveSearchedDestination(Destination)} */
public void saveSearchedDestination(Destination destination) {
storage.saveSearchedDestination(destination);
}
/** {@link cgData#saveWaypoints(String, List, boolean)} */
public boolean saveWaypoints(final cgCache cache) {
return storage.saveWaypoints(cache);
}
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) {
return storage.saveTrackable(trackable);
}
/** {@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);
}
/** {@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 LogEntry 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(Destination)} */
public boolean removeSearchedDestinations(Destination destination) {
return storage.removeSearchedDestination(destination);
}
/** {@link cgData#moveToList(String, int)} */
public void moveToList(List<cgCache> caches, int listId) {
storage.moveToList(caches, 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);
}
public Set<cgWaypoint> getWaypointsInViewport(final Viewport viewport, boolean excludeMine, boolean excludeDisabled) {
return storage.loadWaypoints(viewport, excludeMine, excludeDisabled);
}
}
|