diff options
Diffstat (limited to 'main/src/cgeo/geocaching/files')
| -rw-r--r-- | main/src/cgeo/geocaching/files/FileList.java | 16 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/GPXImporter.java | 14 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/GPXParser.java | 44 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/LocParser.java | 38 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/LocalStorage.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/ParserException.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/SimpleDirChooser.java | 13 |
7 files changed, 63 insertions, 70 deletions
diff --git a/main/src/cgeo/geocaching/files/FileList.java b/main/src/cgeo/geocaching/files/FileList.java index df95085..990d2ae 100644 --- a/main/src/cgeo/geocaching/files/FileList.java +++ b/main/src/cgeo/geocaching/files/FileList.java @@ -8,6 +8,7 @@ import cgeo.geocaching.utils.Log; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.mapsforge.core.IOUtils; import android.app.ProgressDialog; import android.content.DialogInterface; @@ -242,15 +243,8 @@ public abstract class FileList<T extends ArrayAdapter<File>> extends AbstractLis Log.e("Could not get additional mount points for user content. " + "Proceeding with external storage only (" + extStorage + ")"); } finally { - try { - if (fr != null) { - fr.close(); - } - if (br != null) { - br.close(); - } - } catch (IOException e) { - } + IOUtils.closeQuietly(fr); + IOUtils.closeQuietly(br); } } return storages; @@ -283,8 +277,8 @@ public abstract class FileList<T extends ArrayAdapter<File>> extends AbstractLis private void setExtensions(final String[] extensionsIn) { extensions = extensionsIn; for (int i = 0; i < extensions.length; i++) { - String extension = extensions[i]; - if (extension.length() == 0 || extension.charAt(0) != '.') { + final String extension = extensions[i]; + if (StringUtils.isEmpty(extension) || extension.charAt(0) != '.') { extensions[i] = "." + extension; } } diff --git a/main/src/cgeo/geocaching/files/GPXImporter.java b/main/src/cgeo/geocaching/files/GPXImporter.java index fb78360..750d5e0 100644 --- a/main/src/cgeo/geocaching/files/GPXImporter.java +++ b/main/src/cgeo/geocaching/files/GPXImporter.java @@ -5,7 +5,7 @@ import cgeo.geocaching.SearchResult; import cgeo.geocaching.Settings;
import cgeo.geocaching.StaticMapsProvider;
import cgeo.geocaching.cgCache;
-import cgeo.geocaching.cgeoapplication;
+import cgeo.geocaching.cgData;
import cgeo.geocaching.activity.IAbstractActivity;
import cgeo.geocaching.activity.Progress;
import cgeo.geocaching.enumerations.LoadFlags;
@@ -124,7 +124,7 @@ public class GPXImporter { final Handler importStepHandler;
final CancellableHandler progressHandler;
- public ImportThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
+ protected ImportThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
this.listId = listId;
this.importStepHandler = importStepHandler;
this.progressHandler = progressHandler;
@@ -132,10 +132,9 @@ public class GPXImporter { @Override
public void run() {
- final Collection<cgCache> caches;
try {
importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_START));
- caches = doImport();
+ final Collection<cgCache> caches = doImport();
Log.i("Imported successfully " + caches.size() + " caches.");
final SearchResult search = new SearchResult();
@@ -171,10 +170,9 @@ public class GPXImporter { protected abstract Collection<cgCache> doImport() throws IOException, ParserException;
private boolean importStaticMaps(final SearchResult importedCaches) {
- final cgeoapplication app = cgeoapplication.getInstance();
int storedCacheMaps = 0;
for (String geocode : importedCaches.getGeocodes()) {
- cgCache cache = app.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
+ cgCache cache = cgData.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
Log.d("GPXImporter.ImportThread.importStaticMaps start downloadMaps for cache " + geocode);
StaticMapsProvider.downloadMaps(cache);
storedCacheMaps++;
@@ -206,7 +204,7 @@ public class GPXImporter { static abstract class ImportGpxThread extends ImportThread {
- public ImportGpxThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
+ protected ImportGpxThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
super(listId, importStepHandler, progressHandler);
}
@@ -276,7 +274,7 @@ public class GPXImporter { static abstract class ImportGpxZipThread extends ImportGpxThread {
- public ImportGpxZipThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
+ protected ImportGpxZipThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
super(listId, importStepHandler, progressHandler);
}
diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java index b9d3f53..2ac19bb 100644 --- a/main/src/cgeo/geocaching/files/GPXParser.java +++ b/main/src/cgeo/geocaching/files/GPXParser.java @@ -4,6 +4,7 @@ import cgeo.geocaching.LogEntry; import cgeo.geocaching.R; import cgeo.geocaching.StoredList; import cgeo.geocaching.cgCache; +import cgeo.geocaching.cgData; import cgeo.geocaching.cgTrackable; import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.cgeoapplication; @@ -287,7 +288,7 @@ public abstract class GPXParser extends FileParser { // take the name as code, if nothing else is available if (StringUtils.isBlank(cache.getGeocode())) { if (StringUtils.isNotBlank(name)) { - cache.setGeocode(name.trim().toUpperCase()); + cache.setGeocode(name.trim()); } } @@ -311,8 +312,8 @@ public abstract class GPXParser extends FileParser { // finally store the cache in the database result.add(geocode); - cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); - cgeoapplication.getInstance().removeAllFromCache(); + cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); + cgData.removeAllFromCache(); showProgressMessage(progressHandler, progressStream.getProgress()); } else if (StringUtils.isNotBlank(cache.getName()) && StringUtils.containsIgnoreCase(type, "waypoint")) { @@ -326,10 +327,9 @@ public abstract class GPXParser extends FileParser { fixCache(cache); if (cache.getName().length() > 2) { - final String cacheGeocodeForWaypoint = "GC" + cache.getName().substring(2).toUpperCase(); - + final String cacheGeocodeForWaypoint = "GC" + cache.getName().substring(2).toUpperCase(Locale.US); // lookup cache for waypoint in already parsed caches - final cgCache cacheForWaypoint = cgeoapplication.getInstance().loadCache(cacheGeocodeForWaypoint, LoadFlags.LOAD_CACHE_OR_DB); + final cgCache cacheForWaypoint = cgData.loadCache(cacheGeocodeForWaypoint, LoadFlags.LOAD_CACHE_OR_DB); if (cacheForWaypoint != null) { final cgWaypoint waypoint = new cgWaypoint(cache.getShortdesc(), convertWaypointSym2Type(sym), false); waypoint.setId(-1); @@ -347,7 +347,7 @@ public abstract class GPXParser extends FileParser { newPoints.add(waypoint); cgWaypoint.mergeWayPoints(newPoints, mergedWayPoints, true); cacheForWaypoint.setWaypoints(newPoints, false); - cgeoapplication.getInstance().saveCache(cacheForWaypoint, EnumSet.of(SaveFlag.SAVE_DB)); + cgData.saveCache(cacheForWaypoint, EnumSet.of(SaveFlag.SAVE_DB)); showProgressMessage(progressHandler, progressStream.getProgress()); } } @@ -410,7 +410,7 @@ public abstract class GPXParser extends FileParser { public void end(String body) { final String[] content = body.split("\\|"); if (content.length > 0) { - type = content[0].toLowerCase().trim(); + type = content[0].toLowerCase(Locale.US).trim(); } } }); @@ -420,7 +420,7 @@ public abstract class GPXParser extends FileParser { @Override public void end(final String body) { - sym = body.toLowerCase(); + sym = body.toLowerCase(Locale.US); if (sym.contains("geocache") && sym.contains("found")) { cache.setFound(true); } @@ -526,7 +526,7 @@ public abstract class GPXParser extends FileParser { @Override public void end(String body) { - cache.setType(CacheType.getByPattern(validate(body.toLowerCase()))); + cache.setType(CacheType.getByPattern(validate(body))); } }); @@ -535,7 +535,7 @@ public abstract class GPXParser extends FileParser { @Override public void end(String body) { - cache.setSize(CacheSize.getById(validate(body.toLowerCase()))); + cache.setSize(CacheSize.getById(validate(body))); } }); @@ -665,7 +665,7 @@ public abstract class GPXParser extends FileParser { try { if (attrs.getIndex("ref") > -1) { - trackable.setGeocode(attrs.getValue("ref").toUpperCase()); + trackable.setGeocode(attrs.getValue("ref")); } } catch (Exception e) { // nothing @@ -745,7 +745,7 @@ public abstract class GPXParser extends FileParser { @Override public void end(String body) { - final String logType = validate(body).toLowerCase(); + final String logType = validate(body); log.type = LogType.getByType(logType); } }); @@ -772,9 +772,9 @@ public abstract class GPXParser extends FileParser { try { progressStream = new ProgressInputStream(stream); Xml.parse(progressStream, Xml.Encoding.UTF_8, root.getContentHandler()); - return cgeoapplication.getInstance().loadCaches(result, EnumSet.of(LoadFlag.LOAD_DB_MINIMAL)); + return cgData.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()); + Log.w("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); } } @@ -806,17 +806,21 @@ public abstract class GPXParser extends FileParser { static WaypointType convertWaypointSym2Type(final String sym) { if ("parking area".equalsIgnoreCase(sym)) { return WaypointType.PARKING; - } else if ("stages of a multicache".equalsIgnoreCase(sym)) { + } + if ("stages of a multicache".equalsIgnoreCase(sym)) { return WaypointType.STAGE; - } else if ("question to answer".equalsIgnoreCase(sym)) { + } + if ("question to answer".equalsIgnoreCase(sym)) { return WaypointType.PUZZLE; - } else if ("trailhead".equalsIgnoreCase(sym)) { + } + if ("trailhead".equalsIgnoreCase(sym)) { return WaypointType.TRAILHEAD; - } else if ("final location".equalsIgnoreCase(sym)) { + } + if ("final location".equalsIgnoreCase(sym)) { return WaypointType.FINAL; } // this is not fully correct, but lets also look for localized waypoint types - for (WaypointType waypointType : WaypointType.ALL_TYPES_EXCEPT_OWN) { + for (WaypointType waypointType : WaypointType.ALL_TYPES_EXCEPT_OWN_AND_ORIGINAL) { final String localized = waypointType.getL10n(); if (StringUtils.isNotEmpty(localized)) { if (localized.equalsIgnoreCase(sym)) { diff --git a/main/src/cgeo/geocaching/files/LocParser.java b/main/src/cgeo/geocaching/files/LocParser.java index b17b203..9c24d39 100644 --- a/main/src/cgeo/geocaching/files/LocParser.java +++ b/main/src/cgeo/geocaching/files/LocParser.java @@ -2,7 +2,7 @@ package cgeo.geocaching.files; import cgeo.geocaching.SearchResult; import cgeo.geocaching.cgCache; -import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.cgData; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LoadFlags; @@ -65,7 +65,7 @@ public final class LocParser extends FileParser { contained.add(geocode); } } - Set<cgCache> caches = cgeoapplication.getInstance().loadCaches(contained, LoadFlags.LOAD_CACHE_OR_DB); + Set<cgCache> caches = cgData.loadCaches(contained, LoadFlags.LOAD_CACHE_OR_DB); for (cgCache cache : caches) { cgCache coord = cidCoords.get(cache.getGeocode()); copyCoordToCache(coord, cache); @@ -77,7 +77,7 @@ public final class LocParser extends FileParser { cache.setDifficulty(coord.getDifficulty()); cache.setTerrain(coord.getTerrain()); cache.setSize(coord.getSize()); - cache.setGeocode(coord.getGeocode().toUpperCase()); + cache.setGeocode(coord.getGeocode()); cache.setReliableLatLon(true); if (StringUtils.isBlank(cache.getName())) { cache.setName(coord.getName()); @@ -140,6 +140,7 @@ public final class LocParser extends FileParser { cache.setType(CacheType.UNKNOWN); // type is not given in the LOC file cache.setListId(listId); cache.setDetailed(true); + cache.store(null); } Log.i("Caches found in .loc file: " + caches.size()); return caches; @@ -149,8 +150,7 @@ public final class LocParser extends FileParser { final cgCache cache = new cgCache(); final Matcher matcherGeocode = patternGeocode.matcher(pointString); if (matcherGeocode.find()) { - final String geocode = matcherGeocode.group(1).trim().toUpperCase(); - cache.setGeocode(geocode.toUpperCase()); + cache.setGeocode(matcherGeocode.group(1).trim()); } final Matcher matcherName = patternName.matcher(pointString); @@ -168,21 +168,25 @@ public final class LocParser extends FileParser { } final Matcher matcherDifficulty = patternDifficulty.matcher(pointString); - if (matcherDifficulty.find()) { - cache.setDifficulty(Float.parseFloat(matcherDifficulty.group(1).trim())); - } + try { + if (matcherDifficulty.find()) { + cache.setDifficulty(Float.parseFloat(matcherDifficulty.group(1).trim())); + } - final Matcher matcherTerrain = patternTerrain.matcher(pointString); - if (matcherTerrain.find()) { - cache.setTerrain(Float.parseFloat(matcherTerrain.group(1).trim())); - } + final Matcher matcherTerrain = patternTerrain.matcher(pointString); + if (matcherTerrain.find()) { + cache.setTerrain(Float.parseFloat(matcherTerrain.group(1).trim())); + } - final Matcher matcherContainer = patternContainer.matcher(pointString); - if (matcherContainer.find()) { - final int size = Integer.parseInt(matcherContainer.group(1).trim()); - if (size >= 1 && size <= 8) { - cache.setSize(SIZES[size - 1]); + final Matcher matcherContainer = patternContainer.matcher(pointString); + if (matcherContainer.find()) { + final int size = Integer.parseInt(matcherContainer.group(1).trim()); + if (size >= 1 && size <= 8) { + cache.setSize(SIZES[size - 1]); + } } + } catch (NumberFormatException e) { + Log.e("LocParser.parseCache", e); } return cache; diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java index da0f981..4cf28f7 100644 --- a/main/src/cgeo/geocaching/files/LocalStorage.java +++ b/main/src/cgeo/geocaching/files/LocalStorage.java @@ -299,9 +299,9 @@ public class LocalStorage { } private static boolean copy(final InputStream input, final OutputStream output) { - final byte[] buffer = new byte[4096]; - int length; try { + int length; + final byte[] buffer = new byte[4096]; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } diff --git a/main/src/cgeo/geocaching/files/ParserException.java b/main/src/cgeo/geocaching/files/ParserException.java index 5aa152c..c0076cc 100644 --- a/main/src/cgeo/geocaching/files/ParserException.java +++ b/main/src/cgeo/geocaching/files/ParserException.java @@ -13,10 +13,6 @@ public class ParserException extends Exception { super(detailMessage); } - public ParserException(Throwable throwable) { - super(throwable); - } - public ParserException(String detailMessage, Throwable throwable) { super(detailMessage, throwable); } diff --git a/main/src/cgeo/geocaching/files/SimpleDirChooser.java b/main/src/cgeo/geocaching/files/SimpleDirChooser.java index 346780d..9e99aec 100644 --- a/main/src/cgeo/geocaching/files/SimpleDirChooser.java +++ b/main/src/cgeo/geocaching/files/SimpleDirChooser.java @@ -18,7 +18,6 @@ import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; -import android.widget.ListView; import android.widget.TextView; import java.io.File; @@ -43,7 +42,10 @@ public class SimpleDirChooser extends ListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Bundle extras = getIntent().getExtras(); - String startDir = extras.getString(START_DIR); + String startDir = ""; + if (extras != null) { + startDir = extras.getString(START_DIR); + } if (StringUtils.isBlank(startDir)) { startDir = Environment.getExternalStorageDirectory().getPath(); } else { @@ -101,11 +103,6 @@ public class SimpleDirChooser extends ListActivity { this.setListAdapter(adapter); } - @Override - protected void onListItemClick(ListView l, View v, int position, long id) { - super.onListItemClick(l, v, position, id); - } - public class FileArrayAdapter extends ArrayAdapter<Option> { private Context content; @@ -200,7 +197,7 @@ public class SimpleDirChooser extends ListActivity { } } - public class Option implements Comparable<Option> { + public static class Option implements Comparable<Option> { private final String name; private final String path; private boolean checked = false; |
