aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/connector/oc
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/connector/oc')
-rw-r--r--main/src/cgeo/geocaching/connector/oc/OCApiLiveConnector.java6
-rw-r--r--main/src/cgeo/geocaching/connector/oc/OkapiClient.java55
2 files changed, 51 insertions, 10 deletions
diff --git a/main/src/cgeo/geocaching/connector/oc/OCApiLiveConnector.java b/main/src/cgeo/geocaching/connector/oc/OCApiLiveConnector.java
index 049c633..3771443 100644
--- a/main/src/cgeo/geocaching/connector/oc/OCApiLiveConnector.java
+++ b/main/src/cgeo/geocaching/connector/oc/OCApiLiveConnector.java
@@ -3,6 +3,7 @@ package cgeo.geocaching.connector.oc;
import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.DataStore;
import cgeo.geocaching.Geocache;
+import cgeo.geocaching.ICache;
import cgeo.geocaching.LogCacheActivity;
import cgeo.geocaching.SearchResult;
import cgeo.geocaching.connector.ILoggingManager;
@@ -149,6 +150,11 @@ public class OCApiLiveConnector extends OCApiConnector implements ISearchByCente
}
@Override
+ public boolean isOwner(ICache cache) {
+ return StringUtils.isNotEmpty(getUserName()) && StringUtils.equals(cache.getOwnerDisplayName(), getUserName());
+ }
+
+ @Override
public String getUserName() {
return userInfo.getName();
}
diff --git a/main/src/cgeo/geocaching/connector/oc/OkapiClient.java b/main/src/cgeo/geocaching/connector/oc/OkapiClient.java
index 3c93488..7d64b31 100644
--- a/main/src/cgeo/geocaching/connector/oc/OkapiClient.java
+++ b/main/src/cgeo/geocaching/connector/oc/OkapiClient.java
@@ -6,6 +6,7 @@ import cgeo.geocaching.Geocache;
import cgeo.geocaching.Image;
import cgeo.geocaching.LogEntry;
import cgeo.geocaching.R;
+import cgeo.geocaching.Trackable;
import cgeo.geocaching.Waypoint;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.IConnector;
@@ -98,6 +99,11 @@ final class OkapiClient {
private static final String CACHE_CODE = "code";
private static final String CACHE_REQ_PASSWORD = "req_passwd";
private static final String CACHE_MY_NOTES = "my_notes";
+ private static final String CACHE_TRACKABLES_COUNT = "trackables_count";
+ private static final String CACHE_TRACKABLES = "trackables";
+
+ private static final String TRK_GEOCODE = "code";
+ private static final String TRK_NAME = "name";
private static final String LOG_TYPE = "type";
private static final String LOG_COMMENT = "comment";
@@ -112,9 +118,9 @@ final class OkapiClient {
// the several realms of possible fields for cache retrieval:
// Core: for livemap requests (L3 - only with level 3 auth)
// Additional: additional fields for full cache (L3 - only for level 3 auth, current - only for connectors with current api)
- private static final String SERVICE_CACHE_CORE_FIELDS = "code|name|location|type|status|difficulty|terrain|size|size2|date_hidden";
+ private static final String SERVICE_CACHE_CORE_FIELDS = "code|name|location|type|status|difficulty|terrain|size|size2|date_hidden|trackables_count";
private static final String SERVICE_CACHE_CORE_L3_FIELDS = "is_found";
- private static final String SERVICE_CACHE_ADDITIONAL_FIELDS = "owner|founds|notfounds|rating|rating_votes|recommendations|description|hint|images|latest_logs|alt_wpts|attrnames|req_passwd";
+ private static final String SERVICE_CACHE_ADDITIONAL_FIELDS = "owner|founds|notfounds|rating|rating_votes|recommendations|description|hint|images|latest_logs|alt_wpts|attrnames|req_passwd|trackables";
private static final String SERVICE_CACHE_ADDITIONAL_CURRENT_FIELDS = "gc_code|attribution_note|attr_acodes";
private static final String SERVICE_CACHE_ADDITIONAL_L3_FIELDS = "is_watched|my_notes";
@@ -328,8 +334,11 @@ final class OkapiClient {
parseCoreCache(response, cache);
// not used: url
- final JSONObject owner = response.getJSONObject(CACHE_OWNER);
- cache.setOwnerDisplayName(parseUser(owner));
+ final JSONObject ownerObject = response.getJSONObject(CACHE_OWNER);
+ final String owner = parseUser(ownerObject);
+ cache.setOwnerDisplayName(owner);
+ // OpenCaching has no distinction between user id and user display name. Set the ID anyway to simplify c:geo workflows.
+ cache.setOwnerUserId(owner);
cache.getLogCounts().put(LogType.FOUND_IT, response.getInt(CACHE_FOUNDS));
cache.getLogCounts().put(LogType.DIDNT_FIND_IT, response.getInt(CACHE_NOTFOUNDS));
@@ -375,6 +384,9 @@ final class OkapiClient {
//TODO: Store license per cache
//cache.setLicense(response.getString("attribution_note"));
cache.setWaypoints(parseWaypoints(response.getJSONArray(CACHE_WPTS)), false);
+
+ cache.setInventory(parseTrackables(response.getJSONArray(CACHE_TRACKABLES)));
+
if (!response.isNull(CACHE_IS_WATCHED)) {
cache.setOnWatchlist(response.getBoolean(CACHE_IS_WATCHED));
}
@@ -409,6 +421,8 @@ final class OkapiClient {
cache.setDifficulty((float) response.getDouble(CACHE_DIFFICULTY));
cache.setTerrain((float) response.getDouble(CACHE_TERRAIN));
+ cache.setInventoryItems(response.getInt(CACHE_TRACKABLES_COUNT));
+
if (!response.isNull(CACHE_IS_FOUND)) {
cache.setFound(response.getBoolean(CACHE_IS_FOUND));
}
@@ -478,6 +492,27 @@ final class OkapiClient {
return result;
}
+ private static List<Trackable> parseTrackables(final JSONArray trackablesJson) {
+ if (trackablesJson.length() == 0) {
+ return Collections.emptyList();
+ }
+ final List<Trackable> result = new ArrayList<Trackable>();
+ for (int i = 0; i < trackablesJson.length(); i++) {
+ try {
+ final JSONObject trackableResponse = trackablesJson.getJSONObject(i);
+ final Trackable trk = new Trackable();
+ trk.setGeocode(trackableResponse.getString(TRK_GEOCODE));
+ trk.setName(trackableResponse.getString(TRK_NAME));
+ result.add(trk);
+ } catch (final JSONException e) {
+ Log.e("OkapiClient.parseWaypoints", e);
+ // Don't overwrite internal state with possibly partial result
+ return null;
+ }
+ }
+ return result;
+ }
+
private static LogType parseLogType(final String logType) {
if ("Found it".equalsIgnoreCase(logType)) {
return LogType.FOUND_IT;
@@ -593,7 +628,7 @@ final class OkapiClient {
try {
final String size = response.getString(CACHE_SIZE2);
return CacheSize.getById(size);
- } catch (JSONException e) {
+ } catch (final JSONException e) {
Log.e("OkapiClient.getCacheSize", e);
return getCacheSizeDeprecated(response);
}
@@ -702,7 +737,7 @@ final class OkapiClient {
params.add("langpref", getPreferredLanguage());
if (connector.getSupportedAuthLevel() == OAuthLevel.Level3) {
- ImmutablePair<String, String> tokens = Settings.getTokenPair(connector.getTokenPublicPrefKeyId(), connector.getTokenSecretPrefKeyId());
+ final ImmutablePair<String, String> tokens = Settings.getTokenPair(connector.getTokenPublicPrefKeyId(), connector.getTokenSecretPrefKeyId());
OAuth.signOAuth(host, service.methodName, "GET", false, params, tokens.left, tokens.right, connector.getCK(), connector.getCS());
} else {
connector.addAuthentication(params);
@@ -769,7 +804,7 @@ final class OkapiClient {
return null;
}
- JSONObject data = result.data;
+ final JSONObject data = result.data;
if (!data.isNull(USER_UUID)) {
try {
return data.getString(USER_UUID);
@@ -792,7 +827,7 @@ final class OkapiClient {
return new UserInfo(StringUtils.EMPTY, 0, UserInfoStatus.getFromOkapiError(error.getResult()));
}
- JSONObject data = result.data;
+ final JSONObject data = result.data;
String name = StringUtils.EMPTY;
boolean successUserName = false;
@@ -828,7 +863,7 @@ final class OkapiClient {
* response containing an error object
* @return OkapiError object with detailed information
*/
- public static OkapiError decodeErrorResponse(HttpResponse response) {
+ public static OkapiError decodeErrorResponse(final HttpResponse response) {
final JSONResult result = new JSONResult(response);
if (!result.isSuccess) {
return new OkapiError(result.data);
@@ -846,7 +881,7 @@ final class OkapiClient {
public final JSONObject data;
public JSONResult(final @Nullable HttpResponse response) {
- boolean isSuccess = Network.isSuccess(response);
+ final boolean isSuccess = Network.isSuccess(response);
final String responseData = Network.getResponseDataAlways(response);
JSONObject data = null;
if (responseData != null) {