aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgeoapplication.java
blob: 5518a4a1ca290ddd586d19e6d473f28b445e197b (plain)
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
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.LogType;
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.EnumSet;
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;
    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.");

        CacheCache.getInstance().removeAll();
    }

    @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 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;
    }

    public boolean isThere(String geocode, String guid, boolean detailed, boolean checkTime) {
        return storage.isThere(geocode, guid, detailed, checkTime);
    }

    public boolean isOffline(String geocode, String guid) {
        return storage.isOffline(geocode, guid);
    }

    public String getGeocode(String guid) {
        return storage.getGeocodeForGuid(guid);
    }

    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 cgCache getCacheByGeocode(String geocode) {
        if (StringUtils.isBlank(geocode)) {
            return null;
        }

        return getCacheByGeocode(geocode, LoadFlags.LOADALL);
    }

    public cgCache getCacheByGeocode(final String geocode, final EnumSet<LoadFlag> loadFlags) {
        cgCache cache = CacheCache.getInstance().getCacheFromCache(geocode);
        if (cache != null) {
            return cache;
        }

        cache = storage.loadCache(geocode, loadFlags);

        if (cache != null && cache.isDetailed() && loadFlags == LoadFlags.LOADALL) {
            // "Store" cache for the next access in the cache
            CacheCache.getInstance().putCacheInCache(cache);
        }

        return cache;
    }

    public cgTrackable getTrackableByGeocode(String geocode) {
        if (StringUtils.isBlank(geocode)) {
            return null;
        }

        cgTrackable trackable = null;
        trackable = storage.loadTrackable(geocode);

        return trackable;
    }

    @SuppressWarnings("static-method")
    public void removeCacheFromCache(final String geocode) {
        CacheCache.getInstance().removeCacheFromCache(geocode);
    }

    @SuppressWarnings("static-method")
    public void putCacheInCache(final cgCache cache) {
        CacheCache.getInstance().putCacheInCache(cache);
    }

    public String[] geocodesInCache() {
        return storage.allDetailedThere();
    }

    public List<Number> getBounds(String geocode) {
        if (geocode == null) {
            return null;
        }

        List<String> geocodeList = new ArrayList<String>();
        geocodeList.add(geocode);

        return getBounds(geocodeList);
    }

    public List<Number> getBounds(final SearchResult search) {
        if (search == null) {
            return null;
        }

        return getBounds(search.getGeocodes());
    }

    public List<Number> getBounds(final List<String> geocodes) {
        if (CollectionUtils.isEmpty(geocodes)) {
            return null;
        }

        return storage.getBounds(geocodes.toArray());
    }

    public cgCache getCache(final SearchResult search) {
        if (search == null) {
            return null;
        }

        final List<String> geocodeList = search.getGeocodes();

        return getCacheByGeocode(geocodeList.get(0), LoadFlags.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 SearchResult search, final boolean loadWaypoints) {
        return getCaches(search, null, null, null, null, loadWaypoints ? EnumSet.of(LoadFlag.LOADWAYPOINTS, LoadFlag.LOADOFFLINELOG) : EnumSet.of(LoadFlag.LOADOFFLINELOG));
    }

    public List<cgCache> getCaches(final SearchResult search, Long centerLat, Long centerLon, Long spanLat, Long spanLon) {
        return getCaches(search, centerLat, centerLon, spanLat, spanLon, EnumSet.of(LoadFlag.LOADWAYPOINTS, LoadFlag.LOADOFFLINELOG));
    }

    public List<cgCache> getCaches(final SearchResult search, final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final EnumSet<LoadFlag> loadFlags) {
        if (search == null) {
            List<cgCache> cachesOut = new ArrayList<cgCache>();

            final List<cgCache> cachesPre = storage.loadCaches(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 = storage.loadCaches(geocodeList.toArray(), null, null, null, null, loadFlags);
        if (cachesPre != null) {
            cachesOut.addAll(cachesPre);
        }

        return cachesOut;
    }

    public SearchResult getBatchOfStoredCaches(final boolean detailedOnly, final Geopoint coords, final CacheType cacheType, final int list) {
        final List<String> geocodes = storage.loadBatchOfStoredGeocodes(detailedOnly, coords, cacheType, list);
        return new SearchResult(geocodes);
    }

    public List<cgDestination> getHistoryOfSearchedLocations() {
        return storage.loadHistoryOfSearchedLocations();
    }

    public SearchResult getHistoryOfCaches(final boolean detailedOnly, final CacheType cacheType) {
        final List<String> geocodes = storage.loadBatchOfHistoricGeocodes(detailedOnly, cacheType);
        return new SearchResult(geocodes);
    }

    public SearchResult getCachedInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) {
        final List<String> geocodes = storage.getCachedInViewport(centerLat, centerLon, spanLat, spanLon, cacheType);
        return new SearchResult(geocodes);
    }

    public SearchResult getStoredInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) {
        final List<String> geocodes = storage.getStoredInViewport(centerLat, centerLon, spanLat, spanLon, cacheType);
        return new SearchResult(geocodes);
    }

    public int getAllStoredCachesCount(final boolean detailedOnly, final CacheType cacheType, final Integer list) {
        return storage.getAllStoredCachesCount(detailedOnly, cacheType, list);
    }

    public int getAllHistoricCachesCount() {
        return storage.getAllHistoricCachesCount();
    }

    public void markStored(String geocode, int listId) {
        storage.markStored(geocode, listId);
    }

    public boolean markDropped(String geocode) {
        return storage.markDropped(geocode);
    }

    public boolean markFound(String geocode) {
        return storage.markFound(geocode);
    }

    public boolean clearSearchedDestinations() {
        return storage.clearSearchedDestinations();
    }

    public boolean saveSearchedDestination(cgDestination destination) {
        return storage.saveSearchedDestination(destination);
    }

    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)) {
            removeCacheFromCache(geocode);
            return true;
        }
        return false;
    }

    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);
    }

    public void addSearch(final List<cgCache> cacheList, final int listId) {
        if (CollectionUtils.isEmpty(cacheList)) {
            return;
        }

        for (final cgCache cache : cacheList) {
            cache.setListId(listId);
            storeWithMerge(cache, false);
        }
    }

    public boolean addCacheToSearch(SearchResult search, cgCache cache) {
        if (search == null || cache == null) {
            return false;
        }

        final boolean status = storeWithMerge(cache, cache.getListId() >= 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(), LoadFlags.LOADALL);
            cache.gatherMissingFrom(oldCache);
        }
        return storage.saveCache(cache);
    }

    public void dropStored(int listId) {
        storage.dropStored(listId);
    }

    /**
     * {@link cgData#dropCaches(List)}
     */
    public void dropCaches(final List<String> geocodes) {
        storage.dropCaches(geocodes);
    }

    public List<cgTrackable> loadInventory(String geocode) {
        return storage.loadInventory(geocode);
    }

    public Map<LogType, 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 setLastCoords(final Geopoint coords) {
        lastCoords = coords;
    }

    public Geopoint getLastCoords() {
        return lastCoords;
    }

    public boolean saveLogOffline(String geocode, Date date, LogType 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<StoredList> getLists() {
        return storage.getLists(getResources());
    }

    public StoredList 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);
    }
}