aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorblafoo <github@blafoo.de>2012-02-26 11:41:06 +0100
committerblafoo <github@blafoo.de>2012-02-26 11:41:06 +0100
commit2b1126eb283dd1a9bed824655e1669d9db7988c3 (patch)
treedef567be23d1eb935568fd9153793a22bed4b8a2 /main/src
parent3063feaeac0756cd597e9679ada76e31a7d4d0ea (diff)
downloadcgeo-2b1126eb283dd1a9bed824655e1669d9db7988c3.zip
cgeo-2b1126eb283dd1a9bed824655e1669d9db7988c3.tar.gz
cgeo-2b1126eb283dd1a9bed824655e1669d9db7988c3.tar.bz2
Parse PNG to get cache type (issue #1176)
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/cgBase.java18
-rw-r--r--main/src/cgeo/geocaching/connector/gc/GCBase.java47
2 files changed, 53 insertions, 12 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/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);
}
/**