diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2011-11-09 19:18:58 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2011-11-09 20:27:34 +0100 |
| commit | dc1dcb57cf7b4b3b183e240e1a529816d3107ff4 (patch) | |
| tree | 807df44cf77dd9214b1ed7e132bfd38ac5acd1d0 | |
| parent | 97803bb9bfaaa9b1b7cd2b0adf3c582ba3ca04b1 (diff) | |
| download | cgeo-dc1dcb57cf7b4b3b183e240e1a529816d3107ff4.zip cgeo-dc1dcb57cf7b4b3b183e240e1a529816d3107ff4.tar.gz cgeo-dc1dcb57cf7b4b3b183e240e1a529816d3107ff4.tar.bz2 | |
Move Go4Cache result parsing into the Go4Cache class
| -rw-r--r-- | main/src/cgeo/geocaching/Go4Cache.java | 74 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 59 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/maps/CGeoMap.java | 23 |
3 files changed, 79 insertions, 77 deletions
diff --git a/main/src/cgeo/geocaching/Go4Cache.java b/main/src/cgeo/geocaching/Go4Cache.java index d916e32..d2ed032 100644 --- a/main/src/cgeo/geocaching/Go4Cache.java +++ b/main/src/cgeo/geocaching/Go4Cache.java @@ -2,12 +2,20 @@ package cgeo.geocaching; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter.Format; +import cgeo.geocaching.geopoint.Viewport; import cgeo.geocaching.utils.CryptUtils; import org.apache.commons.lang3.StringUtils; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import android.util.Log; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.concurrent.ArrayBlockingQueue; /** @@ -101,4 +109,70 @@ public class Go4Cache extends Thread { Log.e(Settings.tag, "Go4Cache.run: interrupted", e); } } + + /** + * Return an immutable list of users present in the given viewport. + * + * @param username + * the current username + * @param viewport + * the current viewport + * @return the list of users present in the viewport + */ + public static List<cgUser> getGeocachersInViewport(final String username, final Viewport viewport) { + final List<cgUser> users = new ArrayList<cgUser>(); + + if (username == null) { + return users; + } + + final Parameters params = new Parameters( + "u", username, + "ltm", viewport.bottomLeft.format(Format.LAT_DECDEGREE_RAW), + "ltx", viewport.topRight.format(Format.LAT_DECDEGREE_RAW), + "lnm", viewport.bottomLeft.format(Format.LON_DECDEGREE_RAW), + "lnx", viewport.topRight.format(Format.LON_DECDEGREE_RAW)); + + final String data = cgBase.getResponseData(cgBase.postRequest("http://api.go4cache.com/get.php", params)); + + if (StringUtils.isBlank(data)) { + Log.e(Settings.tag, "cgeoBase.getGeocachersInViewport: No data from server"); + return null; + } + + try { + final JSONArray usersData = new JSONObject(data).getJSONArray("users"); + final int count = usersData.length(); + for (int i = 0; i < count; i++) { + final JSONObject oneUser = usersData.getJSONObject(i); + users.add(parseUser(oneUser)); + } + } catch (Exception e) { + Log.e(Settings.tag, "cgBase.getGeocachersInViewport: " + e.toString()); + } + + return Collections.unmodifiableList(users); + } + + /** + * Parse user information from go4cache.com. + * + * @param oneUser + * a JSON object + * @return a cgCache user filled with information + * @throws JSONException + * if JSON could not be parsed correctly + * @throws ParseException + * if the date could not be parsed as expected + */ + private static cgUser parseUser(final JSONObject oneUser) throws JSONException, ParseException { + final cgUser user = new cgUser(); + final String located = oneUser.getString("located"); + user.located = cgBase.dateSqlIn.parse(located); + user.username = oneUser.getString("user"); + user.coords = new Geopoint(oneUser.getDouble("latitude"), oneUser.getDouble("longitude")); + user.action = oneUser.getString("action"); + user.client = oneUser.getString("client"); + return user; + } } diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index 4734820..17a7835 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -2236,65 +2236,6 @@ public class cgBase { return page; } - public static List<cgUser> getGeocachersInViewport(final String username, final Double latMin, final Double latMax, final Double lonMin, final Double lonMax) { - final List<cgUser> users = new ArrayList<cgUser>(); - - if (username == null) { - return users; - } - if (latMin == null || latMax == null || lonMin == null || lonMax == null) { - return users; - } - - final Parameters params = new Parameters( - "u", username, - "ltm", String.format((Locale) null, "%.6f", latMin), - "ltx", String.format((Locale) null, "%.6f", latMax), - "lnm", String.format((Locale) null, "%.6f", lonMin), - "lnx", String.format((Locale) null, "%.6f", lonMax)); - - final String data = getResponseData(postRequest("http://api.go4cache.com/get.php", params)); - - if (StringUtils.isBlank(data)) { - Log.e(Settings.tag, "cgeoBase.getGeocachersInViewport: No data from server"); - return null; - } - - try { - final JSONObject dataJSON = new JSONObject(data); - - final JSONArray usersData = dataJSON.getJSONArray("users"); - if (usersData != null && usersData.length() > 0) { - int count = usersData.length(); - JSONObject oneUser = null; - for (int i = 0; i < count; i++) { - final cgUser user = new cgUser(); - oneUser = usersData.getJSONObject(i); - if (oneUser != null) { - final String located = oneUser.getString("located"); - if (located != null) { - user.located = dateSqlIn.parse(located); - } else { - user.located = new Date(); - } - user.username = oneUser.getString("user"); - user.coords = new Geopoint(oneUser.getDouble("latitude"), oneUser.getDouble("longitude")); - user.action = oneUser.getString("action"); - user.client = oneUser.getString("client"); - - if (user.coords != null) { - users.add(user); - } - } - } - } - } catch (Exception e) { - Log.e(Settings.tag, "cgBase.getGeocachersInViewport: " + e.toString()); - } - - return users; - } - public static List<cgCache> filterSearchResults(final cgSearch search, final cgCacheWrap caches, final boolean excludeDisabled, final boolean excludeMine, final CacheType cacheType) { List<cgCache> cacheList = new ArrayList<cgCache>(); if (caches != null) { diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 67cf398..85e916c 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -1,5 +1,6 @@ package cgeo.geocaching.maps; +import cgeo.geocaching.Go4Cache; import cgeo.geocaching.R; import cgeo.geocaching.Settings; import cgeo.geocaching.Settings.mapSourceEnum; @@ -1494,6 +1495,9 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory @Override public void run() { + final Geopoint center = new Geopoint((int) centerLat, (int) centerLon); + final Viewport viewport = new Viewport(center, spanLat / 1e6 * 1.5, spanLon / 1e6 * 1.5); + try { stop = false; working = true; @@ -1503,24 +1507,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory return; } - double latMin = (centerLat / 1e6) - ((spanLat / 1e6) / 2) - ((spanLat / 1e6) / 4); - double latMax = (centerLat / 1e6) + ((spanLat / 1e6) / 2) + ((spanLat / 1e6) / 4); - double lonMin = (centerLon / 1e6) - ((spanLon / 1e6) / 2) - ((spanLon / 1e6) / 4); - double lonMax = (centerLon / 1e6) + ((spanLon / 1e6) / 2) + ((spanLon / 1e6) / 4); - double llCache; - - if (latMin > latMax) { - llCache = latMax; - latMax = latMin; - latMin = llCache; - } - if (lonMin > lonMax) { - llCache = lonMax; - lonMax = lonMin; - lonMin = llCache; - } - - users = cgBase.getGeocachersInViewport(Settings.getUsername(), latMin, latMax, lonMin, lonMax); + users = Go4Cache.getGeocachersInViewport(Settings.getUsername(), viewport); if (stop) { return; |
