diff options
-rw-r--r-- | main/src/cgeo/geocaching/AdressListActivity.java | 11 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeocaches.java | 23 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCParser.java | 28 |
3 files changed, 59 insertions, 3 deletions
diff --git a/main/src/cgeo/geocaching/AdressListActivity.java b/main/src/cgeo/geocaching/AdressListActivity.java index a67f4a2..8ce16dd 100644 --- a/main/src/cgeo/geocaching/AdressListActivity.java +++ b/main/src/cgeo/geocaching/AdressListActivity.java @@ -5,6 +5,7 @@ import cgeo.geocaching.ui.AddressListAdapter; import cgeo.geocaching.utils.Log; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import android.app.ProgressDialog; import android.location.Address; @@ -49,7 +50,13 @@ public class AdressListActivity extends AbstractListActivity { try { return geocoder.getFromLocationName(keyword, 20); } catch (Exception e) { - Log.e("AdressListActivity.doInBackground", e); + // non Google devices come without the geocoder + if (StringUtils.containsIgnoreCase(e.getMessage(), "Service not Available")) { + Log.i("No geocoder available"); + } + else { + Log.e("AdressListActivity.doInBackground", e); + } return null; } } @@ -62,8 +69,8 @@ public class AdressListActivity extends AbstractListActivity { adapter.add(address); // don't use addAll, it's only available with API >= 11 } } else { - showToast(res.getString(R.string.err_search_address_no_match)); finish(); + cgeocaches.startActivityAddress(AdressListActivity.this, null, keyword); } } diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java index 3b54025..4e12f07 100644 --- a/main/src/cgeo/geocaching/cgeocaches.java +++ b/main/src/cgeo/geocaching/cgeocaches.java @@ -534,8 +534,14 @@ public class cgeocaches extends AbstractListActivity { showProgress(true); showFooterLoadingCaches(); - thread = new LoadByCoordsThread(coords); + if (coords != null) { + thread = new LoadByCoordsThread(coords); + } + else { + thread = new LoadByAddressThread(address); + } thread.setRecaptchaHandler(new SearchHandler(this, res, thread)); + thread.start(); break; case USERNAME: title = username; @@ -1353,6 +1359,21 @@ public class cgeocaches extends AbstractListActivity { } } + private class LoadByAddressThread extends AbstractSearchThread { + final private String address; + + public LoadByAddressThread(final String address) { + super(loadCachesHandler); + this.address = address; + } + + @Override + public void runSearch() { + search = GCParser.searchByAddress(address, Settings.getCacheType(), Settings.isShowCaptcha()); + replaceCacheListFromSearch(); + } + } + private class LoadDetailsThread extends Thread { final private Handler handler; diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java index 2ca505d..0c00f8f 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCParser.java +++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java @@ -862,6 +862,34 @@ public abstract class GCParser { return searchByAny(cacheType, false, showCaptcha, params); } + public static SearchResult searchByAddress(final String address, final CacheType cacheType, final boolean showCaptcha) { + if (StringUtils.isBlank(address)) { + Log.e("GCParser.searchByAddress: No address given"); + return null; + } + try { + JSONObject response = Network.requestJSON("http://www.geocaching.com/api/geocode", new Parameters("q", address)); + if (response == null) { + return null; + } + if (!StringUtils.equalsIgnoreCase(response.getString("status"), "success")) { + return null; + } + if (!response.has("data")) { + return null; + } + JSONObject data = response.getJSONObject("data"); + if (data == null) { + return null; + } + return searchByCoords(new Geopoint(data.getDouble("lat"), data.getDouble("lng")), cacheType, showCaptcha); + } catch (JSONException e) { + Log.w("GCParser.searchByAddress", e); + } + + return null; + } + public static cgTrackable searchTrackable(final String geocode, final String guid, final String id) { if (StringUtils.isBlank(geocode) && StringUtils.isBlank(guid) && StringUtils.isBlank(id)) { Log.w("GCParser.searchTrackable: No geocode nor guid nor id given"); |