aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/cgData.java10
-rw-r--r--main/src/cgeo/geocaching/cgeoapplication.java3
-rw-r--r--main/src/cgeo/geocaching/connector/ox/OXGPXParser.java23
-rw-r--r--main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java17
-rw-r--r--main/src/cgeo/geocaching/enumerations/LoadFlags.java5
-rw-r--r--main/src/cgeo/geocaching/files/FileParser.java4
-rw-r--r--main/src/cgeo/geocaching/files/GPX10Parser.java2
-rw-r--r--main/src/cgeo/geocaching/files/GPXImporter.java36
-rw-r--r--main/src/cgeo/geocaching/files/GPXParser.java47
9 files changed, 78 insertions, 69 deletions
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index 491f8ad..26ddc2c 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -2437,9 +2437,7 @@ public class cgData {
}
public void removeCache(final String geocode, EnumSet<LoadFlags.RemoveFlag> removeFlags) {
- Set<String> geocodes = new HashSet<String>();
- geocodes.add(geocode);
- removeCaches(geocodes, removeFlags);
+ removeCaches(Collections.singleton(geocode), removeFlags);
}
/**
@@ -2477,7 +2475,11 @@ public class cgData {
database.delete(dbTableLogs, baseWhereClause, null);
database.delete(dbTableLogCount, baseWhereClause, null);
database.delete(dbTableLogsOffline, baseWhereClause, null);
- database.delete(dbTableWaypoints, baseWhereClause + " and type <> 'own'", null);
+ String wayPointClause = baseWhereClause;
+ if (!removeFlags.contains(RemoveFlag.REMOVE_OWN_WAYPOINTS_ONLY_FOR_TESTING)) {
+ wayPointClause += " and type <> 'own'";
+ }
+ database.delete(dbTableWaypoints, wayPointClause, null);
database.delete(dbTableTrackables, baseWhereClause, null);
database.setTransactionSuccessful();
} finally {
diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java
index 8a2e984..572420d 100644
--- a/main/src/cgeo/geocaching/cgeoapplication.java
+++ b/main/src/cgeo/geocaching/cgeoapplication.java
@@ -61,7 +61,10 @@ public class cgeoapplication extends Application {
@Override
public void onLowMemory() {
Log.i("Cleaning applications cache.");
+ removeAllFromCache();
+ }
+ public void removeAllFromCache() {
storage.removeAllFromCache();
}
diff --git a/main/src/cgeo/geocaching/connector/ox/OXGPXParser.java b/main/src/cgeo/geocaching/connector/ox/OXGPXParser.java
new file mode 100644
index 0000000..377785a
--- /dev/null
+++ b/main/src/cgeo/geocaching/connector/ox/OXGPXParser.java
@@ -0,0 +1,23 @@
+package cgeo.geocaching.connector.ox;
+
+import cgeo.geocaching.cgCache;
+import cgeo.geocaching.files.GPX10Parser;
+
+public class OXGPXParser extends GPX10Parser {
+
+ private final boolean isDetailed;
+
+ public OXGPXParser(int listIdIn, boolean isDetailed) {
+ super(listIdIn);
+ this.isDetailed = isDetailed;
+ }
+
+ @Override
+ protected void afterParsing(cgCache cache) {
+ cache.setUpdated(System.currentTimeMillis());
+ if (isDetailed) {
+ cache.setDetailedUpdate(cache.getUpdated());
+ cache.setDetailed(true);
+ }
+ }
+}
diff --git a/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java b/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java
index 7ed6073..17be540 100644
--- a/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java
+++ b/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java
@@ -2,9 +2,6 @@ package cgeo.geocaching.connector.ox;
import cgeo.geocaching.StoredList;
import cgeo.geocaching.cgCache;
-import cgeo.geocaching.cgeoapplication;
-import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
-import cgeo.geocaching.files.GPX10Parser;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter;
import cgeo.geocaching.network.Network;
@@ -13,11 +10,11 @@ import cgeo.geocaching.utils.CryptUtils;
import cgeo.geocaching.utils.Log;
import ch.boye.httpclientandroidlib.HttpResponse;
+
import org.apache.commons.collections.CollectionUtils;
import java.util.Collection;
import java.util.Collections;
-import java.util.EnumSet;
public class OpenCachingApi {
@@ -43,21 +40,11 @@ public class OpenCachingApi {
}
Collection<cgCache> caches;
try {
- caches = new GPX10Parser(StoredList.STANDARD_LIST_ID).parse(response.getEntity().getContent(), null);
+ caches = new OXGPXParser(StoredList.STANDARD_LIST_ID, isDetailed).parse(response.getEntity().getContent(), null);
} catch (Exception e) {
Log.e("Error importing from OpenCaching.com", e);
return Collections.emptyList();
}
- for (cgCache cache : caches) {
- cache.setUpdated(System.currentTimeMillis());
- if (isDetailed) {
- cache.setDetailedUpdate(cache.getUpdated());
- cache.setDetailed(true);
- }
-
- // save full detailed caches
- cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));
- }
return caches;
}
diff --git a/main/src/cgeo/geocaching/enumerations/LoadFlags.java b/main/src/cgeo/geocaching/enumerations/LoadFlags.java
index c56a7ba..fb894ac 100644
--- a/main/src/cgeo/geocaching/enumerations/LoadFlags.java
+++ b/main/src/cgeo/geocaching/enumerations/LoadFlags.java
@@ -37,9 +37,10 @@ public interface LoadFlags {
public enum RemoveFlag {
REMOVE_CACHE, // save only to CacheCache
- REMOVE_DB // includes removing from CacheCache
+ REMOVE_DB, // includes removing from CacheCache
+ REMOVE_OWN_WAYPOINTS_ONLY_FOR_TESTING // only to be used in unit testing (as we never delete own waypoints)
}
- public final static EnumSet<RemoveFlag> REMOVE_ALL = EnumSet.allOf(RemoveFlag.class);
+ public final static EnumSet<RemoveFlag> REMOVE_ALL = EnumSet.of(RemoveFlag.REMOVE_CACHE, RemoveFlag.REMOVE_DB);
} \ No newline at end of file
diff --git a/main/src/cgeo/geocaching/files/FileParser.java b/main/src/cgeo/geocaching/files/FileParser.java
index 090573a..0308d81 100644
--- a/main/src/cgeo/geocaching/files/FileParser.java
+++ b/main/src/cgeo/geocaching/files/FileParser.java
@@ -16,11 +16,11 @@ import java.util.concurrent.CancellationException;
public abstract class FileParser {
/**
* Parses caches from input stream.
- *
+ *
* @param stream
* @param progressHandler
* for reporting parsing progress (in bytes read from input stream)
- * @return collection of parsed caches
+ * @return collection of caches
* @throws IOException
* if the input stream can't be read
* @throws ParserException
diff --git a/main/src/cgeo/geocaching/files/GPX10Parser.java b/main/src/cgeo/geocaching/files/GPX10Parser.java
index 85884de..0ca2606 100644
--- a/main/src/cgeo/geocaching/files/GPX10Parser.java
+++ b/main/src/cgeo/geocaching/files/GPX10Parser.java
@@ -2,7 +2,7 @@ package cgeo.geocaching.files;
import android.sax.Element;
-public final class GPX10Parser extends GPXParser {
+public class GPX10Parser extends GPXParser {
public GPX10Parser(int listIdIn) {
super(listIdIn, "http://www.topografix.com/GPX/1/0", "1.0");
diff --git a/main/src/cgeo/geocaching/files/GPXImporter.java b/main/src/cgeo/geocaching/files/GPXImporter.java
index 1bb3c3f..8369161 100644
--- a/main/src/cgeo/geocaching/files/GPXImporter.java
+++ b/main/src/cgeo/geocaching/files/GPXImporter.java
@@ -9,7 +9,6 @@ import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.activity.IAbstractActivity;
import cgeo.geocaching.activity.Progress;
import cgeo.geocaching.enumerations.LoadFlags;
-import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.Log;
@@ -32,7 +31,6 @@ import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
-import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.zip.ZipEntry;
@@ -42,7 +40,6 @@ public class GPXImporter {
static final int IMPORT_STEP_START = 0;
static final int IMPORT_STEP_READ_FILE = 1;
static final int IMPORT_STEP_READ_WPT_FILE = 2;
- static final int IMPORT_STEP_STORE_CACHES = 3;
static final int IMPORT_STEP_STORE_STATIC_MAPS = 4;
static final int IMPORT_STEP_FINISHED = 5;
static final int IMPORT_STEP_FINISHED_WITH_ERROR = 6;
@@ -139,11 +136,13 @@ public class GPXImporter {
try {
importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_START));
caches = doImport();
-
- importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_STORE_CACHES, R.string.gpx_import_storing, caches.size()));
- SearchResult search = storeParsedCaches(caches);
Log.i("Imported successfully " + caches.size() + " caches.");
+ final SearchResult search = new SearchResult();
+ for (cgCache cache : caches) {
+ search.addCache(cache);
+ }
+
if (Settings.isStoreOfflineMaps() || Settings.isStoreOfflineWpMaps()) {
importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_STORE_STATIC_MAPS, R.string.gpx_import_store_static_maps, search.getCount()));
boolean finishedWithoutCancel = importStaticMaps(search);
@@ -171,24 +170,6 @@ public class GPXImporter {
protected abstract Collection<cgCache> doImport() throws IOException, ParserException;
- private SearchResult storeParsedCaches(Collection<cgCache> caches) {
- final SearchResult search = new SearchResult();
- final cgeoapplication app = cgeoapplication.getInstance();
- int storedCaches = 0;
- for (cgCache cache : caches) {
- search.addCache(cache);
- if (app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB))) {
- storedCaches++;
- }
-
- if (progressHandler.isCancelled()) {
- throw new CancellationException();
- }
- progressHandler.sendMessage(progressHandler.obtainMessage(0, storedCaches, 0));
- }
- return search;
- }
-
private boolean importStaticMaps(final SearchResult importedCaches) {
final cgeoapplication app = cgeoapplication.getInstance();
int storedCacheMaps = 0;
@@ -398,13 +379,6 @@ public class GPXImporter {
progress.setMaxProgressAndReset(msg.arg2);
break;
- case IMPORT_STEP_STORE_CACHES:
- progress.setProgressDivider(1);
- progress.setMessage(res.getString(msg.arg1));
- progress.setMaxProgressAndReset(msg.arg2);
- showProgressAfterCancel = true;
- break;
-
case IMPORT_STEP_STORE_STATIC_MAPS:
progress.dismiss();
Message skipMessage = importStepHandler.obtainMessage(IMPORT_STEP_STATIC_MAPS_SKIPPED, msg.arg2, 0);
diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java
index 454c494..ea26a66 100644
--- a/main/src/cgeo/geocaching/files/GPXParser.java
+++ b/main/src/cgeo/geocaching/files/GPXParser.java
@@ -10,6 +10,9 @@ import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
+import cgeo.geocaching.enumerations.LoadFlags;
+import cgeo.geocaching.enumerations.LoadFlags.LoadFlag;
+import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.Geopoint;
@@ -33,10 +36,10 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.EnumSet;
+import java.util.HashSet;
+import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -89,7 +92,7 @@ public abstract class GPXParser extends FileParser {
/**
* Parser result. Maps geocode to cache.
*/
- private final Map<String, cgCache> result = new HashMap<String, cgCache>(100);
+ private final Set<String> result = new HashSet<String>(100);
private ProgressInputStream progressStream;
private final class UserDataListener implements EndTextElementListener {
@@ -298,11 +301,17 @@ public abstract class GPXParser extends FileParser {
createNoteFromGSAKUserdata();
- final String key = cache.getGeocode();
- if (result.containsKey(key)) {
- Log.w("Duplicate geocode during GPX import: " + key);
+ final String geocode = cache.getGeocode();
+ if (result.contains(geocode)) {
+ Log.w("Duplicate geocode during GPX import: " + geocode);
}
- result.put(key, cache);
+ // modify cache depending on the use case/connector
+ afterParsing(cache);
+
+ // finally store the cache in the database
+ result.add(geocode);
+ cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));
+ cgeoapplication.getInstance().removeAllFromCache();
showProgressMessage(progressHandler, progressStream.getProgress());
} else if (StringUtils.isNotBlank(cache.getName())
&& StringUtils.containsIgnoreCase(type, "waypoint")) {
@@ -319,7 +328,7 @@ public abstract class GPXParser extends FileParser {
final String cacheGeocodeForWaypoint = "GC" + cache.getName().substring(2).toUpperCase();
// lookup cache for waypoint in already parsed caches
- final cgCache cacheForWaypoint = result.get(cacheGeocodeForWaypoint);
+ final cgCache cacheForWaypoint = cgeoapplication.getInstance().loadCache(cacheGeocodeForWaypoint, LoadFlags.LOAD_CACHE_OR_DB);
if (cacheForWaypoint != null) {
final cgWaypoint waypoint = new cgWaypoint(cache.getShortdesc(), convertWaypointSym2Type(sym), false);
waypoint.setId(-1);
@@ -330,12 +339,14 @@ public abstract class GPXParser extends FileParser {
waypoint.setCoords(cache.getCoords());
waypoint.setNote(cache.getDescription());
- ArrayList<cgWaypoint> mergedWayPoints = new ArrayList<cgWaypoint>();
+ final ArrayList<cgWaypoint> mergedWayPoints = new ArrayList<cgWaypoint>();
mergedWayPoints.addAll(cacheForWaypoint.getWaypoints());
- cgWaypoint.mergeWayPoints(mergedWayPoints, Collections.singletonList(waypoint), true);
- cacheForWaypoint.setWaypoints(mergedWayPoints, false);
- result.put(cacheGeocodeForWaypoint, cacheForWaypoint);
+ final ArrayList<cgWaypoint> newPoints = new ArrayList<cgWaypoint>();
+ newPoints.add(waypoint);
+ cgWaypoint.mergeWayPoints(newPoints, mergedWayPoints, true);
+ cacheForWaypoint.setWaypoints(newPoints, false);
+ cgeoapplication.getInstance().saveCache(cacheForWaypoint, EnumSet.of(SaveFlag.SAVE_DB));
showProgressMessage(progressHandler, progressStream.getProgress());
}
}
@@ -760,7 +771,7 @@ public abstract class GPXParser extends FileParser {
try {
progressStream = new ProgressInputStream(stream);
Xml.parse(progressStream, Xml.Encoding.UTF_8, root.getContentHandler());
- return result.values();
+ return cgeoapplication.getInstance().loadCaches(result, EnumSet.of(LoadFlag.LOAD_DB_MINIMAL));
} catch (SAXException e) {
Log.e("Cannot parse .gpx file as GPX " + version + ": could not parse XML - " + e.toString());
throw new ParserException("Cannot parse .gpx file as GPX " + version + ": could not parse XML", e);
@@ -768,6 +779,14 @@ public abstract class GPXParser extends FileParser {
}
/**
+ * @param cache
+ * currently imported cache
+ */
+ protected void afterParsing(cgCache cache) {
+ // can be overridden by sub classes
+ }
+
+ /**
* GPX 1.0 and 1.1 use different XML elements to put the cache into, therefore needs to be overwritten in the
* version specific subclasses
*