aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2011-10-16 20:59:04 +0200
committerBananeweizen <bananeweizen@gmx.de>2011-10-16 20:59:04 +0200
commit74277776f7249dcfa8ccca4297a4a1d120db97b1 (patch)
tree844145a5d7e5b93c41ec0783c52a8bfadac939a7
parentfe6fad3dbd067720dc24c7a62e85672919da97f6 (diff)
downloadcgeo-74277776f7249dcfa8ccca4297a4a1d120db97b1.zip
cgeo-74277776f7249dcfa8ccca4297a4a1d120db97b1.tar.gz
cgeo-74277776f7249dcfa8ccca4297a4a1d120db97b1.tar.bz2
#310, import GPX from mail
* tested with Gmail only, not with other mail clients * uses the GPX activity, so you will see the GPX files of your SD card in the background * some refactorings and cleanups in GPX parser
-rw-r--r--main/AndroidManifest.xml10
-rw-r--r--main/res/values-de/strings.xml1
-rw-r--r--main/res/values/strings.xml1
-rw-r--r--main/src/cgeo/geocaching/cgData.java16
-rw-r--r--main/src/cgeo/geocaching/cgGPXListAdapter.java2
-rw-r--r--main/src/cgeo/geocaching/cgList.java12
-rw-r--r--main/src/cgeo/geocaching/cgeocaches.java2
-rw-r--r--main/src/cgeo/geocaching/cgeogpxes.java92
-rw-r--r--main/src/cgeo/geocaching/files/GPXParser.java56
-rw-r--r--main/src/cgeo/geocaching/files/LocParser.java4
-rw-r--r--tests/src/cgeo/geocaching/files/GPXParserTest.java2
11 files changed, 138 insertions, 60 deletions
diff --git a/main/AndroidManifest.xml b/main/AndroidManifest.xml
index deb72e1..fb00a1c 100644
--- a/main/AndroidManifest.xml
+++ b/main/AndroidManifest.xml
@@ -292,6 +292,16 @@
<action android:name="cgeo.geocaching.CGEOSELMAP"/>
</intent-filter>
</activity>
+ <activity android:name=".cgeogpxes"
+ android:label="@string/app_name">
+ <intent-filter>
+ <action android:name="android.intent.action.VIEW" />
+ <category android:name="android.intent.category.DEFAULT" />
+ <category android:name="android.intent.category.BROWSABLE" />
+ <data android:mimeType="text/xml" />
+ <data android:mimeType="application/xml" />
+ </intent-filter>
+ </activity>
<provider android:name="cgeo.geocaching.apps.LocusDataStorageProvider"
android:authorities="cgeo.geocaching.apps.locusdatastorageprovider" />
</application>
diff --git a/main/res/values-de/strings.xml b/main/res/values-de/strings.xml
index 2fb9541..779ab41 100644
--- a/main/res/values-de/strings.xml
+++ b/main/res/values-de/strings.xml
@@ -567,6 +567,7 @@
<string name="gpx_import_title">Importiere GPX-Datei</string>
<string name="gpx_import_title_reading_file">Lese Datei</string>
<string name="gpx_import_title_caches_imported">Ergebnis</string>
+ <string name="gpx_import_confirm">Soll die GPX-Datei importiert werden?</string>
<!-- map file select -->
<string name="map_file_select_title">Wähle eine Kartendatei</string>
diff --git a/main/res/values/strings.xml b/main/res/values/strings.xml
index e1a8d3a..cc9e67e 100644
--- a/main/res/values/strings.xml
+++ b/main/res/values/strings.xml
@@ -584,6 +584,7 @@
<string name="gpx_import_title">Import GPX</string>
<string name="gpx_import_title_reading_file">Reading file</string>
<string name="gpx_import_title_caches_imported">Result</string>
+ <string name="gpx_import_confirm">Do you want to import the GPX file into c:geo?</string>
<!-- map file select -->
<string name="map_file_select_title">Select map file</string>
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index 18ddf2b..0ef6b50 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -3131,7 +3131,7 @@ public class cgData {
List<cgList> lists = new ArrayList<cgList>();
- lists.add(new cgList(true, 1, res.getString(R.string.list_inbox)));
+ lists.add(new cgList(cgList.STANDARD_LIST_ID, res.getString(R.string.list_inbox)));
// lists.add(new cgList(true, 2, res.getString(R.string.list_wpt)));
ArrayList<cgList> storedLists = readLists(null, "title COLLATE NOCASE ASC");
@@ -3158,16 +3158,10 @@ public class cgData {
int indexId = cursor.getColumnIndex("_id");
int indexTitle = cursor.getColumnIndex("title");
int indexUpdated = cursor.getColumnIndex("updated");
- int indexLatitude = cursor.getColumnIndex("latitude");
- int indexLongitude = cursor.getColumnIndex("longitude");
do {
- cgList list = new cgList(false);
-
- list.id = (cursor.getInt(indexId)) + 10;
- list.title = cursor.getString(indexTitle);
+ cgList list = new cgList(cursor.getInt(indexId) + 10, cursor.getString(indexTitle));
list.updated = cursor.getLong(indexUpdated);
- list.coords = getCoords(cursor, indexLatitude, indexLongitude);
result.add(list);
} while (cursor.moveToNext());
@@ -3182,10 +3176,10 @@ public class cgData {
}
public cgList getList(int id, Resources res) {
- if (id == 1) {
- return new cgList(true, 1, res.getString(R.string.list_inbox));
+ if (id == cgList.STANDARD_LIST_ID) {
+ return new cgList(cgList.STANDARD_LIST_ID, res.getString(R.string.list_inbox));
} else if (id == 2) {
- return new cgList(true, 2, res.getString(R.string.list_wpt));
+ return new cgList(2, res.getString(R.string.list_wpt));
} else if (id >= 10) {
init();
diff --git a/main/src/cgeo/geocaching/cgGPXListAdapter.java b/main/src/cgeo/geocaching/cgGPXListAdapter.java
index 786d1f2..6daad37 100644
--- a/main/src/cgeo/geocaching/cgGPXListAdapter.java
+++ b/main/src/cgeo/geocaching/cgGPXListAdapter.java
@@ -66,7 +66,7 @@ public class cgGPXListAdapter extends ArrayAdapter<File> {
// tap on item
public void onClick(View view) {
- parent.loadGPX(file);
+ parent.importGPX(file);
}
}
}
diff --git a/main/src/cgeo/geocaching/cgList.java b/main/src/cgeo/geocaching/cgList.java
index 162af76..f176129 100644
--- a/main/src/cgeo/geocaching/cgList.java
+++ b/main/src/cgeo/geocaching/cgList.java
@@ -1,20 +1,14 @@
package cgeo.geocaching;
-import cgeo.geocaching.geopoint.Geopoint;
public class cgList {
- public boolean def = false;
+ public static final int STANDARD_LIST_ID = 1;
+
public int id = 0;
public String title = null;
public Long updated = null;
- public Geopoint coords = null;
-
- public cgList(boolean defIn) {
- def = defIn;
- }
- public cgList(boolean defIn, int idIn, String titleIn) {
- def = defIn;
+ public cgList(int idIn, String titleIn) {
id = idIn;
title = titleIn;
}
diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java
index e0250cf..4025301 100644
--- a/main/src/cgeo/geocaching/cgeocaches.java
+++ b/main/src/cgeo/geocaching/cgeocaches.java
@@ -590,7 +590,7 @@ public class cgeocaches extends AbstractListActivity {
case OFFLINE:
listId = Settings.getLastList();
if (listId <= 0) {
- listId = 1;
+ listId = cgList.STANDARD_LIST_ID;
title = res.getString(R.string.caches_stored);
} else {
final cgList list = app.getList(listId);
diff --git a/main/src/cgeo/geocaching/cgeogpxes.java b/main/src/cgeo/geocaching/cgeogpxes.java
index 8e5d22b..f19287d 100644
--- a/main/src/cgeo/geocaching/cgeogpxes.java
+++ b/main/src/cgeo/geocaching/cgeogpxes.java
@@ -7,7 +7,9 @@ import cgeo.geocaching.files.LocParser;
import org.apache.commons.lang3.StringUtils;
import android.app.Activity;
+import android.app.AlertDialog;
import android.app.ProgressDialog;
+import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
@@ -15,6 +17,8 @@ import android.os.Handler;
import android.os.Message;
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
import java.util.List;
import java.util.UUID;
@@ -44,7 +48,6 @@ public class cgeogpxes extends FileList<cgGPXListAdapter> {
};
final private Handler loadCachesHandler = new Handler() {
-
@Override
public void handleMessage(Message msg) {
if (parseDialog != null) {
@@ -74,9 +77,33 @@ public class cgeogpxes extends FileList<cgGPXListAdapter> {
listId = extras.getInt(EXTRAS_LIST_ID);
}
if (listId <= 0) {
- listId = 1;
+ listId = cgList.STANDARD_LIST_ID;
}
+ if (getIntent().getScheme().equals("content")) {
+ new AlertDialog.Builder(this)
+ .setTitle(res.getString(R.string.gpx_import_title))
+ .setMessage(res.getString(R.string.gpx_import_confirm))
+ .setCancelable(false)
+ .setPositiveButton(getString(android.R.string.yes), new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int id) {
+ try {
+ final InputStream attachment = getContentResolver().openInputStream(getIntent().getData());
+ importGPX(attachment);
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ })
+ .setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int id) {
+ dialog.cancel();
+ }
+ })
+ .create()
+ .show();
+ }
}
@Override
@@ -84,39 +111,65 @@ public class cgeogpxes extends FileList<cgGPXListAdapter> {
setTitle(res.getString(R.string.gpx_import_title));
}
- public void loadGPX(File file) {
+ public void importGPX(final File file) {
+ createProgressDialog((int) file.length());
+ new ImportFileThread(file).start();
+ }
+
+ public void importGPX(final InputStream stream) {
+ createProgressDialog(-1);
+ new ImportStreamThread(stream).start();
+ }
+ private void createProgressDialog(int maxBytes) {
parseDialog = new ProgressDialog(this);
parseDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
parseDialog.setTitle(res.getString(R.string.gpx_import_title_reading_file));
parseDialog.setMessage(res.getString(R.string.gpx_import_loading));
parseDialog.setCancelable(false);
- parseDialog.setMax((int) file.length());
+ parseDialog.setMax(maxBytes);
parseDialog.show();
-
- new loadCaches(file).start();
}
- private class loadCaches extends Thread {
+ private abstract class ImportThread extends Thread {
- File file = null;
+ @Override
+ public void run() {
+ final UUID searchId = doImport();
+ loadCachesHandler.sendMessage(loadCachesHandler.obtainMessage(0, app.getCount(searchId), 0));
+ }
- public loadCaches(File fileIn) {
- file = fileIn;
+ protected abstract UUID doImport();
+ }
+
+ private class ImportFileThread extends ImportThread {
+ private final File file;
+
+ public ImportFileThread(final File file) {
+ this.file = file;
}
@Override
- public void run() {
- final UUID searchId;
- final String name = file.getName().toLowerCase();
- if (name.endsWith("gpx")) {
- searchId = GPXParser.parseGPX(file, listId, changeParseDialogHandler);
+ protected UUID doImport() {
+ if (StringUtils.endsWithIgnoreCase(file.getName(), GPXParser.GPX_FILE_EXTENSION)) {
+ return GPXParser.importGPX(file, listId, changeParseDialogHandler);
}
else {
- searchId = LocParser.parseLoc(file, listId, changeParseDialogHandler);
+ return LocParser.parseLoc(file, listId, changeParseDialogHandler);
}
+ }
+ }
- loadCachesHandler.sendMessage(loadCachesHandler.obtainMessage(0, app.getCount(searchId), 0));
+ private class ImportStreamThread extends ImportThread {
+ private final InputStream stream;
+
+ public ImportStreamThread(InputStream stream) {
+ this.stream = stream;
+ }
+
+ @Override
+ protected UUID doImport() {
+ return GPXParser.importGPX(stream, listId, changeParseDialogHandler);
}
}
@@ -130,9 +183,8 @@ public class cgeogpxes extends FileList<cgGPXListAdapter> {
protected boolean filenameBelongsToList(final String filename) {
if (super.filenameBelongsToList(filename)) {
// filter out waypoint files
- return !StringUtils.endsWithIgnoreCase(filename, "-wpts.gpx");
- } else {
- return false;
+ return !StringUtils.endsWithIgnoreCase(filename, GPXParser.WAYPOINTS_FILE_SUFFIX_AND_EXTENSION);
}
+ return false;
}
}
diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java
index 3ef200f..bb16f9d 100644
--- a/main/src/cgeo/geocaching/files/GPXParser.java
+++ b/main/src/cgeo/geocaching/files/GPXParser.java
@@ -67,7 +67,8 @@ public abstract class GPXParser extends FileParser {
*/
private static final String GSAK_NS = "http://www.gsak.net/xmlv1/5";
- private static final String GPX_FILE_EXTENSION = ".gpx";
+ public static final String GPX_FILE_EXTENSION = ".gpx";
+ public static final String WAYPOINTS_FILE_SUFFIX_AND_EXTENSION = "-wpts.gpx";
private int listId = 1;
final protected String namespace;
@@ -865,7 +866,7 @@ public abstract class GPXParser extends FileParser {
}
}
- public static UUID parseGPX(File file, int listId, Handler handler) {
+ public static UUID importGPX(File file, int listId, Handler handler) {
try {
// parse cache file
GPXParser parser = new GPX10Parser(listId);
@@ -884,31 +885,56 @@ public abstract class GPXParser extends FileParser {
}
if (parsed) {
- final cgSearch search = new cgSearch();
- final cgeoapplication app = cgeoapplication.getInstance();
- int storedCaches = 0;
- for (cgCache cache : parser.getParsedCaches()) {
- // remove from cache, cache can be re-imported
- app.removeCacheFromCache(cache.geocode);
- app.addCacheToSearch(search, cache);
- showCountMessage(handler, R.string.gpx_import_storing, ++storedCaches);
- }
- Log.i(Settings.tag, "Caches found in .gpx file: " + parser.getParsedCaches().size());
- return search.getCurrentId();
+ return storeParsedCaches(handler, parser);
}
} catch (Exception e) {
- Log.e(Settings.tag, "cgBase.parseGPX: " + e.toString());
+ Log.e(Settings.tag, "GPXParser.importGPX: " + e.toString());
}
return null;
}
+ public static UUID importGPX(InputStream stream, int listId, Handler handler) {
+ try {
+ // parse cache file
+ GPXParser parser = new GPX10Parser(listId);
+ boolean parsed = parser.parse(stream, handler);
+ if (!parsed) {
+ parser = new GPX11Parser(listId);
+ parsed = parser.parse(stream, handler);
+ }
+
+ if (parsed) {
+ return storeParsedCaches(handler, parser);
+ }
+
+ } catch (Exception e) {
+ Log.e(Settings.tag, "GPXParser.importGPX: " + e.toString());
+ }
+
+ return null;
+ }
+
+ private static UUID storeParsedCaches(Handler handler, GPXParser parser) {
+ final cgSearch search = new cgSearch();
+ final cgeoapplication app = cgeoapplication.getInstance();
+ int storedCaches = 0;
+ for (cgCache cache : parser.getParsedCaches()) {
+ // remove from cache, cache can be re-imported
+ app.removeCacheFromCache(cache.geocode);
+ app.addCacheToSearch(search, cache);
+ showCountMessage(handler, R.string.gpx_import_storing, ++storedCaches);
+ }
+ Log.i(Settings.tag, "Caches found in .gpx file: " + parser.getParsedCaches().size());
+ return search.getCurrentId();
+ }
+
// 1234567.gpx -> 1234567-wpts.gpx
static File getWaypointsFileForGpx(File file) {
final String name = file.getName();
if (StringUtils.endsWithIgnoreCase(name, GPX_FILE_EXTENSION) && (StringUtils.length(name) > GPX_FILE_EXTENSION.length())) {
- final String wptsName = StringUtils.substringBeforeLast(name, ".") + "-wpts" + StringUtils.right(name, GPX_FILE_EXTENSION.length());
+ final String wptsName = StringUtils.substringBeforeLast(name, ".") + WAYPOINTS_FILE_SUFFIX_AND_EXTENSION;
return new File(file.getParentFile(), wptsName);
} else {
return null;
diff --git a/main/src/cgeo/geocaching/files/LocParser.java b/main/src/cgeo/geocaching/files/LocParser.java
index 6f8d26c..ddd368a 100644
--- a/main/src/cgeo/geocaching/files/LocParser.java
+++ b/main/src/cgeo/geocaching/files/LocParser.java
@@ -165,9 +165,9 @@ public final class LocParser extends FileParser {
}
caches.totalCnt = caches.cacheList.size();
showCountMessage(handler, R.string.gpx_import_loading_stored, search.getCount());
- Log.i(Settings.tag, "Caches found in .gpx file: " + caches.totalCnt);
+ Log.i(Settings.tag, "Caches found in .loc file: " + caches.totalCnt);
} catch (Exception e) {
- Log.e(Settings.tag, "cgBase.parseGPX: " + e.toString());
+ Log.e(Settings.tag, "LocParser.parseLoc: " + e.toString());
}
return search.getCurrentId();
diff --git a/tests/src/cgeo/geocaching/files/GPXParserTest.java b/tests/src/cgeo/geocaching/files/GPXParserTest.java
index 2dc9162..e2bba84 100644
--- a/tests/src/cgeo/geocaching/files/GPXParserTest.java
+++ b/tests/src/cgeo/geocaching/files/GPXParserTest.java
@@ -181,7 +181,7 @@ public class GPXParserTest extends InstrumentationTestCase {
assertEquals(new File("/mnt/sdcard/1-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("/mnt/sdcard/1.gpx")));
assertEquals(new File("/mnt/sd.card/1-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("/mnt/sd.card/1.gpx")));
assertEquals(new File("1234567.9-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("1234567.9.gpx")));
- assertEquals(new File("1234567-wpts.GPX"), GPXParser.getWaypointsFileForGpx(new File("1234567.GPX")));
+ assertEquals(new File("1234567-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("1234567.GPX")));
assertEquals(new File("gpx.gpx-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("gpx.gpx.gpx")));
assertNull(GPXParser.getWaypointsFileForGpx(new File("123.gpy")));
assertNull(GPXParser.getWaypointsFileForGpx(new File("gpx")));