From 46a3be19f33bc07aa129bc83407bda8befc50e08 Mon Sep 17 00:00:00 2001 From: Ralf Hubert <_rahu_@web.de> Date: Mon, 1 Aug 2011 15:03:32 +0200 Subject: Display Images from Logs (Issue #40) - changed db-version to 54 (need table for logimages) - renamed cgSpoiler to cgImage - renamed cgeospoilers to cgeoimages - added setting to store/not store logimages TODO: - draw a pretty icon - maybe translate the new strings --- src/cgeo/geocaching/cgBase.java | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'src/cgeo/geocaching/cgBase.java') diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java index 4573e97..2a6cc7f 100644 --- a/src/cgeo/geocaching/cgBase.java +++ b/src/cgeo/geocaching/cgBase.java @@ -1101,6 +1101,7 @@ public class cgBase { final Pattern patternCountLog = Pattern.compile(" src=\"\\/images\\/icons\\/([^\\.]*).gif\" alt=\"[^\"]*\" title=\"[^\"]*\" />([0-9]*)[^0-9]+", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); final Pattern patternLogs = Pattern.compile("]*>[^<]*(.*)[^<]*
[^<]*]*>[^<]*[^<]*]*> ([a-zA-Z]+) (\\d+)(, (\\d+))? by ]+>([^<]+)[<^]*([^\\(]*\\((\\d+) found\\))?(]*>)+((?:(?!).)*)(]*>)+", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); + final Pattern patternLogImgs = Pattern.compile("a href=\"http://img.geocaching.com/cache/log/([^\"]+)\".+?([^<]*)", Pattern.CASE_INSENSITIVE); final Pattern patternAttributes = Pattern.compile("

[^<]*]+>[^\\w]*Attributes[^<]*

[^<]*
(([^<]*\"[^\"]+\"[^]*>)+)[^<]*]*>", Pattern.CASE_INSENSITIVE); final Pattern patternSpoilers = Pattern.compile("((]*>[^<]*]+>[^<]*[^>]+[^<]*[^<]*]*>([^<]*(]*>)+)?)+)[^<]*", Pattern.CASE_INSENSITIVE); @@ -1515,7 +1516,7 @@ public class cgBase { while (matcherSpoilersInside.find()) { if (matcherSpoilersInside.groupCount() > 0) { - final cgSpoiler spoiler = new cgSpoiler(); + final cgImage spoiler = new cgImage(); spoiler.url = matcherSpoilersInside.group(1); if (matcherSpoilersInside.group(2) != null) { @@ -1526,7 +1527,7 @@ public class cgBase { } if (cache.spoilers == null) { - cache.spoilers = new ArrayList(); + cache.spoilers = new ArrayList(); } cache.spoilers.add(spoiler); } @@ -1697,6 +1698,19 @@ public class cgBase { if (matcherLog.group(8) != null) { logDone.found = new Integer(matcherLog.group(8)); } + + final Matcher matcherImg = patternLogImgs.matcher(logs[k]); + + while (matcherImg.find()) { + final cgImage logImage = new cgImage(); + logImage.url = "http://img.geocaching.com/cache/log/" + matcherImg.group(1); + logImage.title = matcherImg.group(2); + if (logDone.logImages == null) { + logDone.logImages = new ArrayList(); + } + logDone.logImages.add(logImage); + } + logDone.log = logTmp; if (cache.logs == null) { @@ -5137,10 +5151,21 @@ public class cgBase { // store spoilers if (cache.spoilers != null && cache.spoilers.isEmpty() == false) { - for (cgSpoiler oneSpoiler : cache.spoilers) { + for (cgImage oneSpoiler : cache.spoilers) { imgGetter.getDrawable(oneSpoiler.url); } } + + // store images from logs + if (settings.storeLogImg == 1) { + for (cgLog log : cache.logs) { + if (log.logImages != null && log.logImages.isEmpty() == false) { + for (cgImage oneLogImg : log.logImages) { + imgGetter.getDrawable(oneLogImg.url); + } + } + } + } // store map previews if (settings.storeOfflineMaps == 1 && cache.latitude != null && cache.longitude != null) { -- cgit v1.1 From 7800d4761313a036f32e3d31747099f009c0e1da Mon Sep 17 00:00:00 2001 From: Ralf Hubert <_rahu_@web.de> Date: Mon, 1 Aug 2011 19:43:41 +0200 Subject: some changes after code-discussion --- src/cgeo/geocaching/cgBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/cgeo/geocaching/cgBase.java') diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java index 2a6cc7f..43e93ce 100644 --- a/src/cgeo/geocaching/cgBase.java +++ b/src/cgeo/geocaching/cgBase.java @@ -5157,7 +5157,7 @@ public class cgBase { } // store images from logs - if (settings.storeLogImg == 1) { + if (settings.storelogimages == true) { for (cgLog log : cache.logs) { if (log.logImages != null && log.logImages.isEmpty() == false) { for (cgImage oneLogImg : log.logImages) { -- cgit v1.1 From 53645ae292deb7f14ae3da2a19e2e9f11407746d Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Mon, 1 Aug 2011 23:45:57 +0200 Subject: cleanup * make potentially static methods static * remove unused constants * remove unused imports * move a cache related (otherwise static base) method to the cache class --- src/cgeo/geocaching/cgBase.java | 131 ++-------------------------------------- 1 file changed, 6 insertions(+), 125 deletions(-) (limited to 'src/cgeo/geocaching/cgBase.java') diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java index 4573e97..51e6372 100644 --- a/src/cgeo/geocaching/cgBase.java +++ b/src/cgeo/geocaching/cgBase.java @@ -97,7 +97,7 @@ public class cgBase { public static DateFormat dateOutShort = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()); private Resources res = null; private HashMap cookies = new HashMap(); - private final String passMatch = "[/\\?&]*[Pp]ass(word)?=[^&^#^$]+"; + private static final String passMatch = "[/\\?&]*[Pp]ass(word)?=[^&^#^$]+"; private final Pattern patternLoggedIn = Pattern.compile("You are logged in as[^<]*]*>([^<]+)[^<]*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); private final Pattern patternLogged2In = Pattern.compile("[^\\w]*Hello,[^<]*]+>([^<]+)[^<]*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); private final Pattern patternViewstate = Pattern.compile("id=\"__VIEWSTATE\"[^(value)]+value=\"([^\"]+)\"[^>]+>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); @@ -106,9 +106,6 @@ public class cgBase { public static final double deg2rad = Math.PI / 180; public static final double rad2deg = 180 / Math.PI; public static final float erad = 6371.0f; - public static final int mapAppAny = 0; - public static final int mapAppLocus = 1; - public static final int mapAppRmaps = 2; private cgeoapplication app = null; private cgSettings settings = null; private SharedPreferences prefs = null; @@ -1879,7 +1876,7 @@ public class cgBase { return caches; } - private Date parseDate(String input) { + private static Date parseDate(String input) { if (input == null) { return null; } @@ -3969,7 +3966,7 @@ public class cgBase { return -1; // error } - boolean guidOnPage = checkPageForGuid(cache, page); + boolean guidOnPage = cache.isGuidContainedInPage(page); if (guidOnPage) { Log.i(cgSettings.tag, "cgBase.addToWatchlist: cache is on watchlist"); cache.onWatchlist = true; @@ -4010,7 +4007,7 @@ public class cgBase { params.put("ctl00$ContentBody$btnYes", "Yes"); page = request(false, host, path, method, params, false, false, false).getData(); - boolean guidOnPage = checkPageForGuid(cache, page); + boolean guidOnPage = cache.isGuidContainedInPage(page); if (! guidOnPage) { Log.i(cgSettings.tag, "cgBase.removeFromWatchlist: cache removed from watchlist"); cache.onWatchlist = false; @@ -4020,29 +4017,6 @@ public class cgBase { return guidOnPage ? -1 : 0; // on watchlist (=error) / not on watchlist } - /** - * checks if a page contains the guid of a cache - * - * @param cache the cache to look for - * @param page the page to search in - * - * @return true: page contains guid of cache, false: otherwise - */ - private boolean checkPageForGuid(cgCache cache, String page) { - // check if the guid of the cache is anywhere in the page - if (cache.guid == null || cache.guid.length() == 0) - return false; - Pattern patternOk = Pattern.compile(cache.guid, Pattern.CASE_INSENSITIVE); - Matcher matcherOk = patternOk.matcher(page); - if (matcherOk.find()) { - Log.i(cgSettings.tag, "cgBase.checkPageForGuid: guid '" + cache.guid + "' found"); - return true; - } else { - Log.i(cgSettings.tag, "cgBase.checkPageForGuid: guid '" + cache.guid + "' not found"); - return false; - } - } - final public static HostnameVerifier doNotVerify = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { @@ -4183,7 +4157,7 @@ public class cgBase { } } - private void readIntoBuffer(BufferedReader br, StringBuffer buffer) throws IOException { + private static void readIntoBuffer(BufferedReader br, StringBuffer buffer) throws IOException { int bufferSize = 1024*16; char[] bytes = new char[bufferSize]; int bytesRead; @@ -4197,99 +4171,6 @@ public class cgBase { } } - /* - public ArrayList translate(ArrayList text, String target) { - if (settings.translate == false) { - return text; - } - - String[] languages = null; - if (settings.languages != null) { - languages = settings.languages.split(" "); - } - - ArrayList translated = new ArrayList(); - String language = null; - - if (text == null || text.isEmpty()) { - return text; - } - - // cut to 5000 characters (limitation of Google Translation API) - for (String textOne : text) { - int len = urlencode_rfc3986(textOne).length(); - if (len > 5000) { - textOne = Html.fromHtml(textOne).toString(); - len = urlencode_rfc3986(textOne).length(); - - if (len > 5000) { - int cut = 2000; - if (textOne.length() > cut) { - cut = 1000; - } - - textOne = textOne.substring(0, cut) + "..."; - } - } - } - - try { - if (target == null) { - final Locale locale = Locale.getDefault(); - target = locale.getLanguage(); - } - - final String scheme = "https://"; - final String host = "www.googleapis.com"; - final String path = "/language/translate/v2"; - - final ArrayList params = new ArrayList(); - params.add("key=" + urlencode_rfc3986("AIzaSyAJH8x5etFHUbFifmgChlWoCVmwBFSwShQ")); - params.add("target=" + urlencode_rfc3986(target)); - for (String textOne : text) { - params.add("q=" + urlencode_rfc3986(textOne)); - } - params.add("format=" + urlencode_rfc3986("html")); - - String page = requestJSON(scheme, host, path, "POST", implode("&", params.toArray())); - - if (page == null || page.length() == 0) { - return text; - } - - JSONObject json = new JSONObject(page); - JSONObject jsonData = json.getJSONObject("data"); - JSONArray jsonTranslations = jsonData.getJSONArray("translations"); - int translationCnt = jsonTranslations.length(); - - for (int i = 0; i < translationCnt; i ++) { - JSONObject jsonTranslation = jsonTranslations.getJSONObject(i); - language = jsonTranslation.getString("detectedSourceLanguage"); - - boolean toTranslate = true; - if (languages != null) { - for (String lng : languages) { - if (lng.equalsIgnoreCase(language)) { - toTranslate = false; - } - } - } - - if (toTranslate == false) { - translated.add(text.get(i)); - } else { - Log.i(cgSettings.tag, "Translating #" + i + ": " + language + ">" + target); - translated.add(jsonTranslation.getString("translatedText")); - } - } - } catch (Exception e) { - Log.w(cgSettings.tag, "cgBase.translate: " + e.toString()); - } - - return translated; - } - */ - public String getLocalIpAddress() { try { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { @@ -4673,7 +4554,7 @@ public class cgBase { return response; } - private String replaceWhitespace(final StringBuffer buffer) { + private static String replaceWhitespace(final StringBuffer buffer) { final int length = buffer.length(); final char[] bytes = new char[length]; buffer.getChars(0, length, bytes, 0); -- cgit v1.1