diff options
| -rw-r--r-- | main/src/cgeo/geocaching/LogTemplateProvider.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/StaticMapsProvider.java | 49 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 64 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 10 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgDirectionImg.java | 35 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgHtmlImg.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeocoords.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeonavigate.java | 2 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeotouch.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeovisit.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/connector/GCConnector.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/geopoint/Geopoint.java | 16 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/maps/CGeoMap.java | 4 |
13 files changed, 92 insertions, 116 deletions
diff --git a/main/src/cgeo/geocaching/LogTemplateProvider.java b/main/src/cgeo/geocaching/LogTemplateProvider.java index db65ac7..4a11652 100644 --- a/main/src/cgeo/geocaching/LogTemplateProvider.java +++ b/main/src/cgeo/geocaching/LogTemplateProvider.java @@ -4,8 +4,6 @@ import org.apache.commons.lang3.StringUtils; import android.util.Log; -import java.util.HashMap; -import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -87,8 +85,7 @@ public class LogTemplateProvider { return ""; } String findCount = ""; - final Map<String, String> params = new HashMap<String, String>(); - final String page = cgBase.getResponseData(base.request("http://www.geocaching.com/email/", params, false, false, false)); + final String page = cgBase.getResponseData(base.request("http://www.geocaching.com/email/", null, false, false, false)); int current = parseFindCount(page); if (current >= 0) { diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java index 7932363..a33e8b2 100644 --- a/main/src/cgeo/geocaching/StaticMapsProvider.java +++ b/main/src/cgeo/geocaching/StaticMapsProvider.java @@ -65,7 +65,6 @@ public class StaticMapsProvider { HttpGet getMethod = null; HttpResponse httpResponse = null; HttpEntity entity = null; - BufferedHttpEntity bufferedEntity = null; boolean ok = false; @@ -85,34 +84,30 @@ public class StaticMapsProvider { break; } - bufferedEntity = new BufferedHttpEntity(entity); - if (bufferedEntity != null) { - InputStream is = (InputStream) bufferedEntity.getContent(); - FileOutputStream fos = new FileOutputStream(fileName); - - int fileSize = 0; - try { - byte[] buffer = new byte[4096]; - int bytesRead; - while ((bytesRead = is.read(buffer)) != -1) { - fos.write(buffer, 0, bytesRead); - fileSize += bytesRead; - } - fos.flush(); - ok = true; - } catch (IOException e) { - Log.e(cgSettings.tag, "cgMapImg.getDrawable (saving to cache): " + e.toString()); - } finally { - is.close(); - fos.close(); + final BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity); + InputStream is = (InputStream) bufferedEntity.getContent(); + FileOutputStream fos = new FileOutputStream(fileName); + + int fileSize = 0; + try { + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = is.read(buffer)) != -1) { + fos.write(buffer, 0, bytesRead); + fileSize += bytesRead; } + fos.flush(); + ok = true; + } catch (IOException e) { + Log.e(cgSettings.tag, "cgMapImg.getDrawable (saving to cache): " + e.toString()); + } finally { + is.close(); + fos.close(); + } - bufferedEntity = null; - - // delete image if it has no contents - if (ok && fileSize < MIN_MAP_IMAGE_BYTES) { - (new File(fileName)).delete(); - } + // delete image if it has no contents + if (ok && fileSize < MIN_MAP_IMAGE_BYTES) { + (new File(fileName)).delete(); } if (ok) { diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index d048a44..ed6b9d5 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -79,7 +79,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -499,7 +498,7 @@ public class cgBase { return -3; // no login information stored } - loginResponse = request("https://www.geocaching.com/login/default.aspx", new HashMap<String, String>(), false, false, false); + loginResponse = request("https://www.geocaching.com/login/default.aspx", null, false, false, false); loginData = getResponseData(loginResponse); if (StringUtils.isNotBlank(loginData)) { if (checkLogin(loginData)) { @@ -894,7 +893,7 @@ public class cgBase { recaptchaText = thread.getText(); } - if (cids.size() > 0 && (recaptchaChallenge == null || (recaptchaChallenge != null && StringUtils.isNotBlank(recaptchaText)))) { + if (cids.size() > 0 && (recaptchaChallenge == null || StringUtils.isNotBlank(recaptchaText))) { Log.i(cgSettings.tag, "Trying to get .loc for " + cids.size() + " caches"); try { @@ -1669,7 +1668,7 @@ public class cgBase { } final String userToken = userTokenMatcher.group(1); - final HashMap<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); params.put("tkn", userToken); params.put("idx", "1"); params.put("num", "35"); @@ -1875,7 +1874,7 @@ public class cgBase { final Map<String, cgRating> ratings = new HashMap<String, cgRating>(); try { - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); if (settings.isLogin()) { final Map<String, String> login = settings.getGCvoteLogin(); if (login != null) { @@ -2646,7 +2645,7 @@ public class cgBase { cacheType = null; } - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); if (cacheType != null && cacheIDs.containsKey(cacheType)) { params.put("tx", cacheIDs.get(cacheType)); } else { @@ -2695,7 +2694,7 @@ public class cgBase { cacheType = null; } - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); if (cacheType != null && cacheIDs.containsKey(cacheType)) { params.put("tx", cacheIDs.get(cacheType)); } else { @@ -2743,7 +2742,7 @@ public class cgBase { cacheType = null; } - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); if (cacheType != null && cacheIDs.containsKey(cacheType)) { params.put("tx", cacheIDs.get(cacheType)); } else { @@ -2797,7 +2796,7 @@ public class cgBase { cacheType = null; } - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); if (cacheType != null && cacheIDs.containsKey(cacheType)) { params.put("tx", cacheIDs.get(cacheType)); } else { @@ -2994,7 +2993,7 @@ public class cgBase { return null; } - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); if (StringUtils.isNotBlank(geocode)) { params.put("tracker", geocode); } else if (StringUtils.isNotBlank(guid)) { @@ -3402,48 +3401,33 @@ public class cgBase { return encoded; } - public String prepareParameters(Map<String, String> params, boolean my, boolean addF) { - String paramsDone = null; - - if (my != true && settings.excludeMine > 0) { + public String prepareParameters(final Parameters params, final boolean my, final boolean addF) { + if (my != true && settings.excludeMine > 0 && addF) { if (params == null) { - params = new HashMap<String, String>(); - } - if (addF) { - params.put("f", "1"); + return "f=1"; } - + params.put("f", "1"); Log.i(cgSettings.tag, "Skipping caches found or hidden by user."); } if (params != null) { - Set<Map.Entry<String, String>> entrySet = params.entrySet(); - List<String> paramsEncoded = new ArrayList<String>(); + final List<String> paramsEncoded = new ArrayList<String>(params.size()); - for (Map.Entry<String, String> entry : entrySet) - { - String key = entry.getKey(); - String value = entry.getValue(); + for (final NameValuePair nameValue : params) { + final String key = nameValue.getName(); + final String value = StringUtils.defaultString(nameValue.getValue()); - if (key.charAt(0) == '^') { - key = ""; - } - if (value == null) { - value = ""; - } - - paramsEncoded.add(key + "=" + urlencode_rfc3986(value)); + // TODO: Document the justification of the legacy test below + paramsEncoded.add((key.charAt(0) != '^' ? key : "") + "=" + urlencode_rfc3986(value)); } - paramsDone = StringUtils.join(paramsEncoded.toArray(), '&'); + return StringUtils.join(paramsEncoded.toArray(), '&'); } else { - paramsDone = ""; + return ""; } - - return paramsDone; } - public String[] requestViewstates(final String uri, Map<String, String> params, boolean xContentType, boolean my) { + public String[] requestViewstates(final String uri, final Parameters params, boolean xContentType, boolean my) { final HttpResponse response = request(uri, params, xContentType, my, false); return getViewstates(getResponseData(response)); @@ -3473,7 +3457,7 @@ public class cgBase { return data; } - public String requestLogged(final String uri, Map<String, String> params, boolean xContentType, boolean my, boolean addF) { + public String requestLogged(final String uri, final Parameters params, boolean xContentType, boolean my, boolean addF) { HttpResponse response = request(uri, params, xContentType, my, addF); String data = getResponseData(response); @@ -3490,7 +3474,7 @@ public class cgBase { return data; } - public HttpResponse request(final String uri, Map<String, String> params, boolean xContentType, boolean my, boolean addF) { + public HttpResponse request(final String uri, final Parameters params, boolean xContentType, boolean my, boolean addF) { final String paramsDone = prepareParameters(params, my, addF); return request(uri, paramsDone, xContentType); } diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index e4e6311..f51d276 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -903,10 +903,7 @@ public class cgData { list.add((String) cursor.getString(index)); } while (cursor.moveToNext()); } else { - if (cursor != null) { - cursor.close(); - } - + cursor.close(); return null; } } @@ -1969,10 +1966,7 @@ public class cgData { caches.add(cache); } while (cursor.moveToNext()); } else { - if (cursor != null) { - cursor.close(); - } - + cursor.close(); return null; } } diff --git a/main/src/cgeo/geocaching/cgDirectionImg.java b/main/src/cgeo/geocaching/cgDirectionImg.java index 1aa7795..d8c7d06 100644 --- a/main/src/cgeo/geocaching/cgDirectionImg.java +++ b/main/src/cgeo/geocaching/cgDirectionImg.java @@ -47,7 +47,6 @@ public class cgDirectionImg { HttpGet getMethod = null; HttpResponse httpResponse = null; HttpEntity entity = null; - BufferedHttpEntity bufferedEntity = null; boolean ok = false; @@ -60,28 +59,26 @@ public class cgDirectionImg { getMethod = new HttpGet("http://www.geocaching.com/ImgGen/seek/CacheDir.ashx?k=" + code); httpResponse = client.execute(getMethod); entity = httpResponse.getEntity(); - bufferedEntity = new BufferedHttpEntity(entity); + final BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity); Log.i(cgSettings.tag, "[" + entity.getContentLength() + "B] Downloading direction image " + code); - if (bufferedEntity != null) { - InputStream is = (InputStream) bufferedEntity.getContent(); - FileOutputStream fos = new FileOutputStream(fileName); - - try { - byte[] buffer = new byte[4096]; - int l; - while ((l = is.read(buffer)) != -1) { - fos.write(buffer, 0, l); - } - ok = true; - fos.flush(); - } catch (IOException e) { - Log.e(cgSettings.tag, "cgDirectionImg.getDrawable (saving to cache): " + e.toString()); - } finally { - is.close(); - fos.close(); + InputStream is = (InputStream) bufferedEntity.getContent(); + FileOutputStream fos = new FileOutputStream(fileName); + + try { + byte[] buffer = new byte[4096]; + int l; + while ((l = is.read(buffer)) != -1) { + fos.write(buffer, 0, l); } + ok = true; + fos.flush(); + } catch (IOException e) { + Log.e(cgSettings.tag, "cgDirectionImg.getDrawable (saving to cache): " + e.toString()); + } finally { + is.close(); + fos.close(); } if (ok) { diff --git a/main/src/cgeo/geocaching/cgHtmlImg.java b/main/src/cgeo/geocaching/cgHtmlImg.java index 880d5b4..3362561 100644 --- a/main/src/cgeo/geocaching/cgHtmlImg.java +++ b/main/src/cgeo/geocaching/cgHtmlImg.java @@ -206,9 +206,8 @@ public class cgHtmlImg implements Html.ImageGetter { bfOptions.inSampleSize = 2; } - if (bufferedEntity != null) { - imagePre = BitmapFactory.decodeStream(bufferedEntity.getContent(), null, bfOptions); - } + imagePre = BitmapFactory.decodeStream(bufferedEntity.getContent(), null, bfOptions); + if (imagePre != null) { break; } diff --git a/main/src/cgeo/geocaching/cgeocoords.java b/main/src/cgeo/geocaching/cgeocoords.java index ec56a4a..7b430d0 100644 --- a/main/src/cgeo/geocaching/cgeocoords.java +++ b/main/src/cgeo/geocaching/cgeocoords.java @@ -360,7 +360,7 @@ public class cgeocoords extends Dialog { private boolean calc(final boolean signalError) { if (currentFormat == coordInputFormatEnum.Plain) { try { - gp = new Geopoint(eLat.getText().toString() + " " + eLon.getText().toString()); + gp = new Geopoint(eLat.getText().toString(), eLon.getText().toString()); } catch (ParseException e) { if (signalError) { context.showToast(context.getResources().getString(R.string.err_parse_lat_lon)); @@ -413,6 +413,8 @@ public class cgeocoords extends Dialog { latitude = latDeg + latMin / 60.0 + latSec / 60.0 / 60.0 + latSecFrac / 60.0 / 60.0; longitude = lonDeg + lonMin / 60.0 + lonSec / 60.0 / 60.0 + lonSecFrac / 60.0 / 60.0; break; + case Plain: + // This case has been handled above } latitude *= (bLat.getText().toString().equalsIgnoreCase("S") ? -1 : 1); longitude *= (bLon.getText().toString().equalsIgnoreCase("W") ? -1 : 1); diff --git a/main/src/cgeo/geocaching/cgeonavigate.java b/main/src/cgeo/geocaching/cgeonavigate.java index f4f8393..5325a58 100644 --- a/main/src/cgeo/geocaching/cgeonavigate.java +++ b/main/src/cgeo/geocaching/cgeonavigate.java @@ -411,7 +411,7 @@ public class cgeonavigate extends AbstractActivity { } if (settings.useCompass == 0 || (geo.speedNow != null && geo.speedNow > 5)) { // use GPS when speed is higher than 18 km/h - if (geo != null && geo.bearingNow != null) { + if (geo.bearingNow != null) { northHeading = geo.bearingNow; } else { northHeading = 0; diff --git a/main/src/cgeo/geocaching/cgeotouch.java b/main/src/cgeo/geocaching/cgeotouch.java index 827a765..baf085c 100644 --- a/main/src/cgeo/geocaching/cgeotouch.java +++ b/main/src/cgeo/geocaching/cgeotouch.java @@ -22,9 +22,7 @@ import android.widget.TextView; import java.util.ArrayList; import java.util.Calendar; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class cgeotouch extends cgLogForm { private cgTrackable trackable = null; @@ -372,7 +370,7 @@ public class cgeotouch extends cgLogForm { @Override public void run() { - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); showProgressHandler.sendEmptyMessage(0); gettingViewstate = true; diff --git a/main/src/cgeo/geocaching/cgeovisit.java b/main/src/cgeo/geocaching/cgeovisit.java index e8104ab..e500adb 100644 --- a/main/src/cgeo/geocaching/cgeovisit.java +++ b/main/src/cgeo/geocaching/cgeovisit.java @@ -28,7 +28,6 @@ import android.widget.TextView; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -374,7 +373,7 @@ public class cgeovisit extends cgLogForm { return false; } - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); params.put("userName", login.get("username")); params.put("password", login.get("password")); params.put("cacheId", guid); @@ -699,7 +698,7 @@ public class cgeovisit extends cgLogForm { @Override public void run() { - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); showProgressHandler.sendEmptyMessage(0); gettingViewstate = true; diff --git a/main/src/cgeo/geocaching/connector/GCConnector.java b/main/src/cgeo/geocaching/connector/GCConnector.java index 14d619e..e269c8b 100644 --- a/main/src/cgeo/geocaching/connector/GCConnector.java +++ b/main/src/cgeo/geocaching/connector/GCConnector.java @@ -1,5 +1,6 @@ package cgeo.geocaching.connector; +import cgeo.geocaching.Parameters; import cgeo.geocaching.cgBase; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgCacheWrap; @@ -12,9 +13,7 @@ import org.apache.commons.lang3.StringUtils; import android.util.Log; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.UUID; public class GCConnector extends AbstractConnector implements IConnector { @@ -67,7 +66,7 @@ public class GCConnector extends AbstractConnector implements IConnector { @Override public UUID searchByGeocode(final cgBase base, String geocode, final String guid, final cgeoapplication app, final cgSearch search, final int reason) { - final Map<String, String> params = new HashMap<String, String>(); + final Parameters params = new Parameters(); if (StringUtils.isNotBlank(geocode)) { params.put("wp", geocode); } else if (StringUtils.isNotBlank(guid)) { diff --git a/main/src/cgeo/geocaching/geopoint/Geopoint.java b/main/src/cgeo/geocaching/geopoint/Geopoint.java index 37c9e16..f398f75 100644 --- a/main/src/cgeo/geocaching/geopoint/Geopoint.java +++ b/main/src/cgeo/geocaching/geopoint/Geopoint.java @@ -57,12 +57,24 @@ public final class Geopoint * string to parse * @see GeopointParser.parse() */ - public Geopoint(final String text) - { + public Geopoint(final String text) { this(GeopointParser.parseLatitude(text), GeopointParser.parseLongitude(text)); } /** + * Creates new Geopoint with latitude and longitude parsed from string. + * + * @param latText + * latitude string to parse + * @param lonText + * longitude string to parse + * @see GeopointParser.parse() + */ + public Geopoint(final String latText, final String lonText) { + this(GeopointParser.parseLatitude(latText), GeopointParser.parseLongitude(lonText)); + } + + /** * Creates new Geopoint with given Location. * * @param gp diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 46890a5..512441a 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -1278,7 +1278,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory final List<cgCache> cachesProtected = new ArrayList<cgCache>(caches); final List<CachesOverlayItemImpl> items = new ArrayList<CachesOverlayItemImpl>(); - if (cachesProtected != null && !cachesProtected.isEmpty()) { + if (!cachesProtected.isEmpty()) { int icon = 0; Drawable pin = null; CachesOverlayItemImpl item = null; @@ -1719,7 +1719,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory centerLon = maxLon; } - if (cnt != null && cnt > 0) { + if (cnt > 0) { mapController.setCenter(settings.getMapFactory().getGeoPointBase(new Geopoint(centerLat, centerLon))); if (Math.abs(maxLat - minLat) != 0 && Math.abs(maxLon - minLon) != 0) { mapController.zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon)); |
