aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2012-03-04 12:09:24 +0100
committerBananeweizen <bananeweizen@gmx.de>2012-03-04 12:09:24 +0100
commitd1f6c0164f8c8442ee0dc17cbff0e18db06286bf (patch)
treef779f57b74ebbc1d051abd26f2dde49966396d10 /main/src/cgeo/geocaching
parent79bda3a54d92f51be99aebffdc67aa11b8ddf09f (diff)
downloadcgeo-d1f6c0164f8c8442ee0dc17cbff0e18db06286bf.zip
cgeo-d1f6c0164f8c8442ee0dc17cbff0e18db06286bf.tar.gz
cgeo-d1f6c0164f8c8442ee0dc17cbff0e18db06286bf.tar.bz2
performance refactorings
* avoid creating message objects, there is a pool * only get GCVotes for filtered caches, not for all * don't use GepointParser in LocParser, we know those are pure floats * String concatenation
Diffstat (limited to 'main/src/cgeo/geocaching')
-rw-r--r--main/src/cgeo/geocaching/CacheDetailActivity.java4
-rw-r--r--main/src/cgeo/geocaching/SearchResult.java4
-rw-r--r--main/src/cgeo/geocaching/StaticMapsActivity.java2
-rw-r--r--main/src/cgeo/geocaching/cgBase.java44
-rw-r--r--main/src/cgeo/geocaching/cgeocaches.java18
-rw-r--r--main/src/cgeo/geocaching/cgeonavigate.java2
-rw-r--r--main/src/cgeo/geocaching/cgeopopup.java5
-rw-r--r--main/src/cgeo/geocaching/cgeowaypoint.java2
-rw-r--r--main/src/cgeo/geocaching/cgeowaypointadd.java2
-rw-r--r--main/src/cgeo/geocaching/connector/gc/GCBase.java6
-rw-r--r--main/src/cgeo/geocaching/files/LocParser.java14
-rw-r--r--main/src/cgeo/geocaching/gcvote/GCVote.java34
-rw-r--r--main/src/cgeo/geocaching/ui/CompassView.java2
13 files changed, 80 insertions, 59 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java
index b54c0cb..53cb2a9 100644
--- a/main/src/cgeo/geocaching/CacheDetailActivity.java
+++ b/main/src/cgeo/geocaching/CacheDetailActivity.java
@@ -17,8 +17,8 @@ import cgeo.geocaching.geopoint.HumanDistance;
import cgeo.geocaching.geopoint.IConversion;
import cgeo.geocaching.network.HtmlImage;
import cgeo.geocaching.network.Parameters;
-import cgeo.geocaching.ui.Formatter;
import cgeo.geocaching.ui.DecryptTextClickListener;
+import cgeo.geocaching.ui.Formatter;
import cgeo.geocaching.utils.BaseUtils;
import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.ClipboardUtils;
@@ -781,7 +781,7 @@ public class CacheDetailActivity extends AbstractActivity {
@Override
public void run() {
search = cgBase.searchByGeocode(geocode, StringUtils.isBlank(geocode) ? guid : null, 0, false, handler);
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
}
diff --git a/main/src/cgeo/geocaching/SearchResult.java b/main/src/cgeo/geocaching/SearchResult.java
index edda547..bac9c23 100644
--- a/main/src/cgeo/geocaching/SearchResult.java
+++ b/main/src/cgeo/geocaching/SearchResult.java
@@ -5,6 +5,7 @@ import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LoadFlags.LoadFlag;
import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.enumerations.StatusCode;
+import cgeo.geocaching.gcvote.GCVote;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@@ -144,6 +145,7 @@ public class SearchResult implements Parcelable {
SearchResult result = new SearchResult(this);
result.geocodes.clear();
+ final ArrayList<cgCache> cachesForVote = new ArrayList<cgCache>();
final Set<cgCache> caches = cgeoapplication.getInstance().loadCaches(geocodes, LoadFlags.LOAD_CACHE_OR_DB);
for (cgCache cache : caches) {
@@ -153,8 +155,10 @@ public class SearchResult implements Parcelable {
(cacheType != CacheType.ALL && cacheType != cache.getType());
if (!excludeCache) {
result.addCache(cache);
+ cachesForVote.add(cache);
}
}
+ GCVote.loadRatings(cachesForVote);
return result;
}
diff --git a/main/src/cgeo/geocaching/StaticMapsActivity.java b/main/src/cgeo/geocaching/StaticMapsActivity.java
index 3646c37..e7c975c 100644
--- a/main/src/cgeo/geocaching/StaticMapsActivity.java
+++ b/main/src/cgeo/geocaching/StaticMapsActivity.java
@@ -184,7 +184,7 @@ public class StaticMapsActivity extends AbstractActivity {
}
}
- loadMapsHandler.sendMessage(new Message());
+ loadMapsHandler.sendMessage(Message.obtain());
} catch (Exception e) {
Log.e(Settings.tag, "StaticMapsActivity.LoadMapsThread.run: " + e.toString());
}
diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java
index 92eba6f..8db3494 100644
--- a/main/src/cgeo/geocaching/cgBase.java
+++ b/main/src/cgeo/geocaching/cgBase.java
@@ -588,7 +588,7 @@ public class cgBase {
}
}
- // location is reliable because the search return correct coords independant of the login status
+ // location is reliable because the search return correct coordinates independent of the login status
cache.setReliableLatLon(true);
searchResult.addCache(cache);
@@ -639,7 +639,7 @@ public class cgBase {
}
params.put("ctl00$ContentBody$uxDownloadLoc", "Download Waypoints");
- final String coordinates = getResponseData(postRequest("http://www.geocaching.com/seek/nearest.aspx", params));
+ final String coordinates = getResponseData(postRequest("http://www.geocaching.com/seek/nearest.aspx", params), false);
if (StringUtils.isNotBlank(coordinates)) {
if (coordinates.contains("You have not agreed to the license agreement. The license agreement is required before you can start downloading GPX or LOC files from Geocaching.com")) {
@@ -658,8 +658,7 @@ public class cgBase {
}
// get direction images
- if (Settings.getLoadDirImg())
- {
+ if (Settings.getLoadDirImg()) {
final Set<cgCache> caches = cgeoapplication.getInstance().loadCaches(searchResult.getGeocodes(), LoadFlags.LOAD_CACHE_OR_DB);
for (cgCache cache : caches) {
if (cache.getCoords() == null && StringUtils.isNotEmpty(cache.getDirectionImg())) {
@@ -668,33 +667,6 @@ public class cgBase {
}
}
- if (Settings.isRatingWanted()) {
- // get ratings
- if (guids.size() > 0) {
- Log.i(Settings.tag, "Trying to get ratings for " + cids.size() + " caches");
-
- try {
- final Map<String, GCVoteRating> ratings = GCVote.getRating(guids, null);
-
- if (MapUtils.isNotEmpty(ratings)) {
- // save found cache coordinates
- final Set<cgCache> caches = cgeoapplication.getInstance().loadCaches(searchResult.getGeocodes(), LoadFlags.LOAD_CACHE_OR_DB);
- for (cgCache cache : caches) {
- if (ratings.containsKey(cache.getGuid())) {
- GCVoteRating rating = ratings.get(cache.getGuid());
-
- cache.setRating(rating.getRating());
- cache.setVotes(rating.getVotes());
- cache.setMyVote(rating.getMyVote());
- }
- }
- }
- } catch (Exception e) {
- Log.e(Settings.tag, "cgBase.parseSearch.GCvote: " + e.toString());
- }
- }
- }
-
return searchResult;
}
@@ -2246,7 +2218,7 @@ public class cgBase {
return getViewstates(getResponseData(response));
}
- static public String getResponseDataOnError(final HttpResponse response, boolean replaceWhitespace) {
+ static private String getResponseDataNoError(final HttpResponse response, boolean replaceWhitespace) {
try {
String data = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
return replaceWhitespace ? BaseUtils.replaceWhitespace(data) : data;
@@ -2264,7 +2236,7 @@ public class cgBase {
if (!isSuccess(response)) {
return null;
}
- return getResponseDataOnError(response, replaceWhitespace);
+ return getResponseDataNoError(response, replaceWhitespace);
}
/**
@@ -2520,7 +2492,7 @@ public class cgBase {
if (cache == null) {
if (handler != null) {
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
return;
@@ -2578,7 +2550,7 @@ public class cgBase {
cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));
if (handler != null) {
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
} catch (Exception e) {
Log.e(Settings.tag, "cgBase.storeCache");
@@ -2590,7 +2562,7 @@ public class cgBase {
cgeoapplication.getInstance().markDropped(cache.getGeocode());
cgeoapplication.getInstance().removeCache(cache.getGeocode(), EnumSet.of(RemoveFlag.REMOVE_CACHE));
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
} catch (Exception e) {
Log.e(Settings.tag, "cgBase.dropCache: " + e.toString());
}
diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java
index 9defae4..79bb994 100644
--- a/main/src/cgeo/geocaching/cgeocaches.java
+++ b/main/src/cgeo/geocaching/cgeocaches.java
@@ -1750,7 +1750,7 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void run() {
search = cgBase.searchByStored(coords, Settings.getCacheType(), listId);
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
}
@@ -1765,7 +1765,7 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void run() {
search = cgeoapplication.getInstance().getHistoryOfCaches(true, coords != null ? Settings.getCacheType() : CacheType.ALL);
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
}
@@ -1781,7 +1781,7 @@ public class cgeocaches extends AbstractListActivity {
public void run() {
search = cgBase.searchByNextPage(this, search, Settings.isShowCaptcha());
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
}
@@ -1808,7 +1808,7 @@ public class cgeocaches extends AbstractListActivity {
public void run() {
search = cgBase.searchByCoords(this, coords, cacheType, Settings.isShowCaptcha());
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
}
@@ -1834,7 +1834,7 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void run() {
search = cgBase.searchByKeyword(this, keyword, cacheType, Settings.isShowCaptcha());
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
}
@@ -1860,7 +1860,7 @@ public class cgeocaches extends AbstractListActivity {
@Override
public void run() {
search = cgBase.searchByUsername(this, username, cacheType, Settings.isShowCaptcha());
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
}
@@ -1893,7 +1893,7 @@ public class cgeocaches extends AbstractListActivity {
search = cgBase.searchByOwner(this, username, cacheType, Settings.isShowCaptcha());
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
}
@@ -2035,7 +2035,7 @@ public class cgeocaches extends AbstractListActivity {
String GCcode = response;
delay = 1;
- Message mes = new Message();
+ Message mes = Message.obtain();
mes.what = 1;
mes.obj = GCcode;
handler.sendMessage(mes);
@@ -2043,7 +2043,7 @@ public class cgeocaches extends AbstractListActivity {
cgBase.storeCache(cgeocaches.this, null, GCcode, listIdLFW, null);
- Message mes1 = new Message();
+ Message mes1 = Message.obtain();
mes1.what = 2;
mes1.obj = GCcode;
handler.sendMessage(mes1);
diff --git a/main/src/cgeo/geocaching/cgeonavigate.java b/main/src/cgeo/geocaching/cgeonavigate.java
index 57717d4..01a9e74 100644
--- a/main/src/cgeo/geocaching/cgeonavigate.java
+++ b/main/src/cgeo/geocaching/cgeonavigate.java
@@ -422,7 +422,7 @@ public class cgeonavigate extends AbstractActivity {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
if (handler != null) {
- handler.sendMessage(new Message());
+ handler.sendMessage(Message.obtain());
}
try {
diff --git a/main/src/cgeo/geocaching/cgeopopup.java b/main/src/cgeo/geocaching/cgeopopup.java
index e558ede..ac74519 100644
--- a/main/src/cgeo/geocaching/cgeopopup.java
+++ b/main/src/cgeo/geocaching/cgeopopup.java
@@ -365,13 +365,12 @@ public class cgeopopup extends AbstractActivity {
public void run() {
GCVoteRating rating = GCVote.getRating(cache.getGuid(), geocode);
- Message msg = new Message();
- Bundle bundle = new Bundle();
-
if (rating == null) {
return;
}
+ Message msg = Message.obtain();
+ Bundle bundle = new Bundle();
bundle.putFloat("rating", rating.getRating());
bundle.putInt("votes", rating.getVotes());
msg.setData(bundle);
diff --git a/main/src/cgeo/geocaching/cgeowaypoint.java b/main/src/cgeo/geocaching/cgeowaypoint.java
index 3cf2df0..624a9fe 100644
--- a/main/src/cgeo/geocaching/cgeowaypoint.java
+++ b/main/src/cgeo/geocaching/cgeowaypoint.java
@@ -292,7 +292,7 @@ public class cgeowaypoint extends AbstractActivity {
try {
waypoint = app.loadWaypoint(id);
- loadWaypointHandler.sendMessage(new Message());
+ loadWaypointHandler.sendMessage(Message.obtain());
} catch (Exception e) {
Log.e(Settings.tag, "cgeowaypoint.loadWaypoint.run: " + e.toString());
}
diff --git a/main/src/cgeo/geocaching/cgeowaypointadd.java b/main/src/cgeo/geocaching/cgeowaypointadd.java
index fa1d619..e7d00c4 100644
--- a/main/src/cgeo/geocaching/cgeowaypointadd.java
+++ b/main/src/cgeo/geocaching/cgeowaypointadd.java
@@ -266,7 +266,7 @@ public class cgeowaypointadd extends AbstractActivity {
try {
waypoint = app.loadWaypoint(id);
- loadWaypointHandler.sendMessage(new Message());
+ loadWaypointHandler.sendMessage(Message.obtain());
} catch (Exception e) {
Log.e(Settings.tag, "cgeowaypoint.loadWaypoint.run: " + e.toString());
}
diff --git a/main/src/cgeo/geocaching/connector/gc/GCBase.java b/main/src/cgeo/geocaching/connector/gc/GCBase.java
index 8ec758c..a4664aa 100644
--- a/main/src/cgeo/geocaching/connector/gc/GCBase.java
+++ b/main/src/cgeo/geocaching/connector/gc/GCBase.java
@@ -462,9 +462,9 @@ public class GCBase {
*/
private static int[] splitJSONKey(String key) {
// two possible positions for the ,
- int seperator = key.charAt(2) == ',' ? 2 : 3;
- int x = Integer.parseInt(key.substring(1, seperator));
- int y = Integer.parseInt(key.substring(seperator + 2, key.length() - 1));
+ int separator = key.charAt(2) == ',' ? 2 : 3;
+ int x = Integer.parseInt(key.substring(1, separator));
+ int y = Integer.parseInt(key.substring(separator + 2, key.length() - 1));
return new int[] { x, y };
}
diff --git a/main/src/cgeo/geocaching/files/LocParser.java b/main/src/cgeo/geocaching/files/LocParser.java
index 46e1227..6dfc70d 100644
--- a/main/src/cgeo/geocaching/files/LocParser.java
+++ b/main/src/cgeo/geocaching/files/LocParser.java
@@ -8,6 +8,7 @@ import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
+import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointParser;
import cgeo.geocaching.utils.CancellableHandler;
@@ -103,7 +104,7 @@ public final class LocParser extends FileParser {
final Matcher matcherLat = patternLat.matcher(pointString);
final Matcher matcherLon = patternLon.matcher(pointString);
if (matcherLat.find() && matcherLon.find()) {
- pointCoord.setCoords(GeopointParser.parse(matcherLat.group(1).trim(), matcherLon.group(1).trim()));
+ pointCoord.setCoords(parsePoint(matcherLat.group(1).trim(), matcherLon.group(1).trim()));
}
final Matcher matcherDifficulty = patternDifficulty.matcher(pointString);
if (matcherDifficulty.find()) {
@@ -147,6 +148,17 @@ public final class LocParser extends FileParser {
return coords;
}
+ private static Geopoint parsePoint(String latitude, String longitude) {
+ // the loc file contains the coordinates as plain floating point values, therefore avoid using the GeopointParser
+ try {
+ return new Geopoint(Double.valueOf(latitude), Double.valueOf(longitude));
+ } catch (NumberFormatException e) {
+ Log.e(Settings.tag, "LOC format has changed");
+ }
+ // fall back to parser, just in case the format changes
+ return GeopointParser.parse(latitude, longitude);
+ }
+
public LocParser(int listId) {
this.listId = listId;
}
diff --git a/main/src/cgeo/geocaching/gcvote/GCVote.java b/main/src/cgeo/geocaching/gcvote/GCVote.java
index 077add2..663fd20 100644
--- a/main/src/cgeo/geocaching/gcvote/GCVote.java
+++ b/main/src/cgeo/geocaching/gcvote/GCVote.java
@@ -6,6 +6,7 @@ import cgeo.geocaching.cgCache;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.utils.LeastRecentlyUsedCache;
+import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
@@ -223,4 +224,37 @@ public final class GCVote {
return result.trim().equalsIgnoreCase("ok");
}
+
+ public static void loadRatings(ArrayList<cgCache> caches) {
+ if (!Settings.isRatingWanted()) {
+ return;
+ }
+
+ final ArrayList<String> guids = new ArrayList<String>(caches.size());
+ for (final cgCache cache : caches) {
+ String guid = cache.getGuid();
+ if (StringUtils.isNotBlank(guid)) {
+ guids.add(guid);
+ }
+ }
+
+ try {
+ final Map<String, GCVoteRating> ratings = GCVote.getRating(guids, null);
+
+ if (MapUtils.isNotEmpty(ratings)) {
+ // save found cache coordinates
+ for (cgCache cache : caches) {
+ if (ratings.containsKey(cache.getGuid())) {
+ GCVoteRating rating = ratings.get(cache.getGuid());
+
+ cache.setRating(rating.getRating());
+ cache.setVotes(rating.getVotes());
+ cache.setMyVote(rating.getMyVote());
+ }
+ }
+ }
+ } catch (Exception e) {
+ Log.e(Settings.tag, "GCvote.loadRatings: " + e.toString());
+ }
+ }
}
diff --git a/main/src/cgeo/geocaching/ui/CompassView.java b/main/src/cgeo/geocaching/ui/CompassView.java
index 20495c0..6fc576f 100644
--- a/main/src/cgeo/geocaching/ui/CompassView.java
+++ b/main/src/cgeo/geocaching/ui/CompassView.java
@@ -184,7 +184,7 @@ public class CompassView extends View {
cacheHeadingShown = smoothUpdate(cacheHeadingMeasured, cacheHeadingShown);
}
- changeHandler.sendMessage(new Message());
+ changeHandler.sendMessage(Message.obtain());
}
}
}