diff options
Diffstat (limited to 'main/src')
| -rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 18 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeowaypoint.java | 15 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCBase.java | 47 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/maps/CachesOverlay.java | 1 |
4 files changed, 58 insertions, 23 deletions
diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index 3b057a7..e9e038d 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -31,6 +31,7 @@ import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.CookieStore; @@ -60,6 +61,8 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.Handler; @@ -1796,14 +1799,21 @@ public class cgBase { return searchByAny(thread, cacheType, false, listId, showCaptcha, params); } - /** Request .png image for a tile. Ignore the image - just load it */ - public static void requestMapTile(final String url, final String referer) { + /** Request .png image for a tile. */ + public static Bitmap requestMapTile(final String url, final String referer) { final HttpGet request = new HttpGet(url); request.addHeader("Accept", "image/png,image/*;q=0.8,*/*;q=0.5"); - request.addHeader("Accept-Encoding", "gzip, deflate"); + //request.addHeader("Accept-Encoding", "gzip, deflate"); request.addHeader("Referer", referer); request.addHeader("X-Requested-With", "XMLHttpRequest"); - request(request); + final HttpResponse response = request(request); + try { + final HttpEntity entity = response.getEntity(); + return BitmapFactory.decodeStream(entity.getContent()); + } catch (IOException e) { + Log.e(Settings.tag, "cgBase.requestMapTile() " + e.getMessage()); + } + return null; } /** Request JSON informations for a tile */ diff --git a/main/src/cgeo/geocaching/cgeowaypoint.java b/main/src/cgeo/geocaching/cgeowaypoint.java index 7fbf14a..aac3005 100644 --- a/main/src/cgeo/geocaching/cgeowaypoint.java +++ b/main/src/cgeo/geocaching/cgeowaypoint.java @@ -32,7 +32,6 @@ public class cgeowaypoint extends AbstractActivity { private static final int MENU_ID_DEFAULT_NAVIGATION = 2; private static final int MENU_ID_OPEN_GEOCACHE = 6; private cgWaypoint waypoint = null; - private String geocode = null; private int id = -1; private ProgressDialog waitDialog = null; private cgGeo geo = null; @@ -147,7 +146,6 @@ public class cgeowaypoint extends AbstractActivity { // try to get data from extras if (extras != null) { id = extras.getInt("waypoint"); - geocode = extras.getString("geocode"); } if (id <= 0) { @@ -239,7 +237,7 @@ public class cgeowaypoint extends AbstractActivity { menu.findItem(MENU_ID_DEFAULT_NAVIGATION).setVisible(visible); menu.findItem(MENU_ID_CACHES_AROUND).setVisible(visible); - boolean openGeocache = StringUtils.isEmpty(geocode) && StringUtils.isNotEmpty(waypoint.getGeocode()); + boolean openGeocache = waypoint != null && StringUtils.isNotEmpty(waypoint.getGeocode()); menu.findItem(MENU_ID_OPEN_GEOCACHE).setVisible(openGeocache); } catch (Exception e) { // nothing @@ -322,13 +320,10 @@ public class cgeowaypoint extends AbstractActivity { public void onClick(View arg0) { if (app.deleteWaypoint(id)) { - String tmpCode = geocode; - if (StringUtils.isEmpty(tmpCode)) { - tmpCode = waypoint.getGeocode(); - } - StaticMapsProvider.removeWpStaticMaps(id, tmpCode); - if (!StringUtils.isEmpty(tmpCode)) { - app.removeCache(tmpCode, EnumSet.of(RemoveFlag.REMOVE_CACHE)); + String geocode = waypoint.getGeocode(); + StaticMapsProvider.removeWpStaticMaps(id, geocode); + if (!StringUtils.isEmpty(geocode)) { + app.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE)); } finish(); diff --git a/main/src/cgeo/geocaching/connector/gc/GCBase.java b/main/src/cgeo/geocaching/connector/gc/GCBase.java index 00e42b4..5f2534d 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCBase.java +++ b/main/src/cgeo/geocaching/connector/gc/GCBase.java @@ -7,7 +7,6 @@ import cgeo.geocaching.StoredList; import cgeo.geocaching.cgBase; import cgeo.geocaching.cgCache; import cgeo.geocaching.enumerations.CacheType; -import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Viewport; import cgeo.geocaching.utils.BaseUtils; import cgeo.geocaching.utils.LeastRecentlyUsedCache; @@ -19,6 +18,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.graphics.Bitmap; import android.util.Log; import java.util.ArrayList; @@ -90,13 +90,16 @@ public class GCBase { } // The PNG must be request before ! Else the following request would return with 204 - No Content - cgBase.requestMapTile(GCConstants.URL_MAP_TILE + url, referer); + Bitmap bitmap = cgBase.requestMapTile(GCConstants.URL_MAP_TILE + url, referer); + + assert bitmap.getWidth() == Tile.TILE_SIZE : "Bitmap has wrong width"; + assert bitmap.getHeight() == Tile.TILE_SIZE : "Bitmap has wrong height"; String data = cgBase.requestMapInfo(GCConstants.URL_MAP_INFO + url, referer); if (StringUtils.isEmpty(data)) { Log.e(Settings.tag, "GCBase.searchByViewport: No data from server for tile (" + tile.getX() + "/" + tile.getY() + ")"); } else { - final SearchResult search = parseMapJSON(data, tile); + final SearchResult search = parseMapJSON(data, tile, bitmap); if (search == null || CollectionUtils.isEmpty(search.getGeocodes())) { Log.e(Settings.tag, "GCBase.searchByViewport: No cache parsed for viewport " + viewport); } @@ -121,7 +124,7 @@ public class GCBase { * Retrieved data. * @return SearchResult. Never null. */ - public static SearchResult parseMapJSON(final String data, Tile tile) { + public static SearchResult parseMapJSON(final String data, Tile tile, Bitmap bitmap) { final SearchResult searchResult = new SearchResult(); @@ -221,8 +224,9 @@ public class GCBase { // if we have "better" coords from a previous search -> reuse them if (cache.getZoomlevel() < tile.getZoomlevel() || cache.getCoords() == null) { - cache.setCoords(getCoordsForUTFGrid(tile, pos)); - + UTFGridPosition xy = getPositionInGrid(pos); + cache.setCoords(tile.getCoord(xy)); + parsePNG(cache, bitmap, xy); // Log.d(Settings.tag, "id=" + id + " geocode=" + cache.getGeocode() + " coords=" + cache.getCoords().toString()); } searchResult.addCache(cache); @@ -237,6 +241,33 @@ public class GCBase { return searchResult; } + // Try to get the cache type from the PNG image for a tile */ + private static void parsePNG(cgCache cache, Bitmap bitmap, UTFGridPosition xy) { + int posX = xy.getX() * 4; + int posY = xy.getY() * 4; + for (int x = posX; x <= posX + 3; x++) { + for (int y = posY; y <= posY + 3; y++) { + int color = bitmap.getPixel(x, y) & 0x00FFFFFF; + if (color == 0x80af64) { // green + cache.setType(CacheType.TRADITIONAL); + return; + } + if (color == 0xffde19) { // yellow + cache.setType(CacheType.MULTI); + return; + } + if (color == 0x001a86) { // blue + cache.setType(CacheType.MYSTERY); + return; + } + if (color == 0xb8682c) { // brown + cache.setFound(true); + return; + } + } + } + } + /** * Calculate needed tiles for the given viewport * @@ -253,7 +284,7 @@ public class GCBase { } /** Calculate from a list of positions (x/y) the coords */ - protected static Geopoint getCoordsForUTFGrid(Tile tile, List<UTFGridPosition> positions) { + protected static UTFGridPosition getPositionInGrid(List<UTFGridPosition> positions) { int minX = UTFGrid.GRID_MAXX; int maxX = 0; int minY = UTFGrid.GRID_MAXY; @@ -264,7 +295,7 @@ public class GCBase { minY = Math.min(minY, pos.y); maxY = Math.max(maxY, pos.y); } - return tile.getCoord(new UTFGridPosition((minX + maxX) / 2, (minY + maxY) / 2)); + return new UTFGridPosition((minX + maxX) / 2, (minY + maxY) / 2); } /** diff --git a/main/src/cgeo/geocaching/maps/CachesOverlay.java b/main/src/cgeo/geocaching/maps/CachesOverlay.java index deda643..37ac3a5 100644 --- a/main/src/cgeo/geocaching/maps/CachesOverlay.java +++ b/main/src/cgeo/geocaching/maps/CachesOverlay.java @@ -212,7 +212,6 @@ public class CachesOverlay extends AbstractItemizedOverlay { Intent popupIntent = new Intent(context, cgeowaypoint.class); popupIntent.putExtra("waypoint", coordinate.getId()); - popupIntent.putExtra("geocode", coordinate.getGeocode()); context.startActivity(popupIntent); } else { |
