diff options
author | Marco Jacob <mjacob@union06.de> | 2012-02-24 12:51:01 +0100 |
---|---|---|
committer | Marco Jacob <mjacob@union06.de> | 2012-02-24 12:51:01 +0100 |
commit | 19eff4998cae6cd76f401c546b5b7e0211649728 (patch) | |
tree | 583b5030d8fb377985f0993bdd4583554ebaf899 /main | |
parent | d53b44562e1d531292f19c4b46028b47fdd7fd3f (diff) | |
download | cgeo-19eff4998cae6cd76f401c546b5b7e0211649728.zip cgeo-19eff4998cae6cd76f401c546b5b7e0211649728.tar.gz cgeo-19eff4998cae6cd76f401c546b5b7e0211649728.tar.bz2 |
import static maps after gpx import
Diffstat (limited to 'main')
-rw-r--r-- | main/res/values-de/strings.xml | 1 | ||||
-rw-r--r-- | main/res/values/strings.xml | 1 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/StaticMapsProvider.java | 43 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/files/GPXImporter.java | 37 |
4 files changed, 66 insertions, 16 deletions
diff --git a/main/res/values-de/strings.xml b/main/res/values-de/strings.xml index 982b9f2..bf2691c 100644 --- a/main/res/values-de/strings.xml +++ b/main/res/values-de/strings.xml @@ -573,6 +573,7 @@ <!-- gpx --> <string name="gpx_import_loading_caches">Lade Caches aus GPX-Datei</string> <string name="gpx_import_loading_waypoints">Lade Wegpunkte aus GPX-Datei</string> + <string name="gpx_import_store_static_maps">Schreibe statische Karten</string> <string name="gpx_import_storing">Schreibe Caches in Datenbank</string> <string name="gpx_import_caches_imported">Caches importiert.</string> <string name="gpx_import_title">Importiere GPX-Datei</string> diff --git a/main/res/values/strings.xml b/main/res/values/strings.xml index 175e657..5cdfd7e 100644 --- a/main/res/values/strings.xml +++ b/main/res/values/strings.xml @@ -600,6 +600,7 @@ <!-- gpx --> <string name="gpx_import_loading_caches">Loading caches from .gpx file</string> <string name="gpx_import_loading_waypoints">Loading waypoints file</string> + <string name="gpx_import_store_static_maps">Storing static maps</string> <string name="gpx_import_storing">Writing caches to database</string> <string name="gpx_import_caches_imported">caches imported</string> <string name="gpx_import_title">Import GPX</string> diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java index 6384d59..e09b93b 100644 --- a/main/src/cgeo/geocaching/StaticMapsProvider.java +++ b/main/src/cgeo/geocaching/StaticMapsProvider.java @@ -1,5 +1,8 @@ package cgeo.geocaching; +import cgeo.geocaching.concurrent.PriorityThreadFactory; +import cgeo.geocaching.concurrent.Task; +import cgeo.geocaching.concurrent.ThreadPool; import cgeo.geocaching.files.LocalStorage; import cgeo.geocaching.geopoint.GeopointFormatter.Format; @@ -14,13 +17,14 @@ import android.view.Display; import android.view.WindowManager; import java.io.File; +import java.util.concurrent.TimeUnit; public class StaticMapsProvider { private static final String MARKERS_URL = "http://cgeo.carnero.cc/_markers/"; - /** - * in my tests the "no image available" image had 5470 bytes, while "street only" maps had at least 20000 bytes - */ + /** In my tests the "no image available" image had 5470 bytes, while "street only" maps had at least 20000 bytes */ private static final int MIN_MAP_IMAGE_BYTES = 6000; + /** ThreadPool restricting this to 1 Thread. **/ + private static final ThreadPool pool = new ThreadPool(1, new PriorityThreadFactory(Thread.MIN_PRIORITY)); public static File getMapFile(final String geocode, String prefix, final int level, final boolean createDirs) { return LocalStorage.getStorageFile(geocode, "map_" + prefix + level, false, createDirs); @@ -51,12 +55,22 @@ public class StaticMapsProvider { } } + public static void downloadMaps(cgCache cache, cgeoapplication app) { + final Display display = ((WindowManager) app.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + downloadMaps(cache, display); + } + public static void downloadMaps(cgCache cache, Activity activity) { + final Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + downloadMaps(cache, display); + } + + public static void downloadMaps(cgCache cache, Display display) { if ((!Settings.isStoreOfflineMaps() && !Settings.isStoreOfflineWpMaps()) || StringUtils.isBlank(cache.getGeocode())) { return; } - int edge = guessMinDisplaySide(activity); + int edge = guessMinDisplaySide(display); if (Settings.isStoreOfflineMaps() && cache.getCoords() != null) { storeCacheStaticMap(cache, edge); @@ -71,7 +85,8 @@ public class StaticMapsProvider { } public static void storeWaypointStaticMap(cgCache cache, Activity activity, cgWaypoint waypoint) { - int edge = StaticMapsProvider.guessMinDisplaySide(activity); + final Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + int edge = StaticMapsProvider.guessMinDisplaySide(display); storeWaypointStaticMap(cache, edge, waypoint); } @@ -86,7 +101,8 @@ public class StaticMapsProvider { } public static void storeCacheStaticMap(cgCache cache, Activity activity) { - int edge = StaticMapsProvider.guessMinDisplaySide(activity); + final Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + int edge = StaticMapsProvider.guessMinDisplaySide(display); storeCacheStaticMap(cache, edge); } @@ -110,8 +126,7 @@ public class StaticMapsProvider { downloadMaps(cache, cacheMarkerUrl, "", latlonMap, edge, waypoints.toString()); } - private static int guessMinDisplaySide(Activity activity) { - final Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + private static int guessMinDisplaySide(Display display) { final int maxWidth = display.getWidth() - 25; final int maxHeight = display.getHeight() - 25; int edge = 0; @@ -125,14 +140,18 @@ public class StaticMapsProvider { private static void downloadMaps(final cgCache cache, final String markerUrl, final String prefix, final String latlonMap, final int edge, final String waypoints) { - Thread staticMapsThread = new Thread("getting static map") { + + Task currentTask = new Task("getting static map") { @Override public void run() { downloadMapsInThread(cache, markerUrl, prefix, latlonMap, edge, waypoints); } }; - staticMapsThread.setPriority(Thread.MIN_PRIORITY); - staticMapsThread.start(); + try { + pool.add(currentTask, 20, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Log.e(Settings.tag, "StaticMapsProvider.downloadMaps error adding task: " + e.toString()); + } } private static String getCacheMarkerUrl(final cgCache cache) { @@ -165,7 +184,7 @@ public class StaticMapsProvider { /** * Check if at least one map file exists for the given geocode. - * + * * @param geocode * @return <code>true</code> if at least one mapfile exists; <code>false</code> otherwise */ diff --git a/main/src/cgeo/geocaching/files/GPXImporter.java b/main/src/cgeo/geocaching/files/GPXImporter.java index 836f6e8..b8dc945 100644 --- a/main/src/cgeo/geocaching/files/GPXImporter.java +++ b/main/src/cgeo/geocaching/files/GPXImporter.java @@ -3,10 +3,12 @@ package cgeo.geocaching.files; import cgeo.geocaching.R;
import cgeo.geocaching.SearchResult;
import cgeo.geocaching.Settings;
+import cgeo.geocaching.StaticMapsProvider;
import cgeo.geocaching.cgCache;
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;
@@ -32,6 +34,7 @@ import java.util.Collection; import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@@ -41,10 +44,11 @@ public class GPXImporter { 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_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;
+ 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;
+ static final int IMPORT_STEP_CANCEL = 7;
+ static final int IMPORT_STEP_CANCELED = 8;
public static final String GPX_FILE_EXTENSION = ".gpx";
public static final String ZIP_FILE_EXTENSION = ".zip";
@@ -140,6 +144,11 @@ public class GPXImporter { SearchResult search = storeParsedCaches(caches);
Log.i(Settings.tag, "Imported successfully " + caches.size() + " caches.");
+ if (Settings.isStoreOfflineMaps() || Settings.isStoreOfflineWpMaps()) {
+ importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_STORE_STATIC_MAPS, R.string.gpx_import_store_static_maps, search.getCount()));
+ importStaticMaps(search);
+ }
+
importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_FINISHED, search.getCount(), 0, search));
} catch (IOException e) {
Log.i(Settings.tag, "Importing caches failed - error reading data: " + e.getMessage());
@@ -175,6 +184,21 @@ public class GPXImporter { }
return search;
}
+
+ private void importStaticMaps(final SearchResult importedCaches) {
+ final cgeoapplication app = cgeoapplication.getInstance();
+ Set<cgCache> caches = importedCaches.getCachesFromSearchResult(LoadFlags.LOADWAYPOINTS);
+ int storedCacheMaps = 0;
+ for (cgCache cache : caches) {
+ Log.d(Settings.tag, "GPXImporter.ImportThread.importStaticMaps start downloadMaps");
+ StaticMapsProvider.downloadMaps(cache, app);
+ storedCacheMaps++;
+ if (progressHandler.isCancelled()) {
+ throw new CancellationException();
+ }
+ progressHandler.sendMessage(progressHandler.obtainMessage(0, storedCacheMaps, 0));
+ }
+ }
}
static class ImportLocFileThread extends ImportThread {
@@ -367,6 +391,11 @@ public class GPXImporter { progress.setMaxProgressAndReset(msg.arg2);
break;
+ case IMPORT_STEP_STORE_STATIC_MAPS:
+ progress.setMessage(res.getString(msg.arg1));
+ progress.setMaxProgressAndReset(msg.arg2);
+ break;
+
case IMPORT_STEP_FINISHED:
progress.dismiss();
fromActivity.helpDialog(res.getString(R.string.gpx_import_title_caches_imported), msg.arg1 + " " + res.getString(R.string.gpx_import_caches_imported));
|