aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgCache.java
blob: af3def13deeb57959148380c45a5803e389d8623 (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
package cgeo.geocaching;

import cgeo.geocaching.activity.IAbstractActivity;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.IConnector;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.geopoint.Geopoint;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.text.Spannable;
import android.util.Log;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Internal c:geo representation of a "cache"
 */
public class cgCache implements ICache {

    public Long updated = null;
    public Long detailedUpdate = null;
    public Long visitedDate = null;
    public Integer reason = 0;
    public Boolean detailed = false;
    /**
     * Code of the cache like GCABCD
     */
    public String geocode = "";
    public String cacheId = "";
    public String guid = "";
    public String type = "";
    public String name = "";
    public Spannable nameSp = null;
    public String owner = "";
    public String ownerReal = "";
    public Date hidden = null;
    public String hint = "";
    public CacheSize size = null;
    public Float difficulty = Float.valueOf(0);
    public Float terrain = Float.valueOf(0);
    public Float direction = null;
    public Float distance = null;
    public String latlon = "";
    public String location = "";
    public Geopoint coords = null;
    public boolean reliableLatLon = false;
    public Double elevation = null;
    public String personalNote = null;
    public String shortdesc = "";
    public String description = "";
    public boolean disabled = false;
    public boolean archived = false;
    public boolean members = false;
    public boolean found = false;
    public boolean favourite = false;
    public boolean own = false;
    public Integer favouriteCnt = null;
    public Float rating = null;
    public Integer votes = null;
    public Float myVote = null;
    public int inventoryItems = 0;
    public boolean onWatchlist = false;
    public List<String> attributes = null;
    public List<cgWaypoint> waypoints = null;
    public ArrayList<cgImage> spoilers = null;
    public List<cgLog> logs = null;
    public List<cgTrackable> inventory = null;
    public Map<Integer, Integer> logCounts = new HashMap<Integer, Integer>();
    public boolean logOffline = false;
    // temporary values
    public boolean statusChecked = false;
    public boolean statusCheckedView = false;
    public String directionImg = null;

    /**
     * Gather missing information from another cache object.
     *
     * @param other
     *            the other version, or null if non-existent
     */
    public void gatherMissingFrom(final cgCache other) {
        if (other == null) {
            return;
        }

        updated = System.currentTimeMillis();
        if (detailed == false && other.detailed) {
            detailed = true;
            detailedUpdate = updated;
        }

        if (visitedDate == null || visitedDate == 0) {
            visitedDate = other.visitedDate;
        }
        if (reason == null || reason == 0) {
            reason = other.reason;
        }
        if (StringUtils.isBlank(geocode)) {
            geocode = other.geocode;
        }
        if (StringUtils.isBlank(cacheId)) {
            cacheId = other.cacheId;
        }
        if (StringUtils.isBlank(guid)) {
            guid = other.guid;
        }
        if (StringUtils.isBlank(type)) {
            type = other.type;
        }
        if (StringUtils.isBlank(name)) {
            name = other.name;
        }
        if (StringUtils.isBlank(nameSp)) {
            nameSp = other.nameSp;
        }
        if (StringUtils.isBlank(owner)) {
            owner = other.owner;
        }
        if (StringUtils.isBlank(ownerReal)) {
            ownerReal = other.ownerReal;
        }
        if (hidden == null) {
            hidden = other.hidden;
        }
        if (StringUtils.isBlank(hint)) {
            hint = other.hint;
        }
        if (size == null) {
            size = other.size;
        }
        if (difficulty == null || difficulty == 0) {
            difficulty = other.difficulty;
        }
        if (terrain == null || terrain == 0) {
            terrain = other.terrain;
        }
        if (direction == null) {
            direction = other.direction;
        }
        if (distance == null) {
            distance = other.distance;
        }
        if (StringUtils.isBlank(latlon)) {
            latlon = other.latlon;
        }
        if (StringUtils.isBlank(location)) {
            location = other.location;
        }
        if (coords == null) {
            coords = other.coords;
        }
        if (elevation == null) {
            elevation = other.elevation;
        }
        if (StringUtils.isBlank(personalNote)) {
            personalNote = other.personalNote;
        }
        if (StringUtils.isBlank(shortdesc)) {
            shortdesc = other.shortdesc;
        }
        if (StringUtils.isBlank(description)) {
            description = other.description;
        }
        if (favouriteCnt == null) {
            favouriteCnt = other.favouriteCnt;
        }
        if (rating == null) {
            rating = other.rating;
        }
        if (votes == null) {
            votes = other.votes;
        }
        if (myVote == null) {
            myVote = other.myVote;
        }
        if (attributes == null) {
            attributes = other.attributes;
        }
        if (waypoints == null) {
            waypoints = other.waypoints;
        }
        else {
            cgWaypoint.mergeWayPoints(waypoints, other.waypoints);
        }
        if (spoilers == null) {
            spoilers = other.spoilers;
        }
        if (inventory == null) {
            // If inventoryItems is 0, it can mean both
            // "don't know" or "0 items". Since we cannot distinguish
            // them here, only populate inventoryItems from
            // old data when we have to do it for inventory.
            inventory = other.inventory;
            inventoryItems = other.inventoryItems;
        }
        if (logs == null || logs.isEmpty()) { // keep last known logs if none
            logs = other.logs;
        }
    }

    public boolean hasTrackables() {
        return inventoryItems > 0;
    }

    public boolean canBeAddedToCalendar() {
        // is event type?
        if (!isEventCache()) {
            return false;
        }
        // has event date set?
        if (hidden == null) {
            return false;
        }
        // is in future?
        Date today = new Date();
        today.setHours(0);
        today.setMinutes(0);
        today.setSeconds(0);
        if (hidden.compareTo(today) <= 0) {
            return false;
        }
        return true;
    }

    /**
     * checks if a page contains the guid of a cache
     *
     * @param cache
     *            the cache to look for
     * @param page
     *            the page to search in
     *
     * @return true: page contains guid of cache, false: otherwise
     */
    boolean isGuidContainedInPage(final String page) {
        // check if the guid of the cache is anywhere in the page
        if (StringUtils.isBlank(guid)) {
            return false;
        }
        Pattern patternOk = Pattern.compile(guid, Pattern.CASE_INSENSITIVE);
        Matcher matcherOk = patternOk.matcher(page);
        if (matcherOk.find()) {
            Log.i(cgSettings.tag, "cgCache.isGuidContainedInPage: guid '" + guid + "' found");
            return true;
        } else {
            Log.i(cgSettings.tag, "cgCache.isGuidContainedInPage: guid '" + guid + "' not found");
            return false;
        }
    }

    public boolean isEventCache() {
        return "event".equalsIgnoreCase(type) || "mega".equalsIgnoreCase(type) || "cito".equalsIgnoreCase(type);
    }

    public boolean logVisit(IAbstractActivity fromActivity) {
        if (StringUtils.isBlank(cacheId)) {
            fromActivity.showToast(((Activity) fromActivity).getResources().getString(R.string.err_cannot_log_visit));
            return true;
        }
        Intent logVisitIntent = new Intent((Activity) fromActivity, cgeovisit.class);
        logVisitIntent.putExtra(cgeovisit.EXTRAS_ID, cacheId);
        logVisitIntent.putExtra(cgeovisit.EXTRAS_GEOCODE, geocode.toUpperCase());
        logVisitIntent.putExtra(cgeovisit.EXTRAS_FOUND, found);

        ((Activity) fromActivity).startActivity(logVisitIntent);

        return true;
    }

    public boolean logOffline(final IAbstractActivity fromActivity, final int logType, final cgSettings settings, final cgBase base) {
        String log = "";
        if (StringUtils.isNotBlank(settings.getSignature())
                && settings.signatureAutoinsert) {
            log = LogTemplateProvider.applyTemplates(settings.getSignature(), base, true);
        }
        logOffline(fromActivity, log, Calendar.getInstance(), logType);
        return true;
    }

    void logOffline(final IAbstractActivity fromActivity, final String log, Calendar date, final int logType) {
        if (logType <= 0) {
            return;
        }
        cgeoapplication app = (cgeoapplication) ((Activity) fromActivity).getApplication();
        final boolean status = app.saveLogOffline(geocode, date.getTime(), logType, log);

        Resources res = ((Activity) fromActivity).getResources();
        if (status) {
            fromActivity.showToast(res.getString(R.string.info_log_saved));
            app.saveVisitDate(geocode);
        } else {
            fromActivity.showToast(res.getString(R.string.err_log_post_failed));
        }
    }

    public List<Integer> getPossibleLogTypes(cgSettings settings) {
        boolean isOwner = owner != null && owner.equalsIgnoreCase(settings.getUsername());
        List<Integer> types = new ArrayList<Integer>();
        if ("event".equals(type) || "mega".equals(type) || "cito".equals(type) || "lostfound".equals(type)) {
            types.add(cgBase.LOG_WILL_ATTEND);
            types.add(cgBase.LOG_NOTE);
            types.add(cgBase.LOG_ATTENDED);
            types.add(cgBase.LOG_NEEDS_ARCHIVE);
            if (isOwner) {
                types.add(cgBase.LOG_ANNOUNCEMENT);
            }
        } else if ("webcam".equals(type)) {
            types.add(cgBase.LOG_WEBCAM_PHOTO_TAKEN);
            types.add(cgBase.LOG_DIDNT_FIND_IT);
            types.add(cgBase.LOG_NOTE);
            types.add(cgBase.LOG_NEEDS_ARCHIVE);
            types.add(cgBase.LOG_NEEDS_MAINTENANCE);
        } else {
            types.add(cgBase.LOG_FOUND_IT);
            types.add(cgBase.LOG_DIDNT_FIND_IT);
            types.add(cgBase.LOG_NOTE);
            types.add(cgBase.LOG_NEEDS_ARCHIVE);
            types.add(cgBase.LOG_NEEDS_MAINTENANCE);
        }
        if (isOwner) {
            types.add(cgBase.LOG_OWNER_MAINTENANCE);
            types.add(cgBase.LOG_TEMP_DISABLE_LISTING);
            types.add(cgBase.LOG_ENABLE_LISTING);
            types.add(cgBase.LOG_ARCHIVE);
            types.remove(Integer.valueOf(cgBase.LOG_UPDATE_COORDINATES));
        }
        return types;
    }

    public void openInBrowser(Activity fromActivity) {
        fromActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getCacheUrl())));
    }

    private String getCacheUrl() {
        return getConnector().getCacheUrl(this);
    }

    private IConnector getConnector() {
        return ConnectorFactory.getConnector(this);
    }

    public boolean canOpenInBrowser() {
        return getCacheUrl() != null;
    }

    public boolean supportsRefresh() {
        return getConnector().supportsRefreshCache(this);
    }

    public boolean supportsWatchList() {
        return getConnector().supportsWatchList();
    }

    public boolean supportsLogging() {
        return getConnector().supportsLogging();
    }

    @Override
    public Float getDifficulty() {
        return difficulty;
    }

    @Override
    public String getGeocode() {
        return geocode;
    }

    @Override
    public String getLatitude() {
        return coords != null ? cgBase.formatLatitude(coords.getLatitude(), true) : null;
    }

    @Override
    public String getLongitude() {
        return coords != null ? cgBase.formatLongitude(coords.getLongitude(), true) : null;
    }

    @Override
    public String getOwner() {
        return owner;
    }

    @Override
    public CacheSize getSize() {
        return size;
    }

    @Override
    public Float getTerrain() {
        return terrain;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public boolean isArchived() {
        return archived;
    }

    @Override
    public boolean isDisabled() {
        return disabled;
    }

    @Override
    public boolean isMembersOnly() {
        return members;
    }

    @Override
    public boolean isOwn() {
        return own;
    }

    @Override
    public String getOwnerReal() {
        return ownerReal;
    }

    @Override
    public String getHint() {
        return hint;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public String getShortDescription() {
        return shortdesc;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getCacheId() {
        return cacheId;
    }

    @Override
    public String getGuid() {
        return guid;
    }

    @Override
    public String getLocation() {
        return location;
    }

    @Override
    public String getPersonalNote() {
        return personalNote;
    }

    public boolean supportsUserActions() {
        return getConnector().supportsUserActions();
    }

    public boolean supportsCachesAround() {
        return getConnector().supportsCachesAround();
    }

    public void shareCache(Activity fromActivity, Resources res) {
        if (geocode == null) {
            return;
        }

        StringBuilder subject = new StringBuilder("Geocache ");
        subject.append(geocode.toUpperCase());
        if (StringUtils.isNotBlank(name)) {
            subject.append(" - ").append(name);
        }

        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, subject.toString());
        intent.putExtra(Intent.EXTRA_TEXT, getUrl());

        fromActivity.startActivity(Intent.createChooser(intent, res.getText(R.string.action_bar_share_title)));
    }

    public String getUrl() {
        return getConnector().getCacheUrl(this);
    }

}