aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgCache.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/cgCache.java')
-rw-r--r--main/src/cgeo/geocaching/cgCache.java156
1 files changed, 77 insertions, 79 deletions
diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java
index 7035d65..7fbbf0c 100644
--- a/main/src/cgeo/geocaching/cgCache.java
+++ b/main/src/cgeo/geocaching/cgCache.java
@@ -38,7 +38,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;
@@ -66,7 +65,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 +97,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 +225,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;
}
@@ -294,12 +289,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 +310,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 +344,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 +381,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 +445,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 +461,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 +541,10 @@ public class cgCache implements ICache, IWaypoint {
return getConnector().supportsLogging();
}
+ public boolean supportsOwnCoordinates() {
+ return getConnector().supportsOwnCoordinates();
+ }
+
@Override
public float getDifficulty() {
return difficulty;
@@ -603,7 +610,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 +668,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 +707,6 @@ public class cgCache implements ICache, IWaypoint {
this.favorite = favourite;
}
-
@Override
public boolean isWatchlist() {
return onWatchlist;
@@ -809,14 +815,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 +956,7 @@ public class cgCache implements ICache, IWaypoint {
}
}
}
-
- return saveToDatabase && cgeoapplication.getInstance().saveWaypoints(this);
+ return saveToDatabase && cgData.saveWaypoints(this);
}
/**
@@ -1015,7 +1012,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 +1163,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 +1197,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 +1248,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 +1281,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 +1292,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) {
@@ -1391,8 +1391,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 +1402,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 +1516,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 +1538,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;
}
@@ -1613,8 +1612,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;
}