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
|
package cgeo.geocaching.connector.oc;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.Image;
import cgeo.geocaching.LogEntry;
import cgeo.geocaching.R;
import cgeo.geocaching.Settings;
import cgeo.geocaching.Waypoint;
import cgeo.geocaching.cgData;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.IConnector;
import cgeo.geocaching.connector.LogResult;
import cgeo.geocaching.connector.gc.GCConnector;
import cgeo.geocaching.enumerations.CacheAttribute;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter;
import cgeo.geocaching.geopoint.Viewport;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.OAuth;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.Uri;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
final public class OkapiClient {
private static final SimpleDateFormat logDateFormat;
static {
logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ", Locale.US);
logDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
}
private static final String CACHE_ATTRNAMES = "attrnames";
private static final String WPT_LOCATION = "location";
private static final String WPT_DESCRIPTION = "description";
private static final String WPT_TYPE = "type";
private static final String WPT_NAME = "name";
private static final String CACHE_IS_WATCHED = "is_watched";
private static final String CACHE_WPTS = "alt_wpts";
private static final String CACHE_STATUS_ARCHIVED = "Archived";
private static final String CACHE_STATUS_DISABLED = "Temporarily unavailable";
private static final String CACHE_IS_FOUND = "is_found";
private static final String CACHE_SIZE = "size";
private static final String CACHE_VOTES = "rating_votes";
private static final String CACHE_NOTFOUNDS = "notfounds";
private static final String CACHE_FOUNDS = "founds";
private static final String CACHE_HIDDEN = "date_hidden";
private static final String CACHE_LATEST_LOGS = "latest_logs";
private static final String CACHE_IMAGE_URL = "url";
private static final String CACHE_IMAGE_CAPTION = "caption";
private static final String CACHE_IMAGE_IS_SPOILER = "is_spoiler";
private static final String CACHE_IMAGES = "images";
private static final String CACHE_HINT = "hint";
private static final String CACHE_DESCRIPTION = "description";
private static final String CACHE_RECOMMENDATIONS = "recommendations";
private static final String CACHE_RATING = "rating";
private static final String CACHE_TERRAIN = "terrain";
private static final String CACHE_DIFFICULTY = "difficulty";
private static final String CACHE_OWNER = "owner";
private static final String CACHE_STATUS = "status";
private static final String CACHE_TYPE = "type";
private static final String CACHE_LOCATION = "location";
private static final String CACHE_NAME = "name";
private static final String CACHE_CODE = "code";
private static final String LOG_TYPE = "type";
private static final String LOG_COMMENT = "comment";
private static final String LOG_DATE = "date";
private static final String LOG_USER = "user";
private static final String USER_USERNAME = "username";
private static final String SERVICE_CACHE = "/okapi/services/caches/geocache";
private static final String SERVICE_CACHE_CORE_FIELDS = "code|name|location|type|status|difficulty|terrain|size|is_found";
private static final String SERVICE_CACHE_FIELDS = SERVICE_CACHE_CORE_FIELDS + "|owner|founds|notfounds|rating|rating_votes|recommendations|description|hint|images|latest_logs|date_hidden|attribution_note|alt_wpts|is_watched|attrnames|gc_code";
private static final String SERVICE_SEARCH_AND_RETRIEVE = "/okapi/services/caches/shortcuts/search_and_retrieve";
private static final String METHOD_SEARCH_NEAREST = "services/caches/search/nearest";
private static final String METHOD_SEARCH_BBOX = "services/caches/search/bbox";
private static final String METHOD_RETRIEVE_CACHES = "services/caches/geocaches";
private static final String SERVICE_MARK_CACHE = "/okapi/services/caches/mark";
private static final String SERVICE_SUBMIT_LOG = "/okapi/services/logs/submit";
public static Geocache getCache(final String geoCode) {
final Parameters params = new Parameters("cache_code", geoCode, "fields", SERVICE_CACHE_FIELDS, "attribution_append", "none");
final JSONObject data = request(ConnectorFactory.getConnector(geoCode), SERVICE_CACHE, params);
if (data == null) {
return null;
}
return parseCache(data);
}
public static List<Geocache> getCachesAround(final Geopoint center, IConnector connector) {
String centerString = GeopointFormatter.format(GeopointFormatter.Format.LAT_DECDEGREE_RAW, center) + "|" + GeopointFormatter.format(GeopointFormatter.Format.LON_DECDEGREE_RAW, center);
final Parameters params = new Parameters("search_method", METHOD_SEARCH_NEAREST);
final Map<String, String> valueMap = new LinkedHashMap<String, String>();
valueMap.put("center", centerString);
valueMap.put("limit", "20");
addFilterParams(valueMap);
params.add("search_params", new JSONObject(valueMap).toString());
addRetrieveParams(params);
final JSONObject data = request(connector, SERVICE_SEARCH_AND_RETRIEVE, params);
if (data == null) {
return Collections.emptyList();
}
return parseCaches(data);
}
public static List<Geocache> getCachesBBox(final Viewport viewport, IConnector connector) {
if (viewport.getLatitudeSpan() == 0 || viewport.getLongitudeSpan() == 0) {
return Collections.emptyList();
}
String bboxString = GeopointFormatter.format(GeopointFormatter.Format.LAT_DECDEGREE_RAW, viewport.bottomLeft)
+ "|" + GeopointFormatter.format(GeopointFormatter.Format.LON_DECDEGREE_RAW, viewport.bottomLeft)
+ "|" + GeopointFormatter.format(GeopointFormatter.Format.LAT_DECDEGREE_RAW, viewport.topRight)
+ "|" + GeopointFormatter.format(GeopointFormatter.Format.LON_DECDEGREE_RAW, viewport.topRight);
final Parameters params = new Parameters("search_method", METHOD_SEARCH_BBOX);
final Map<String, String> valueMap = new LinkedHashMap<String, String>();
valueMap.put("bbox", bboxString);
addFilterParams(valueMap);
params.add("search_params", new JSONObject(valueMap).toString());
addRetrieveParams(params);
final JSONObject data = request(connector, SERVICE_SEARCH_AND_RETRIEVE, params);
if (data == null) {
return Collections.emptyList();
}
return parseCaches(data);
}
public static boolean setWatchState(final Geocache cache, final boolean watched, IConnector connector) {
final Parameters params = new Parameters("cache_code", cache.getGeocode());
params.add("watched", watched ? "true" : "false");
final JSONObject data = request(connector, SERVICE_MARK_CACHE, params);
if (data == null) {
return false;
}
cache.setOnWatchlist(watched);
return true;
}
public static LogResult postLog(final Geocache cache, LogType logType, Calendar date, String log, IConnector connector) {
final Parameters params = new Parameters("cache_code", cache.getGeocode());
params.add("logtype", logType.oc_type);
params.add("comment", log);
params.add("comment_format", "plaintext");
params.add("when", logDateFormat.format(date.getTime()));
if (logType.equals(LogType.NEEDS_MAINTENANCE)) {
params.add("needs_maintenance", "true");
}
final JSONObject data = request(connector, SERVICE_SUBMIT_LOG, params);
if (data == null) {
return new LogResult(StatusCode.LOG_POST_ERROR, "");
}
try {
if (data.getBoolean("success")) {
return new LogResult(StatusCode.NO_ERROR, data.getString("log_uuid"));
}
return new LogResult(StatusCode.LOG_POST_ERROR, "");
} catch (JSONException e) {
Log.e("OkapiClient.postLog", e);
}
return new LogResult(StatusCode.LOG_POST_ERROR, "");
}
private static List<Geocache> parseCaches(final JSONObject response) {
try {
// Check for empty result
final String result = response.getString("results");
if (StringUtils.isBlank(result) || StringUtils.equals(result, "[]")) {
return Collections.emptyList();
}
// Get and iterate result list
final JSONObject cachesResponse = response.getJSONObject("results");
if (cachesResponse != null) {
List<Geocache> caches = new ArrayList<Geocache>(cachesResponse.length());
@SuppressWarnings("unchecked")
Iterator<String> keys = cachesResponse.keys();
while (keys.hasNext()) {
String key = keys.next();
Geocache cache = parseSmallCache(cachesResponse.getJSONObject(key));
if (cache != null) {
caches.add(cache);
}
}
return caches;
}
} catch (JSONException e) {
Log.e("OkapiClient.parseCachesResult", e);
}
return Collections.emptyList();
}
private static Geocache parseSmallCache(final JSONObject response) {
final Geocache cache = new Geocache();
cache.setReliableLatLon(true);
try {
parseCoreCache(response, cache);
cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_CACHE));
} catch (JSONException e) {
Log.e("OkapiClient.parseSmallCache", e);
}
return cache;
}
private static Geocache parseCache(final JSONObject response) {
final Geocache cache = new Geocache();
cache.setReliableLatLon(true);
try {
parseCoreCache(response, cache);
// not used: url
final JSONObject owner = response.getJSONObject(CACHE_OWNER);
cache.setOwnerDisplayName(parseUser(owner));
cache.getLogCounts().put(LogType.FOUND_IT, response.getInt(CACHE_FOUNDS));
cache.getLogCounts().put(LogType.DIDNT_FIND_IT, response.getInt(CACHE_NOTFOUNDS));
if (!response.isNull(CACHE_RATING)) {
cache.setRating((float) response.getDouble(CACHE_RATING));
}
cache.setVotes(response.getInt(CACHE_VOTES));
cache.setFavoritePoints(response.getInt(CACHE_RECOMMENDATIONS));
// not used: req_password
// Prepend gc-link to description if available
StringBuilder description = new StringBuilder(500);
if (!response.isNull("gc_code")) {
String gccode = response.getString("gc_code");
description.append(cgeoapplication.getInstance().getResources()
.getString(R.string.cache_listed_on, GCConnector.getInstance().getName()))
.append(": <a href=\"http://coord.info/")
.append(gccode)
.append("\">")
.append(gccode)
.append("</a><br /><br />");
}
description.append(response.getString(CACHE_DESCRIPTION));
cache.setDescription(description.toString());
cache.setHint(response.getString(CACHE_HINT));
// not used: hints
final JSONArray images = response.getJSONArray(CACHE_IMAGES);
if (images != null) {
for (int i = 0; i < images.length(); i++) {
JSONObject imageResponse = images.getJSONObject(i);
if (imageResponse.getBoolean(CACHE_IMAGE_IS_SPOILER)) {
final String title = imageResponse.getString(CACHE_IMAGE_CAPTION);
final String url = absoluteUrl(imageResponse.getString(CACHE_IMAGE_URL), cache.getGeocode());
cache.addSpoiler(new Image(url, title));
}
}
}
cache.setAttributes(parseAttributes(response.getJSONArray(CACHE_ATTRNAMES)));
cache.setLogs(parseLogs(response.getJSONArray(CACHE_LATEST_LOGS)));
cache.setHidden(parseDate(response.getString(CACHE_HIDDEN)));
//TODO: Store license per cache
//cache.setLicense(response.getString("attribution_note"));
cache.setWaypoints(parseWaypoints(response.getJSONArray(CACHE_WPTS)), false);
cache.setOnWatchlist(response.getBoolean(CACHE_IS_WATCHED));
cache.setDetailedUpdatedNow();
// save full detailed caches
cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));
} catch (JSONException e) {
Log.e("OkapiClient.parseCache", e);
}
return cache;
}
private static void parseCoreCache(final JSONObject response, final Geocache cache) throws JSONException {
cache.setGeocode(response.getString(CACHE_CODE));
cache.setName(response.getString(CACHE_NAME));
// not used: names
setLocation(cache, response.getString(CACHE_LOCATION));
cache.setType(getCacheType(response.getString(CACHE_TYPE)));
final String status = response.getString(CACHE_STATUS);
cache.setDisabled(status.equalsIgnoreCase(CACHE_STATUS_DISABLED));
cache.setArchived(status.equalsIgnoreCase(CACHE_STATUS_ARCHIVED));
cache.setSize(getCacheSize(response));
cache.setDifficulty((float) response.getDouble(CACHE_DIFFICULTY));
cache.setTerrain((float) response.getDouble(CACHE_TERRAIN));
cache.setFound(response.getBoolean(CACHE_IS_FOUND));
}
private static String absoluteUrl(String url, String geocode) {
final Uri uri = Uri.parse(url);
if (!uri.isAbsolute()) {
final IConnector connector = ConnectorFactory.getConnector(geocode);
final String host = connector.getHost();
if (StringUtils.isNotBlank(host)) {
return "http://" + host + "/" + url;
}
}
return url;
}
private static String parseUser(JSONObject user) throws JSONException {
return user.getString(USER_USERNAME);
}
private static List<LogEntry> parseLogs(JSONArray logsJSON) {
List<LogEntry> result = null;
for (int i = 0; i < logsJSON.length(); i++) {
try {
JSONObject logResponse = logsJSON.getJSONObject(i);
LogEntry log = new LogEntry(
parseUser(logResponse.getJSONObject(LOG_USER)),
parseDate(logResponse.getString(LOG_DATE)).getTime(),
parseLogType(logResponse.getString(LOG_TYPE)),
logResponse.getString(LOG_COMMENT).trim());
if (result == null) {
result = new ArrayList<LogEntry>();
}
result.add(log);
} catch (JSONException e) {
Log.e("OkapiClient.parseLogs", e);
}
}
return result;
}
private static List<Waypoint> parseWaypoints(JSONArray wptsJson) {
List<Waypoint> result = null;
for (int i = 0; i < wptsJson.length(); i++) {
try {
JSONObject wptResponse = wptsJson.getJSONObject(i);
Waypoint wpt = new Waypoint(wptResponse.getString(WPT_NAME),
parseWptType(wptResponse.getString(WPT_TYPE)),
false);
wpt.setNote(wptResponse.getString(WPT_DESCRIPTION));
Geopoint pt = parseCoords(wptResponse.getString(WPT_LOCATION));
if (pt != null) {
wpt.setCoords(pt);
}
if (result == null) {
result = new ArrayList<Waypoint>();
}
result.add(wpt);
} catch (JSONException e) {
Log.e("OkapiClient.parseWaypoints", e);
}
}
return result;
}
private static LogType parseLogType(String logType) {
if ("Found it".equalsIgnoreCase(logType)) {
return LogType.FOUND_IT;
}
if ("Didn't find it".equalsIgnoreCase(logType)) {
return LogType.DIDNT_FIND_IT;
}
return LogType.NOTE;
}
private static WaypointType parseWptType(String wptType) {
if ("parking".equalsIgnoreCase(wptType)) {
return WaypointType.PARKING;
}
if ("path".equalsIgnoreCase(wptType)) {
return WaypointType.TRAILHEAD;
}
if ("stage".equalsIgnoreCase(wptType)) {
return WaypointType.STAGE;
}
if ("physical-stage".equalsIgnoreCase(wptType)) {
return WaypointType.STAGE;
}
if ("virtual-stage".equalsIgnoreCase(wptType)) {
return WaypointType.PUZZLE;
}
if ("final".equalsIgnoreCase(wptType)) {
return WaypointType.FINAL;
}
if ("poi".equalsIgnoreCase(wptType)) {
return WaypointType.TRAILHEAD;
}
return WaypointType.WAYPOINT;
}
private static Date parseDate(final String date) {
final SimpleDateFormat ISO8601DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
final String strippedDate = date.replaceAll("\\+0([0-9]){1}\\:00", "+0$100");
try {
return ISO8601DATEFORMAT.parse(strippedDate);
} catch (ParseException e) {
Log.e("OkapiClient.parseDate", e);
}
return null;
}
private static Geopoint parseCoords(final String location) {
final String latitude = StringUtils.substringBefore(location, "|");
final String longitude = StringUtils.substringAfter(location, "|");
if (StringUtils.isNotBlank(latitude) && StringUtils.isNotBlank(longitude)) {
return new Geopoint(latitude, longitude);
}
return null;
}
private static List<String> parseAttributes(JSONArray nameList) {
List<String> result = new ArrayList<String>();
for (int i = 0; i < nameList.length(); i++) {
try {
String name = nameList.getString(i);
CacheAttribute attr = CacheAttribute.getByOcId(AttributeParser.getOcDeId(name));
if (attr != null) {
result.add(attr.rawName);
}
} catch (JSONException e) {
Log.e("OkapiClient.parseAttributes", e);
}
}
return result;
}
private static void setLocation(final Geocache cache, final String location) {
final String latitude = StringUtils.substringBefore(location, "|");
final String longitude = StringUtils.substringAfter(location, "|");
cache.setCoords(new Geopoint(latitude, longitude));
}
private static CacheSize getCacheSize(final JSONObject response) {
if (response.isNull(CACHE_SIZE)) {
return CacheSize.NOT_CHOSEN;
}
double size = 0;
try {
size = response.getDouble(CACHE_SIZE);
} catch (JSONException e) {
Log.e("OkapiClient.getCacheSize", e);
}
switch ((int) Math.round(size)) {
case 1:
return CacheSize.MICRO;
case 2:
return CacheSize.SMALL;
case 3:
return CacheSize.REGULAR;
case 4:
return CacheSize.LARGE;
case 5:
return CacheSize.LARGE;
default:
break;
}
return CacheSize.NOT_CHOSEN;
}
private static CacheType getCacheType(final String cacheType) {
if (cacheType.equalsIgnoreCase("Traditional")) {
return CacheType.TRADITIONAL;
}
if (cacheType.equalsIgnoreCase("Multi")) {
return CacheType.MULTI;
}
if (cacheType.equalsIgnoreCase("Quiz")) {
return CacheType.MYSTERY;
}
if (cacheType.equalsIgnoreCase("Virtual")) {
return CacheType.VIRTUAL;
}
if (cacheType.equalsIgnoreCase("Event")) {
return CacheType.EVENT;
}
if (cacheType.equalsIgnoreCase("Webcam")) {
return CacheType.WEBCAM;
}
if (cacheType.equalsIgnoreCase("Math/Physics")) {
return CacheType.MYSTERY;
}
if (cacheType.equalsIgnoreCase("Drive-In")) {
return CacheType.TRADITIONAL;
}
return CacheType.UNKNOWN;
}
private static JSONObject request(final IConnector connector, final String service, final Parameters params) {
if (connector == null) {
return null;
}
if (!(connector instanceof OCApiConnector)) {
return null;
}
final String host = connector.getHost();
if (StringUtils.isBlank(host)) {
return null;
}
params.add("langpref", getPreferredLanguage());
OAuth.signOAuth(host, service, "GET", false, params, Settings.getOCDETokenPublic(), Settings.getOCDETokenSecret(), ((OCApiLiveConnector) connector).getCK(), ((OCApiLiveConnector) connector).getCS());
final String uri = "http://" + host + service;
return Network.requestJSON(uri, params);
}
private static String getPreferredLanguage() {
final String code = Locale.getDefault().getCountry();
if (StringUtils.isNotBlank(code)) {
return StringUtils.lowerCase(code) + "|en";
}
return "en";
}
private static void addFilterParams(final Map<String, String> valueMap) {
if (!Settings.isExcludeDisabledCaches()) {
valueMap.put("status", "Available|Temporarily unavailable");
}
if (Settings.isExcludeMyCaches()) {
valueMap.put("exclude_my_own", "true");
valueMap.put("found_status", "notfound_only");
}
if (Settings.getCacheType() != CacheType.ALL) {
valueMap.put("type", getFilterFromType(Settings.getCacheType()));
}
}
private static void addRetrieveParams(final Parameters params) {
params.add("retr_method", METHOD_RETRIEVE_CACHES);
params.add("retr_params", "{\"fields\": \"" + SERVICE_CACHE_CORE_FIELDS + "\"}");
params.add("wrap", "true");
}
private static String getFilterFromType(CacheType cacheType) {
switch (cacheType) {
case EVENT:
return "Event";
case MULTI:
return "Multi";
case MYSTERY:
return "Quiz";
case TRADITIONAL:
return "Traditional";
case VIRTUAL:
return "Virtual";
case WEBCAM:
return "Webcam";
default:
return "";
}
}
}
|