diff options
Diffstat (limited to 'main/src/cgeo/geocaching/connector/gc')
7 files changed, 103 insertions, 75 deletions
diff --git a/main/src/cgeo/geocaching/connector/gc/GCConnector.java b/main/src/cgeo/geocaching/connector/gc/GCConnector.java index 835359a..111c865 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCConnector.java +++ b/main/src/cgeo/geocaching/connector/gc/GCConnector.java @@ -1,11 +1,12 @@ package cgeo.geocaching.connector.gc; +import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.DataStore; import cgeo.geocaching.Geocache; import cgeo.geocaching.ICache; +import cgeo.geocaching.LogCacheActivity; import cgeo.geocaching.R; import cgeo.geocaching.SearchResult; -import cgeo.geocaching.cgData; -import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.connector.AbstractConnector; import cgeo.geocaching.connector.ILoggingManager; import cgeo.geocaching.connector.capability.ILogin; @@ -15,15 +16,14 @@ import cgeo.geocaching.connector.capability.ISearchByViewPort; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Viewport; -import cgeo.geocaching.settings.SettingsActivity; import cgeo.geocaching.settings.Settings; +import cgeo.geocaching.settings.SettingsActivity; import cgeo.geocaching.utils.CancellableHandler; import cgeo.geocaching.utils.Log; -import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import android.app.Activity; import android.content.Context; import android.os.Handler; @@ -104,7 +104,7 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, } @Override - public ILoggingManager getLoggingManager(Activity activity, Geocache cache) { + public ILoggingManager getLoggingManager(final LogCacheActivity activity, final Geocache cache) { return new GCLoggingManager(activity, cache); } @@ -137,10 +137,10 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, if (StringUtils.isEmpty(page)) { final SearchResult search = new SearchResult(); - if (cgData.isThere(geocode, guid, true, false)) { + if (DataStore.isThere(geocode, guid, true, false)) { if (StringUtils.isBlank(geocode) && StringUtils.isNotBlank(guid)) { Log.i("Loading old cache from cache."); - search.addGeocode(cgData.getGeocodeForGuid(guid)); + search.addGeocode(DataStore.getGeocodeForGuid(guid)); } else { search.addGeocode(geocode); } @@ -152,6 +152,7 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, search.setError(StatusCode.COMMUNICATION_ERROR); return search; } + assert page != null; final SearchResult searchResult = GCParser.parseCache(page, handler); @@ -189,7 +190,7 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, public boolean addToWatchlist(Geocache cache) { final boolean added = GCParser.addToWatchlist(cache); if (added) { - cgData.saveChangedCache(cache); + DataStore.saveChangedCache(cache); } return added; } @@ -198,7 +199,7 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, public boolean removeFromWatchlist(Geocache cache) { final boolean removed = GCParser.removeFromWatchlist(cache); if (removed) { - cgData.saveChangedCache(cache); + DataStore.saveChangedCache(cache); } return removed; } @@ -215,7 +216,7 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, public static boolean addToFavorites(Geocache cache) { final boolean added = GCParser.addToFavorites(cache); if (added) { - cgData.saveChangedCache(cache); + DataStore.saveChangedCache(cache); } return added; } @@ -232,7 +233,7 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, public static boolean removeFromFavorites(Geocache cache) { final boolean removed = GCParser.removeFromFavorites(cache); if (removed) { - cgData.saveChangedCache(cache); + DataStore.saveChangedCache(cache); } return removed; } @@ -241,7 +242,7 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, public boolean uploadModifiedCoordinates(Geocache cache, Geopoint wpt) { final boolean uploaded = GCParser.uploadModifiedCoordinates(cache, wpt); if (uploaded) { - cgData.saveChangedCache(cache); + DataStore.saveChangedCache(cache); } return uploaded; } @@ -250,7 +251,7 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, public boolean deleteModifiedCoordinates(Geocache cache) { final boolean deleted = GCParser.deleteModifiedCoordinates(cache); if (deleted) { - cgData.saveChangedCache(cache); + DataStore.saveChangedCache(cache); } return deleted; } @@ -259,7 +260,7 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, public boolean uploadPersonalNote(Geocache cache) { final boolean uploaded = GCParser.uploadPersonalNote(cache); if (uploaded) { - cgData.saveChangedCache(cache); + DataStore.saveChangedCache(cache); } return uploaded; } @@ -299,13 +300,13 @@ public class GCConnector extends AbstractConnector implements ISearchByGeocode, final StatusCode status = Login.login(); if (status == StatusCode.NO_ERROR) { - cgeoapplication.getInstance().checkLogin = false; + CgeoApplication.getInstance().checkLogin = false; Login.detectGcCustomDate(); } - if (cgeoapplication.getInstance().showLoginToast && handler != null) { + if (CgeoApplication.getInstance().showLoginToast && handler != null) { handler.sendMessage(handler.obtainMessage(0, status)); - cgeoapplication.getInstance().showLoginToast = false; + CgeoApplication.getInstance().showLoginToast = false; // invoke settings activity to insert login details if (status == StatusCode.NO_LOGIN_INFO_STORED && fromActivity != null) { diff --git a/main/src/cgeo/geocaching/connector/gc/GCLoggingManager.java b/main/src/cgeo/geocaching/connector/gc/GCLoggingManager.java index 2aa5c75..8bed2ea 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCLoggingManager.java +++ b/main/src/cgeo/geocaching/connector/gc/GCLoggingManager.java @@ -3,7 +3,6 @@ package cgeo.geocaching.connector.gc; import cgeo.geocaching.Geocache; import cgeo.geocaching.LogCacheActivity; import cgeo.geocaching.R; -import cgeo.geocaching.settings.Settings; import cgeo.geocaching.TrackableLog; import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.connector.ILoggingManager; @@ -13,12 +12,13 @@ import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.loaders.UrlLoader; import cgeo.geocaching.network.Parameters; +import cgeo.geocaching.settings.Settings; import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.eclipse.jdt.annotation.Nullable; -import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.LoaderManager; @@ -38,11 +38,12 @@ public class GCLoggingManager implements ILoggingManager, LoaderManager.LoaderCa private List<LogType> possibleLogTypes; private boolean hasLoaderError = true; - public GCLoggingManager(Activity activity, Geocache cache) { - this.activity = (LogCacheActivity) activity; + public GCLoggingManager(final LogCacheActivity activity, final Geocache cache) { + this.activity = activity; this.cache = cache; } + @Nullable @Override public Loader<String> onCreateLoader(int arg0, Bundle arg1) { if (!Settings.isLogin()) { // allow offline logging diff --git a/main/src/cgeo/geocaching/connector/gc/GCMap.java b/main/src/cgeo/geocaching/connector/gc/GCMap.java index 4fdde56..e2c7dfa 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCMap.java +++ b/main/src/cgeo/geocaching/connector/gc/GCMap.java @@ -1,9 +1,9 @@ package cgeo.geocaching.connector.gc; +import cgeo.geocaching.DataStore; import cgeo.geocaching.Geocache; import cgeo.geocaching.SearchResult; -import cgeo.geocaching.cgData; -import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LiveMapStrategy.Strategy; @@ -19,7 +19,7 @@ import cgeo.geocaching.ui.Formatter; import cgeo.geocaching.utils.LeastRecentlyUsedMap; import cgeo.geocaching.utils.Log; -import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONException; @@ -236,7 +236,9 @@ public class GCMap { } Log.d("Retrieved " + searchResult.getCount() + " caches for tile " + tile.toString()); - } catch (Exception e) { + } catch (RuntimeException e) { + Log.e("GCMap.parseMapJSON", e); + } catch (JSONException e) { Log.e("GCMap.parseMapJSON", e); } @@ -253,7 +255,7 @@ public class GCMap { * @return */ public static SearchResult searchByViewport(final Viewport viewport, final String[] tokens) { - int speed = (int) cgeoapplication.getInstance().currentGeo().getSpeed() * 60 * 60 / 1000; // in km/h + int speed = (int) CgeoApplication.getInstance().currentGeo().getSpeed() * 60 * 60 / 1000; // in km/h Strategy strategy = Settings.getLiveMapStrategy(); if (strategy == Strategy.AUTO) { strategy = speed >= 30 ? Strategy.FAST : Strategy.DETAILED; @@ -359,7 +361,7 @@ public class GCMap { // Check for vanished found caches if (tiles.iterator().next().getZoomLevel() >= Tile.ZOOMLEVEL_MIN_PERSONALIZED) { - searchResult.addFilteredGeocodes(cgData.getCachedMissingFromSearch(searchResult, tiles, GCConnector.getInstance(), Tile.ZOOMLEVEL_MIN_PERSONALIZED - 1)); + searchResult.addFilteredGeocodes(DataStore.getCachedMissingFromSearch(searchResult, tiles, GCConnector.getInstance(), Tile.ZOOMLEVEL_MIN_PERSONALIZED - 1)); } } @@ -370,7 +372,7 @@ public class GCMap { SearchResult search = GCParser.searchByCoords(center, Settings.getCacheType(), false, null); if (search != null && !search.isEmpty()) { final Set<String> geocodes = search.getGeocodes(); - lastSearchViewport = cgData.getBounds(geocodes); + lastSearchViewport = DataStore.getBounds(geocodes); searchResult.addGeocodes(geocodes); } } diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java index b373cb1..71f8045 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCParser.java +++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java @@ -1,5 +1,7 @@ package cgeo.geocaching.connector.gc; +import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.DataStore; import cgeo.geocaching.Geocache; import cgeo.geocaching.Image; import cgeo.geocaching.LogEntry; @@ -8,8 +10,6 @@ import cgeo.geocaching.SearchResult; import cgeo.geocaching.Trackable; import cgeo.geocaching.TrackableLog; import cgeo.geocaching.Waypoint; -import cgeo.geocaching.cgData; -import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LoadFlags; @@ -34,12 +34,14 @@ import cgeo.geocaching.utils.MatcherWrapper; import cgeo.geocaching.utils.TextUtils; import ch.boye.httpclientandroidlib.HttpResponse; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.ListUtils; + +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -147,7 +149,7 @@ public abstract class GCParser { } } } - } catch (final Exception e) { + } catch (final RuntimeException e) { // failed to parse GUID and/or Disabled Log.w("GCParser.parseSearch: Failed to parse GUID and/or Disabled data"); } @@ -308,19 +310,15 @@ public abstract class GCParser { final String coordinates = Network.getResponseData(Network.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")) { - Log.i("User has not agreed to the license agreement. Can\'t download .loc file."); - - searchResult.setError(StatusCode.UNAPPROVED_LICENSE); - - return searchResult; - } + if (StringUtils.contains(coordinates, "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")) { + Log.i("User has not agreed to the license agreement. Can\'t download .loc file."); + searchResult.setError(StatusCode.UNAPPROVED_LICENSE); + return searchResult; } LocParser.parseLoc(searchResult, coordinates); - } catch (final Exception e) { + } catch (final RuntimeException e) { Log.e("GCParser.parseSearch.CIDs", e); } } @@ -357,7 +355,7 @@ public abstract class GCParser { // save full detailed caches CancellableHandler.sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_cache); - cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); + DataStore.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); // update progress message so user knows we're still working. This is more of a place holder than // actual indication of what the program is doing @@ -569,7 +567,7 @@ public abstract class GCParser { } cache.setAttributes(attributes); } - } catch (final Exception e) { + } catch (final RuntimeException e) { // failed to parse cache attributes Log.w("GCParser.parseCache: Failed to parse cache attributes"); } @@ -598,7 +596,7 @@ public abstract class GCParser { } cache.addSpoiler(new Image(url, title, description)); } - } catch (final Exception e) { + } catch (final RuntimeException e) { // failed to parse cache spoilers Log.w("GCParser.parseCache: Failed to parse cache spoilers"); } @@ -632,7 +630,7 @@ public abstract class GCParser { } } } - } catch (final Exception e) { + } catch (final RuntimeException e) { // failed to parse cache inventory Log.w("GCParser.parseCache: Failed to parse cache inventory (2)"); } @@ -654,7 +652,7 @@ public abstract class GCParser { } } } - } catch (final Exception e) { + } catch (final NumberFormatException e) { // failed to parse logs Log.w("GCParser.parseCache: Failed to parse cache log count"); } @@ -667,7 +665,7 @@ public abstract class GCParser { final String originalCoords = TextUtils.getMatch(page, GCConstants.PATTERN_LATLON_ORIG, false, null); if (null != originalCoords) { - final Waypoint waypoint = new Waypoint(cgeoapplication.getInstance().getString(R.string.cache_coordinates_original), WaypointType.ORIGINAL, false); + final Waypoint waypoint = new Waypoint(CgeoApplication.getInstance().getString(R.string.cache_coordinates_original), WaypointType.ORIGINAL, false); waypoint.setCoords(new Geopoint(originalCoords)); cache.addOrChangeWaypoint(waypoint, false); cache.setUserModifiedCoords(true); @@ -706,7 +704,7 @@ public abstract class GCParser { // waypoint name // res is null during the unit tests - final String name = TextUtils.getMatch(wp[6], GCConstants.PATTERN_WPNAME, true, 1, cgeoapplication.getInstance().getString(R.string.waypoint), true); + final String name = TextUtils.getMatch(wp[6], GCConstants.PATTERN_WPNAME, true, 1, CgeoApplication.getInstance().getString(R.string.waypoint), true); // waypoint type final String resulttype = TextUtils.getMatch(wp[3], GCConstants.PATTERN_WPTYPE, null); @@ -847,6 +845,7 @@ public abstract class GCParser { * @param recaptchaReceiver * @return */ + @Nullable private static SearchResult searchByAny(final CacheType cacheType, final boolean my, final boolean showCaptcha, final Parameters params, RecaptchaReceiver recaptchaReceiver) { insertCacheType(params, cacheType); @@ -858,6 +857,7 @@ public abstract class GCParser { Log.e("GCParser.searchByAny: No data from server"); return null; } + assert page != null; final SearchResult searchResult = parseSearch(fullUri, page, showCaptcha, recaptchaReceiver); if (searchResult == null || CollectionUtils.isEmpty(searchResult.getGeocodes())) { @@ -944,6 +944,7 @@ public abstract class GCParser { return null; } + @Nullable public static Trackable 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"); @@ -968,6 +969,7 @@ public abstract class GCParser { Log.e("GCParser.searchTrackable: No data from server"); return trackable; } + assert page != null; trackable = parseTrackable(page, geocode); if (trackable == null) { @@ -1083,7 +1085,7 @@ public abstract class GCParser { page = Network.getResponseData(Network.postRequest(uri, params)); } - } catch (final Exception e) { + } catch (final RuntimeException e) { Log.e("GCParser.postLog.confim", e); } @@ -1094,7 +1096,7 @@ public abstract class GCParser { Log.i("Log successfully posted to cache #" + cacheid); if (geocode != null) { - cgData.saveVisitDate(geocode); + DataStore.saveVisitDate(geocode); } Login.getLoginStatus(page); @@ -1136,6 +1138,7 @@ public abstract class GCParser { Log.e("GCParser.uploadLogImage: No data from server"); return new ImmutablePair<StatusCode, String>(StatusCode.UNKNOWN_ERROR, null); } + assert page != null; final String[] viewstates = Login.getViewstates(page); @@ -1293,7 +1296,8 @@ public abstract class GCParser { return !guidOnPage; // on watch list (=error) / not on watch list } - static String requestHtmlPage(final String geocode, final String guid, final String log, final String numlogs) { + @Nullable + static String requestHtmlPage(@Nullable final String geocode, @Nullable final String guid, final String log, final String numlogs) { final Parameters params = new Parameters("decrypt", "y"); if (StringUtils.isNotBlank(geocode)) { params.put("wp", geocode); @@ -1396,7 +1400,7 @@ public abstract class GCParser { trackable.setOwnerGuid(matcherOwner.group(1)); trackable.setOwner(matcherOwner.group(2).trim()); } - } catch (final Exception e) { + } catch (final RuntimeException e) { // failed to parse trackable owner name Log.w("GCParser.parseTrackable: Failed to parse trackable owner name"); } @@ -1427,7 +1431,7 @@ public abstract class GCParser { if (TextUtils.matches(page, GCConstants.PATTERN_TRACKABLE_SPOTTEDOWNER)) { trackable.setSpottedType(Trackable.SPOTTED_OWNER); } - } catch (final Exception e) { + } catch (final RuntimeException e) { // failed to parse trackable last known place Log.w("GCParser.parseTrackable: Failed to parse trackable last known place"); } @@ -1476,12 +1480,12 @@ public abstract class GCParser { trackable.setDetails(convertLinks(details)); } } - } catch (final Exception e) { + } catch (final RuntimeException e) { // failed to parse trackable details & image Log.w("GCParser.parseTrackable: Failed to parse trackable details & image"); } if (StringUtils.isEmpty(trackable.getDetails()) && page.contains(GCConstants.ERROR_TB_NOT_ACTIVATED)) { - trackable.setDetails(cgeoapplication.getInstance().getString(R.string.trackable_not_activated)); + trackable.setDetails(CgeoApplication.getInstance().getString(R.string.trackable_not_activated)); } // trackable logs @@ -1538,8 +1542,8 @@ public abstract class GCParser { trackable.setTrackingcode(possibleTrackingcode); } - if (cgeoapplication.getInstance() != null) { - cgData.saveTrackable(trackable); + if (CgeoApplication.getInstance() != null) { + DataStore.saveTrackable(trackable); } return trackable; @@ -1601,8 +1605,17 @@ public abstract class GCParser { rawResponse = TextUtils.getMatch(page, GCConstants.PATTERN_LOGBOOK, ""); } + return parseLogs(friends, rawResponse); + } + + private static List<LogEntry> parseLogs(final boolean friends, String rawResponse) { final List<LogEntry> logs = new ArrayList<LogEntry>(); + // for non logged in users the log book is not shown + if (StringUtils.isBlank(rawResponse)) { + return logs; + } + try { final JSONObject resp = new JSONObject(rawResponse); if (!resp.getString("status").equals("success")) { @@ -1658,9 +1671,10 @@ public abstract class GCParser { return logs; } + @NonNull public static List<LogType> parseTypes(String page) { if (StringUtils.isEmpty(page)) { - return ListUtils.EMPTY_LIST; + return Collections.emptyList(); } final List<LogType> types = new ArrayList<LogType>(); diff --git a/main/src/cgeo/geocaching/connector/gc/Login.java b/main/src/cgeo/geocaching/connector/gc/Login.java index 0d8fb05..beb49f1 100644 --- a/main/src/cgeo/geocaching/connector/gc/Login.java +++ b/main/src/cgeo/geocaching/connector/gc/Login.java @@ -1,7 +1,7 @@ package cgeo.geocaching.connector.gc; +import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.R; -import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.network.Cookies; import cgeo.geocaching.network.HtmlImage; @@ -13,10 +13,11 @@ import cgeo.geocaching.utils.MatcherWrapper; import cgeo.geocaching.utils.TextUtils; import ch.boye.httpclientandroidlib.HttpResponse; - import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import android.graphics.drawable.BitmapDrawable; @@ -76,7 +77,7 @@ public abstract class Login { return StatusCode.NO_LOGIN_INFO_STORED; } - Login.setActualStatus(cgeoapplication.getInstance().getString(R.string.init_login_popup_working)); + Login.setActualStatus(CgeoApplication.getInstance().getString(R.string.init_login_popup_working)); HttpResponse loginResponse = Network.getRequest("https://www.geocaching.com/login/default.aspx"); String loginData = Network.getResponseData(loginResponse); if (loginResponse != null && loginResponse.getStatusLine().getStatusCode() == 503 && TextUtils.matches(loginData, GCConstants.PATTERN_MAINTENANCE)) { @@ -119,6 +120,7 @@ public abstract class Login { // FIXME: should it be CONNECTION_FAILED to match the first attempt? return StatusCode.COMMUNICATION_ERROR; // no login page } + assert loginData != null; // Caught above if (Login.getLoginStatus(loginData)) { Log.i("Successfully logged in Geocaching.com as " + login.left + " (" + Settings.getMemberStatus() + ')'); @@ -171,7 +173,7 @@ public abstract class Login { resetLoginStatus(); setActualCachesFound(-1); - setActualStatus(cgeoapplication.getInstance().getString(R.string.err_login)); + setActualStatus(CgeoApplication.getInstance().getString(R.string.err_login)); } static void setActualCachesFound(final int found) { @@ -212,13 +214,14 @@ public abstract class Login { * @param page * @return <code>true</code> if user is logged in, <code>false</code> otherwise */ - public static boolean getLoginStatus(final String page) { + public static boolean getLoginStatus(@Nullable final String page) { if (StringUtils.isBlank(page)) { Log.e("Login.checkLogin: No page given"); return false; } + assert page != null; - setActualStatus(cgeoapplication.getInstance().getString(R.string.init_login_popup_ok)); + setActualStatus(CgeoApplication.getInstance().getString(R.string.init_login_popup_ok)); // on every page except login page setActualLoginStatus(TextUtils.matches(page, GCConstants.PATTERN_LOGIN_NAME)); @@ -246,7 +249,7 @@ public abstract class Login { return true; } - setActualStatus(cgeoapplication.getInstance().getString(R.string.init_login_popup_failed)); + setActualStatus(CgeoApplication.getInstance().getString(R.string.init_login_popup_failed)); return false; } @@ -276,7 +279,8 @@ public abstract class Login { public static BitmapDrawable downloadAvatarAndGetMemberStatus() { try { - final String profile = TextUtils.replaceWhitespace(Network.getResponseData(Network.getRequest("http://www.geocaching.com/my/"))); + final String responseData = StringUtils.defaultString(Network.getResponseData(Network.getRequest("http://www.geocaching.com/my/"))); + final String profile = TextUtils.replaceWhitespace(responseData); Settings.setMemberStatus(TextUtils.getMatch(profile, GCConstants.PATTERN_MEMBER_STATUS, true, null)); if (profile.contains(GCConstants.MEMBER_STATUS_RENEW)) { @@ -473,10 +477,13 @@ public abstract class Login { * @param params * @return */ - public static String getRequestLogged(final String uri, final Parameters params) { - final String data = Network.getResponseData(Network.getRequest(uri, params), canRemoveWhitespace(uri)); + @Nullable + public static String getRequestLogged(@NonNull final String uri, @Nullable final Parameters params) { + final HttpResponse response = Network.getRequest(uri, params); + final String data = Network.getResponseData(response, canRemoveWhitespace(uri)); - if (getLoginStatus(data)) { + // A page not found will not be found if the user logs in either + if (Network.isPageNotFound(response) || getLoginStatus(data)) { return data; } diff --git a/main/src/cgeo/geocaching/connector/gc/SearchHandler.java b/main/src/cgeo/geocaching/connector/gc/SearchHandler.java index 4358399..795ed2f 100644 --- a/main/src/cgeo/geocaching/connector/gc/SearchHandler.java +++ b/main/src/cgeo/geocaching/connector/gc/SearchHandler.java @@ -91,7 +91,7 @@ public class SearchHandler extends Handler { dlg.create().show(); } - } catch (Exception e) { + } catch (MalformedURLException e) { Log.e("Error in reCAPTCHA handler", e); } } diff --git a/main/src/cgeo/geocaching/connector/gc/UTFGridPosition.java b/main/src/cgeo/geocaching/connector/gc/UTFGridPosition.java index 5965fff..eff193a 100644 --- a/main/src/cgeo/geocaching/connector/gc/UTFGridPosition.java +++ b/main/src/cgeo/geocaching/connector/gc/UTFGridPosition.java @@ -15,9 +15,12 @@ public final class UTFGridPosition { private final static Pattern PATTERN_JSON_KEY = Pattern.compile("[^\\d]*" + "(\\d+),\\s*(\\d+)" + "[^\\d]*"); // (12, 34) public UTFGridPosition(final int x, final int y) { - assert x >= 0 && x <= UTFGrid.GRID_MAXX : "x outside bounds"; - assert y >= 0 && y <= UTFGrid.GRID_MAXY : "y outside bounds"; - + if (x < 0 || x > UTFGrid.GRID_MAXX) { + throw new IllegalArgumentException("x outside bounds"); + } + if (y < 0 || y > UTFGrid.GRID_MAXY) { + throw new IllegalArgumentException("y outside bounds"); + } this.x = x; this.y = y; } |
