diff options
Diffstat (limited to 'main/src/cgeo/geocaching/files/GPXImporter.java')
| -rw-r--r-- | main/src/cgeo/geocaching/files/GPXImporter.java | 65 |
1 files changed, 29 insertions, 36 deletions
diff --git a/main/src/cgeo/geocaching/files/GPXImporter.java b/main/src/cgeo/geocaching/files/GPXImporter.java index 10d791b..a1c04d7 100644 --- a/main/src/cgeo/geocaching/files/GPXImporter.java +++ b/main/src/cgeo/geocaching/files/GPXImporter.java @@ -3,7 +3,7 @@ package cgeo.geocaching.files; import cgeo.geocaching.Geocache; import cgeo.geocaching.R; import cgeo.geocaching.SearchResult; -import cgeo.geocaching.Settings; +import cgeo.geocaching.settings.Settings; import cgeo.geocaching.StaticMapsProvider; import cgeo.geocaching.cgData; import cgeo.geocaching.activity.IAbstractActivity; @@ -57,12 +57,12 @@ public class GPXImporter { private static final List<String> GPX_MIME_TYPES = Arrays.asList("text/xml", "application/xml"); private static final List<String> ZIP_MIME_TYPES = Arrays.asList("application/zip", "application/x-compressed", "application/x-zip-compressed", "application/x-zip", "application/octet-stream"); - private Progress progress = new Progress(true); + private final Progress progress = new Progress(true); - private Resources res; - private int listId; - private IAbstractActivity fromActivity; - private Handler importFinishedHandler; + private final Resources res; + private final int listId; + private final IAbstractActivity fromActivity; + private final Handler importFinishedHandler; public GPXImporter(final IAbstractActivity fromActivity, final int listId, final Handler importFinishedHandler) { this.listId = listId; @@ -143,14 +143,12 @@ public class GPXImporter { final Collection<Geocache> caches = doImport(); Log.i("Imported successfully " + caches.size() + " caches."); - final SearchResult search = new SearchResult(); - for (Geocache cache : caches) { - search.addAndPutInCache(cache); - } + final SearchResult search = new SearchResult(caches); + // Do not put imported caches into the cachecache. That would consume lots of memory for no benefit. 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); + final boolean finishedWithoutCancel = importStaticMaps(search); // Skip last message if static maps where canceled if (!finishedWithoutCancel) { return; @@ -158,16 +156,16 @@ public class GPXImporter { } importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED, search.getCount(), 0, search)); - } catch (IOException e) { + } catch (final IOException e) { Log.i("Importing caches failed - error reading data: ", e); importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED_WITH_ERROR, R.string.gpx_import_error_io, 0, e.getLocalizedMessage())); - } catch (ParserException e) { + } catch (final ParserException e) { Log.i("Importing caches failed - data format error", e); importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED_WITH_ERROR, R.string.gpx_import_error_parser, 0, e.getLocalizedMessage())); - } catch (CancellationException e) { + } catch (final CancellationException e) { Log.i("Importing caches canceled"); importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_CANCELED)); - } catch (Exception e) { + } catch (final Exception e) { Log.e("Importing caches failed - unknown error: ", e); importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED_WITH_ERROR, R.string.gpx_import_error_unexpected, 0, e.getLocalizedMessage())); } @@ -207,14 +205,14 @@ public class GPXImporter { protected Collection<Geocache> doImport() throws IOException, ParserException { Log.i("Import LOC file: " + file.getAbsolutePath()); importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, (int) file.length())); - LocParser parser = new LocParser(listId); + final LocParser parser = new LocParser(listId); return parser.parse(file, progressHandler); } } static class ImportLocAttachmentThread extends ImportThread { private final Uri uri; - private ContentResolver contentResolver; + private final ContentResolver contentResolver; public ImportLocAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, CancellableHandler progressHandler) { super(listId, importStepHandler, progressHandler); @@ -226,8 +224,8 @@ public class GPXImporter { protected Collection<Geocache> doImport() throws IOException, ParserException { Log.i("Import LOC from uri: " + uri); importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, -1)); - InputStream is = contentResolver.openInputStream(uri); - LocParser parser = new LocParser(listId); + final InputStream is = contentResolver.openInputStream(uri); + final LocParser parser = new LocParser(listId); try { return parser.parse(is, progressHandler); } finally { @@ -247,7 +245,7 @@ public class GPXImporter { try { // try to parse cache file as GPX 10 return doImport(new GPX10Parser(listId)); - } catch (ParserException pe) { + } catch (final ParserException pe) { // didn't work -> lets try GPX11 return doImport(new GPX11Parser(listId)); } @@ -285,7 +283,7 @@ public class GPXImporter { static class ImportGpxAttachmentThread extends ImportGpxThread { private final Uri uri; - private ContentResolver contentResolver; + private final ContentResolver contentResolver; public ImportGpxAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, CancellableHandler progressHandler) { super(listId, importStepHandler, progressHandler); @@ -297,7 +295,7 @@ public class GPXImporter { protected Collection<Geocache> doImport(GPXParser parser) throws IOException, ParserException { Log.i("Import GPX from uri: " + uri); importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, -1)); - InputStream is = contentResolver.openInputStream(uri); + final InputStream is = contentResolver.openInputStream(uri); try { return parser.parse(is, progressHandler); } finally { @@ -371,7 +369,7 @@ public class GPXImporter { static class ImportGpxZipAttachmentThread extends ImportGpxZipThread { private final Uri uri; - private ContentResolver contentResolver; + private final ContentResolver contentResolver; public ImportGpxZipAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, CancellableHandler progressHandler) { super(listId, importStepHandler, progressHandler); @@ -394,13 +392,11 @@ public class GPXImporter { }; final private Handler importStepHandler = new Handler() { - private boolean showProgressAfterCancel = false; - @Override public void handleMessage(Message msg) { switch (msg.what) { case IMPORT_STEP_START: - Message cancelMessage = importStepHandler.obtainMessage(IMPORT_STEP_CANCEL); + final Message cancelMessage = importStepHandler.obtainMessage(IMPORT_STEP_CANCEL); progress.show((Context) fromActivity, res.getString(R.string.gpx_import_title_reading_file), res.getString(R.string.gpx_import_loading_caches), ProgressDialog.STYLE_HORIZONTAL, cancelMessage); break; @@ -412,7 +408,7 @@ public class GPXImporter { case IMPORT_STEP_STORE_STATIC_MAPS: progress.dismiss(); - Message skipMessage = importStepHandler.obtainMessage(IMPORT_STEP_STATIC_MAPS_SKIPPED, msg.arg2, 0); + final Message skipMessage = importStepHandler.obtainMessage(IMPORT_STEP_STATIC_MAPS_SKIPPED, msg.arg2, 0); progress.show((Context) fromActivity, res.getString(R.string.gpx_import_title_static_maps), res.getString(R.string.gpx_import_store_static_maps), ProgressDialog.STYLE_HORIZONTAL, skipMessage); progress.setMaxProgressAndReset(msg.arg2); break; @@ -420,7 +416,7 @@ public class GPXImporter { case IMPORT_STEP_STATIC_MAPS_SKIPPED: progress.dismiss(); progressHandler.cancel(); - StringBuilder bufferSkipped = new StringBuilder(20); + final StringBuilder bufferSkipped = new StringBuilder(20); bufferSkipped.append(res.getString(R.string.gpx_import_static_maps_skipped)).append(", ").append(msg.arg1).append(' ').append(res.getString(R.string.gpx_import_caches_imported)); fromActivity.helpDialog(res.getString(R.string.gpx_import_title_caches_imported), bufferSkipped.toString()); importFinished(); @@ -444,11 +440,8 @@ public class GPXImporter { break; case IMPORT_STEP_CANCELED: - StringBuilder bufferCanceled = new StringBuilder(20); + final StringBuilder bufferCanceled = new StringBuilder(20); bufferCanceled.append(res.getString(R.string.gpx_import_canceled)); - if (showProgressAfterCancel) { - bufferCanceled.append(", ").append(progress.getProgress()).append(' ').append(res.getString(R.string.gpx_import_caches_imported)); - } fromActivity.showShortToast(bufferCanceled.toString()); importFinished(); break; @@ -469,13 +462,13 @@ public class GPXImporter { return null; } final String gpxFileName = gpxfile.getName(); - File dir = gpxfile.getParentFile(); - String[] filenameList = dir.list(); - for (String filename : filenameList) { + final File dir = gpxfile.getParentFile(); + final String[] filenameList = dir.list(); + for (final String filename : filenameList) { if (!StringUtils.containsIgnoreCase(filename, WAYPOINTS_FILE_SUFFIX)) { continue; } - String expectedGpxFileName = StringUtils.substringBeforeLast(filename, WAYPOINTS_FILE_SUFFIX) + final String expectedGpxFileName = StringUtils.substringBeforeLast(filename, WAYPOINTS_FILE_SUFFIX) + StringUtils.substringAfterLast(filename, WAYPOINTS_FILE_SUFFIX); if (gpxFileName.equals(expectedGpxFileName)) { return filename; |
