aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2011-10-02 18:02:06 +0200
committerBananeweizen <bananeweizen@gmx.de>2011-10-02 18:02:06 +0200
commit03baf34dab919e783ac08838ad71619d556a0c79 (patch)
treee090659011699f90ff6348f50a8b96a4a481aeb5 /main/src
parent521afec96029f7511b9c20509178682d3db61049 (diff)
downloadcgeo-03baf34dab919e783ac08838ad71619d556a0c79.zip
cgeo-03baf34dab919e783ac08838ad71619d556a0c79.tar.gz
cgeo-03baf34dab919e783ac08838ad71619d556a0c79.tar.bz2
fix #552: don't invoke getRating for non GC caches
I had to change a bit of the options handling to be able to extract the gcvote related code into an own stateless class. If you have remarks about that, I'm happy to discuss alternatives of how to change the settings code.
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/GCVote.java189
-rw-r--r--main/src/cgeo/geocaching/LogTemplateProvider.java2
-rw-r--r--main/src/cgeo/geocaching/activity/ActivityMixin.java2
-rw-r--r--main/src/cgeo/geocaching/cgBase.java190
-rw-r--r--main/src/cgeo/geocaching/cgCache.java4
-rw-r--r--main/src/cgeo/geocaching/cgSettings.java84
-rw-r--r--main/src/cgeo/geocaching/cgeocaches.java2
-rw-r--r--main/src/cgeo/geocaching/cgeoinit.java70
-rw-r--r--main/src/cgeo/geocaching/cgeopopup.java32
-rw-r--r--main/src/cgeo/geocaching/cgeotouch.java2
-rw-r--r--main/src/cgeo/geocaching/cgeovisit.java30
-rw-r--r--main/src/cgeo/geocaching/maps/CGeoMap.java2
12 files changed, 322 insertions, 287 deletions
diff --git a/main/src/cgeo/geocaching/GCVote.java b/main/src/cgeo/geocaching/GCVote.java
new file mode 100644
index 0000000..7e271e5
--- /dev/null
+++ b/main/src/cgeo/geocaching/GCVote.java
@@ -0,0 +1,189 @@
+package cgeo.geocaching;
+
+import cgeo.geocaching.utils.CollectionUtils;
+
+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.Map.Entry;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public final class GCVote {
+ private static final Pattern patternLogIn = Pattern.compile("loggedIn='([^']+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern patternGuid = Pattern.compile("cacheId='([^']+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern patternRating = Pattern.compile("voteAvg='([0-9.]+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern patternVotes = Pattern.compile("voteCnt='([0-9]+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern patternVote = Pattern.compile("voteUser='([0-9.]+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern patternVoteElement = Pattern.compile("<vote ([^>]+)>", Pattern.CASE_INSENSITIVE);
+
+ public static cgRating getRating(String guid, String geocode) {
+ List<String> guids = null;
+ List<String> geocodes = null;
+
+ if (StringUtils.isNotBlank(guid)) {
+ guids = new ArrayList<String>();
+ guids.add(guid);
+ } else if (StringUtils.isNotBlank(geocode)) {
+ geocodes = new ArrayList<String>();
+ geocodes.add(geocode);
+ } else {
+ return null;
+ }
+
+ final Map<String, cgRating> ratings = getRating(guids, geocodes);
+ if (ratings != null) {
+ for (Entry<String, cgRating> entry : ratings.entrySet()) {
+ return entry.getValue();
+ }
+ }
+
+ return null;
+ }
+
+ public static Map<String, cgRating> getRating(List<String> guids, List<String> geocodes) {
+ if (guids == null && geocodes == null) {
+ return null;
+ }
+
+ final Map<String, cgRating> ratings = new HashMap<String, cgRating>();
+
+ try {
+ final Parameters params = new Parameters();
+ if (cgSettings.isLogin()) {
+ final Map<String, String> login = cgSettings.getGCvoteLogin();
+ if (login != null) {
+ params.put("userName", login.get("username"));
+ params.put("password", login.get("password"));
+ }
+ }
+ if (CollectionUtils.isNotEmpty(guids)) {
+ params.put("cacheIds", StringUtils.join(guids.toArray(), ','));
+ } else {
+ params.put("waypoints", StringUtils.join(geocodes.toArray(), ','));
+ }
+ params.put("version", "cgeo");
+ final String votes = cgBase.getResponseData(cgBase.request("http://gcvote.com/getVotes.php", params, false, false, false));
+ if (votes == null) {
+ return null;
+ }
+
+ String voteData = null;
+ final Matcher matcherVoteElement = patternVoteElement.matcher(votes);
+ while (matcherVoteElement.find()) {
+ if (matcherVoteElement.groupCount() > 0) {
+ voteData = matcherVoteElement.group(1);
+ }
+
+ if (voteData == null) {
+ continue;
+ }
+
+ String guid = null;
+ cgRating rating = new cgRating();
+ boolean loggedIn = false;
+
+ try {
+ final Matcher matcherGuid = patternGuid.matcher(voteData);
+ if (matcherGuid.find()) {
+ if (matcherGuid.groupCount() > 0) {
+ guid = (String) matcherGuid.group(1);
+ }
+ }
+ } catch (Exception e) {
+ Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse guid");
+ }
+
+ try {
+ final Matcher matcherLoggedIn = patternLogIn.matcher(votes);
+ if (matcherLoggedIn.find()) {
+ if (matcherLoggedIn.groupCount() > 0) {
+ if (matcherLoggedIn.group(1).equalsIgnoreCase("true")) {
+ loggedIn = true;
+ }
+ }
+ }
+ } catch (Exception e) {
+ Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse loggedIn");
+ }
+
+ try {
+ final Matcher matcherRating = patternRating.matcher(voteData);
+ if (matcherRating.find()) {
+ if (matcherRating.groupCount() > 0) {
+ rating.rating = Float.parseFloat(matcherRating.group(1));
+ }
+ }
+ } catch (Exception e) {
+ Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse rating");
+ }
+
+ try {
+ final Matcher matcherVotes = patternVotes.matcher(voteData);
+ if (matcherVotes.find()) {
+ if (matcherVotes.groupCount() > 0) {
+ rating.votes = Integer.parseInt(matcherVotes.group(1));
+ }
+ }
+ } catch (Exception e) {
+ Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse vote count");
+ }
+
+ if (loggedIn) {
+ try {
+ final Matcher matcherVote = patternVote.matcher(voteData);
+ if (matcherVote.find()) {
+ if (matcherVote.groupCount() > 0) {
+ rating.myVote = Float.parseFloat(matcherVote.group(1));
+ }
+ }
+ } catch (Exception e) {
+ Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse user's vote");
+ }
+ }
+
+ if (StringUtils.isNotBlank(guid)) {
+ ratings.put(guid, rating);
+ }
+ }
+ } catch (Exception e) {
+ Log.e(cgSettings.tag, "cgBase.getRating: " + e.toString());
+ }
+
+ return ratings;
+ }
+
+ public static boolean setRating(cgCache cache, double vote) {
+ if (!cache.supportsGCVote()) {
+ return false;
+ }
+ String guid = cache.guid;
+ if (StringUtils.isBlank(guid)) {
+ return false;
+ }
+ if (vote <= 0.0 || vote > 5.0) {
+ return false;
+ }
+
+ final Map<String, String> login = cgSettings.getGCvoteLogin();
+ if (login == null) {
+ return false;
+ }
+
+ final Parameters params = new Parameters();
+ params.put("userName", login.get("username"));
+ params.put("password", login.get("password"));
+ params.put("cacheId", guid);
+ params.put("voteUser", String.format("%.1f", vote).replace(',', '.'));
+ params.put("version", "cgeo");
+
+ final String result = cgBase.getResponseData(cgBase.request("http://gcvote.com/setVote.php", params, false, false, false));
+
+ return result.trim().equalsIgnoreCase("ok");
+ }
+}
diff --git a/main/src/cgeo/geocaching/LogTemplateProvider.java b/main/src/cgeo/geocaching/LogTemplateProvider.java
index 4a11652..e9c1f1d 100644
--- a/main/src/cgeo/geocaching/LogTemplateProvider.java
+++ b/main/src/cgeo/geocaching/LogTemplateProvider.java
@@ -85,7 +85,7 @@ public class LogTemplateProvider {
return "";
}
String findCount = "";
- final String page = cgBase.getResponseData(base.request("http://www.geocaching.com/email/", null, false, false, false));
+ final String page = cgBase.getResponseData(cgBase.request("http://www.geocaching.com/email/", null, false, false, false));
int current = parseFindCount(page);
if (current >= 0) {
diff --git a/main/src/cgeo/geocaching/activity/ActivityMixin.java b/main/src/cgeo/geocaching/activity/ActivityMixin.java
index fd60962..558b51d 100644
--- a/main/src/cgeo/geocaching/activity/ActivityMixin.java
+++ b/main/src/cgeo/geocaching/activity/ActivityMixin.java
@@ -131,7 +131,7 @@ public final class ActivityMixin {
}
cgSettings settings = activity.getSettings();
Resources res = ((Activity) activity).getResources();
- if (settings.isLogin()) {
+ if (cgSettings.isLogin()) {
if (settings.getLogOffline()) {
SubMenu logMenu = menu.addSubMenu(1, IAbstractActivity.MENU_LOG_VISIT_OFFLINE, 0, res.getString(R.string.cache_menu_visit_offline)).setIcon(MENU_ICON_LOG_VISIT);
List<Integer> logTypes = cache.getPossibleLogTypes(settings);
diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java
index 625cda1..6ce31ad 100644
--- a/main/src/cgeo/geocaching/cgBase.java
+++ b/main/src/cgeo/geocaching/cgBase.java
@@ -81,7 +81,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
-import java.util.Map.Entry;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -93,6 +92,10 @@ import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
+/**
+ * @author bananeweizen
+ *
+ */
public class cgBase {
private final static Pattern patternType = Pattern.compile("<img src=\"[^\"]*/WptTypes/\\d+\\.gif\" alt=\"([^\"]+)\" (title=\"[^\"]*\" )?width=\"32\" height=\"32\"[^>]*>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
@@ -188,7 +191,12 @@ public class cgBase {
private cgeoapplication app = null;
private cgSettings settings = null;
public String version = null;
- private String idBrowser = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.86 Safari/533.4";
+
+ /**
+ * FIXME: browser id should become part of settings (where it can be created more easily depending on the current
+ * settings)
+ */
+ private static String idBrowser = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.86 Safari/533.4";
Context context = null;
final private static Map<String, Integer> gcIcons = new HashMap<String, Integer>();
final private static Map<String, Integer> wpIcons = new HashMap<String, Integer>();
@@ -364,7 +372,7 @@ public class cgBase {
version = null;
}
- if (settings.asBrowser == 1) {
+ if (cgSettings.asBrowser()) {
final long rndBrowser = Math.round(Math.random() * 6);
switch ((int) rndBrowser) {
case 0:
@@ -953,7 +961,7 @@ public class cgBase {
Log.i(cgSettings.tag, "Trying to get ratings for " + cids.size() + " caches");
try {
- final Map<String, cgRating> ratings = getRating(guids, null);
+ final Map<String, cgRating> ratings = GCVote.getRating(guids, null);
if (CollectionUtils.isNotEmpty(ratings)) {
// save found cache coordinates
@@ -1532,7 +1540,7 @@ public class cgBase {
cache.elevation = getElevation(cache.coords);
}
- final cgRating rating = getRating(cache.guid, cache.geocode);
+ final cgRating rating = GCVote.getRating(cache.guid, cache.geocode);
if (rating != null) {
cache.rating = rating.rating;
cache.votes = rating.votes;
@@ -1727,149 +1735,6 @@ public class cgBase {
}
}
- public cgRating getRating(String guid, String geocode) {
- List<String> guids = null;
- List<String> geocodes = null;
-
- if (StringUtils.isNotBlank(guid)) {
- guids = new ArrayList<String>();
- guids.add(guid);
- } else if (StringUtils.isNotBlank(geocode)) {
- geocodes = new ArrayList<String>();
- geocodes.add(geocode);
- } else {
- return null;
- }
-
- final Map<String, cgRating> ratings = getRating(guids, geocodes);
- if (ratings != null) {
- for (Entry<String, cgRating> entry : ratings.entrySet()) {
- return entry.getValue();
- }
- }
-
- return null;
- }
-
- public Map<String, cgRating> getRating(List<String> guids, List<String> geocodes) {
- if (guids == null && geocodes == null) {
- return null;
- }
-
- final Map<String, cgRating> ratings = new HashMap<String, cgRating>();
-
- try {
- final Parameters params = new Parameters();
- if (settings.isLogin()) {
- final Map<String, String> login = settings.getGCvoteLogin();
- if (login != null) {
- params.put("userName", login.get("username"));
- params.put("password", login.get("password"));
- }
- }
- if (CollectionUtils.isNotEmpty(guids)) {
- params.put("cacheIds", StringUtils.join(guids.toArray(), ','));
- } else {
- params.put("waypoints", StringUtils.join(geocodes.toArray(), ','));
- }
- params.put("version", "cgeo");
- final String votes = getResponseData(request("http://gcvote.com/getVotes.php", params, false, false, false));
- if (votes == null) {
- return null;
- }
-
- final Pattern patternLogIn = Pattern.compile("loggedIn='([^']+)'", Pattern.CASE_INSENSITIVE);
- final Pattern patternGuid = Pattern.compile("cacheId='([^']+)'", Pattern.CASE_INSENSITIVE);
- final Pattern patternRating = Pattern.compile("voteAvg='([0-9.]+)'", Pattern.CASE_INSENSITIVE);
- final Pattern patternVotes = Pattern.compile("voteCnt='([0-9]+)'", Pattern.CASE_INSENSITIVE);
- final Pattern patternVote = Pattern.compile("voteUser='([0-9.]+)'", Pattern.CASE_INSENSITIVE);
-
- String voteData = null;
- final Pattern patternVoteElement = Pattern.compile("<vote ([^>]+)>", Pattern.CASE_INSENSITIVE);
- final Matcher matcherVoteElement = patternVoteElement.matcher(votes);
- while (matcherVoteElement.find()) {
- if (matcherVoteElement.groupCount() > 0) {
- voteData = matcherVoteElement.group(1);
- }
-
- if (voteData == null) {
- continue;
- }
-
- String guid = null;
- cgRating rating = new cgRating();
- boolean loggedIn = false;
-
- try {
- final Matcher matcherGuid = patternGuid.matcher(voteData);
- if (matcherGuid.find()) {
- if (matcherGuid.groupCount() > 0) {
- guid = (String) matcherGuid.group(1);
- }
- }
- } catch (Exception e) {
- Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse guid");
- }
-
- try {
- final Matcher matcherLoggedIn = patternLogIn.matcher(votes);
- if (matcherLoggedIn.find()) {
- if (matcherLoggedIn.groupCount() > 0) {
- if (matcherLoggedIn.group(1).equalsIgnoreCase("true")) {
- loggedIn = true;
- }
- }
- }
- } catch (Exception e) {
- Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse loggedIn");
- }
-
- try {
- final Matcher matcherRating = patternRating.matcher(voteData);
- if (matcherRating.find()) {
- if (matcherRating.groupCount() > 0) {
- rating.rating = Float.parseFloat(matcherRating.group(1));
- }
- }
- } catch (Exception e) {
- Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse rating");
- }
-
- try {
- final Matcher matcherVotes = patternVotes.matcher(voteData);
- if (matcherVotes.find()) {
- if (matcherVotes.groupCount() > 0) {
- rating.votes = Integer.parseInt(matcherVotes.group(1));
- }
- }
- } catch (Exception e) {
- Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse vote count");
- }
-
- if (loggedIn) {
- try {
- final Matcher matcherVote = patternVote.matcher(voteData);
- if (matcherVote.find()) {
- if (matcherVote.groupCount() > 0) {
- rating.myVote = Float.parseFloat(matcherVote.group(1));
- }
- }
- } catch (Exception e) {
- Log.w(cgSettings.tag, "cgBase.getRating: Failed to parse user's vote");
- }
- }
-
- if (StringUtils.isNotBlank(guid)) {
- ratings.put(guid, rating);
- }
- }
- } catch (Exception e) {
- Log.e(cgSettings.tag, "cgBase.getRating: " + e.toString());
- }
-
- return ratings;
- }
-
public cgTrackable parseTrackable(String page) {
if (StringUtils.isBlank(page)) {
Log.e(cgSettings.tag, "cgeoBase.parseTrackable: No page given");
@@ -2603,14 +2468,14 @@ public class cgBase {
return null;
}
- List<cgCache> cacheList = processSearchResults(search, caches, settings.excludeDisabled, settings.excludeMine, settings.cacheType);
+ List<cgCache> cacheList = processSearchResults(search, caches, settings.excludeDisabled, cgSettings.getExcludeMine() ? 1 : 0, settings.cacheType);
app.addSearch(search, cacheList, true, reason);
return search.getCurrentId();
}
- private String requestJSONgc(final String uri, final String params) {
+ private static String requestJSONgc(final String uri, final String params) {
String page;
final HttpPost request = new HttpPost("http://www.geocaching.com/map/default.aspx/MapAction");
try {
@@ -2687,6 +2552,16 @@ public class cgBase {
return users;
}
+ /**
+ * FIXME: excludeDisabled, excludeMine should be boolean after settings rework
+ *
+ * @param search
+ * @param caches
+ * @param excludeDisabled
+ * @param excludeMine
+ * @param cacheType
+ * @return
+ */
public static List<cgCache> processSearchResults(final cgSearch search, final cgCacheWrap caches, final int excludeDisabled, final int excludeMine, final String cacheType) {
List<cgCache> cacheList = new ArrayList<cgCache>();
if (caches != null) {
@@ -3130,8 +3005,8 @@ public class cgBase {
return encoded;
}
- public String prepareParameters(final Parameters params, final boolean my, final boolean addF) {
- if (my != true && settings.excludeMine > 0 && addF) {
+ public static String prepareParameters(final Parameters params, final boolean my, final boolean addF) {
+ if (!my && cgSettings.getExcludeMine() && addF) {
if (params == null) {
return "f=1";
}
@@ -3143,8 +3018,9 @@ public class cgBase {
}
static private String prepareParameters(final Parameters params) {
- if (params == null)
+ if (params == null) {
return "";
+ }
return URLEncodedUtils.format(params, HTTP.UTF_8);
}
@@ -3200,7 +3076,7 @@ public class cgBase {
return data;
}
- public HttpResponse request(final String uri, final Parameters params, boolean xContentType, boolean my, boolean addF) {
+ public static 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);
}
@@ -3253,7 +3129,7 @@ public class cgBase {
}
}
- public HttpResponse request(final String uri, final String params, final Boolean xContentType) {
+ public static HttpResponse request(final String uri, final String params, final Boolean xContentType) {
final HttpRequestBase request = new HttpGet(Uri.parse(uri).buildUpon().encodedQuery(params).build().toString());
request.setHeader("X-Requested-With", "XMLHttpRequest");
@@ -3265,8 +3141,8 @@ public class cgBase {
return request(request);
}
- private HttpResponse request(final HttpRequestBase request) {
- if (settings.asBrowser == 1) {
+ private static HttpResponse request(final HttpRequestBase request) {
+ if (cgSettings.asBrowser()) {
request.setHeader("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7");
request.setHeader("Accept-Language", "en-US");
request.getParams().setParameter(CoreProtocolPNames.USER_AGENT, idBrowser);
diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java
index af3def1..c56d0f7 100644
--- a/main/src/cgeo/geocaching/cgCache.java
+++ b/main/src/cgeo/geocaching/cgCache.java
@@ -503,4 +503,8 @@ public class cgCache implements ICache {
return getConnector().getCacheUrl(this);
}
+ public boolean supportsGCVote() {
+ return StringUtils.startsWithIgnoreCase(geocode, "GC");
+ }
+
}
diff --git a/main/src/cgeo/geocaching/cgSettings.java b/main/src/cgeo/geocaching/cgSettings.java
index 2a79313..32af792 100644
--- a/main/src/cgeo/geocaching/cgSettings.java
+++ b/main/src/cgeo/geocaching/cgSettings.java
@@ -136,11 +136,11 @@ public class cgSettings {
public int maptrail = 1;
public boolean useEnglish = false;
public boolean showCaptcha = false;
- public int excludeMine = 0;
+ // public int excludeMine = 0;
public int excludeDisabled = 0;
public int storeOfflineMaps = 0;
public boolean storelogimages = false;
- public int asBrowser = 1;
+ // public int asBrowser = 1;
public int useCompass = 1;
public int useGNavigation = 1;
public int showAddress = 1;
@@ -164,6 +164,12 @@ public class cgSettings {
// private variables
private Context context = null;
private SharedPreferences prefs = null;
+ /**
+ * FIXME: this static instance of the preferences is only needed as long as the cgSettings class has
+ * not yet been reworked to a singleton. It makes it possible to gradually rework all preference access methods
+ * to be static.
+ */
+ private static SharedPreferences sharedPrefs = null;
private String username = null;
private String password = null;
private coordInputFormatEnum coordInput = coordInputFormatEnum.Plain;
@@ -179,6 +185,7 @@ public class cgSettings {
public cgSettings(Context contextIn, SharedPreferences prefsIn) {
context = contextIn;
prefs = prefsIn;
+ sharedPrefs = prefsIn;
load();
}
@@ -199,11 +206,9 @@ public class cgSettings {
maptrail = prefs.getInt(KEY_MAP_TRAIL, 1);
useEnglish = prefs.getBoolean(KEY_USE_ENGLISH, false);
showCaptcha = prefs.getBoolean(KEY_SHOW_CAPTCHA, false);
- excludeMine = prefs.getInt(KEY_EXCLUDE_OWN, 0);
excludeDisabled = prefs.getInt(KEY_EXCLUDE_DISABLED, 0);
storeOfflineMaps = prefs.getInt("offlinemaps", 1);
storelogimages = prefs.getBoolean(KEY_STORE_LOG_IMAGES, false);
- asBrowser = prefs.getInt(KEY_AS_BROWSER, 1);
useCompass = prefs.getInt(KEY_USE_COMPASS, 1);
useGNavigation = prefs.getInt(KEY_USE_GOOGLE_NAVIGATION, 1);
showAddress = prefs.getInt(KEY_SHOW_ADDRESS, 1);
@@ -269,15 +274,11 @@ public class cgSettings {
}
}
- public boolean isLogin() {
- final String preUsername = prefs.getString(KEY_USERNAME, null);
- final String prePassword = prefs.getString(KEY_PASSWORD, null);
+ public static boolean isLogin() {
+ final String preUsername = sharedPrefs.getString(KEY_USERNAME, null);
+ final String prePassword = sharedPrefs.getString(KEY_PASSWORD, null);
- if (StringUtils.isBlank(preUsername) || StringUtils.isBlank(prePassword)) {
- return false;
- } else {
- return true;
- }
+ return (!StringUtils.isBlank(preUsername) && !StringUtils.isBlank(prePassword));
}
/**
@@ -366,25 +367,17 @@ public class cgSettings {
});
}
- public Map<String, String> getGCvoteLogin() {
- final Map<String, String> login = new HashMap<String, String>();
+ public static Map<String, String> getGCvoteLogin() {
+ final String preUsername = sharedPrefs.getString(KEY_USERNAME, null);
+ final String prePassword = sharedPrefs.getString(KEY_GCVOTE_PASSWORD, null);
- if (username == null || password == null) {
- final String preUsername = prefs.getString(KEY_USERNAME, null);
- final String prePassword = prefs.getString(KEY_GCVOTE_PASSWORD, null);
-
- if (preUsername == null || prePassword == null) {
- return null;
- }
-
- login.put(KEY_USERNAME, preUsername);
- login.put(KEY_PASSWORD, prePassword);
-
- username = preUsername;
- } else {
- login.put(KEY_USERNAME, username);
- login.put(KEY_PASSWORD, password);
+ if (StringUtils.isBlank(preUsername) || StringUtils.isBlank(prePassword)) {
+ return null;
}
+ final Map<String, String> login = new HashMap<String, String>();
+
+ login.put(KEY_USERNAME, preUsername);
+ login.put(KEY_PASSWORD, prePassword);
return login;
}
@@ -615,4 +608,37 @@ public class cgSettings {
});
}
+ public static boolean asBrowser() {
+ return sharedPrefs.getInt(KEY_AS_BROWSER, 1) == 1;
+ }
+
+ public static boolean getExcludeMine() {
+ return sharedPrefs.getInt(KEY_EXCLUDE_OWN, 0) == 1;
+ }
+
+ private static boolean editSharedSettings(final PrefRunnable runnable) {
+ final SharedPreferences.Editor prefsEdit = sharedPrefs.edit();
+ runnable.edit(prefsEdit);
+ return prefsEdit.commit();
+ }
+
+ public static void setExcludeMine(final boolean exclude) {
+ editSharedSettings(new PrefRunnable() {
+
+ @Override
+ public void edit(Editor edit) {
+ edit.putInt(KEY_EXCLUDE_OWN, exclude ? 1 : 0);
+ }
+ });
+ }
+
+ public static void setAsBrowser(final boolean asBrowser) {
+ editSharedSettings(new PrefRunnable() {
+
+ @Override
+ public void edit(Editor edit) {
+ edit.putInt(KEY_AS_BROWSER, asBrowser ? 1 : 0);
+ }
+ });
+ }
}
diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java
index 8707884..9b166f8 100644
--- a/main/src/cgeo/geocaching/cgeocaches.java
+++ b/main/src/cgeo/geocaching/cgeocaches.java
@@ -2055,7 +2055,7 @@ public class cgeocaches extends AbstractListActivity {
if (deviceCode == null) {
deviceCode = "";
}
- HttpResponse responseFromWeb = base.request("http://send2.cgeo.org/read.html", "code=" + cgBase.urlencode_rfc3986(deviceCode), true);
+ HttpResponse responseFromWeb = cgBase.request("http://send2.cgeo.org/read.html", "code=" + cgBase.urlencode_rfc3986(deviceCode), true);
if (responseFromWeb.getStatusLine().getStatusCode() == 200) {
final String response = cgBase.getResponseData(responseFromWeb);
diff --git a/main/src/cgeo/geocaching/cgeoinit.java b/main/src/cgeo/geocaching/cgeoinit.java
index 6e5104f..ce0217c 100644
--- a/main/src/cgeo/geocaching/cgeoinit.java
+++ b/main/src/cgeo/geocaching/cgeoinit.java
@@ -342,13 +342,19 @@ public class cgeoinit extends AbstractActivity {
}
useEnglishButton.setOnClickListener(new cgeoChangeUseEnglish());
- CheckBox excludeButton = (CheckBox) findViewById(R.id.exclude);
+ final CheckBox excludeButton = (CheckBox) findViewById(R.id.exclude);
if (prefs.getInt("excludemine", 0) == 0) {
excludeButton.setChecked(false);
} else {
excludeButton.setChecked(true);
}
- excludeButton.setOnClickListener(new cgeoChangeExclude());
+ excludeButton.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ cgSettings.setExcludeMine(excludeButton.isChecked());
+ }
+ });
CheckBox disabledButton = (CheckBox) findViewById(R.id.disabled);
if (prefs.getInt("excludedisabled", 0) == 0) {
@@ -428,13 +434,19 @@ public class cgeoinit extends AbstractActivity {
}
});
- CheckBox browserButton = (CheckBox) findViewById(R.id.browser);
+ final CheckBox browserButton = (CheckBox) findViewById(R.id.browser);
if (prefs.getInt("asbrowser", 1) == 0) {
browserButton.setChecked(false);
} else {
browserButton.setChecked(true);
}
- browserButton.setOnClickListener(new cgeoChangeBrowser());
+ browserButton.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ cgSettings.setAsBrowser(browserButton.isChecked());
+ }
+ });
// Altitude settings
EditText altitudeEdit = (EditText) findViewById(R.id.altitude);
@@ -740,30 +752,6 @@ public class cgeoinit extends AbstractActivity {
}
}
- private class cgeoChangeExclude implements View.OnClickListener {
-
- public void onClick(View arg0) {
- SharedPreferences.Editor edit = prefs.edit();
- if (prefs.getInt("excludemine", 0) == 0) {
- edit.putInt("excludemine", 1);
- settings.excludeMine = 1;
- } else {
- edit.putInt("excludemine", 0);
- settings.excludeMine = 0;
- }
- edit.commit();
-
- CheckBox excludeButton = (CheckBox) findViewById(R.id.exclude);
- if (prefs.getInt("excludemine", 0) == 0) {
- excludeButton.setChecked(false);
- } else {
- excludeButton.setChecked(true);
- }
-
- return;
- }
- }
-
private class cgeoChangeAutovisit implements View.OnClickListener {
public void onClick(View arg0) {
@@ -980,30 +968,6 @@ public class cgeoinit extends AbstractActivity {
}
}
- private class cgeoChangeBrowser implements View.OnClickListener {
-
- public void onClick(View arg0) {
- SharedPreferences.Editor edit = prefs.edit();
- if (prefs.getInt("asbrowser", 1) == 0) {
- edit.putInt("asbrowser", 1);
- settings.asBrowser = 1;
- } else {
- edit.putInt("asbrowser", 0);
- settings.asBrowser = 0;
- }
- edit.commit();
-
- CheckBox browserButton = (CheckBox) findViewById(R.id.browser);
- if (prefs.getInt("asbrowser", 1) == 0) {
- browserButton.setChecked(false);
- } else {
- browserButton.setChecked(true);
- }
-
- return;
- }
- }
-
private class cgeoChangeMapSource implements OnItemSelectedListener {
@Override
@@ -1078,7 +1042,7 @@ public class cgeoinit extends AbstractActivity {
String params = "name=" + cgBase.urlencode_rfc3986(nam) + "&code=" + cgBase.urlencode_rfc3986(cod);
- HttpResponse response = base.request("http://send2.cgeo.org/auth.html", params, true);
+ HttpResponse response = cgBase.request("http://send2.cgeo.org/auth.html", params, true);
if (response.getStatusLine().getStatusCode() == 200)
{
diff --git a/main/src/cgeo/geocaching/cgeopopup.java b/main/src/cgeo/geocaching/cgeopopup.java
index c6448d9..a74e681 100644
--- a/main/src/cgeo/geocaching/cgeopopup.java
+++ b/main/src/cgeo/geocaching/cgeopopup.java
@@ -156,7 +156,7 @@ public class cgeopopup extends AbstractActivity {
menu.findItem(5).setVisible(false);
}
- boolean visitPossible = fromDetail == false && settings.isLogin();
+ boolean visitPossible = fromDetail == false && cgSettings.isLogin();
menu.findItem(MENU_LOG_VISIT).setEnabled(visitPossible);
} catch (Exception e) {
// nothing
@@ -363,25 +363,27 @@ public class cgeopopup extends AbstractActivity {
if (cache.rating != null && cache.rating > 0) {
setRating(cache.rating, cache.votes);
} else {
- (new Thread() {
+ if (cache.supportsGCVote()) {
+ (new Thread() {
- public void run() {
- cgRating rating = base.getRating(cache.guid, geocode);
+ public void run() {
+ cgRating rating = GCVote.getRating(cache.guid, geocode);
- Message msg = new Message();
- Bundle bundle = new Bundle();
+ Message msg = new Message();
+ Bundle bundle = new Bundle();
- if (rating == null || rating.rating == null) {
- return;
- }
+ if (rating == null || rating.rating == null) {
+ return;
+ }
- bundle.putFloat("rating", rating.rating);
- bundle.putInt("votes", rating.votes);
- msg.setData(bundle);
+ bundle.putFloat("rating", rating.rating);
+ bundle.putInt("votes", rating.votes);
+ msg.setData(bundle);
- ratingHandler.sendMessage(msg);
- }
- }).start();
+ ratingHandler.sendMessage(msg);
+ }
+ }).start();
+ }
}
// more details
diff --git a/main/src/cgeo/geocaching/cgeotouch.java b/main/src/cgeo/geocaching/cgeotouch.java
index baf085c..f423d87 100644
--- a/main/src/cgeo/geocaching/cgeotouch.java
+++ b/main/src/cgeo/geocaching/cgeotouch.java
@@ -384,7 +384,7 @@ public class cgeotouch extends cgLogForm {
return;
}
- final String page = cgBase.getResponseData(base.request("http://www.geocaching.com/track/log.aspx", params, false, false, false));
+ final String page = cgBase.getResponseData(cgBase.request("http://www.geocaching.com/track/log.aspx", params, false, false, false));
viewstates = cgBase.getViewstates(page);
diff --git a/main/src/cgeo/geocaching/cgeovisit.java b/main/src/cgeo/geocaching/cgeovisit.java
index e500adb..f9575ba 100644
--- a/main/src/cgeo/geocaching/cgeovisit.java
+++ b/main/src/cgeo/geocaching/cgeovisit.java
@@ -30,7 +30,6 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
-import java.util.Map;
public class cgeovisit extends cgLogForm {
static final String EXTRAS_FOUND = "found";
@@ -360,31 +359,6 @@ public class cgeovisit extends cgLogForm {
return String.format(Locale.getDefault(), "%.1f", rating);
}
- public boolean setRating(String guid, double vote) {
- if (StringUtils.isBlank(guid)) {
- return false;
- }
- if (vote < 0.0 || vote > 5.0) {
- return false;
- }
-
- final Map<String, String> login = settings.getGCvoteLogin();
- if (login == null) {
- return false;
- }
-
- final Parameters params = new Parameters();
- params.put("userName", login.get("username"));
- params.put("password", login.get("password"));
- params.put("cacheId", guid);
- params.put("voteUser", String.format("%.1f", rating).replace(',', '.'));
- params.put("version", "cgeo");
-
- final String result = cgBase.getResponseData(base.request("http://gcvote.com/setVote.php", params, false, false, false));
-
- return result.trim().equalsIgnoreCase("ok");
- }
-
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo info) {
super.onCreateContextMenu(menu, view, info);
@@ -712,7 +686,7 @@ public class cgeovisit extends cgLogForm {
return;
}
- final String page = cgBase.getResponseData(base.request("http://www.geocaching.com/seek/log.aspx", params, false, false, false));
+ final String page = cgBase.getResponseData(cgBase.request("http://www.geocaching.com/seek/log.aspx", params, false, false, false));
viewstates = cgBase.getViewstates(page);
trackables = cgBase.parseTrackableLog(page);
@@ -804,7 +778,7 @@ public class cgeovisit extends cgLogForm {
}
if (status == 1 && typeSelected == cgBase.LOG_FOUND_IT && settings.isGCvoteLogin()) {
- setRating(cache.guid, rating);
+ GCVote.setRating(cache, rating);
}
return status;
diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java
index 3af820b..4772f89 100644
--- a/main/src/cgeo/geocaching/maps/CGeoMap.java
+++ b/main/src/cgeo/geocaching/maps/CGeoMap.java
@@ -1145,7 +1145,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory
//if in live map and stored caches are found / disables are also shown.
if (live && settings.maplive >= 1) {
- final boolean excludeMine = settings.excludeMine > 0;
+ final boolean excludeMine = cgSettings.getExcludeMine();
final boolean excludeDisabled = settings.excludeDisabled > 0;
for (int i = caches.size() - 1; i >= 0; i--) {