diff options
Diffstat (limited to 'main/src/cgeo/geocaching/cgCache.java')
| -rw-r--r-- | main/src/cgeo/geocaching/cgCache.java | 175 |
1 files changed, 90 insertions, 85 deletions
diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index 7035d65..8f867b5 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -11,6 +11,7 @@ import cgeo.geocaching.connector.gc.GCConnector; import cgeo.geocaching.connector.gc.GCConstants; import cgeo.geocaching.connector.gc.Tile; import cgeo.geocaching.enumerations.CacheAttribute; +import cgeo.geocaching.enumerations.CacheRealm; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LoadFlags; @@ -27,6 +28,7 @@ import cgeo.geocaching.utils.LazyInitializedList; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.LogTemplateProvider; import cgeo.geocaching.utils.LogTemplateProvider.LogContext; +import cgeo.geocaching.utils.MatcherWrapper; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -38,7 +40,6 @@ import android.net.Uri; import android.os.Handler; import android.os.Message; import android.text.Html; -import android.text.Spannable; import java.util.ArrayList; import java.util.Calendar; @@ -48,7 +49,6 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; import java.util.regex.Pattern; /** @@ -66,7 +66,6 @@ public class cgCache implements ICache, IWaypoint { private String guid = ""; private CacheType cacheType = CacheType.UNKNOWN; private String name = ""; - private Spannable nameSp = null; private String ownerDisplayName = ""; private String ownerUserId = ""; private Date hidden = null; @@ -99,20 +98,20 @@ public class cgCache implements ICache, IWaypoint { private LazyInitializedList<String> attributes = new LazyInitializedList<String>() { @Override protected List<String> loadFromDatabase() { - return cgeoapplication.getInstance().loadAttributes(geocode); + return cgData.loadAttributes(geocode); } }; private LazyInitializedList<cgWaypoint> waypoints = new LazyInitializedList<cgWaypoint>() { @Override protected List<cgWaypoint> loadFromDatabase() { - return cgeoapplication.getInstance().loadWaypoints(geocode); + return cgData.loadWaypoints(geocode); } }; private List<cgImage> spoilers = null; private LazyInitializedList<LogEntry> logs = new LazyInitializedList<LogEntry>() { @Override protected List<LogEntry> loadFromDatabase() { - return cgeoapplication.getInstance().loadLogs(geocode); + return cgData.loadLogs(geocode); } }; private List<cgTrackable> inventory = null; @@ -227,9 +226,6 @@ public class cgCache implements ICache, IWaypoint { if (StringUtils.isBlank(name)) { name = other.name; } - if (StringUtils.isBlank(nameSp)) { - nameSp = other.nameSp; - } if (StringUtils.isBlank(ownerDisplayName)) { ownerDisplayName = other.ownerDisplayName; } @@ -278,6 +274,8 @@ public class cgCache implements ICache, IWaypoint { if (StringUtils.isBlank(description)) { description = other.description; } + // FIXME: this makes no sense to favor this over the other. 0 should not be a special case here as it is + // in the range of acceptable values. This is probably the case at other places (rating, votes, etc.) too. if (favoritePoints == 0) { favoritePoints = other.favoritePoints; } @@ -294,12 +292,12 @@ public class cgCache implements ICache, IWaypoint { attributes.set(other.attributes); } if (waypoints.isEmpty()) { - waypoints.set(other.waypoints); + this.setWaypoints(other.waypoints.asList(), false); } else { ArrayList<cgWaypoint> newPoints = new ArrayList<cgWaypoint>(waypoints.asList()); - cgWaypoint.mergeWayPoints(newPoints, other.getWaypoints(), false); - waypoints.set(newPoints); + cgWaypoint.mergeWayPoints(newPoints, other.waypoints.asList(), false); + this.setWaypoints(newPoints, false); } if (spoilers == null) { spoilers = other.spoilers; @@ -315,12 +313,21 @@ public class cgCache implements ICache, IWaypoint { if (logs.isEmpty()) { // keep last known logs if none logs.set(other.logs); } - if (logCounts.size() == 0) { + if (logCounts.isEmpty()) { logCounts = other.logCounts; } - if (!userModifiedCoords) { - userModifiedCoords = other.userModifiedCoords; + + // if cache has ORIGINAL type waypoint ... it is considered that it has modified coordinates, otherwise not + userModifiedCoords = false; + if (waypoints != null) { + for (cgWaypoint wpt : waypoints) { + if (wpt.getWaypointType() == WaypointType.ORIGINAL) { + userModifiedCoords = true; + break; + } + } } + if (!reliableLatLon) { reliableLatLon = other.reliableLatLon; } @@ -340,7 +347,8 @@ public class cgCache implements ICache, IWaypoint { /** * Compare two caches quickly. For map and list fields only the references are compared ! * - * @param other the other cache to compare this one to + * @param other + * the other cache to compare this one to * @return true if both caches have the same content */ private boolean isEqualTo(final cgCache other) { @@ -376,7 +384,6 @@ public class cgCache implements ICache, IWaypoint { (direction != null ? direction.equals(other.direction) : null == other.direction) && (distance != null ? distance.equals(other.distance) : null == other.distance) && (elevation != null ? elevation.equals(other.elevation) : null == other.elevation) && - nameSp == other.nameSp && rating == other.rating && votes == other.votes && myVote == other.myVote && @@ -441,7 +448,7 @@ public class cgCache implements ICache, IWaypoint { } Intent logVisitIntent = new Intent((Activity) fromActivity, VisitCacheActivity.class); logVisitIntent.putExtra(VisitCacheActivity.EXTRAS_ID, cacheId); - logVisitIntent.putExtra(VisitCacheActivity.EXTRAS_GEOCODE, geocode.toUpperCase()); + logVisitIntent.putExtra(VisitCacheActivity.EXTRAS_GEOCODE, geocode); logVisitIntent.putExtra(VisitCacheActivity.EXTRAS_FOUND, found); ((Activity) fromActivity).startActivity(logVisitIntent); @@ -457,13 +464,12 @@ public class cgCache implements ICache, IWaypoint { if (logType == LogType.UNKNOWN) { return; } - cgeoapplication app = (cgeoapplication) fromActivity.getApplication(); - final boolean status = app.saveLogOffline(geocode, date.getTime(), logType, log); + final boolean status = cgData.saveLogOffline(geocode, date.getTime(), logType, log); Resources res = fromActivity.getResources(); if (status) { ActivityMixin.showToast(fromActivity, res.getString(R.string.info_log_saved)); - app.saveVisitDate(geocode); + cgData.saveVisitDate(geocode); logOffline = true; notifyChange(); @@ -538,6 +544,14 @@ public class cgCache implements ICache, IWaypoint { return getConnector().supportsLogging(); } + public boolean supportsOwnCoordinates() { + return getConnector().supportsOwnCoordinates(); + } + + public CacheRealm getCacheRealm() { + return getConnector().getCacheRealm(); + } + @Override public float getDifficulty() { return difficulty; @@ -603,7 +617,7 @@ public class cgCache implements ICache, IWaypoint { @Override public String getDescription() { if (description == null) { - description = StringUtils.defaultString(cgeoapplication.getInstance().getCacheDescription(geocode)); + description = StringUtils.defaultString(cgData.getCacheDescription(geocode)); } return description; } @@ -661,7 +675,7 @@ public class cgCache implements ICache, IWaypoint { } StringBuilder subject = new StringBuilder("Geocache "); - subject.append(geocode.toUpperCase()); + subject.append(geocode); if (StringUtils.isNotBlank(name)) { subject.append(" - ").append(name); } @@ -700,7 +714,6 @@ public class cgCache implements ICache, IWaypoint { this.favorite = favourite; } - @Override public boolean isWatchlist() { return onWatchlist; @@ -749,7 +762,7 @@ public class cgCache implements ICache, IWaypoint { @Override public String getNameForSorting() { if (null == nameForSorting) { - final Matcher matcher = NUMBER_PATTERN.matcher(name); + final MatcherWrapper matcher = new MatcherWrapper(NUMBER_PATTERN, name); if (matcher.find()) { nameForSorting = name.replace(matcher.group(), StringUtils.leftPad(matcher.group(), 6, '0')); } @@ -809,14 +822,6 @@ public class cgCache implements ICache, IWaypoint { this.detailed = detailed; } - public Spannable getNameSp() { - return nameSp; - } - - public void setNameSp(Spannable nameSp) { - this.nameSp = nameSp; - } - public void setHidden(final Date hidden) { if (hidden == null) { this.hidden = null; @@ -958,8 +963,7 @@ public class cgCache implements ICache, IWaypoint { } } } - - return saveToDatabase && cgeoapplication.getInstance().saveWaypoints(this); + return saveToDatabase && cgData.saveWaypoints(this); } /** @@ -1015,7 +1019,7 @@ public class cgCache implements ICache, IWaypoint { } public void setGeocode(String geocode) { - this.geocode = geocode; + this.geocode = StringUtils.upperCase(geocode); } public void setCacheId(String cacheId) { @@ -1166,8 +1170,7 @@ public class cgCache implements ICache, IWaypoint { // when waypoint was edited, finalDefined may have changed resetFinalDefined(); } - - return saveToDatabase && cgeoapplication.getInstance().saveWaypoint(waypoint.getId(), geocode, waypoint); + return saveToDatabase && cgData.saveWaypoint(waypoint.getId(), geocode, waypoint); } public boolean hasWaypoints() { @@ -1201,43 +1204,47 @@ public class cgCache implements ICache, IWaypoint { } public void setUserModifiedCoords(boolean coordsChanged) { - this.userModifiedCoords = coordsChanged; + userModifiedCoords = coordsChanged; } /** * Duplicate a waypoint. * - * @param index the waypoint to duplicate + * @param original + * the waypoint to duplicate * @return <code>true</code> if the waypoint was duplicated, <code>false</code> otherwise (invalid index) */ - public boolean duplicateWaypoint(final int index) { - final cgWaypoint original = getWaypoint(index); + public boolean duplicateWaypoint(final cgWaypoint original) { if (original == null) { return false; } + final int index = getWaypointIndex(original); final cgWaypoint copy = new cgWaypoint(original); copy.setUserDefined(); copy.setName(cgeoapplication.getInstance().getString(R.string.waypoint_copy_of) + " " + copy.getName()); waypoints.add(index + 1, copy); - return cgeoapplication.getInstance().saveWaypoint(-1, geocode, copy); + return cgData.saveWaypoint(-1, geocode, copy); } /** * delete a user defined waypoint * - * @param index - * of the waypoint in cache's waypoint list + * @param waypoint + * to be removed from cache * @return <code>true</code>, if the waypoint was deleted */ - public boolean deleteWaypoint(final int index) { - final cgWaypoint waypoint = getWaypoint(index); + public boolean deleteWaypoint(final cgWaypoint waypoint) { if (waypoint == null) { return false; } + if (waypoint.getId() <= 0) { + return false; + } if (waypoint.isUserDefined()) { + final int index = getWaypointIndex(waypoint); waypoints.remove(index); - cgeoapplication.getInstance().deleteWaypoint(waypoint.getId()); - cgeoapplication.getInstance().removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE)); + cgData.deleteWaypoint(waypoint.getId()); + cgData.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE)); // Check status if Final is defined if (waypoint.isFinalWithCoords()) { resetFinalDefined(); @@ -1248,19 +1255,17 @@ public class cgCache implements ICache, IWaypoint { } /** - * delete a user defined waypoint + * deletes any waypoint * * @param waypoint - * to be removed from cache - * @return <code>true</code>, if the waypoint was deleted */ - public boolean deleteWaypoint(final cgWaypoint waypoint) { - if (waypoint.getId() <= 0) { - return false; - } + public void deleteWaypointForce(cgWaypoint waypoint) { final int index = getWaypointIndex(waypoint); - return index >= 0 && deleteWaypoint(index); + waypoints.remove(index); + cgData.deleteWaypoint(waypoint.getId()); + cgData.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE)); + resetFinalDefined(); } /** @@ -1283,7 +1288,8 @@ public class cgCache implements ICache, IWaypoint { /** * Retrieve a given waypoint. * - * @param index the index of the waypoint + * @param index + * the index of the waypoint * @return waypoint or <code>null</code> if index is out of range */ public cgWaypoint getWaypoint(final int index) { @@ -1293,7 +1299,8 @@ public class cgCache implements ICache, IWaypoint { /** * Lookup a waypoint by its id. * - * @param id the id of the waypoint to look for + * @param id + * the id of the waypoint to look for * @return waypoint or <code>null</code> */ public cgWaypoint getWaypointById(final int id) { @@ -1313,7 +1320,7 @@ public class cgCache implements ICache, IWaypoint { final Pattern coordPattern = Pattern.compile("\\b[nNsS]{1}\\s*\\d"); // begin of coordinates int count = 1; String note = getPersonalNote(); - Matcher matcher = coordPattern.matcher(note); + MatcherWrapper matcher = new MatcherWrapper(coordPattern, note); while (matcher.find()) { try { final Geopoint point = new Geopoint(note.substring(matcher.start())); @@ -1330,7 +1337,7 @@ public class cgCache implements ICache, IWaypoint { } note = note.substring(matcher.start() + 1); - matcher = coordPattern.matcher(note); + matcher = new MatcherWrapper(coordPattern, note); } } catch (Exception e) { Log.e("cgCache.parseWaypointsFromNote: " + e.toString()); @@ -1391,8 +1398,8 @@ public class cgCache implements ICache, IWaypoint { public void drop(Handler handler) { try { - cgeoapplication.getInstance().markDropped(Collections.singletonList(this)); - cgeoapplication.getInstance().removeCache(getGeocode(), EnumSet.of(RemoveFlag.REMOVE_CACHE)); + cgData.markDropped(Collections.singletonList(this)); + cgData.removeCache(getGeocode(), EnumSet.of(RemoveFlag.REMOVE_CACHE)); handler.sendMessage(Message.obtain()); } catch (Exception e) { @@ -1402,48 +1409,48 @@ public class cgCache implements ICache, IWaypoint { public void checkFields() { if (StringUtils.isBlank(getGeocode())) { - Log.e("geo code not parsed correctly"); + Log.w("geo code not parsed correctly for " + geocode); } if (StringUtils.isBlank(getName())) { - Log.e("name not parsed correctly"); + Log.w("name not parsed correctly for " + geocode); } if (StringUtils.isBlank(getGuid())) { - Log.e("guid not parsed correctly"); + Log.w("guid not parsed correctly for " + geocode); } if (getTerrain() == 0.0) { - Log.e("terrain not parsed correctly"); + Log.w("terrain not parsed correctly for " + geocode); } if (getDifficulty() == 0.0) { - Log.e("difficulty not parsed correctly"); + Log.w("difficulty not parsed correctly for " + geocode); } if (StringUtils.isBlank(getOwnerDisplayName())) { - Log.e("owner display name not parsed correctly"); + Log.w("owner display name not parsed correctly for " + geocode); } if (StringUtils.isBlank(getOwnerUserId())) { - Log.e("owner user id real not parsed correctly"); + Log.w("owner user id real not parsed correctly for " + geocode); } if (getHiddenDate() == null) { - Log.e("hidden not parsed correctly"); + Log.w("hidden not parsed correctly for " + geocode); } if (getFavoritePoints() < 0) { - Log.e("favoriteCount not parsed correctly"); + Log.w("favoriteCount not parsed correctly for " + geocode); } if (getSize() == null) { - Log.e("size not parsed correctly"); + Log.w("size not parsed correctly for " + geocode); } if (getType() == null || getType() == CacheType.UNKNOWN) { - Log.e("type not parsed correctly"); + Log.w("type not parsed correctly for " + geocode); } if (getCoords() == null) { - Log.e("coordinates not parsed correctly"); + Log.w("coordinates not parsed correctly for " + geocode); } if (StringUtils.isBlank(getLocation())) { - Log.e("location not parsed correctly"); + Log.w("location not parsed correctly for " + geocode); } } public void refresh(int newListId, CancellableHandler handler) { - cgeoapplication.getInstance().removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE)); + cgData.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE)); storeCache(null, geocode, newListId, true, handler); } @@ -1516,7 +1523,7 @@ public class cgCache implements ICache, IWaypoint { } cache.setListId(listId); - cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); + cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); if (CancellableHandler.isCancelled(handler)) { return; @@ -1538,10 +1545,9 @@ public class cgCache implements ICache, IWaypoint { return null; } - final cgeoapplication app = cgeoapplication.getInstance(); - if (!forceReload && listId == StoredList.TEMPORARY_LIST_ID && (app.isOffline(geocode, guid) || app.isThere(geocode, guid, true, true))) { + if (!forceReload && listId == StoredList.TEMPORARY_LIST_ID && (cgData.isOffline(geocode, guid) || cgData.isThere(geocode, guid, true, true))) { final SearchResult search = new SearchResult(); - final String realGeocode = StringUtils.isNotBlank(geocode) ? geocode : app.getGeocode(guid); + final String realGeocode = StringUtils.isNotBlank(geocode) ? geocode : cgData.getGeocodeForGuid(guid); search.addGeocode(realGeocode); return search; } @@ -1573,7 +1579,7 @@ public class cgCache implements ICache, IWaypoint { } // 12:34 final Pattern time = Pattern.compile("\\b(\\d{1,2})\\:(\\d\\d)\\b"); - final Matcher matcher = time.matcher(getDescription()); + final MatcherWrapper matcher = new MatcherWrapper(time, getDescription()); while (matcher.find()) { try { final int hours = Integer.valueOf(matcher.group(1)); @@ -1589,7 +1595,7 @@ public class cgCache implements ICache, IWaypoint { final String hourLocalized = cgeoapplication.getInstance().getString(R.string.cache_time_full_hours); if (StringUtils.isNotBlank(hourLocalized)) { final Pattern fullHours = Pattern.compile("\\b(\\d{1,2})\\s+" + Pattern.quote(hourLocalized), Pattern.CASE_INSENSITIVE); - final Matcher matcherHours = fullHours.matcher(getDescription()); + final MatcherWrapper matcherHours = new MatcherWrapper(fullHours, getDescription()); if (matcherHours.find()) { try { final int hours = Integer.valueOf(matcherHours.group(1)); @@ -1613,8 +1619,7 @@ public class cgCache implements ICache, IWaypoint { * @return */ public boolean hasAttribute(CacheAttribute attribute, boolean yes) { - // lazy loading of attributes - cgCache fullCache = cgeoapplication.getInstance().loadCache(getGeocode(), EnumSet.of(LoadFlag.LOAD_ATTRIBUTES)); + cgCache fullCache = cgData.loadCache(getGeocode(), EnumSet.of(LoadFlag.LOAD_ATTRIBUTES)); if (fullCache == null) { fullCache = this; } |
