diff options
Diffstat (limited to 'src/cgeo/geocaching/cgBase.java')
| -rw-r--r-- | src/cgeo/geocaching/cgBase.java | 95 |
1 files changed, 53 insertions, 42 deletions
diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java index 1287f79..f659e6a 100644 --- a/src/cgeo/geocaching/cgBase.java +++ b/src/cgeo/geocaching/cgBase.java @@ -484,6 +484,23 @@ public class cgBase { setViewstates(getViewstates(page), params); } + /** + * checks if an Array of Strings is empty or not. Empty means: + * - Array is null + * - or all elements are null or empty strings + */ + public static boolean isEmpty(String[] a) { + if (a == null) + return true; + + for (String s: a) { + if (StringUtils.isNotEmpty(s)) { + return false; + } + } + return true; + } + public class loginThread extends Thread { @@ -520,7 +537,7 @@ public class cgBase { viewstates = getViewstates(loginData); - if (ArrayUtils.isEmpty(viewstates)) { + if (isEmpty(viewstates)) { Log.e(cgSettings.tag, "cgeoBase.login: Failed to find viewstates"); return -1; // no viewstates } @@ -1021,7 +1038,7 @@ public class cgBase { final JSONObject dataJSON = new JSONObject(json); final JSONObject extra = dataJSON.getJSONObject("cs"); - if ( StringUtils.isNotBlank(data)) { + if (extra != null && extra.length() > 0) { int count = extra.getInt("count"); if (count > 0 && extra.has("cc")) { @@ -1290,20 +1307,7 @@ public class cgBase { } // cache found - try - { - final Matcher matcherFound = patternFound.matcher(page); - final Matcher matcherFoundAlternative = patternFoundAlternative.matcher(page); - - if (matcherFound.find() || matcherFoundAlternative.find()) { - cache.found = true; - } - } - catch (Exception e) - { - // failed to parse found - Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse found"); - } + cache.found = patternFound.matcher(page).find() || patternFoundAlternative.matcher(page).find(); // cache type try { @@ -1402,7 +1406,7 @@ public class cgBase { try { final Matcher matcherPersonalNote = patternPersonalNote.matcher(page); if (matcherPersonalNote.find() && matcherPersonalNote.groupCount() > 0) { - cache.personalNote = getMatch(matcherPersonalNote.group(1).trim()); + cache.personalNote = getMatch(matcherPersonalNote.group(1)); } } catch (Exception e) { // failed to parse cache personal note @@ -1780,13 +1784,13 @@ public class cgBase { } private static void checkFields(cgCache cache) { - if (StringUtils.isEmpty(cache.geocode)) { + if (StringUtils.isBlank(cache.geocode)) { Log.w(cgSettings.tag, "geo code not parsed correctly"); } - if (StringUtils.isEmpty(cache.name)) { + if (StringUtils.isBlank(cache.name)) { Log.w(cgSettings.tag, "name not parsed correctly"); } - if (StringUtils.isEmpty(cache.guid)) { + if (StringUtils.isBlank(cache.guid)) { Log.w(cgSettings.tag, "guid not parsed correctly"); } if (cache.terrain == null || cache.terrain == 0.0) { @@ -1795,10 +1799,10 @@ public class cgBase { if (cache.difficulty == null || cache.difficulty == 0.0) { Log.w(cgSettings.tag, "difficulty not parsed correctly"); } - if (StringUtils.isEmpty(cache.owner)) { + if (StringUtils.isBlank(cache.owner)) { Log.w(cgSettings.tag, "owner not parsed correctly"); } - if (StringUtils.isEmpty(cache.ownerReal)) { + if (StringUtils.isBlank(cache.ownerReal)) { Log.w(cgSettings.tag, "owner real not parsed correctly"); } if (cache.hidden == null) { @@ -1807,10 +1811,10 @@ public class cgBase { if (cache.favouriteCnt == null) { Log.w(cgSettings.tag, "favoriteCount not parsed correctly"); } - if (StringUtils.isEmpty(cache.size)) { + if (StringUtils.isBlank(cache.size)) { Log.w(cgSettings.tag, "size not parsed correctly"); } - if (StringUtils.isNotBlank(cache.type)) { + if (StringUtils.isBlank(cache.type)) { Log.w(cgSettings.tag, "type not parsed correctly"); } if (cache.latitude == null) { @@ -1819,14 +1823,14 @@ public class cgBase { if (cache.longitude == null) { Log.w(cgSettings.tag, "longitude not parsed correctly"); } - if (StringUtils.isEmpty(cache.location)) { + if (StringUtils.isBlank(cache.location)) { Log.w(cgSettings.tag, "location not parsed correctly"); } } private static String getMatch(String match) { // creating a new String via String constructor is necessary here!! - return new String(match); + return new String(match.trim()); // Java copies the whole page String, when matching with regular expressions // later this would block the garbage collector, as we only need tiny parts of the page // see http://developer.android.com/reference/java/lang/String.html#backing_array @@ -2748,7 +2752,7 @@ public class cgBase { return searchId; } - if (ArrayUtils.isEmpty(viewstates)) { + if (isEmpty(viewstates)) { Log.e(cgSettings.tag, "cgeoBase.searchByNextPage: No viewstate given"); return searchId; } @@ -2834,7 +2838,7 @@ public class cgBase { } if (forceReload == false && reason == 0 && (app.isOffline(geocode, guid) || app.isThere(geocode, guid, true, true))) { - if (StringUtils.isBlank(geocode) && StringUtils.isBlank(guid)) { + if (StringUtils.isBlank(geocode) && StringUtils.isNotBlank(guid)) { geocode = app.getGeocode(guid); } @@ -2867,7 +2871,7 @@ public class cgBase { if (StringUtils.isEmpty(page)) { if (app.isThere(geocode, guid, true, false)) { - if (StringUtils.isBlank(geocode) && StringUtils.isBlank(guid)) { + if (StringUtils.isBlank(geocode) && StringUtils.isNotBlank(guid)) { Log.i(cgSettings.tag, "Loading old cache from cache."); geocode = app.getGeocode(guid); @@ -2984,7 +2988,7 @@ public class cgBase { return null; } - if (StringUtils.isBlank(latitude)) { + if (StringUtils.isBlank(cacheType)) { cacheType = null; } @@ -3311,7 +3315,7 @@ public class cgBase { } search.viewstates = caches.viewstates; search.totalCnt = caches.totalCnt; - + if (CollectionUtils.isNotEmpty(caches.cacheList)) { for (cgCache cache : caches.cacheList) { if ((excludeDisabled == 0 || (excludeDisabled == 1 && cache.disabled == false)) @@ -3326,7 +3330,7 @@ public class cgBase { } return cacheList; } - + public cgTrackable searchTrackable(Map<String, String> parameters) { final String geocode = parameters.get("geocode"); final String guid = parameters.get("guid"); @@ -3368,7 +3372,7 @@ public class cgBase { public int postLog(cgeoapplication app, String geocode, String cacheid, String[] viewstates, int logType, int year, int month, int day, String log, List<cgTrackableLog> trackables) { - if (ArrayUtils.isEmpty(viewstates)) { + if (isEmpty(viewstates)) { Log.e(cgSettings.tag, "cgeoBase.postLog: No viewstate given"); return 1000; } @@ -3465,7 +3469,7 @@ public class cgBase { if (matcher.find() && matcher.groupCount() > 0) { final String[] viewstatesConfirm = getViewstates(page); - if (ArrayUtils.isEmpty(viewstatesConfirm)) { + if (isEmpty(viewstatesConfirm)) { Log.e(cgSettings.tag, "cgeoBase.postLog: No viewstate for confirm log"); return 1000; } @@ -3530,7 +3534,7 @@ public class cgBase { public int postLogTrackable(String tbid, String trackingCode, String[] viewstates, int logType, int year, int month, int day, String log) { - if (ArrayUtils.isEmpty(viewstates)) { + if (isEmpty(viewstates)) { Log.e(cgSettings.tag, "cgeoBase.postLogTrackable: No viewstate given"); return 1000; } @@ -3723,7 +3727,7 @@ public class cgBase { if (app == null) { return; } - if (settings == null || StringUtils.isBlank(settings.tokenPublic) || StringUtils.isNotBlank(settings.tokenSecret)) { + if (settings == null || StringUtils.isBlank(settings.tokenPublic) || StringUtils.isBlank(settings.tokenSecret)) { return; } @@ -4108,7 +4112,7 @@ public class cgBase { response = request(secureRedir, newLocation.getHost(), newLocation.getPath(), "GET", new HashMap<String, String>(), requestId, false, false, false); } } else { - if (StringUtils.isNotBlank(buffer)) { + if (StringUtils.isNotEmpty(buffer)) { replaceWhitespace(buffer); String data = buffer.toString(); buffer = null; @@ -4957,7 +4961,6 @@ public class cgBase { gcIcons.put("mystery-disabled", R.drawable.marker_cache_mystery_disabled); gcIcons.put("gchq-disabled", R.drawable.marker_cache_gchq_disabled); } - } public static boolean runNavigation(Activity activity, Resources res, cgSettings settings, Double latitude, Double longitude) { @@ -5065,7 +5068,6 @@ public class cgBase { * Generate a time string according to system-wide settings (locale, 12/24 hour) * such as "13:24". * - * @param context a context * @param date milliseconds since the epoch * @return the formatted string */ @@ -5077,7 +5079,6 @@ public class cgBase { * Generate a date string according to system-wide settings (locale, date format) * such as "20 December" or "20 December 2010". The year will only be included when necessary. * - * @param context a context * @param date milliseconds since the epoch * @return the formatted string */ @@ -5090,7 +5091,6 @@ public class cgBase { * such as "20 December 2010". The year will always be included, making it suitable * to generate long-lived log entries. * - * @param context a context * @param date milliseconds since the epoch * @return the formatted string */ @@ -5102,7 +5102,6 @@ public class cgBase { * Generate a numeric date string according to system-wide settings (locale, date format) * such as "10/20/2010". * - * @param context a context * @param date milliseconds since the epoch * @return the formatted string */ @@ -5111,6 +5110,18 @@ public class cgBase { } /** + * Generate a numeric date and time string according to system-wide settings (locale, + * date format) such as "7 sept. à 12:35". + * + * @param context a Context + * @param date milliseconds since the epoch + * @return the formatted string + */ + public static String formatShortDateTime(Context context, long date) { + return DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL); + } + + /** * TODO This method is only needed until the settings are a singleton * @return */ |
