diff options
| -rw-r--r-- | main/res/values/strings.xml | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/connector/oc/OkapiClient.java | 24 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/enumerations/CacheSize.java | 37 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/enumerations/CacheSizeTest.java | 2 |
4 files changed, 44 insertions, 23 deletions
diff --git a/main/res/values/strings.xml b/main/res/values/strings.xml index 7a036d2..38a2a8e 100644 --- a/main/res/values/strings.xml +++ b/main/res/values/strings.xml @@ -45,7 +45,9 @@ <string name="cache_size_virtual">Virtual</string> <string name="cache_size_notchosen">Not chosen</string> <string name="cache_size_unknown">Unknown</string> - + <string name="cache_size_nano">Nano</string> + <string name="cache_size_very_large">Very large</string> + <!-- waypoints --> <string name="wp_final">Final Location</string> <string name="wp_stage">Stage of a Multicache</string> diff --git a/main/src/cgeo/geocaching/connector/oc/OkapiClient.java b/main/src/cgeo/geocaching/connector/oc/OkapiClient.java index 95482d9..c33a90e 100644 --- a/main/src/cgeo/geocaching/connector/oc/OkapiClient.java +++ b/main/src/cgeo/geocaching/connector/oc/OkapiClient.java @@ -75,7 +75,8 @@ final class OkapiClient { 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_SIZE_DEPRECATED = "size"; + private static final String CACHE_SIZE2 = "size2"; private static final String CACHE_VOTES = "rating_votes"; private static final String CACHE_NOTFOUNDS = "notfounds"; private static final String CACHE_FOUNDS = "founds"; @@ -112,7 +113,7 @@ 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|date_hidden"; + 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_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_CURRENT_FIELDS = "gc_code|attribution_note|attr_acodes"; @@ -562,12 +563,25 @@ final class OkapiClient { } private static CacheSize getCacheSize(final JSONObject response) { - if (response.isNull(CACHE_SIZE)) { + if (response.isNull(CACHE_SIZE2)) { + return getCacheSizeDeprecated(response); + } + try { + final String size = response.getString(CACHE_SIZE2); + return CacheSize.getById(size); + } catch (JSONException e) { + Log.e("OkapiClient.getCacheSize", e); + return getCacheSizeDeprecated(response); + } + } + + private static CacheSize getCacheSizeDeprecated(final JSONObject response) { + if (response.isNull(CACHE_SIZE_DEPRECATED)) { return CacheSize.NOT_CHOSEN; } double size = 0; try { - size = response.getDouble(CACHE_SIZE); + size = response.getDouble(CACHE_SIZE_DEPRECATED); } catch (final JSONException e) { Log.e("OkapiClient.getCacheSize", e); } @@ -581,7 +595,7 @@ final class OkapiClient { case 4: return CacheSize.LARGE; case 5: - return CacheSize.LARGE; + return CacheSize.VERY_LARGE; default: break; } diff --git a/main/src/cgeo/geocaching/enumerations/CacheSize.java b/main/src/cgeo/geocaching/enumerations/CacheSize.java index ee42c66..1255455 100644 --- a/main/src/cgeo/geocaching/enumerations/CacheSize.java +++ b/main/src/cgeo/geocaching/enumerations/CacheSize.java @@ -12,30 +12,38 @@ import java.util.Map; * Enum listing cache sizes */ public enum CacheSize { - MICRO("Micro", 1, R.string.cache_size_micro), - SMALL("Small", 2, R.string.cache_size_small), - REGULAR("Regular", 3, R.string.cache_size_regular), - LARGE("Large", 4, R.string.cache_size_large), - VIRTUAL("Virtual", 0, R.string.cache_size_virtual), - NOT_CHOSEN("Not chosen", 0, R.string.cache_size_notchosen), - OTHER("Other", 0, R.string.cache_size_other), - UNKNOWN("Unknown", 0, R.string.cache_size_unknown); // CacheSize not init. yet + NANO("Nano", 0, R.string.cache_size_nano, "nano"), // used by OC only + MICRO("Micro", 1, R.string.cache_size_micro, "micro"), + SMALL("Small", 2, R.string.cache_size_small, "small"), + REGULAR("Regular", 3, R.string.cache_size_regular, "regular"), + LARGE("Large", 4, R.string.cache_size_large, "large"), + VERY_LARGE("Very large", 5, R.string.cache_size_very_large, "xlarge"), // used by OC only + NOT_CHOSEN("Not chosen", 6, R.string.cache_size_notchosen, ""), + VIRTUAL("Virtual", 7, R.string.cache_size_virtual, "none"), + OTHER("Other", 8, R.string.cache_size_other, "other"), + UNKNOWN("Unknown", -1, R.string.cache_size_unknown, ""); // CacheSize not init. yet public final String id; public final int comparable; private final int stringId; + /** + * lookup for OC JSON requests (the numeric size is deprecated for OC) + */ + private final String ocSize2; - CacheSize(String id, int comparable, int stringId) { + CacheSize(final String id, final int comparable, final int stringId, final String ocSize2) { this.id = id; this.comparable = comparable; this.stringId = stringId; + this.ocSize2 = ocSize2; } final private static Map<String, CacheSize> FIND_BY_ID; static { final HashMap<String, CacheSize> mapping = new HashMap<String, CacheSize>(); - for (CacheSize cs : values()) { + for (final CacheSize cs : values()) { mapping.put(cs.id.toLowerCase(Locale.US), cs); + mapping.put(cs.ocSize2.toLowerCase(Locale.US), cs); } // add medium as additional string for Regular mapping.put("medium", CacheSize.REGULAR); @@ -61,21 +69,21 @@ public enum CacheSize { /** * Bad GPX files can contain the container size encoded as number. - * + * * @param id * @return */ private static CacheSize getByNumber(final String id) { try { - int numerical = Integer.parseInt(id); + final int numerical = Integer.parseInt(id); if (numerical != 0) { - for (CacheSize size : CacheSize.values()) { + for (final CacheSize size : CacheSize.values()) { if (size.comparable == numerical) { return size; } } } - } catch (NumberFormatException e) { + } catch (final NumberFormatException e) { // ignore, as this might be a number or not } return UNKNOWN; @@ -85,4 +93,3 @@ public enum CacheSize { return CgeoApplication.getInstance().getBaseContext().getResources().getString(stringId); } } - diff --git a/tests/src/cgeo/geocaching/enumerations/CacheSizeTest.java b/tests/src/cgeo/geocaching/enumerations/CacheSizeTest.java index 2f11dfc..26a1953 100644 --- a/tests/src/cgeo/geocaching/enumerations/CacheSizeTest.java +++ b/tests/src/cgeo/geocaching/enumerations/CacheSizeTest.java @@ -30,8 +30,6 @@ public class CacheSizeTest extends AndroidTestCase { public static void testGetByIdNumeric() { assertEquals(CacheSize.REGULAR, CacheSize.getById("3")); - assertEquals(CacheSize.UNKNOWN, CacheSize.getById("0")); - assertEquals(CacheSize.UNKNOWN, CacheSize.getById("9")); assertEquals(CacheSize.UNKNOWN, CacheSize.getById("-1")); } } |
