aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo')
-rw-r--r--main/src/cgeo/geocaching/CacheDetailActivity.java52
-rw-r--r--main/src/cgeo/geocaching/StaticMapsProvider.java3
-rw-r--r--main/src/cgeo/geocaching/apps/AbstractLocusApp.java4
-rw-r--r--main/src/cgeo/geocaching/cgBase.java34
-rw-r--r--main/src/cgeo/geocaching/cgCache.java79
-rw-r--r--main/src/cgeo/geocaching/cgData.java15
-rw-r--r--main/src/cgeo/geocaching/cgWaypoint.java12
-rw-r--r--main/src/cgeo/geocaching/cgeowaypointadd.java4
-rw-r--r--main/src/cgeo/geocaching/files/GPXParser.java13
-rw-r--r--main/src/cgeo/geocaching/maps/CGeoMap.java7
10 files changed, 137 insertions, 86 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java
index 7ca8137..816b094 100644
--- a/main/src/cgeo/geocaching/CacheDetailActivity.java
+++ b/main/src/cgeo/geocaching/CacheDetailActivity.java
@@ -418,7 +418,7 @@ public class CacheDetailActivity extends AbstractActivity {
}
break;
case CONTEXT_MENU_WAYPOINT_EDIT:
- if (null != cache.getWaypoints() && index < cache.getWaypoints().size()) {
+ if (cache.hasWaypoints() && index < cache.getWaypoints().size()) {
final cgWaypoint waypoint = cache.getWaypoints().get(index);
Intent editIntent = new Intent(this, cgeowaypointadd.class);
editIntent.putExtra("waypoint", waypoint.getId());
@@ -427,52 +427,42 @@ public class CacheDetailActivity extends AbstractActivity {
}
break;
case CONTEXT_MENU_WAYPOINT_DUPLICATE:
- if (null != cache.getWaypoints() && index < cache.getWaypoints().size()) {
- final cgWaypoint copy = new cgWaypoint(cache.getWaypoints().get(index));
- copy.setUserDefined();
- copy.setName(res.getString(R.string.waypoint_copy_of) + " " + copy.getName());
- cache.getWaypoints().add(index + 1, copy);
- app.saveOwnWaypoint(-1, cache.getGeocode(), copy);
- app.removeCacheFromCache(cache.getGeocode());
+ if (cache.duplicateWaypoint(index)) {
notifyDataSetChanged();
}
break;
case CONTEXT_MENU_WAYPOINT_DELETE:
- if (null != cache.getWaypoints() && index < cache.getWaypoints().size()) {
- final cgWaypoint waypoint = cache.getWaypoints().get(index);
- if (waypoint.isUserDefined()) {
- cache.getWaypoints().remove(index);
- app.deleteWaypoint(waypoint.getId());
- app.removeCacheFromCache(cache.getGeocode());
- notifyDataSetChanged();
- }
+ if (cache.deleteWaypoint(index)) {
+ notifyDataSetChanged();
}
break;
case CONTEXT_MENU_WAYPOINT_COMPASS:
- if (null != cache.getWaypoints() && index < cache.getWaypoints().size()) {
- final cgWaypoint waypoint = cache.getWaypoints().get(index);
+ {
+ final cgWaypoint waypoint = cache.getWaypoint(index);
+ if (waypoint != null) {
Collection<cgCoord> coordinatesWithType = new ArrayList<cgCoord>();
coordinatesWithType.add(new cgCoord(waypoint));
cgeonavigate.startActivity(this, waypoint.getPrefix().trim() + "/" + waypoint.getLookup().trim(), waypoint.getName(), waypoint.getCoords(), coordinatesWithType);
}
+ }
break;
case CONTEXT_MENU_WAYPOINT_NAVIGATE:
// No processing necessary, sub-menu gets displayed;
break;
case CONTEXT_MENU_WAYPOINT_CACHES_AROUND:
- if (null != cache.getWaypoints() && index < cache.getWaypoints().size()) {
- final cgWaypoint waypoint = cache.getWaypoints().get(index);
+ {
+ final cgWaypoint waypoint = cache.getWaypoint(index);
+ if (waypoint != null) {
cgeocaches.startActivityCachesAround(this, waypoint.getCoords());
}
+ }
break;
default:
// First check the navigation menu, then the option items
- if (null != cache.getWaypoints() && 0 <= contextMenuWPIndex && contextMenuWPIndex < cache.getWaypoints().size()) {
- final cgWaypoint waypoint = cache.getWaypoints().get(contextMenuWPIndex);
- if (NavigationAppFactory.onMenuItemSelected(item, geolocation, this,
- res, null, null, waypoint, null)) {
- return true;
- }
+ final cgWaypoint waypoint = cache.getWaypoint(contextMenuWPIndex);
+ if (waypoint != null && NavigationAppFactory.onMenuItemSelected(item, geolocation, this,
+ res, null, null, waypoint, null)) {
+ return true;
}
return onOptionsItemSelected(item);
}
@@ -886,7 +876,7 @@ public class CacheDetailActivity extends AbstractActivity {
// waypoints
try {
- if (null != cache.getWaypoints()) {
+ if (cache.hasWaypoints()) {
for (cgWaypoint waypoint : cache.getWaypoints()) {
if (null != waypoint.getCoords()) {
final cgCoord coords = new cgCoord();
@@ -1055,7 +1045,7 @@ public class CacheDetailActivity extends AbstractActivity {
}
// show number of waypoints directly in waypoint title
if (page == Page.WAYPOINTS) {
- int waypointCount = (cache.getWaypoints() == null ? 0 : cache.getWaypoints().size());
+ int waypointCount = (cache.hasWaypoints() ? cache.getWaypoints().size() : 0);
return res.getQuantityString(R.plurals.waypoints, waypointCount, waypointCount);
}
return res.getString(page.titleStringId);
@@ -2315,7 +2305,7 @@ public class CacheDetailActivity extends AbstractActivity {
LinearLayout waypoints = (LinearLayout) view.findViewById(R.id.waypoints);
- if (CollectionUtils.isNotEmpty(cache.getWaypoints())) {
+ if (cache.hasWaypoints()) {
LinearLayout waypointView;
// sort waypoints: PP, Sx, FI, OWN
@@ -2337,7 +2327,7 @@ public class CacheDetailActivity extends AbstractActivity {
if (StringUtils.isNotBlank(cgBase.waypointTypes.get(wpt.getWaypointType()))) {
infoTextList.add(cgBase.waypointTypes.get(wpt.getWaypointType()));
}
- if ("OWN".equalsIgnoreCase(wpt.getPrefix())) {
+ if (cgWaypoint.PREFIX_OWN.equalsIgnoreCase(wpt.getPrefix())) {
infoTextList.add(res.getString(R.string.waypoint_custom));
} else {
if (StringUtils.isNotBlank(wpt.getPrefix())) {
@@ -2397,7 +2387,7 @@ public class CacheDetailActivity extends AbstractActivity {
addWptIntent.putExtra("geocode", cache.getGeocode());
int wpCount = 0;
- if (cache.getWaypoints() != null) {
+ if (cache.hasWaypoints()) {
wpCount = cache.getWaypoints().size();
}
addWptIntent.putExtra("count", wpCount);
diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java
index 9834681..d37905b 100644
--- a/main/src/cgeo/geocaching/StaticMapsProvider.java
+++ b/main/src/cgeo/geocaching/StaticMapsProvider.java
@@ -3,7 +3,6 @@ package cgeo.geocaching;
import cgeo.geocaching.files.LocalStorage;
import cgeo.geocaching.geopoint.GeopointFormatter.Format;
-import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
@@ -70,7 +69,7 @@ public class StaticMapsProvider {
}
final StringBuilder waypoints = new StringBuilder();
- if (CollectionUtils.isNotEmpty(cache.getWaypoints())) {
+ if (cache.hasWaypoints()) {
for (cgWaypoint waypoint : cache.getWaypoints()) {
if (waypoint.getCoords() == null) {
continue;
diff --git a/main/src/cgeo/geocaching/apps/AbstractLocusApp.java b/main/src/cgeo/geocaching/apps/AbstractLocusApp.java
index 22fc3d5..e2d34ba 100644
--- a/main/src/cgeo/geocaching/apps/AbstractLocusApp.java
+++ b/main/src/cgeo/geocaching/apps/AbstractLocusApp.java
@@ -93,7 +93,7 @@ public abstract class AbstractLocusApp extends AbstractApp {
/**
* This method constructs a <code>Point</code> for displaying in Locus
- *
+ *
* @param cache
* @param withWaypoints
* whether to give waypoints to Locus or not
@@ -143,7 +143,7 @@ public abstract class AbstractLocusApp extends AbstractApp {
}
pg.found = cache.isFound();
- if (withWaypoints && cache.getWaypoints() != null) {
+ if (withWaypoints && cache.hasWaypoints()) {
pg.waypoints = new ArrayList<PointGeocachingDataWaypoint>();
for (cgWaypoint waypoint : cache.getWaypoints()) {
if (waypoint == null || waypoint.getCoords() == null) {
diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java
index a41a7b9..aa68c1a 100644
--- a/main/src/cgeo/geocaching/cgBase.java
+++ b/main/src/cgeo/geocaching/cgBase.java
@@ -855,7 +855,7 @@ public class cgBase {
pos = tableInside.indexOf("<div class=\"CacheInformationTable\"");
if (pos == -1) {
- Log.e(Settings.tag, "cgeoBase.parseCache: ID \"CacheInformationTable\" not found on page");
+ Log.e(Settings.tag, "cgeoBase.parseCache: class \"CacheInformationTable\" not found on page");
return null;
}
@@ -1079,18 +1079,11 @@ public class cgBase {
final String originalCoords = BaseUtils.getMatch(page, GCConstants.PATTERN_LATLON_ORIG, false, null);
if (null != originalCoords) {
- final cgWaypoint waypoint = new cgWaypoint();
+ final cgWaypoint waypoint = new cgWaypoint(res.getString(R.string.cache_coordinates_original), WaypointType.WAYPOINT);
waypoint.setCoords(new Geopoint(originalCoords));
- waypoint.setWaypointType(WaypointType.WAYPOINT);
- if (res != null) {
- waypoint.setName(res.getString(R.string.cache_coordinates_original));
- }
- if (null == cache.getWaypoints()) {
- cache.setWaypoints(new ArrayList<cgWaypoint>());
- }
- cache.getWaypoints().add(waypoint);
+ cache.addWaypoint(waypoint);
// cache.setcoordsChanged(true);
}
} catch (Geopoint.GeopointException e) {
@@ -1129,24 +1122,22 @@ public class cgBase {
String[] wp;
for (int j = 1; j < wpItems.length; j++) {
- final cgWaypoint waypoint = new cgWaypoint();
-
wp = wpItems[j].split("<td");
+ // waypoint name
+ final String name = BaseUtils.getMatch(wp[6], GCConstants.PATTERN_WPNAME, true, 1, res.getString(R.string.waypoint), true);
+
// waypoint type
- String resulttype = BaseUtils.getMatch(wp[3], GCConstants.PATTERN_WPTYPE, null);
- if (null != resulttype) {
- waypoint.setWaypointType(WaypointType.findById(resulttype));
- }
+ final String resulttype = BaseUtils.getMatch(wp[3], GCConstants.PATTERN_WPTYPE, null);
+
+ final cgWaypoint waypoint = new cgWaypoint(name, WaypointType.findById(resulttype));
+
// waypoint prefix
waypoint.setPrefix(BaseUtils.getMatch(wp[4], GCConstants.PATTERN_WPPREFIXORLOOKUPORLATLON, true, 2, waypoint.getPrefix(), false));
// waypoint lookup
waypoint.setLookup(BaseUtils.getMatch(wp[5], GCConstants.PATTERN_WPPREFIXORLOOKUPORLATLON, true, 2, waypoint.getLookup(), false));
- // waypoint name
- waypoint.setName(BaseUtils.getMatch(wp[6], GCConstants.PATTERN_WPNAME, true, 1, waypoint.getName(), true));
-
// waypoint latitude and logitude
String latlon = Html.fromHtml(BaseUtils.getMatch(wp[7], GCConstants.PATTERN_WPPREFIXORLOOKUPORLATLON, false, 2, "", false)).toString().trim();
if (!StringUtils.startsWith(latlon, "???")) {
@@ -1162,10 +1153,7 @@ public class cgBase {
// waypoint note
waypoint.setNote(BaseUtils.getMatch(wp[3], GCConstants.PATTERN_WPNOTE, waypoint.getNote()));
- if (cache.getWaypoints() == null) {
- cache.setWaypoints(new ArrayList<cgWaypoint>());
- }
- cache.getWaypoints().add(waypoint);
+ cache.addWaypoint(waypoint);
}
}
}
diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java
index a547469..b8231ef 100644
--- a/main/src/cgeo/geocaching/cgCache.java
+++ b/main/src/cgeo/geocaching/cgCache.java
@@ -24,6 +24,7 @@ import android.util.Log;
import java.util.ArrayList;
import java.util.Calendar;
+import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
@@ -771,8 +772,16 @@ public class cgCache implements ICache {
this.onWatchlist = onWatchlist;
}
+ /**
+ * return an immutable list of waypoints.
+ *
+ * @return always non <code>null</code>
+ */
public List<cgWaypoint> getWaypoints() {
- return waypoints;
+ if (waypoints == null) {
+ return Collections.emptyList();
+ }
+ return Collections.unmodifiableList(waypoints);
}
public void setWaypoints(List<cgWaypoint> waypoints) {
@@ -963,4 +972,72 @@ public class cgCache implements ICache {
this.storageLocation.add(sl);
}
+ public void addWaypoint(final cgWaypoint waypoint) {
+ if (null == waypoints) {
+ waypoints = new ArrayList<cgWaypoint>();
+ }
+ waypoints.add(waypoint);
+ }
+
+ public boolean hasWaypoints() {
+ return CollectionUtils.isNotEmpty(waypoints);
+ }
+
+ /**
+ * @param index
+ * @return <code>true</code>, if the waypoint was duplicated
+ */
+ public boolean duplicateWaypoint(int index) {
+ if (!isValidWaypointIndex(index)) {
+ return false;
+ }
+ final cgWaypoint copy = new cgWaypoint(waypoints.get(index));
+ copy.setUserDefined();
+ copy.setName(cgeoapplication.getInstance().getString(R.string.waypoint_copy_of) + " " + copy.getName());
+ waypoints.add(index + 1, copy);
+ cgeoapplication.getInstance().saveOwnWaypoint(-1, geocode, copy);
+ cgeoapplication.getInstance().removeCacheFromCache(geocode);
+ return true;
+ }
+
+ private boolean isValidWaypointIndex(int index) {
+ if (!hasWaypoints()) {
+ return false;
+ }
+ if (index < 0 || index >= waypoints.size()) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * delete a user defined waypoint
+ *
+ * @param index
+ * @return <code>true</code>, if the waypoint was deleted
+ */
+ public boolean deleteWaypoint(int index) {
+ if (!isValidWaypointIndex(index)) {
+ return false;
+ }
+ final cgWaypoint waypoint = waypoints.get(index);
+ if (waypoint.isUserDefined()) {
+ waypoints.remove(index);
+ cgeoapplication.getInstance().deleteWaypoint(waypoint.getId());
+ cgeoapplication.getInstance().removeCacheFromCache(geocode);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * @param index
+ * @return waypoint or <code>null</code>
+ */
+ public cgWaypoint getWaypoint(int index) {
+ if (!isValidWaypointIndex(index)) {
+ return null;
+ }
+ return waypoints.get(index);
+ }
}
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index 0a77c86..a66bd29 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -1309,7 +1309,7 @@ public class cgData {
}
}
- if (cache.getWaypoints() != null) {
+ if (cache.hasWaypoints()) {
if (!saveWaypoints(cache.getGeocode(), cache.getWaypoints(), true)) {
statusOk = false;
}
@@ -1951,12 +1951,7 @@ public class cgData {
if (loadFlags.contains(LoadFlag.LOADWAYPOINTS)) {
final List<cgWaypoint> waypoints = loadWaypoints(cache.getGeocode());
if (CollectionUtils.isNotEmpty(waypoints)) {
- if (cache.getWaypoints() == null) {
- cache.setWaypoints(new ArrayList<cgWaypoint>());
- } else {
- cache.getWaypoints().clear();
- }
- cache.getWaypoints().addAll(waypoints);
+ cache.setWaypoints(waypoints);
}
}
@@ -2246,14 +2241,14 @@ public class cgData {
}
private static cgWaypoint createWaypointFromDatabaseContent(Cursor cursor) {
- cgWaypoint waypoint = new cgWaypoint();
+ String name = cursor.getString(cursor.getColumnIndex("name"));
+ WaypointType type = WaypointType.findById(cursor.getString(cursor.getColumnIndex("type")));
+ cgWaypoint waypoint = new cgWaypoint(name, type);
waypoint.setId(cursor.getInt(cursor.getColumnIndex("_id")));
waypoint.setGeocode(cursor.getString(cursor.getColumnIndex("geocode")));
- waypoint.setWaypointType(WaypointType.findById(cursor.getString(cursor.getColumnIndex("type"))));
waypoint.setPrefix(cursor.getString(cursor.getColumnIndex("prefix")));
waypoint.setLookup(cursor.getString(cursor.getColumnIndex("lookup")));
- waypoint.setName(cursor.getString(cursor.getColumnIndex("name")));
waypoint.setLatlon(cursor.getString(cursor.getColumnIndex("latlon")));
waypoint.setCoords(getCoords(cursor, cursor.getColumnIndex("latitude"), cursor.getColumnIndex("longitude")));
waypoint.setNote(cursor.getString(cursor.getColumnIndex("note")));
diff --git a/main/src/cgeo/geocaching/cgWaypoint.java b/main/src/cgeo/geocaching/cgWaypoint.java
index 4fb41b5..4bad2ce 100644
--- a/main/src/cgeo/geocaching/cgWaypoint.java
+++ b/main/src/cgeo/geocaching/cgWaypoint.java
@@ -12,6 +12,7 @@ import java.util.List;
public class cgWaypoint implements IWaypoint, Comparable<cgWaypoint> {
+ static final String PREFIX_OWN = "OWN";
private static final int ORDER_UNDEFINED = -2;
private int id = 0;
private String geocode = "geocode";
@@ -25,9 +26,14 @@ public class cgWaypoint implements IWaypoint, Comparable<cgWaypoint> {
private int cachedOrder = ORDER_UNDEFINED;
/**
- * default constructor, no fields are set
+ * require name and type for every waypoint
+ *
+ * @param name
+ * @param type
*/
- public cgWaypoint() {
+ public cgWaypoint(final String name, final WaypointType type) {
+ this.name = name;
+ this.waypointType = type;
}
/**
@@ -103,7 +109,7 @@ public class cgWaypoint implements IWaypoint, Comparable<cgWaypoint> {
public void setUserDefined() {
waypointType = WaypointType.OWN;
- setPrefix("OWN");
+ setPrefix(PREFIX_OWN);
}
private int computeOrder() {
diff --git a/main/src/cgeo/geocaching/cgeowaypointadd.java b/main/src/cgeo/geocaching/cgeowaypointadd.java
index 343fead..0bce408 100644
--- a/main/src/cgeo/geocaching/cgeowaypointadd.java
+++ b/main/src/cgeo/geocaching/cgeowaypointadd.java
@@ -310,12 +310,10 @@ public class cgeowaypointadd extends AbstractActivity {
}
final String note = ((EditText) findViewById(R.id.note)).getText().toString().trim();
- final cgWaypoint waypoint = new cgWaypoint();
- waypoint.setWaypointType(type);
+ final cgWaypoint waypoint = new cgWaypoint(name, type);
waypoint.setGeocode(geocode);
waypoint.setPrefix(prefix);
waypoint.setLookup(lookup);
- waypoint.setName(name);
waypoint.setCoords(coords);
waypoint.setNote(note);
diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java
index 9eab5df..72fd63f 100644
--- a/main/src/cgeo/geocaching/files/GPXParser.java
+++ b/main/src/cgeo/geocaching/files/GPXParser.java
@@ -305,21 +305,20 @@ public abstract class GPXParser extends FileParser {
// lookup cache for waypoint in already parsed caches
final cgCache cacheForWaypoint = result.get(cacheGeocodeForWaypoint);
if (cacheForWaypoint != null) {
- final cgWaypoint waypoint = new cgWaypoint();
+ final cgWaypoint waypoint = new cgWaypoint(cache.getShortdesc(), convertWaypointSym2Type(sym));
waypoint.setId(-1);
- waypoint.setWaypointType(convertWaypointSym2Type(sym));
waypoint.setGeocode(cacheGeocodeForWaypoint);
waypoint.setPrefix(cache.getName().substring(0, 2));
waypoint.setLookup("---");
// there is no lookup code in gpx file
- waypoint.setName(cache.getShortdesc());
waypoint.setCoords(cache.getCoords());
waypoint.setNote(cache.getDescription());
- if (cacheForWaypoint.getWaypoints() == null) {
- cacheForWaypoint.setWaypoints(new ArrayList<cgWaypoint>());
- }
- cgWaypoint.mergeWayPoints(cacheForWaypoint.getWaypoints(), Collections.singletonList(waypoint), true);
+ ArrayList<cgWaypoint> mergedWayPoints = new ArrayList<cgWaypoint>();
+ mergedWayPoints.addAll(cacheForWaypoint.getWaypoints());
+
+ cgWaypoint.mergeWayPoints(mergedWayPoints, Collections.singletonList(waypoint), true);
+ cacheForWaypoint.setWaypoints(mergedWayPoints);
result.put(cacheGeocodeForWaypoint, cacheForWaypoint);
showProgressMessage(progressHandler, progressStream.getProgress());
}
diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java
index 99a528b..c6631be 100644
--- a/main/src/cgeo/geocaching/maps/CGeoMap.java
+++ b/main/src/cgeo/geocaching/maps/CGeoMap.java
@@ -1,6 +1,7 @@
package cgeo.geocaching.maps;
import cgeo.geocaching.R;
+import cgeo.geocaching.SearchResult;
import cgeo.geocaching.Settings;
import cgeo.geocaching.UpdateDirectionCallback;
import cgeo.geocaching.UpdateLocationCallback;
@@ -9,7 +10,6 @@ import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgCoord;
import cgeo.geocaching.cgDirection;
import cgeo.geocaching.cgGeo;
-import cgeo.geocaching.SearchResult;
import cgeo.geocaching.cgWaypoint;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.cgeocaches;
@@ -1331,11 +1331,10 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory
}
// display cache waypoints
- if (cacheOne.getWaypoints() != null
+ if (cacheOne.hasWaypoints()
// Only show waypoints for single view or setting
// when less than showWaypointsthreshold Caches shown
- && (cachesProtected.size() == 1 || (cachesProtected.size() < Settings.getWayPointsThreshold()))
- && !cacheOne.getWaypoints().isEmpty()) {
+ && (cachesProtected.size() == 1 || (cachesProtected.size() < Settings.getWayPointsThreshold()))) {
for (cgWaypoint oneWaypoint : cacheOne.getWaypoints()) {
if (oneWaypoint.getCoords() == null) {
continue;