diff options
Diffstat (limited to 'main')
| -rw-r--r-- | main/.settings/org.eclipse.jdt.core.prefs | 1 | ||||
| -rw-r--r-- | main/res/values-de/strings.xml | 4 | ||||
| -rw-r--r-- | main/res/values/strings.xml | 3 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/GPXImportActivity.java | 2 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/activity/Progress.java | 28 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgGPXListAdapter.java | 2 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/FileParser.java | 17 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/GPXImporter.java | 98 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/GPXParser.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/LocParser.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/CancellableHandler.java | 2 |
11 files changed, 107 insertions, 58 deletions
diff --git a/main/.settings/org.eclipse.jdt.core.prefs b/main/.settings/org.eclipse.jdt.core.prefs index aaf1455..19b01c3 100644 --- a/main/.settings/org.eclipse.jdt.core.prefs +++ b/main/.settings/org.eclipse.jdt.core.prefs @@ -282,3 +282,4 @@ org.eclipse.jdt.core.formatter.use_on_off_tags=false org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true +org.eclipse.jdt.core.compiler.annotation.nullanalysis=enabled
\ No newline at end of file diff --git a/main/res/values-de/strings.xml b/main/res/values-de/strings.xml index c24fd18..0c20bf7 100644 --- a/main/res/values-de/strings.xml +++ b/main/res/values-de/strings.xml @@ -564,8 +564,8 @@ <string name="gpx_import_error_parser">Dateiformat nicht ok</string> <string name="gpx_import_error_unexpected">Unerwarteter Fehler</string> <string name="gpx_import_confirm">Soll die GPX-Datei importiert werden?</string> - <string name="gpx_import_loading">Lade Caches von GPS-Datei</string> - + <string name="gpx_import_canceled">Der GPX-Import wurde abgebrochen</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 c44df8b..a5b1178 100644 --- a/main/res/values/strings.xml +++ b/main/res/values/strings.xml @@ -35,7 +35,7 @@ <string name="ape">Project ape cache</string> <string name="gchq">Groundspeak hq</string> <string name="gps">GPS cache exhibit</string> - <string name="unknown">Unknown cache</string> + <string name="unknown">Unknown type</string> <!-- cache sizes --> <string name="cache_size_micro">micro</string> @@ -583,6 +583,7 @@ <string name="gpx_import_error_parser">File format not ok</string> <string name="gpx_import_error_unexpected">Unexpected error</string> <string name="gpx_import_confirm">Do you want to import the GPX file into c:geo?</string> + <string name="gpx_import_canceled">GPX import was canceled</string> <!-- map file select --> <string name="map_file_select_title">Select map file</string> diff --git a/main/src/cgeo/geocaching/GPXImportActivity.java b/main/src/cgeo/geocaching/GPXImportActivity.java index 77cb8de..30ccab2 100644 --- a/main/src/cgeo/geocaching/GPXImportActivity.java +++ b/main/src/cgeo/geocaching/GPXImportActivity.java @@ -23,7 +23,7 @@ public class GPXImportActivity extends AbstractActivity { .setCancelable(false) .setPositiveButton(getString(android.R.string.yes), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - (new GPXImporter(cgList.STANDARD_LIST_ID)).importGPX(GPXImportActivity.this, getIntent().getData(), getContentResolver()); + (new GPXImporter(GPXImportActivity.this, cgList.STANDARD_LIST_ID)).importGPX(getIntent().getData(), getContentResolver()); } }) .setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() { diff --git a/main/src/cgeo/geocaching/activity/Progress.java b/main/src/cgeo/geocaching/activity/Progress.java index 6edf23e..16b7b40 100644 --- a/main/src/cgeo/geocaching/activity/Progress.java +++ b/main/src/cgeo/geocaching/activity/Progress.java @@ -27,6 +27,22 @@ public class Progress { } } + public synchronized void show(final Context context, final String title, final String message, final int style, final Message cancelMessage) { + if (dialog == null) { + dialog = new ProgressDialog(context); + dialog.setTitle(title); + dialog.setMessage(message); + dialog.setProgressStyle(style); + if (cancelMessage != null) { + dialog.setCancelable(true); + dialog.setCancelMessage(cancelMessage); + } else { + dialog.setCancelable(false); + } + dialog.show(); + } + } + public synchronized void setMessage(final String message) { if (dialog != null && dialog.isShowing()) { dialog.setMessage(message); @@ -37,4 +53,16 @@ public class Progress { return dialog != null && dialog.isShowing(); } + public synchronized void setMaxProgressAndReset(final int max) { + if (dialog != null && dialog.isShowing()) { + dialog.setMax(max); + dialog.setProgress(0); + } + } + + public synchronized void setProgress(final int progress) { + if (dialog != null && dialog.isShowing()) { + dialog.setProgress(progress); + } + } } diff --git a/main/src/cgeo/geocaching/cgGPXListAdapter.java b/main/src/cgeo/geocaching/cgGPXListAdapter.java index 10c75e5..262f3e2 100644 --- a/main/src/cgeo/geocaching/cgGPXListAdapter.java +++ b/main/src/cgeo/geocaching/cgGPXListAdapter.java @@ -70,7 +70,7 @@ public class cgGPXListAdapter extends ArrayAdapter<File> { // tap on item @Override public void onClick(View view) { - (new GPXImporter(parent.getListId())).importGPX(parent, file); + (new GPXImporter(parent, parent.getListId())).importGPX(file); } } } diff --git a/main/src/cgeo/geocaching/files/FileParser.java b/main/src/cgeo/geocaching/files/FileParser.java index a803c2b..20e757c 100644 --- a/main/src/cgeo/geocaching/files/FileParser.java +++ b/main/src/cgeo/geocaching/files/FileParser.java @@ -1,8 +1,7 @@ package cgeo.geocaching.files; import cgeo.geocaching.cgCache; - -import android.os.Handler; +import cgeo.geocaching.utils.CancellableHandler; import java.io.BufferedReader; import java.io.File; @@ -12,6 +11,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.util.Collection; import java.util.Date; +import java.util.concurrent.CancellationException; public abstract class FileParser { /** @@ -26,18 +26,18 @@ public abstract class FileParser { * @throws ParserException * if the input stream contains data not matching the file format of the parser */ - public abstract Collection<cgCache> parse(final InputStream stream, final Handler progressHandler) throws IOException, ParserException; + public abstract Collection<cgCache> parse(final InputStream stream, final CancellableHandler progressHandler) throws IOException, ParserException; /** * Convenience method for parsing a file. - * + * * @param file * @param progressHandler * @return * @throws IOException * @throws ParserException */ - public Collection<cgCache> parse(final File file, final Handler progressHandler) throws IOException, ParserException { + public Collection<cgCache> parse(final File file, final CancellableHandler progressHandler) throws IOException, ParserException { FileInputStream fis = new FileInputStream(file); try { return parse(fis, progressHandler); @@ -46,7 +46,7 @@ public abstract class FileParser { } } - protected static StringBuilder readStream(InputStream is, Handler progressHandler) throws IOException { + protected static StringBuilder readStream(InputStream is, CancellableHandler progressHandler) throws IOException { final StringBuilder buffer = new StringBuilder(); ProgressInputStream progressInputStream = new ProgressInputStream(is); final BufferedReader input = new BufferedReader(new InputStreamReader(progressInputStream)); @@ -63,8 +63,11 @@ public abstract class FileParser { } } - protected static void showProgressMessage(final Handler handler, final int bytesRead) { + protected static void showProgressMessage(final CancellableHandler handler, final int bytesRead) { if (handler != null) { + if (handler.isCancelled()) { + throw new CancellationException(); + } handler.sendMessage(handler.obtainMessage(0, bytesRead, 0)); } } diff --git a/main/src/cgeo/geocaching/files/GPXImporter.java b/main/src/cgeo/geocaching/files/GPXImporter.java index 68cfe4a..4ee7e99 100644 --- a/main/src/cgeo/geocaching/files/GPXImporter.java +++ b/main/src/cgeo/geocaching/files/GPXImporter.java @@ -7,12 +7,15 @@ import cgeo.geocaching.cgSearch; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.activity.IAbstractActivity; +import cgeo.geocaching.activity.Progress; +import cgeo.geocaching.utils.CancellableHandler; import org.apache.commons.lang3.StringUtils; import android.app.Activity; import android.app.ProgressDialog; import android.content.ContentResolver; +import android.content.Context; import android.content.res.Resources; import android.net.Uri; import android.os.Handler; @@ -23,6 +26,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Collection; +import java.util.concurrent.CancellationException; public class GPXImporter { static final int IMPORT_STEP_READ_FILE = 1; @@ -30,39 +34,45 @@ public class GPXImporter { static final int IMPORT_STEP_STORE_CACHES = 3; static final int IMPORT_STEP_FINISHED = 4; static final int IMPORT_STEP_FINISHED_WITH_ERROR = 5; + static final int IMPORT_STEP_CANCEL = 6; + static final int IMPORT_STEP_CANCELED = 7; public static final String GPX_FILE_EXTENSION = ".gpx"; public static final String WAYPOINTS_FILE_SUFFIX_AND_EXTENSION = "-wpts.gpx"; - private ProgressDialog parseDialog = null; + private Progress progress = new Progress(); + private Resources res; private int listId; private IAbstractActivity fromActivity; private boolean closeOnFinish = false; - public GPXImporter(final int listId) { + public GPXImporter(final IAbstractActivity fromActivity, final int listId) { this.listId = listId; + this.fromActivity = fromActivity; + Activity realActivity = (Activity) fromActivity; + res = realActivity.getResources(); } - private void createProgressDialog(IAbstractActivity activity) { - this.fromActivity = activity; - Activity realActivity = (Activity) activity; - res = realActivity.getResources(); - parseDialog = new ProgressDialog(realActivity); - 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_caches)); - parseDialog.setCancelable(false); - parseDialog.setMax(-1); - parseDialog.show(); + public void importGPX(final File file) { + if (StringUtils.endsWithIgnoreCase(file.getName(), GPX_FILE_EXTENSION)) { + new ImportGpxFileThread(file, listId, importStepHandler, progressHandler).start(); + } else { + new ImportLocFileThread(file, listId, importStepHandler, progressHandler).start(); + } + } + + public void importGPX(final Uri uri, ContentResolver contentResolver) { + closeOnFinish = true; + new ImportAttachmentThread(uri, contentResolver, listId, importStepHandler, progressHandler).start(); } static abstract class ImportThread extends Thread { final int listId; final Handler importStepHandler; - final Handler progressHandler; + final CancellableHandler progressHandler; - public ImportThread(int listId, Handler importStepHandler, Handler progressHandler) { + public ImportThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) { this.listId = listId; this.importStepHandler = importStepHandler; this.progressHandler = progressHandler; @@ -85,6 +95,9 @@ public class GPXImporter { } catch (ParserException e) { Log.i(Settings.tag, "Importing caches failed - data format error" + e.getMessage()); importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED_WITH_ERROR, R.string.gpx_import_error_parser, 0, e.getLocalizedMessage())); + } catch (CancellationException e) { + Log.i(Settings.tag, "Importing caches canceled"); + importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_CANCELED)); } catch (Exception e) { Log.e(Settings.tag, "Importing caches failed - unknown error: ", e); importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED_WITH_ERROR, R.string.gpx_import_error_unexpected, 0, e.getLocalizedMessage())); @@ -101,6 +114,10 @@ public class GPXImporter { // remove from cache because a cache might be re-imported app.removeCacheFromCache(cache.getGeocode()); app.addCacheToSearch(search, cache); + + if (progressHandler.isCancelled()) { + throw new CancellationException(); + } progressHandler.sendMessage(progressHandler.obtainMessage(0, ++storedCaches, 0)); } return search; @@ -110,7 +127,7 @@ public class GPXImporter { static class ImportGpxFileThread extends ImportThread { private final File cacheFile; - public ImportGpxFileThread(final File file, int listId, Handler importStepHandler, Handler progressHandler) { + public ImportGpxFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) { super(listId, importStepHandler, progressHandler); this.cacheFile = file; } @@ -145,7 +162,7 @@ public class GPXImporter { static class ImportLocFileThread extends ImportThread { private final File file; - public ImportLocFileThread(final File file, int listId, Handler importStepHandler, Handler progressHandler) { + public ImportLocFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) { super(listId, importStepHandler, progressHandler); this.file = file; } @@ -163,7 +180,7 @@ public class GPXImporter { private final Uri uri; private ContentResolver contentResolver; - public ImportAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, Handler progressHandler) { + public ImportAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, CancellableHandler progressHandler) { super(listId, importStepHandler, progressHandler); this.uri = uri; this.contentResolver = contentResolver; @@ -198,25 +215,10 @@ public class GPXImporter { } } - public void importGPX(final IAbstractActivity fromActivity, final File file) { - createProgressDialog(fromActivity); - if (StringUtils.endsWithIgnoreCase(file.getName(), GPX_FILE_EXTENSION)) { - new ImportGpxFileThread(file, listId, importStepHandler, progressHandler).start(); - } else { - new ImportLocFileThread(file, listId, importStepHandler, progressHandler).start(); - } - } - - public void importGPX(final IAbstractActivity fromActivity, final Uri uri, ContentResolver contentResolver) { - createProgressDialog(fromActivity); - closeOnFinish = true; - new ImportAttachmentThread(uri, contentResolver, listId, importStepHandler, progressHandler).start(); - } - - final private Handler progressHandler = new Handler() { + final private CancellableHandler progressHandler = new CancellableHandler() { @Override - public void handleMessage(Message msg) { - parseDialog.setProgress(msg.arg1); + public void handleRegularMessage(Message msg) { + progress.setProgress(msg.arg1); } }; @@ -225,24 +227,38 @@ public class GPXImporter { public void handleMessage(Message msg) { switch (msg.what) { case IMPORT_STEP_READ_FILE: + Message cancelMessage = importStepHandler.obtainMessage(IMPORT_STEP_CANCEL); + progress.show((Context) fromActivity, res.getString(R.string.gpx_import_title_reading_file), res.getString(msg.arg1), ProgressDialog.STYLE_HORIZONTAL, cancelMessage); + progress.setMaxProgressAndReset(msg.arg2); + break; + case IMPORT_STEP_READ_WPT_FILE: case IMPORT_STEP_STORE_CACHES: - parseDialog.setMessage(res.getString(msg.arg1)); - parseDialog.setMax(msg.arg2); - parseDialog.setProgress(0); + progress.setMessage(res.getString(msg.arg1)); + progress.setMaxProgressAndReset(msg.arg2); break; case IMPORT_STEP_FINISHED: - parseDialog.dismiss(); + progress.dismiss(); fromActivity.helpDialog(res.getString(R.string.gpx_import_title_caches_imported), msg.arg1 + " " + res.getString(R.string.gpx_import_caches_imported)); closeActivity(); break; case IMPORT_STEP_FINISHED_WITH_ERROR: - parseDialog.dismiss(); + progress.dismiss(); fromActivity.helpDialog(res.getString(R.string.gpx_import_title_caches_import_failed), res.getString(msg.arg1) + "\n\n" + msg.obj); closeActivity(); break; + + case IMPORT_STEP_CANCEL: + progress.dismiss(); + progressHandler.cancel(); + break; + + case IMPORT_STEP_CANCELED: + fromActivity.showShortToast(res.getString(R.string.gpx_import_canceled)); + closeActivity(); + break; } } }; diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java index c1901de..8901d46 100644 --- a/main/src/cgeo/geocaching/files/GPXParser.java +++ b/main/src/cgeo/geocaching/files/GPXParser.java @@ -13,12 +13,12 @@ import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.utils.CancellableHandler; import org.apache.commons.lang3.StringUtils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; -import android.os.Handler; import android.sax.Element; import android.sax.EndElementListener; import android.sax.EndTextElementListener; @@ -235,7 +235,7 @@ public abstract class GPXParser extends FileParser { return formatSimple.parse(input); } - public Collection<cgCache> parse(final InputStream stream, final Handler progressHandler) throws IOException, ParserException { + public Collection<cgCache> parse(final InputStream stream, final CancellableHandler progressHandler) throws IOException, ParserException { final RootElement root = new RootElement(namespace, "gpx"); final Element waypoint = root.getChild(namespace, "wpt"); diff --git a/main/src/cgeo/geocaching/files/LocParser.java b/main/src/cgeo/geocaching/files/LocParser.java index e21b2c2..90a6c50 100644 --- a/main/src/cgeo/geocaching/files/LocParser.java +++ b/main/src/cgeo/geocaching/files/LocParser.java @@ -7,10 +7,10 @@ import cgeo.geocaching.cgCoord; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.geopoint.GeopointParser; +import cgeo.geocaching.utils.CancellableHandler; import org.apache.commons.lang3.StringUtils; -import android.os.Handler; import android.util.Log; import java.io.IOException; @@ -145,7 +145,7 @@ public final class LocParser extends FileParser { } @Override - public Collection<cgCache> parse(InputStream stream, Handler progressHandler) throws IOException, ParserException { + public Collection<cgCache> parse(InputStream stream, CancellableHandler progressHandler) throws IOException, ParserException { // TODO: progress reporting happens during reading stream only, not during parsing String streamContent = readStream(stream, progressHandler).toString(); final Map<String, cgCoord> coords = parseCoordinates(streamContent); diff --git a/main/src/cgeo/geocaching/utils/CancellableHandler.java b/main/src/cgeo/geocaching/utils/CancellableHandler.java index 0b490d8..d4915eb 100644 --- a/main/src/cgeo/geocaching/utils/CancellableHandler.java +++ b/main/src/cgeo/geocaching/utils/CancellableHandler.java @@ -9,7 +9,7 @@ import android.os.Message; */ public abstract class CancellableHandler extends Handler { - private boolean cancelled = false; + private volatile boolean cancelled = false; private static class CancelHolder { final Object payload; |
