diff options
| author | bananeweizen <bananeweizen@gmx.de> | 2011-08-15 11:00:58 +0200 |
|---|---|---|
| committer | bananeweizen <bananeweizen@gmx.de> | 2011-08-15 11:00:58 +0200 |
| commit | 8060acb4eaa5eee57c335c5a06403a88f588c231 (patch) | |
| tree | 269f590843b30f88f6f2e33916c20ebd7d008135 /src | |
| parent | e202499778bddb9cb3daf5529c364273b1ca43f3 (diff) | |
| download | cgeo-8060acb4eaa5eee57c335c5a06403a88f588c231.zip cgeo-8060acb4eaa5eee57c335c5a06403a88f588c231.tar.gz cgeo-8060acb4eaa5eee57c335c5a06403a88f588c231.tar.bz2 | |
clean up static maps provider, remove unused class
Diffstat (limited to 'src')
| -rw-r--r-- | src/cgeo/geocaching/StaticMapsProvider.java | 182 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgAddressImg.java | 60 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgBase.java | 57 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgMapImg.java | 114 |
4 files changed, 183 insertions, 230 deletions
diff --git a/src/cgeo/geocaching/StaticMapsProvider.java b/src/cgeo/geocaching/StaticMapsProvider.java new file mode 100644 index 0000000..b649f7b --- /dev/null +++ b/src/cgeo/geocaching/StaticMapsProvider.java @@ -0,0 +1,182 @@ +package cgeo.geocaching; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Locale; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.entity.BufferedHttpEntity; +import org.apache.http.impl.client.DefaultHttpClient; + +import android.app.Activity; +import android.content.Context; +import android.util.Log; +import android.view.Display; +import android.view.WindowManager; + +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 + */ + private static final int MIN_MAP_IMAGE_BYTES = 6000; + + private static void downloadMapsInThread(final cgCache cache, String latlonMap, int edge, String waypoints) { + createStorageDirectory(cache); + + downloadMap(cache, 20, "satellite", 1, latlonMap, edge, waypoints); + downloadMap(cache, 18, "satellite", 2, latlonMap, edge, waypoints); + downloadMap(cache, 16, "roadmap", 3, latlonMap, edge, waypoints); + downloadMap(cache, 14, "roadmap", 4, latlonMap, edge, waypoints); + downloadMap(cache, 11 ,"roadmap", 5, latlonMap, edge, waypoints); + } + + private static void createStorageDirectory(final cgCache cache) { + File dir = new File(cgSettings.getStorage()); + if (dir.exists() == false) { + dir.mkdirs(); + } + dir = new File(getStaticMapsDirectory(cache)); + if (dir.exists() == false) { + dir.mkdirs(); + } + } + + private static String getStaticMapsDirectory(final cgCache cache) { + return cgSettings.getStorage() + cache.geocode; + } + + private static void downloadMap(cgCache cache, int zoom, String mapType, int level, String latlonMap, int edge, String waypoints) { + String mapUrl = "http://maps.google.com/maps/api/staticmap?center=" + latlonMap; + String markerUrl = getMarkerUrl(cache); + + String url = mapUrl + "&zoom=" + zoom + "&size=" + edge + "x" + edge + "&maptype=" + mapType + "&markers=icon%3A" + markerUrl + "%7C" + latlonMap + waypoints + "&sensor=false"; + + final String fileName = getStaticMapsDirectory(cache) + "/map_" + level; + HttpClient client = null; + HttpGet getMethod = null; + HttpResponse httpResponse = null; + HttpEntity entity = null; + BufferedHttpEntity bufferedEntity = null; + + boolean ok = false; + + for (int i = 0; i < 3; i ++) { + if (i > 0) Log.w(cgSettings.tag, "cgMapImg.getDrawable: Failed to download data, retrying. Attempt #" + (i + 1)); + + try { + client = new DefaultHttpClient(); + getMethod = new HttpGet(url); + httpResponse = client.execute(getMethod); + entity = httpResponse.getEntity(); + + // if image is to small, don't download and save, there is no map data for this zoom level + long contentSize = entity.getContentLength(); + if (contentSize > 0 && contentSize <= MIN_MAP_IMAGE_BYTES) { + break; + } + + bufferedEntity = new BufferedHttpEntity(entity); + if (bufferedEntity != null) { + InputStream is = (InputStream)bufferedEntity.getContent(); + FileOutputStream fos = new FileOutputStream(fileName); + + int fileSize = 0; + try { + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = is.read(buffer)) != -1) { + fos.write(buffer, 0, bytesRead); + fileSize += bytesRead; + } + fos.flush(); + ok = true; + } catch (IOException e) { + Log.e(cgSettings.tag, "cgMapImg.getDrawable (saving to cache): " + e.toString()); + } finally { + is.close(); + fos.close(); + } + + bufferedEntity = null; + + // delete image if it has no contents + if (ok && fileSize < MIN_MAP_IMAGE_BYTES) { + (new File(fileName)).delete(); + } + } + + if (ok) { + break; + } + } catch (Exception e) { + Log.e(cgSettings.tag, "cgMapImg.getDrawable (downloading from web): " + e.toString()); + } + } + } + + public static void downloadMaps(cgCache cache, cgSettings settings, Activity activity) { + if (settings.storeOfflineMaps != 1 || cache.latitude == null + || cache.longitude == null || cache.geocode == null || cache.geocode.length() == 0) { + return; + } + + final String latlonMap = String.format((Locale) null, "%.6f", cache.latitude) + "," + String.format((Locale) null, "%.6f", cache.longitude); + final Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + final int maxWidth = display.getWidth() - 25; + final int maxHeight = display.getHeight() - 25; + int edge = 0; + if (maxWidth > maxHeight) { + edge = maxWidth; + } else { + edge = maxHeight; + } + + final StringBuilder waypoints = new StringBuilder(); + if (cache.waypoints != null && cache.waypoints.size() > 0) { + for (cgWaypoint waypoint : cache.waypoints) { + if (waypoint.latitude == null && waypoint.longitude == null) { + continue; + } + + waypoints.append("&markers=icon%3A" + MARKERS_URL + "marker_waypoint_"); + waypoints.append(waypoint.type); + waypoints.append(".png%7C"); + waypoints.append(String.format((Locale) null, "%.6f", waypoint.latitude)); + waypoints.append(","); + waypoints.append(String.format((Locale) null, "%.6f", waypoint.longitude)); + } + } + + // download map images in separate background thread for higher performance + downloadMaps(cache, latlonMap, edge, waypoints.toString()); + } + + private static void downloadMaps(final cgCache cache, final String latlonMap, final int edge, + final String waypoints) { + Thread staticMapsThread = new Thread("getting static map") {@Override + public void run() { + downloadMapsInThread(cache, latlonMap, edge, waypoints); + }}; + staticMapsThread.setPriority(Thread.MIN_PRIORITY); + staticMapsThread.start(); + } + + private static String getMarkerUrl(final cgCache cache) { + String type = "mystery"; + if (cache.found) { + type = cache.type + "_found"; + } else if (cache.disabled) { + type = cache.type + "_disabled"; + } else { + type = cache.type; + } + + return cgBase.urlencode_rfc3986(MARKERS_URL + "marker_cache_" + type + ".png"); + } +} diff --git a/src/cgeo/geocaching/cgAddressImg.java b/src/cgeo/geocaching/cgAddressImg.java deleted file mode 100644 index bdc0ccf..0000000 --- a/src/cgeo/geocaching/cgAddressImg.java +++ /dev/null @@ -1,60 +0,0 @@ -package cgeo.geocaching; - -import android.util.Log; -import android.graphics.Rect; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.drawable.BitmapDrawable; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.entity.BufferedHttpEntity; -import org.apache.http.impl.client.DefaultHttpClient; - -public class cgAddressImg { - public static BitmapDrawable getDrawable(String url) { - Bitmap imagePre = null; - - if (url == null || url.length() == 0) return null; - - HttpClient client = null; - HttpGet getMethod = null; - HttpResponse httpResponse = null; - HttpEntity entity = null; - BufferedHttpEntity bufferedEntity = null; - - for (int i = 0; i < 2; i ++) { - if (i > 0) Log.w(cgSettings.tag, "cgAddressImg.getDrawable: Failed to download data, retrying. Attempt #" + (i + 1)); - - try { - client = new DefaultHttpClient(); - getMethod = new HttpGet(url); - httpResponse = client.execute(getMethod); - entity = httpResponse.getEntity(); - bufferedEntity = new BufferedHttpEntity(entity); - - Log.i(cgSettings.tag, "[" + entity.getContentLength() + "B] Downloading address map " + url); - - if (bufferedEntity != null) imagePre = BitmapFactory.decodeStream(bufferedEntity.getContent(), null, null); - if (imagePre != null) break; - } catch (Exception e) { - Log.e(cgSettings.tag, "cgAddressImg.getDrawable (downloading from web): " + e.toString()); - } - } - - if (imagePre == null) { - Log.d(cgSettings.tag, "cgAddressImg.getDrawable: Failed to obtain image"); - - return null; - } - - final BitmapDrawable image = new BitmapDrawable(imagePre); - image.setBounds(new Rect(0, 0, imagePre.getWidth(), imagePre.getHeight())); - - // imagePre.recycle(); - imagePre = null; - - return image; - } -} diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java index bbf2427..654d7d5 100644 --- a/src/cgeo/geocaching/cgBase.java +++ b/src/cgeo/geocaching/cgBase.java @@ -63,8 +63,6 @@ import android.text.Spannable; import android.text.format.DateUtils; import android.text.style.StrikethroughSpan; import android.util.Log; -import android.view.Display; -import android.view.WindowManager; import android.widget.EditText; import cgeo.geocaching.activity.ActivityMixin; @@ -5050,60 +5048,7 @@ public class cgBase { } // store map previews - if (settings.storeOfflineMaps == 1 && cache.latitude != null && cache.longitude != null) { - final String latlonMap = String.format((Locale) null, "%.6f", cache.latitude) + "," + String.format((Locale) null, "%.6f", cache.longitude); - final Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); - final int maxWidth = display.getWidth() - 25; - final int maxHeight = display.getHeight() - 25; - int edge = 0; - if (maxWidth > maxHeight) { - edge = maxWidth; - } else { - edge = maxHeight; - } - - String type = "mystery"; - if (cache.found) { - type = cache.type + "_found"; - } else if (cache.disabled) { - type = cache.type + "_disabled"; - } else { - type = cache.type; - } - - final String markerUrl = urlencode_rfc3986("http://cgeo.carnero.cc/_markers/marker_cache_" + type + ".png"); - final StringBuilder waypoints = new StringBuilder(); - if (cache.waypoints != null && cache.waypoints.size() > 0) { - for (cgWaypoint waypoint : cache.waypoints) { - if (waypoint.latitude == null && waypoint.longitude == null) { - continue; - } - - waypoints.append("&markers=icon%3Ahttp://cgeo.carnero.cc/_markers/marker_waypoint_"); - waypoints.append(waypoint.type); - waypoints.append(".png%7C"); - waypoints.append(String.format((Locale) null, "%.6f", waypoint.latitude)); - waypoints.append(","); - waypoints.append(String.format((Locale) null, "%.6f", waypoint.longitude)); - } - } - - // download map images in separate background thread for higher performance - final String code = cache.geocode; - final int finalEdge = edge; - Thread staticMapsThread = new Thread("getting static map") {@Override - public void run() { - cgMapImg mapGetter = new cgMapImg(code); - - mapGetter.getDrawable("http://maps.google.com/maps/api/staticmap?center=" + latlonMap + "&zoom=20&size=" + finalEdge + "x" + finalEdge + "&maptype=satellite&markers=icon%3A" + markerUrl + "%7C" + latlonMap + waypoints.toString() + "&sensor=false", 1); - mapGetter.getDrawable("http://maps.google.com/maps/api/staticmap?center=" + latlonMap + "&zoom=18&size=" + finalEdge + "x" + finalEdge + "&maptype=satellite&markers=icon%3A" + markerUrl + "%7C" + latlonMap + waypoints.toString() + "&sensor=false", 2); - mapGetter.getDrawable("http://maps.google.com/maps/api/staticmap?center=" + latlonMap + "&zoom=16&size=" + finalEdge + "x" + finalEdge + "&maptype=roadmap&markers=icon%3A" + markerUrl + "%7C" + latlonMap + waypoints.toString() + "&sensor=false", 3); - mapGetter.getDrawable("http://maps.google.com/maps/api/staticmap?center=" + latlonMap + "&zoom=14&size=" + finalEdge + "x" + finalEdge + "&maptype=roadmap&markers=icon%3A" + markerUrl + "%7C" + latlonMap + waypoints.toString() + "&sensor=false", 4); - mapGetter.getDrawable("http://maps.google.com/maps/api/staticmap?center=" + latlonMap + "&zoom=11&size=" + finalEdge + "x" + finalEdge + "&maptype=roadmap&markers=icon%3A" + markerUrl + "%7C" + latlonMap + waypoints.toString() + "&sensor=false", 5); - }}; - staticMapsThread.setPriority(Thread.MIN_PRIORITY); - staticMapsThread.start(); - } + StaticMapsProvider.downloadMaps(cache, settings, activity); app.markStored(cache.geocode, listId); app.removeCacheFromCache(cache.geocode); diff --git a/src/cgeo/geocaching/cgMapImg.java b/src/cgeo/geocaching/cgMapImg.java deleted file mode 100644 index 769e997..0000000 --- a/src/cgeo/geocaching/cgMapImg.java +++ /dev/null @@ -1,114 +0,0 @@ -package cgeo.geocaching; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.entity.BufferedHttpEntity; -import org.apache.http.impl.client.DefaultHttpClient; - -import android.util.Log; - -public class cgMapImg { - /** - * 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; - private String geocode = null; - - public cgMapImg(String geocodeIn) { - geocode = geocodeIn; - - if (geocode != null && geocode.length() > 0) { - final String dirName = cgSettings.getStorage() + geocode + "/"; - - File dir = null; - dir = new File(cgSettings.getStorage()); - if (dir.exists() == false) { - dir.mkdirs(); - } - dir = new File(dirName); - if (dir.exists() == false) { - dir.mkdirs(); - } - dir = null; - } - } - - public void getDrawable(String url, int level) { - if (url == null || url.length() == 0) { - return; - } - - if (geocode == null || geocode.length() == 0) { - return; - } - - final String fileName = cgSettings.getStorage() + geocode + "/map_" + level; - HttpClient client = null; - HttpGet getMethod = null; - HttpResponse httpResponse = null; - HttpEntity entity = null; - BufferedHttpEntity bufferedEntity = null; - - boolean ok = false; - - for (int i = 0; i < 3; i ++) { - if (i > 0) Log.w(cgSettings.tag, "cgMapImg.getDrawable: Failed to download data, retrying. Attempt #" + (i + 1)); - - try { - client = new DefaultHttpClient(); - getMethod = new HttpGet(url); - httpResponse = client.execute(getMethod); - entity = httpResponse.getEntity(); - - // if image is to small, don't download and save, there is no map data for this zoom level - long contentSize = entity.getContentLength(); - if (contentSize > 0 && contentSize <= MIN_MAP_IMAGE_BYTES) { - break; - } - - bufferedEntity = new BufferedHttpEntity(entity); - if (bufferedEntity != null) { - InputStream is = (InputStream)bufferedEntity.getContent(); - FileOutputStream fos = new FileOutputStream(fileName); - - int fileSize = 0; - try { - byte[] buffer = new byte[4096]; - int bytesRead; - while ((bytesRead = is.read(buffer)) != -1) { - fos.write(buffer, 0, bytesRead); - fileSize += bytesRead; - } - fos.flush(); - ok = true; - } catch (IOException e) { - Log.e(cgSettings.tag, "cgMapImg.getDrawable (saving to cache): " + e.toString()); - } finally { - is.close(); - fos.close(); - } - - bufferedEntity = null; - - // delete image if it has no contents - if (ok && fileSize < MIN_MAP_IMAGE_BYTES) { - (new File(fileName)).delete(); - } - } - - if (ok) { - break; - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgMapImg.getDrawable (downloading from web): " + e.toString()); - } - } - } -} |
