diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2013-05-20 23:11:19 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2013-05-20 23:11:19 +0200 |
commit | 9ba8167a4cd257bf65ea928bbd0f005bb088cab9 (patch) | |
tree | 7ef274e5b9cf121c8a041056c3bb764b3cf90a0a | |
parent | 2c5e216ce1c207c97cc9a01a7efac386fe3f8a74 (diff) | |
parent | d5931b80d2a507ffc989484189520ea18e373c17 (diff) | |
download | cgeo-9ba8167a4cd257bf65ea928bbd0f005bb088cab9.zip cgeo-9ba8167a4cd257bf65ea928bbd0f005bb088cab9.tar.gz cgeo-9ba8167a4cd257bf65ea928bbd0f005bb088cab9.tar.bz2 |
Merge branch 'fix-2733' into release
-rw-r--r-- | main/res/layout/caches_item.xml | 34 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCConstants.java | 4 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCParser.java | 33 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/network/HtmlImage.java | 3 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/sorting/DistanceComparator.java | 19 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/ui/CacheListAdapter.java | 69 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/ui/DirectionImage.java | 32 |
7 files changed, 77 insertions, 117 deletions
diff --git a/main/res/layout/caches_item.xml b/main/res/layout/caches_item.xml index e3b861c..8ea0221 100644 --- a/main/res/layout/caches_item.xml +++ b/main/res/layout/caches_item.xml @@ -75,15 +75,13 @@ <!-- real text based direction and distance --> <RelativeLayout - android:id="@+id/direction_layout" android:layout_width="78dip" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginBottom="1dip" android:layout_marginRight="30dip" - android:layout_marginTop="1dip" - android:visibility="gone" > + android:layout_marginTop="1dip" > <view android:id="@+id/distance" @@ -111,31 +109,21 @@ android:gravity="center" android:minHeight="28px" android:minWidth="28px" - cc:skin="?compass" /> - </RelativeLayout> - - <!-- image based direction and distance --> - - <RelativeLayout - android:id="@+id/dirimg_layout" - android:layout_width="78dip" - android:layout_height="wrap_content" - android:layout_alignParentRight="true" - android:layout_centerVertical="true" - android:layout_marginBottom="1dip" - android:layout_marginRight="30dip" - android:layout_marginTop="1dip" - android:visibility="gone" > + cc:skin="?compass" + android:visibility="gone" /> <ImageView android:id="@+id/dirimg" - android:layout_width="55dip" - android:layout_height="30dip" - android:layout_centerInParent="true" + android:layout_width="78dip" + android:layout_height="28px" + android:layout_centerHorizontal="true" android:layout_gravity="center" + android:layout_marginTop="21dip" android:gravity="center" - android:scaleType="fitXY" - android:src="@null" /> + android:minHeight="28px" + android:minWidth="28px" + android:src="@null" + android:visibility="gone" /> </RelativeLayout> <!-- inventory and favorites --> diff --git a/main/src/cgeo/geocaching/connector/gc/GCConstants.java b/main/src/cgeo/geocaching/connector/gc/GCConstants.java index 98b5f1a..357f971 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCConstants.java +++ b/main/src/cgeo/geocaching/connector/gc/GCConstants.java @@ -117,7 +117,9 @@ public final class GCConstants { public final static Pattern PATTERN_SEARCH_TRACKABLES = Pattern.compile("<a id=\"ctl00_ContentBody_dlResults_ctl[0-9]+_uxTravelBugList\" class=\"tblist\" data-tbcount=\"([0-9]+)\" data-id=\"[^\"]*\"[^>]*>(.*)</a>"); /** Second group used */ public final static Pattern PATTERN_SEARCH_TRACKABLESINSIDE = Pattern.compile("(<img src=\"[^\"]+\" alt=\"([^\"]+)\" title=\"[^\"]*\" />[^<]*)"); - public final static Pattern PATTERN_SEARCH_DIRECTION = Pattern.compile("<img id=\"ctl00_ContentBody_dlResults_ctl[0-9]+_uxDistanceAndHeading\" title=\"[^\"]*\" src=\"[^\"]*/seek/CacheDir\\.ashx\\?k=([^\"]+)\"[^>]*>"); + public final static Pattern PATTERN_SEARCH_DIRECTION_DISTANCE = Pattern.compile("<img src=\"/images/icons/compass/([^\\.]+)\\.gif\"[^>]*>[^<]*<br />([^<]+)</span>"); + public final static Pattern PATTERN_SEARCH_DIFFICULTY_TERRAIN = Pattern.compile("<span class=\"small\">([0-5]([\\.,]5)?)/([0-5]([\\.,]5)?)</span><br />"); + public final static Pattern PATTERN_SEARCH_CONTAINER = Pattern.compile("<img src=\"/images/icons/container/([^\\.]+)\\.gif\""); public final static Pattern PATTERN_SEARCH_GEOCODE = Pattern.compile("\\|\\W*(GC[0-9A-Z]+)[^\\|]*\\|"); public final static Pattern PATTERN_SEARCH_ID = Pattern.compile("name=\"CID\"[^v]*value=\"([0-9]+)\""); public final static Pattern PATTERN_SEARCH_FAVORITE = Pattern.compile("<span id=\"ctl00_ContentBody_dlResults_ctl[0-9]+_uxFavoritesValue\" title=\"[^\"]*\" class=\"favorite-rank\">([0-9]+)</span>"); diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java index da60060..587c904 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCParser.java +++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java @@ -166,9 +166,33 @@ public abstract class GCParser { // cache direction - image if (Settings.getLoadDirImg()) { - cache.setDirectionImg(Network.decode(BaseUtils.getMatch(row, GCConstants.PATTERN_SEARCH_DIRECTION, true, 1, cache.getDirectionImg(), true))); + final String direction = BaseUtils.getMatch(row, GCConstants.PATTERN_SEARCH_DIRECTION_DISTANCE, false, 1, null, false); + final String distance = BaseUtils.getMatch(row, GCConstants.PATTERN_SEARCH_DIRECTION_DISTANCE, false, 2, null, false); + if (direction != null) { + cache.setDirectionImg(direction); + } + if (distance != null) { + cache.setDistance(DistanceParser.parseDistance(distance, Settings.isUseMetricUnits())); + } } + // difficulty/terrain + final MatcherWrapper matcherDT = new MatcherWrapper(GCConstants.PATTERN_SEARCH_DIFFICULTY_TERRAIN, row); + if (matcherDT.find()) { + final Float difficulty = parseStars(matcherDT.group(1)); + if (difficulty != null) { + cache.setDifficulty(difficulty); + } + final Float terrain = parseStars(matcherDT.group(3)); + if (terrain != null) { + cache.setTerrain(terrain); + } + } + + // size + final String container = BaseUtils.getMatch(row, GCConstants.PATTERN_SEARCH_CONTAINER, false, 1, null, false); + cache.setSize(CacheSize.getById(container)); + // cache inventory final MatcherWrapper matcherTbs = new MatcherWrapper(GCConstants.PATTERN_SEARCH_TRACKABLES, row); String inventoryPre = null; @@ -291,7 +315,7 @@ public abstract class GCParser { final Set<Geocache> caches = searchResult.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); for (Geocache cache : caches) { if (cache.getCoords() == null && StringUtils.isNotEmpty(cache.getDirectionImg())) { - DirectionImage.getDrawable(cache.getGeocode(), cache.getDirectionImg()); + DirectionImage.getDrawable(cache.getDirectionImg()); } } } @@ -299,6 +323,11 @@ public abstract class GCParser { return searchResult; } + private static Float parseStars(final String value) { + float floatValue = Float.parseFloat(StringUtils.replaceChars(value, ',', '.')); + return floatValue >= 0.5 && floatValue <= 5.0 ? floatValue : null; + } + static SearchResult parseCache(final String page, final CancellableHandler handler) { final SearchResult searchResult = parseCacheFromText(page, handler); if (searchResult != null && !searchResult.getGeocodes().isEmpty()) { diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java index a409750..38498d6 100644 --- a/main/src/cgeo/geocaching/network/HtmlImage.java +++ b/main/src/cgeo/geocaching/network/HtmlImage.java @@ -44,6 +44,7 @@ public class HtmlImage implements Html.ImageGetter { "trendcounter.com", "hit-counter-download.com" }; + public static final String SHARED = "shared"; final private String geocode; /** @@ -80,7 +81,7 @@ public class HtmlImage implements Html.ImageGetter { } final boolean shared = url.contains("/images/icons/icon_"); - final String pseudoGeocode = shared ? "shared" : geocode; + final String pseudoGeocode = shared ? SHARED : geocode; Bitmap imagePre = loadImageFromStorage(url, pseudoGeocode, shared); diff --git a/main/src/cgeo/geocaching/sorting/DistanceComparator.java b/main/src/cgeo/geocaching/sorting/DistanceComparator.java index 7b0afbb..731e356 100644 --- a/main/src/cgeo/geocaching/sorting/DistanceComparator.java +++ b/main/src/cgeo/geocaching/sorting/DistanceComparator.java @@ -27,13 +27,10 @@ public class DistanceComparator extends AbstractCacheComparator { if (cachedDistances) { return; } - for (Geocache cache : list) { + for (final Geocache cache : list) { if (cache.getCoords() != null) { cache.setDistance(coords.distanceTo(cache.getCoords())); } - else { - cache.setDistance(null); - } } cachedDistances = true; } @@ -46,16 +43,12 @@ public class DistanceComparator extends AbstractCacheComparator { @Override protected int compareCaches(final Geocache cache1, final Geocache cache2) { calculateAllDistances(); - if (cache1.getCoords() == null && cache2.getCoords() == null) { - return 0; - } - if (cache1.getCoords() == null) { - return 1; - } - if (cache2.getCoords() == null) { - return -1; + final Float distance1 = cache1.getDistance(); + final Float distance2 = cache2.getDistance(); + if (distance1 == null) { + return distance2 == null ? 0 : 1; } - return Float.compare(cache1.getDistance(), cache2.getDistance()); + return distance2 == null ? -1 : Float.compare(distance1, distance2); } } diff --git a/main/src/cgeo/geocaching/ui/CacheListAdapter.java b/main/src/cgeo/geocaching/ui/CacheListAdapter.java index 4965e0b..163d396 100644 --- a/main/src/cgeo/geocaching/ui/CacheListAdapter.java +++ b/main/src/cgeo/geocaching/ui/CacheListAdapter.java @@ -22,8 +22,6 @@ import org.apache.commons.lang3.builder.HashCodeBuilder; import android.app.Activity; import android.content.res.Resources; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.text.Spannable; @@ -39,7 +37,6 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ImageView; -import android.widget.RelativeLayout; import android.widget.TextView; import java.util.ArrayList; @@ -100,10 +97,8 @@ public class CacheListAdapter extends ArrayAdapter<Geocache> { TextView favourite; TextView info; ImageView inventory; - RelativeLayout directionLayout; DistanceView distance; CompassMiniView direction; - RelativeLayout dirImgLayout; ImageView dirImg; } @@ -358,10 +353,8 @@ public class CacheListAdapter extends ArrayAdapter<Geocache> { holder.checkbox = (CheckBox) v.findViewById(R.id.checkbox); holder.logStatusMark = (ImageView) v.findViewById(R.id.log_status_mark); holder.text = (TextView) v.findViewById(R.id.text); - holder.directionLayout = (RelativeLayout) v.findViewById(R.id.direction_layout); holder.distance = (DistanceView) v.findViewById(R.id.distance); holder.direction = (CompassMiniView) v.findViewById(R.id.direction); - holder.dirImgLayout = (RelativeLayout) v.findViewById(R.id.dirimg_layout); holder.dirImg = (ImageView) v.findViewById(R.id.dirimg); holder.inventory = (ImageView) v.findViewById(R.id.inventory); holder.favourite = (TextView) v.findViewById(R.id.favourite); @@ -434,64 +427,26 @@ public class CacheListAdapter extends ArrayAdapter<Geocache> { holder.inventory.setVisibility(View.GONE); } - boolean setDiDi = false; + if (cache.getDistance() != null) { + holder.distance.setDistance(cache.getDistance()); + } if (cache.getCoords() != null) { holder.direction.setVisibility(View.VISIBLE); + holder.dirImg.setVisibility(View.GONE); holder.direction.updateAzimuth(azimuth); if (coords != null) { holder.distance.update(coords); holder.direction.updateCurrentCoords(coords); } - setDiDi = true; - } else { - if (cache.getDistance() != null) { - holder.distance.setDistance(cache.getDistance()); - setDiDi = true; - } - if (cache.getDirection() != null) { - holder.direction.setVisibility(View.VISIBLE); - holder.direction.updateAzimuth(azimuth); - holder.direction.updateHeading(cache.getDirection()); - setDiDi = true; - } - } - - if (setDiDi) { - holder.directionLayout.setVisibility(View.VISIBLE); - holder.dirImgLayout.setVisibility(View.GONE); + } else if (cache.getDirection() != null) { + holder.direction.setVisibility(View.VISIBLE); + holder.dirImg.setVisibility(View.GONE); + holder.direction.updateAzimuth(azimuth); + holder.direction.updateHeading(cache.getDirection()); } else { - holder.directionLayout.setVisibility(View.GONE); - holder.distance.clear(); - - final Bitmap dirImgPre = BitmapFactory.decodeFile(DirectionImage.getDirectionFile(cache.getGeocode(), false).getPath()); - final Bitmap dirImg; - if (dirImgPre != null) { // null happens for invalid caches (not yet released) - dirImg = dirImgPre.copy(Bitmap.Config.ARGB_8888, true); - dirImgPre.recycle(); - } - else { - dirImg = null; - } - - if (dirImg != null) { - if (!lightSkin) { - final int length = dirImg.getWidth() * dirImg.getHeight(); - final int[] pixels = new int[length]; - dirImg.getPixels(pixels, 0, dirImg.getWidth(), 0, 0, dirImg.getWidth(), dirImg.getHeight()); - for (int i = 0; i < length; i++) { - if (pixels[i] == 0xff000000) { // replace black with white - pixels[i] = 0xffffffff; - } - } - dirImg.setPixels(pixels, 0, dirImg.getWidth(), 0, 0, dirImg.getWidth(), dirImg.getHeight()); - } - - holder.dirImg.setImageBitmap(dirImg); - holder.dirImgLayout.setVisibility(View.VISIBLE); - } else { - holder.dirImg.setImageBitmap(null); - holder.dirImgLayout.setVisibility(View.GONE); - } + holder.dirImg.setImageDrawable(DirectionImage.getDrawable(cache.getDirectionImg())); + holder.dirImg.setVisibility(View.VISIBLE); + holder.direction.setVisibility(View.GONE); } holder.favourite.setText(Integer.toString(cache.getFavoritePoints())); diff --git a/main/src/cgeo/geocaching/ui/DirectionImage.java b/main/src/cgeo/geocaching/ui/DirectionImage.java index 675d73b..df7275d 100644 --- a/main/src/cgeo/geocaching/ui/DirectionImage.java +++ b/main/src/cgeo/geocaching/ui/DirectionImage.java @@ -1,30 +1,22 @@ package cgeo.geocaching.ui; -import cgeo.geocaching.files.LocalStorage; -import cgeo.geocaching.network.Network; -import cgeo.geocaching.network.Parameters; +import cgeo.geocaching.StoredList; +import cgeo.geocaching.network.HtmlImage; -import ch.boye.httpclientandroidlib.HttpResponse; -import org.apache.commons.lang3.StringUtils; - -import java.io.File; +import android.graphics.drawable.BitmapDrawable; public class DirectionImage { - public static void getDrawable(final String geocode, final String code) { - if (StringUtils.isBlank(geocode) || StringUtils.isBlank(code)) { - return; - } - - final HttpResponse httpResponse = - Network.getRequest("http://www.geocaching.com/ImgGen/seek/CacheDir.ashx", new Parameters("k", code)); - if (httpResponse != null) { - LocalStorage.saveEntityToFile(httpResponse, getDirectionFile(geocode, true)); - } - } + static private HtmlImage htmlImage = new HtmlImage(HtmlImage.SHARED, false, StoredList.STANDARD_LIST_ID, false); - public static File getDirectionFile(final String geocode, final boolean createDirs) { - return LocalStorage.getStorageFile(geocode, "direction.png", false, createDirs); + /** + * Retrieve the direction image corresponding to the direction code. + * + * @param directionCode one of the eight cardinal points + * @return a drawable with the arrow pointing into the right direction + */ + public static BitmapDrawable getDrawable(final String directionCode) { + return htmlImage.getDrawable("http://www.geocaching.com/images/icons/compass/" + directionCode + ".gif"); } } |