diff options
Diffstat (limited to 'main/src/cgeo/geocaching/connector/gc')
6 files changed, 68 insertions, 66 deletions
diff --git a/main/src/cgeo/geocaching/connector/gc/GCConnector.java b/main/src/cgeo/geocaching/connector/gc/GCConnector.java index 835359a..5389a36 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); } @@ -189,7 +189,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 +198,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 +215,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 +232,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 +241,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 +250,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 +259,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 +299,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..ce00e87 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,12 @@ 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.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -147,7 +147,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 +308,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 +353,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 +565,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 +594,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 +628,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 +650,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 +663,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 +702,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); @@ -1083,7 +1079,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 +1090,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); @@ -1396,7 +1392,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 +1423,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 +1472,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 +1534,8 @@ public abstract class GCParser { trackable.setTrackingcode(possibleTrackingcode); } - if (cgeoapplication.getInstance() != null) { - cgData.saveTrackable(trackable); + if (CgeoApplication.getInstance() != null) { + DataStore.saveTrackable(trackable); } return trackable; @@ -1658,9 +1654,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..ad07424 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; @@ -76,7 +76,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 +119,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 +172,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) { @@ -218,7 +219,7 @@ public abstract class Login { return false; } - 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 +247,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 +277,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)) { 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); } } |
