diff options
Diffstat (limited to 'src/cgeo/geocaching')
| -rw-r--r-- | src/cgeo/geocaching/ICache.java | 15 | ||||
| -rw-r--r-- | src/cgeo/geocaching/StaticMapsProvider.java | 2 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgBase.java | 95 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgCache.java | 33 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgCompass.java | 157 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgData.java | 286 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgSettings.java | 2 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeoapplication.java | 47 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeoauth.java | 2 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeodetail.java | 4 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeopoint.java | 5 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeotouch.java | 9 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeovisit.java | 9 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeowaypointadd.java | 4 | ||||
| -rw-r--r-- | src/cgeo/geocaching/files/GPXParser.java | 69 | ||||
| -rw-r--r-- | src/cgeo/geocaching/geopoint/Geopoint.java | 101 | ||||
| -rw-r--r-- | src/cgeo/geocaching/mapcommon/cgMapOverlay.java | 2 | ||||
| -rw-r--r-- | src/cgeo/geocaching/mapcommon/cgeomap.java | 74 |
18 files changed, 411 insertions, 505 deletions
diff --git a/src/cgeo/geocaching/ICache.java b/src/cgeo/geocaching/ICache.java index d32c48c..c91b959 100644 --- a/src/cgeo/geocaching/ICache.java +++ b/src/cgeo/geocaching/ICache.java @@ -1,5 +1,5 @@ /**
- *
+ *
*/
package cgeo.geocaching;
@@ -10,12 +10,7 @@ package cgeo.geocaching; *
*/
public interface ICache {
-
- /**
- * @return The data returned by the HTTP-GET request for this cache. Only for testing purposes.
- */
- public String getData();
-
+
/**
* @return Geocode like GCxxxx
*/
@@ -45,9 +40,9 @@ public interface ICache { */
public Float getTerrain();
/**
- * @return Latitute, e.g. N 52° 12.345
+ * @return Latitude, e.g. N 52° 12.345
*/
- public String getLatitute();
+ public String getLatitude();
/**
* @return Longitude, e.g. E 9° 34.567
*/
@@ -73,7 +68,7 @@ public interface ICache { */
public String getHint();
/**
- * @return Descrition
+ * @return Description
*/
public String getDescription();
/**
diff --git a/src/cgeo/geocaching/StaticMapsProvider.java b/src/cgeo/geocaching/StaticMapsProvider.java index 5962460..50605df 100644 --- a/src/cgeo/geocaching/StaticMapsProvider.java +++ b/src/cgeo/geocaching/StaticMapsProvider.java @@ -124,7 +124,7 @@ public class StaticMapsProvider { public static void downloadMaps(cgCache cache, cgSettings settings, Activity activity) { if (settings.storeOfflineMaps != 1 || cache.latitude == null - || cache.longitude == null || StringUtils.isNotBlank(cache.geocode)) { + || cache.longitude == null || StringUtils.isBlank(cache.geocode)) { return; } 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 */ diff --git a/src/cgeo/geocaching/cgCache.java b/src/cgeo/geocaching/cgCache.java index e9c37ef..09de134 100644 --- a/src/cgeo/geocaching/cgCache.java +++ b/src/cgeo/geocaching/cgCache.java @@ -84,31 +84,7 @@ public class cgCache implements ICache { public boolean statusCheckedView = false; public String directionImg = null; - public cgCache merge(cgData storage) { - - boolean loadA = true; - boolean loadW = true; - boolean loadS = true; - boolean loadL = true; - boolean loadI = true; - - if (attributes == null || attributes.isEmpty()) { - loadA = false; - } - if (waypoints == null || waypoints.isEmpty()) { - loadW = false; - } - if (spoilers == null || spoilers.isEmpty()) { - loadS = false; - } - if (logs == null || logs.isEmpty()) { - loadL = false; - } - if (inventory == null || inventory.isEmpty()) { - loadI = false; - } - - final cgCache oldCache = storage.loadCache(geocode, guid, loadA, loadW, loadS, loadL, loadI, false); + public cgCache merge(cgData storage, cgCache oldCache) { if (oldCache == null) { return this; @@ -392,11 +368,6 @@ public class cgCache implements ICache { } @Override
- public String getData() {
- return null;
- }
-
- @Override
public Float getDifficulty() {
return difficulty;
}
@@ -407,7 +378,7 @@ public class cgCache implements ICache { }
@Override
- public String getLatitute() {
+ public String getLatitude() {
return latitudeString;
}
diff --git a/src/cgeo/geocaching/cgCompass.java b/src/cgeo/geocaching/cgCompass.java index 4a004b3..7ecee4b 100644 --- a/src/cgeo/geocaching/cgCompass.java +++ b/src/cgeo/geocaching/cgCompass.java @@ -15,18 +15,16 @@ import android.view.View; public class cgCompass extends View { private changeThread watchdog = null; - private boolean wantStop = false; - private boolean lock = false; - private boolean drawing = false; + private volatile boolean wantStop = false; private Context context = null; private Bitmap compassUnderlay = null; private Bitmap compassRose = null; private Bitmap compassArrow = null; private Bitmap compassOverlay = null; - private Double azimuth = Double.valueOf(0); - private Double heading = Double.valueOf(0); - private Double cacheHeading = Double.valueOf(0); - private Double northHeading = Double.valueOf(0); + private double azimuth = 0.0; + private double heading = 0.0; + private double cacheHeading = 0.0; + private double northHeading = 0.0; private PaintFlagsDrawFilter setfil = null; private PaintFlagsDrawFilter remfil = null; private int compassUnderlayWidth = 0; @@ -105,11 +103,40 @@ public class cgCompass extends View { } } - protected void updateNorth(Double northHeadingIn, Double cacheHeadingIn) { + protected synchronized void updateNorth(double northHeadingIn, double cacheHeadingIn) { northHeading = northHeadingIn; cacheHeading = cacheHeadingIn; } + /** + * Compute the new value, moving by small increments. + * @param goal the goal to reach + * @param actual the actual value + * @return the new value + */ + static protected double smoothUpdate(double goal, double actual) { + double diff = goal - actual; + final boolean largeDiff = Math.abs(diff) > 5; + + double offset = 0.0; + + if (diff < 0.0) { + diff = diff + 360.0; + } else if (diff >= 360.0) { + diff = diff - 360.0; + } + + // If the difference is smaller than 1 degree, do nothing as it + // causes the arrow to vibrate. + if (diff > 1.0 && diff <= 180.0) { + offset = largeDiff ? 2.0 : 1.0; + } else if (diff > 180.0 && diff < 359.0) { + offset = largeDiff ? -2.0 : -1.0; + } + + return actual + offset; + } + private class changeThread extends Thread { @Override @@ -121,93 +148,11 @@ public class cgCompass extends View { // nothing } - if (Math.abs(azimuth - northHeading) < 2 && Math.abs(heading - cacheHeading) < 2) { - continue; - } - - lock = true; - - Double diff = Double.valueOf(0); - Double diffAbs = Double.valueOf(0); - Double tempAzimuth = Double.valueOf(0); - Double tempHeading = Double.valueOf(0); - - Double actualAzimuth = azimuth; - Double actualHeading = heading; - - diff = northHeading - actualAzimuth; - diffAbs = Math.abs(northHeading - actualAzimuth); - if (diff < 0) { - diff = diff + 360; - } else if (diff >= 360) { - diff = diff - 360; - } - - if (diff > 0 && diff <= 180) { - if (diffAbs > 5) { - tempAzimuth = actualAzimuth + 2; - } else if (diffAbs > 1) { - tempAzimuth = actualAzimuth + 1; - } else { - tempAzimuth = actualAzimuth; - } - } else if (diff > 180 && diff < 360) { - if (diffAbs > 5) { - tempAzimuth = actualAzimuth - 2; - } else if (diffAbs > 1) { - tempAzimuth = actualAzimuth - 1; - } else { - tempAzimuth = actualAzimuth; - } - } else { - tempAzimuth = actualAzimuth; + synchronized(cgCompass.this) { + azimuth = smoothUpdate(northHeading, azimuth); + heading = smoothUpdate(cacheHeading, heading); } - diff = cacheHeading - actualHeading; - diffAbs = Math.abs(cacheHeading - actualHeading); - if (diff < 0) { - diff = diff + 360; - } else if (diff >= 360) { - diff = diff - 360; - } - - if (diff > 0 && diff <= 180) { - if (diffAbs > 5) { - tempHeading = actualHeading + 2; - } else if (diffAbs > 1) { - tempHeading = actualHeading + 1; - } else { - tempHeading = actualHeading; - } - } else if (diff > 180 && diff < 360) { - if (diffAbs > 5) { - tempHeading = actualHeading - 2; - } else if (diffAbs > 1) { - tempHeading = actualHeading - 1; - } else { - tempHeading = actualHeading; - } - } else { - tempHeading = actualHeading; - } - - if (tempAzimuth >= 360) { - tempAzimuth = tempAzimuth - 360; - } else if (tempAzimuth < 0) { - tempAzimuth = tempAzimuth + 360; - } - - if (tempHeading >= 360) { - tempHeading = tempHeading - 360; - } else if (tempHeading < 0) { - tempHeading = tempHeading + 360; - } - - azimuth = tempAzimuth; - heading = tempHeading; - - lock = false; - changeHandler.sendMessage(new Message()); } } @@ -215,15 +160,16 @@ public class cgCompass extends View { @Override protected void onDraw(Canvas canvas) { - if (lock) { - return; - } - if (drawing) { - return; + double currentAzimuth; + double currentHeading; + + synchronized(this) { + currentAzimuth = azimuth; + currentHeading = heading; } - Double azimuthTemp = azimuth; - Double azimuthRelative = azimuthTemp - heading; + double azimuthTemp = currentAzimuth; + double azimuthRelative = azimuthTemp - currentHeading; if (azimuthRelative < 0) { azimuthRelative = azimuthRelative + 360; } else if (azimuthRelative >= 360) { @@ -237,7 +183,6 @@ public class cgCompass extends View { int marginLeftTemp = 0; int marginTopTemp = 0; - drawing = true; super.onDraw(canvas); canvas.save(); @@ -251,16 +196,16 @@ public class cgCompass extends View { marginLeftTemp = (getWidth() - compassRoseWidth) / 2; marginTopTemp = (getHeight() - compassRoseHeight) / 2; - canvas.rotate(-(azimuthTemp.floatValue()), canvasCenterX, canvasCenterY); + canvas.rotate((float) -azimuthTemp, canvasCenterX, canvasCenterY); canvas.drawBitmap(compassRose, marginLeftTemp, marginTopTemp, null); - canvas.rotate(azimuthTemp.floatValue(), canvasCenterX, canvasCenterY); + canvas.rotate((float) azimuthTemp, canvasCenterX, canvasCenterY); marginLeftTemp = (getWidth() - compassArrowWidth) / 2; marginTopTemp = (getHeight() - compassArrowHeight) / 2; - canvas.rotate(-(azimuthRelative.floatValue()), canvasCenterX, canvasCenterY); + canvas.rotate((float) -azimuthRelative, canvasCenterX, canvasCenterY); canvas.drawBitmap(compassArrow, marginLeftTemp, marginTopTemp, null); - canvas.rotate(azimuthRelative.floatValue(), canvasCenterX, canvasCenterY); + canvas.rotate((float) azimuthRelative, canvasCenterX, canvasCenterY); marginLeftTemp = (getWidth() - compassOverlayWidth) / 2; marginTopTemp = (getHeight() - compassOverlayHeight) / 2; @@ -269,8 +214,6 @@ public class cgCompass extends View { canvas.setDrawFilter(remfil); canvas.restore(); - - drawing = false; } @Override diff --git a/src/cgeo/geocaching/cgData.java b/src/cgeo/geocaching/cgData.java index 5959c0c..ebe620f 100644 --- a/src/cgeo/geocaching/cgData.java +++ b/src/cgeo/geocaching/cgData.java @@ -34,6 +34,13 @@ import cgeo.geocaching.utils.CollectionUtils; public class cgData { + /**The list of fields needed for mapping.*/ + private static final String[] CACHE_COLUMNS = new String[]{ + "_id", "updated", "reason", "detailed", "detailedupdate", "visiteddate", "geocode", "cacheid", "guid", "type", "name", "own", "owner", "owner_real", "hidden", "hint", "size", + "difficulty", "distance", "direction", "terrain", "latlon", "latitude_string", "longitude_string", "location", "latitude", "longitude", "elevation", "shortdesc", + "description", "favourite_cnt", "rating", "votes", "myvote", "disabled", "archived", "members", "found", "favourite", "inventorycoins", "inventorytags", + "inventoryunknown", "onWatchlist", "personal_note", "reliable_latlon" + }; public cgCacheWrap caches; private Context context = null; private String path = null; @@ -1015,6 +1022,8 @@ public class cgData { } } + + @Deprecated public boolean isReliableLatLon(String geocode, String guid) { init(); @@ -1195,7 +1204,7 @@ public class cgData { values.put("distance", cache.distance); values.put("direction", cache.direction); // save coordinates - final boolean rel = isReliableLatLon(cache.geocode, cache.guid); + final boolean rel = cache.reliableLatLon; if (cache.reliableLatLon) { // new cache has reliable coordinates, store values.put("latitude", cache.latitude); values.put("longitude", cache.longitude); @@ -1712,6 +1721,19 @@ public class cgData { return loadCache(geocode, guid, false, true, false, false, false, false); } + /** + * Loads a single Cache. + * @param geocode The Geocode GCXXXX + * @param guid + * @param loadA + * @param loadW + * @param loadS + * @param loadL + * @param loadI + * @param loadO + * @return the loaded cache + */ + public cgCache loadCache(String geocode, String guid, boolean loadA, boolean loadW, boolean loadS, boolean loadL, boolean loadI, boolean loadO) { Object[] geocodes = new Object[1]; Object[] guids = new Object[1]; @@ -1728,7 +1750,7 @@ public class cgData { guids = null; } - List<cgCache> caches = loadCaches(geocodes, guids, null, null, null, null, loadA, loadW, loadS, loadL, loadI, loadO); + List<cgCache> caches = loadCaches(geocodes, null, null, null, null, null, loadA, loadW, loadS, loadL, loadI, loadO); if (caches != null && caches.isEmpty() == false) { return caches.get(0); } @@ -1750,7 +1772,17 @@ public class cgData { public List<cgCache> loadCaches(Object[] geocodes, Object[] guids, Long centerLat, Long centerLon, Long spanLat, Long spanLon, boolean loadA, boolean loadW, boolean loadS, boolean loadL, boolean loadI, boolean loadO) { init(); - +// Using more than one of the parametersets results in overly comlex wheres + if (((geocodes != null && geocodes.length > 0) && (guids != null && guids.length > 0))) { + throw new IllegalArgumentException("Please use only one parameter"); + } + if (((geocodes != null && geocodes.length > 0) || (guids != null && guids.length > 0)) + && centerLat != null + && centerLon != null + && spanLat != null + && spanLon != null) { + throw new IllegalArgumentException("Please use only one parameter"); + } StringBuilder where = new StringBuilder(); Cursor cursor = null; List<cgCache> caches = new ArrayList<cgCache>(); @@ -1826,15 +1858,9 @@ public class cgData { where.append(String.format((Locale) null, "%.6f", lonMax)); where.append(')'); } - cursor = databaseRO.query( dbTableCaches, - new String[]{ - "_id", "updated", "reason", "detailed", "detailedupdate", "visiteddate", "geocode", "cacheid", "guid", "type", "name", "own", "owner", "owner_real", "hidden", "hint", "size", - "difficulty", "distance", "direction", "terrain", "latlon", "latitude_string", "longitude_string", "location", "latitude", "longitude", "elevation", "shortdesc", - "description", "favourite_cnt", "rating", "votes", "myvote", "disabled", "archived", "members", "found", "favourite", "inventorycoins", "inventorytags", - "inventoryunknown", "onWatchlist", "personal_note" - }, + CACHE_COLUMNS, where.toString(), null, null, @@ -1843,80 +1869,12 @@ public class cgData { null); if (cursor != null) { - int index = 0; - if (cursor.getCount() > 0) { cursor.moveToFirst(); do { - cgCache cache = new cgCache(); - - cache.updated = (long) cursor.getLong(cursor.getColumnIndex("updated")); - cache.reason = (int) cursor.getInt(cursor.getColumnIndex("reason")); - cache.detailed = cursor.getInt(cursor.getColumnIndex("detailed")) == 1; - cache.detailedUpdate = (Long) cursor.getLong(cursor.getColumnIndex("detailedupdate")); - cache.visitedDate = (Long) cursor.getLong(cursor.getColumnIndex("visiteddate")); - cache.geocode = (String) cursor.getString(cursor.getColumnIndex("geocode")); - cache.cacheid = (String) cursor.getString(cursor.getColumnIndex("cacheid")); - cache.guid = (String) cursor.getString(cursor.getColumnIndex("guid")); - cache.type = (String) cursor.getString(cursor.getColumnIndex("type")); - cache.name = (String) cursor.getString(cursor.getColumnIndex("name")); - cache.own = cursor.getInt(cursor.getColumnIndex("own")) == 1; - cache.owner = (String) cursor.getString(cursor.getColumnIndex("owner")); - cache.ownerReal = (String) cursor.getString(cursor.getColumnIndex("owner_real")); - cache.hidden = new Date((long) cursor.getLong(cursor.getColumnIndex("hidden"))); - cache.hint = (String) cursor.getString(cursor.getColumnIndex("hint")); - cache.size = (String) cursor.getString(cursor.getColumnIndex("size")); - cache.difficulty = (Float) cursor.getFloat(cursor.getColumnIndex("difficulty")); - index = cursor.getColumnIndex("direction"); - if (cursor.isNull(index)) { - cache.direction = null; - } else { - cache.direction = (Double) cursor.getDouble(index); - } - index = cursor.getColumnIndex("distance"); - if (cursor.isNull(index)) { - cache.distance = null; - } else { - cache.distance = (Double) cursor.getDouble(index); - } - cache.terrain = (Float) cursor.getFloat(cursor.getColumnIndex("terrain")); - cache.latlon = (String) cursor.getString(cursor.getColumnIndex("latlon")); - cache.latitudeString = (String) cursor.getString(cursor.getColumnIndex("latitude_string")); - cache.longitudeString = (String) cursor.getString(cursor.getColumnIndex("longitude_string")); - cache.location = (String) cursor.getString(cursor.getColumnIndex("location")); - index = cursor.getColumnIndex("latitude"); - if (cursor.isNull(index)) { - cache.latitude = null; - } else { - cache.latitude = (Double) cursor.getDouble(index); - } - index = cursor.getColumnIndex("longitude"); - if (cursor.isNull(index)) { - cache.longitude = null; - } else { - cache.longitude = (Double) cursor.getDouble(index); - } - index = cursor.getColumnIndex("elevation"); - if (cursor.isNull(index)) { - cache.elevation = null; - } else { - cache.elevation = (Double) cursor.getDouble(index); - } - cache.personalNote = (String) cursor.getString(cursor.getColumnIndex("personal_note")); - cache.shortdesc = (String) cursor.getString(cursor.getColumnIndex("shortdesc")); - cache.description = (String) cursor.getString(cursor.getColumnIndex("description")); - cache.favouriteCnt = (Integer) cursor.getInt(cursor.getColumnIndex("favourite_cnt")); - cache.rating = (Float) cursor.getFloat(cursor.getColumnIndex("rating")); - cache.votes = (Integer) cursor.getInt(cursor.getColumnIndex("votes")); - cache.myVote = (Float) cursor.getFloat(cursor.getColumnIndex("myvote")); - cache.disabled = cursor.getLong(cursor.getColumnIndex("disabled")) == 1L; - cache.archived = cursor.getLong(cursor.getColumnIndex("archived")) == 1L; - cache.members = cursor.getLong(cursor.getColumnIndex("members")) == 1L; - cache.found = cursor.getLong(cursor.getColumnIndex("found")) == 1L; - cache.favourite = cursor.getLong(cursor.getColumnIndex("favourite")) == 1L; - cache.inventoryItems = (Integer) cursor.getInt(cursor.getColumnIndex("inventoryunknown")); - cache.onWatchlist = cursor.getLong(cursor.getColumnIndex("onWatchlist")) == 1L; + //Extracted Method + cgCache cache = createCacheFromDatabaseContent(cursor); if (loadA) { List<String> attributes = loadAttributes(cache.geocode); @@ -2003,6 +1961,86 @@ public class cgData { return caches; } + /** + * maps a Cache from the cursor. Doesn't next. + * @param cursor + * @return + */ + + private static cgCache createCacheFromDatabaseContent(Cursor cursor) { + int index; + cgCache cache = new cgCache(); + + cache.updated = (long) cursor.getLong(cursor.getColumnIndex("updated")); + cache.reason = (int) cursor.getInt(cursor.getColumnIndex("reason")); + cache.detailed = cursor.getInt(cursor.getColumnIndex("detailed")) == 1; + cache.detailedUpdate = (Long) cursor.getLong(cursor.getColumnIndex("detailedupdate")); + cache.visitedDate = (Long) cursor.getLong(cursor.getColumnIndex("visiteddate")); + cache.geocode = (String) cursor.getString(cursor.getColumnIndex("geocode")); + cache.cacheid = (String) cursor.getString(cursor.getColumnIndex("cacheid")); + cache.guid = (String) cursor.getString(cursor.getColumnIndex("guid")); + cache.type = (String) cursor.getString(cursor.getColumnIndex("type")); + cache.name = (String) cursor.getString(cursor.getColumnIndex("name")); + cache.own = cursor.getInt(cursor.getColumnIndex("own")) == 1; + cache.owner = (String) cursor.getString(cursor.getColumnIndex("owner")); + cache.ownerReal = (String) cursor.getString(cursor.getColumnIndex("owner_real")); + cache.hidden = new Date((long) cursor.getLong(cursor.getColumnIndex("hidden"))); + cache.hint = (String) cursor.getString(cursor.getColumnIndex("hint")); + cache.size = (String) cursor.getString(cursor.getColumnIndex("size")); + cache.difficulty = (Float) cursor.getFloat(cursor.getColumnIndex("difficulty")); + index = cursor.getColumnIndex("direction"); + if (cursor.isNull(index)) { + cache.direction = null; + } else { + cache.direction = (Double) cursor.getDouble(index); + } + index = cursor.getColumnIndex("distance"); + if (cursor.isNull(index)) { + cache.distance = null; + } else { + cache.distance = (Double) cursor.getDouble(index); + } + cache.terrain = (Float) cursor.getFloat(cursor.getColumnIndex("terrain")); + cache.latlon = (String) cursor.getString(cursor.getColumnIndex("latlon")); + cache.latitudeString = (String) cursor.getString(cursor.getColumnIndex("latitude_string")); + cache.longitudeString = (String) cursor.getString(cursor.getColumnIndex("longitude_string")); + cache.location = (String) cursor.getString(cursor.getColumnIndex("location")); + index = cursor.getColumnIndex("latitude"); + if (cursor.isNull(index)) { + cache.latitude = null; + } else { + cache.latitude = (Double) cursor.getDouble(index); + } + index = cursor.getColumnIndex("longitude"); + if (cursor.isNull(index)) { + cache.longitude = null; + } else { + cache.longitude = (Double) cursor.getDouble(index); + } + index = cursor.getColumnIndex("elevation"); + if (cursor.isNull(index)) { + cache.elevation = null; + } else { + cache.elevation = (Double) cursor.getDouble(index); + } + cache.personalNote = (String) cursor.getString(cursor.getColumnIndex("personal_note")); + cache.shortdesc = (String) cursor.getString(cursor.getColumnIndex("shortdesc")); + cache.description = (String) cursor.getString(cursor.getColumnIndex("description")); + cache.favouriteCnt = (Integer) cursor.getInt(cursor.getColumnIndex("favourite_cnt")); + cache.rating = (Float) cursor.getFloat(cursor.getColumnIndex("rating")); + cache.votes = (Integer) cursor.getInt(cursor.getColumnIndex("votes")); + cache.myVote = (Float) cursor.getFloat(cursor.getColumnIndex("myvote")); + cache.disabled = cursor.getLong(cursor.getColumnIndex("disabled")) == 1l; + cache.archived = cursor.getLong(cursor.getColumnIndex("archived")) == 1l; + cache.members = cursor.getLong(cursor.getColumnIndex("members")) == 1l; + cache.found = cursor.getLong(cursor.getColumnIndex("found")) == 1l; + cache.favourite = cursor.getLong(cursor.getColumnIndex("favourite")) == 1l; + cache.inventoryItems = (Integer) cursor.getInt(cursor.getColumnIndex("inventoryunknown")); + cache.onWatchlist = cursor.getLong(cursor.getColumnIndex("onWatchlist")) == 1l; + cache.reliableLatLon = cursor.getInt(cursor.getColumnIndex("reliable_latlon"))>0; + return cache; + } + public List<String> loadAttributes(String geocode) { if (StringUtils.isBlank(geocode)) { return null; @@ -2238,31 +2276,40 @@ public class cgData { List<cgLog> logs = new ArrayList<cgLog>(); - Cursor cursor = databaseRO.query( - dbTableLogs, - new String[]{"_id", "type", "author", "log", "date", "found"}, - "geocode = \"" + geocode + "\"", - null, - null, - null, - "date desc, _id asc", - "100"); + Cursor cursor = databaseRO.rawQuery( + "SELECT cg_logs._id as cg_logs_id, type, author, log, date, found, " + dbTableLogImages + "._id as cg_logImages_id, log_id, title, url FROM " + + dbTableLogs + " LEFT OUTER JOIN " + dbTableLogImages + + " ON ( cg_logs._id = log_id ) WHERE geocode = ? ORDER BY date desc, cg_logs._id asc", new String[]{ geocode}); if (cursor != null && cursor.getCount() > 0) { - cursor.moveToFirst(); - - do { - cgLog log = new cgLog(); - log.id = (int) cursor.getInt(cursor.getColumnIndex("_id")); - log.type = (int) cursor.getInt(cursor.getColumnIndex("type")); - log.author = (String) cursor.getString(cursor.getColumnIndex("author")); - log.log = (String) cursor.getString(cursor.getColumnIndex("log")); - log.date = (long) cursor.getLong(cursor.getColumnIndex("date")); - log.found = (int) cursor.getInt(cursor.getColumnIndex("found")); - log.logImages = loadLogImages(log.id); - - logs.add(log); - } while (cursor.moveToNext()); + cgLog log = null; + while (cursor.moveToNext() && logs.size() < 100) { + if (log == null || log.id != cursor.getInt(cursor.getColumnIndex("cg_logs_id"))) { + log = new cgLog(); + log.id = (int) cursor.getInt(cursor.getColumnIndex("cg_logs_id")); + log.type = (int) cursor.getInt(cursor.getColumnIndex("type")); + log.author = (String) cursor.getString(cursor.getColumnIndex("author")); + log.log = (String) cursor.getString(cursor.getColumnIndex("log")); + log.date = (long) cursor.getLong(cursor.getColumnIndex("date")); + log.found = (int) cursor.getInt(cursor.getColumnIndex("found")); + logs.add(log); + } + if (!cursor.isNull(cursor.getColumnIndex("cg_logImages_id"))) { + final cgImage log_img = new cgImage(); + log_img.title = (String) cursor.getString(cursor.getColumnIndex("title")); + if (log_img.title == null) { + log_img.title = ""; + } + log_img.url = (String) cursor.getString(cursor.getColumnIndex("url")); + if (log_img.url == null) { + log_img.url = ""; + } + if (log.logImages == null) { + log.logImages = new ArrayList<cgImage>(); + } + log.logImages.add(log_img); + } + } } if (cursor != null) { @@ -2309,39 +2356,6 @@ public class cgData { return logCounts; } - public List<cgImage> loadLogImages(int log_id) { - init(); - - List<cgImage> logImgList = new ArrayList<cgImage>(); - - Cursor cursor = databaseRO.query( - dbTableLogImages, - new String[]{"_id", "log_id", "title", "url"}, - "log_id = \"" + log_id + "\"", - null, - null, - null, - null, - "100"); - - if (cursor != null && cursor.getCount() > 0) { - cursor.moveToFirst(); - - do { - final cgImage log_img = new cgImage(); - log_img.title = (String)cursor.getString(cursor.getColumnIndex("title")); - log_img.url = (String)cursor.getString(cursor.getColumnIndex("url")); - logImgList.add(log_img); - } while (cursor.moveToNext()); - } - - if (cursor != null) { - cursor.close(); - } - - return logImgList; - } - public List<cgTrackable> loadInventory(String geocode) { if (StringUtils.isBlank(geocode)) { return null; diff --git a/src/cgeo/geocaching/cgSettings.java b/src/cgeo/geocaching/cgSettings.java index b5a21f5..a1d2a59 100644 --- a/src/cgeo/geocaching/cgSettings.java +++ b/src/cgeo/geocaching/cgSettings.java @@ -155,7 +155,7 @@ public class cgSettings { public boolean signatureAutoinsert = false; // usable values - public static final String tag = "c:geo"; + public static final String tag = "cgeo"; /** Name of the preferences file */ public static final String preferences = "cgeo.pref"; diff --git a/src/cgeo/geocaching/cgeoapplication.java b/src/cgeo/geocaching/cgeoapplication.java index f580fa9..3137b00 100644 --- a/src/cgeo/geocaching/cgeoapplication.java +++ b/src/cgeo/geocaching/cgeoapplication.java @@ -8,7 +8,6 @@ import java.util.List; import java.util.Map; import java.util.UUID; -import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import android.app.Application; @@ -271,7 +270,7 @@ public class cgeoapplication extends Application { } public boolean setViewstates(final UUID searchId, String[] viewstates) { - if (ArrayUtils.isEmpty(viewstates)) { + if (cgBase.isEmpty(viewstates)) { return false; } if (searchId == null || searches.containsKey(searchId) == false) { @@ -474,7 +473,8 @@ public class cgeoapplication extends Application { storage = new cgData(this); } - final List<cgCache> cachesPre = storage.loadCaches(geocodeList.toArray(), null, centerLat, centerLon, spanLat, spanLon, loadA, loadW, loadS, loadL, loadI, loadO); + // The list of geocodes is sufficient. more parameters generate an overly complex select. + final List<cgCache> cachesPre = storage.loadCaches(geocodeList.toArray(), null, null, null, null, null, loadA, loadW, loadS, loadL, loadI, loadO); if (cachesPre != null) { cachesOut.addAll(cachesPre); } @@ -692,18 +692,8 @@ public class cgeoapplication extends Application { if (newItem) { // save only newly downloaded data for (cgCache cache : cacheList) { - String geocode = cache.geocode.toUpperCase(); - String guid = cache.guid.toLowerCase(); - cache.reason = reason; - - if (storage.isThere(geocode, guid, false, false)) { - cgCache mergedCache = cache.merge(storage); - storage.saveCache(mergedCache); - } else { - // cache is not saved, new data are for storing - storage.saveCache(cache); - } + storeWithMerge(cache, false); } } @@ -721,23 +711,32 @@ public class cgeoapplication extends Application { searches.put(searchId, search); } - String geocode = cache.geocode.toUpperCase(); - String guid = cache.guid.toLowerCase(); + final boolean status = storeWithMerge(cache, cache.reason >= 1); - boolean status = false; + if (status) { + search.addGeocode(cache.geocode); + } - if (storage.isThere(geocode, guid, false, false) == false || cache.reason >= 1) { // if for offline, do not merge + return status; + } + + /** + * Checks if Cache is already in Database and if so does a merge. + * @param cache The cache to be saved + * @param forceSave override the check and persist the new state. + * @return + */ + + private boolean storeWithMerge(cgCache cache, boolean forceSave) { + boolean status; + cgCache oldCache = null; + if (forceSave || (oldCache = storage.loadCache(cache.geocode, cache.guid, false, true, true, true, true, true)) !=null ) { // if for offline, do not merge status = storage.saveCache(cache); } else { - cgCache mergedCache = cache.merge(storage); + cgCache mergedCache = cache.merge(storage,oldCache); status = storage.saveCache(mergedCache); } - - if (status) { - search.addGeocode(cache.geocode); - } - return status; } diff --git a/src/cgeo/geocaching/cgeoauth.java b/src/cgeo/geocaching/cgeoauth.java index 0554a24..c20c13c 100644 --- a/src/cgeo/geocaching/cgeoauth.java +++ b/src/cgeo/geocaching/cgeoauth.java @@ -122,7 +122,7 @@ public class cgeoauth extends AbstractActivity { startButton.setEnabled(true); startButton.setOnClickListener(new startListener()); - if (StringUtils.isNotBlank(OAtoken) && StringUtils.isNotBlank(OAtokenSecret)) { + if (StringUtils.isBlank(OAtoken) && StringUtils.isBlank(OAtokenSecret)) { // start authorization process startButton.setText(res.getString(R.string.auth_start)); } else { diff --git a/src/cgeo/geocaching/cgeodetail.java b/src/cgeo/geocaching/cgeodetail.java index 182778d..33a7db2 100644 --- a/src/cgeo/geocaching/cgeodetail.java +++ b/src/cgeo/geocaching/cgeodetail.java @@ -613,11 +613,11 @@ public class cgeodetail extends AbstractActivity { try { - if (StringUtils.isBlank(geocode)) { + if (geocode == null && StringUtils.isNotBlank(cache.geocode)) { geocode = cache.geocode; } - if (StringUtils.isBlank(guid)) { + if (guid == null && StringUtils.isNotBlank(cache.guid)) { guid = cache.guid; } diff --git a/src/cgeo/geocaching/cgeopoint.java b/src/cgeo/geocaching/cgeopoint.java index dba015d..530f557 100644 --- a/src/cgeo/geocaching/cgeopoint.java +++ b/src/cgeo/geocaching/cgeopoint.java @@ -14,7 +14,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Bundle; -import android.text.format.DateFormat; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; @@ -66,9 +65,7 @@ public class cgeopoint extends AbstractActivity { longitude.setText(lonString); latitude.setText(latString); - CharSequence dateString = DateFormat.format("dd/MM/yy kk:mm", - loc.getDate()); - date.setText(dateString); + date.setText(cgBase.formatShortDateTime(getContext(), loc.getDate())); return convertView; } diff --git a/src/cgeo/geocaching/cgeotouch.java b/src/cgeo/geocaching/cgeotouch.java index 60c066c..8386018 100644 --- a/src/cgeo/geocaching/cgeotouch.java +++ b/src/cgeo/geocaching/cgeotouch.java @@ -6,7 +6,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import android.app.Dialog; @@ -51,7 +50,7 @@ public class cgeotouch extends cgLogForm { private Handler loadDataHandler = new Handler() { @Override public void handleMessage(Message msg) { - if (ArrayUtils.isEmpty(viewstates) && attempts < 2) { + if (cgBase.isEmpty(viewstates) && attempts < 2) { showToast(res.getString(R.string.err_log_load_data_again)); loadData thread; @@ -59,7 +58,7 @@ public class cgeotouch extends cgLogForm { thread.start(); return; - } else if (ArrayUtils.isEmpty(viewstates) && attempts >= 2) { + } else if (cgBase.isEmpty(viewstates) && attempts >= 2) { showToast(res.getString(R.string.err_log_load_data)); showProgress(false); @@ -290,7 +289,7 @@ public class cgeotouch extends cgLogForm { tweetCheck.setChecked(true); Button buttonPost = (Button)findViewById(R.id.post); - if (ArrayUtils.isEmpty(viewstates)) { + if (cgBase.isEmpty(viewstates)) { buttonPost.setEnabled(false); buttonPost.setOnTouchListener(null); buttonPost.setOnClickListener(null); @@ -446,4 +445,4 @@ public class cgeotouch extends cgLogForm { return 1000; } -}
\ No newline at end of file +} diff --git a/src/cgeo/geocaching/cgeovisit.java b/src/cgeo/geocaching/cgeovisit.java index 65a09c7..da696ec 100644 --- a/src/cgeo/geocaching/cgeovisit.java +++ b/src/cgeo/geocaching/cgeovisit.java @@ -8,7 +8,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import android.app.Dialog; @@ -85,7 +84,7 @@ public class cgeovisit extends cgLogForm { showToast(res.getString(R.string.info_log_type_changed)); } - if (ArrayUtils.isEmpty(viewstates) && attempts < 2) { + if (cgBase.isEmpty(viewstates) && attempts < 2) { showToast(res.getString(R.string.err_log_load_data_again)); loadData thread; @@ -93,7 +92,7 @@ public class cgeovisit extends cgLogForm { thread.start(); return; - } else if (ArrayUtils.isEmpty(viewstates) && attempts >= 2) { + } else if (cgBase.isEmpty(viewstates) && attempts >= 2) { showToast(res.getString(R.string.err_log_load_data)); showProgress(false); @@ -243,7 +242,7 @@ public class cgeovisit extends cgLogForm { if ((StringUtils.isBlank(cacheid)) && StringUtils.isNotBlank(geocode)) { cacheid = app.getCacheid(geocode); } - if ((StringUtils.isBlank(geocode)) && StringUtils.isNotBlank(cacheid)) { + if (StringUtils.isBlank(geocode) && StringUtils.isNotBlank(cacheid)) { geocode = app.getGeocode(cacheid); } @@ -550,7 +549,7 @@ public class cgeovisit extends cgLogForm { if (post == null) { post = (Button) findViewById(R.id.post); } - if (ArrayUtils.isEmpty(viewstates)) { + if (cgBase.isEmpty(viewstates)) { post.setEnabled(false); post.setOnTouchListener(null); post.setOnClickListener(null); diff --git a/src/cgeo/geocaching/cgeowaypointadd.java b/src/cgeo/geocaching/cgeowaypointadd.java index 3c46ad0..1c0491b 100644 --- a/src/cgeo/geocaching/cgeowaypointadd.java +++ b/src/cgeo/geocaching/cgeowaypointadd.java @@ -248,8 +248,8 @@ public class cgeowaypointadd extends AbstractActivity { final String latText = ((Button) findViewById(R.id.buttonLatitude)).getText().toString(); final String lonText = ((Button) findViewById(R.id.buttonLongitude)).getText().toString(); - if (StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText) - && StringUtils.isNotBlank(latText) && StringUtils.isNotBlank(lonText)) { + if (StringUtils.isBlank(bearingText) && StringUtils.isBlank(distanceText) + && StringUtils.isBlank(latText) && StringUtils.isBlank(lonText)) { helpDialog(res.getString(R.string.err_point_no_position_given_title), res.getString(R.string.err_point_no_position_given)); return; } diff --git a/src/cgeo/geocaching/files/GPXParser.java b/src/cgeo/geocaching/files/GPXParser.java index 0719621..101d68b 100644 --- a/src/cgeo/geocaching/files/GPXParser.java +++ b/src/cgeo/geocaching/files/GPXParser.java @@ -48,6 +48,8 @@ public abstract class GPXParser extends FileParser { "http://www.groundspeak.com/cache/1/0", // PQ 1.0 }; + private static final String GSAK_NS = "http://www.gsak.net/xmlv1/5"; + private static cgeoapplication app = null; private int listId = 1; private cgSearch search = null; @@ -64,6 +66,20 @@ public abstract class GPXParser extends FileParser { private String name = null; private String cmt = null; private String desc = null; + protected String[] userData = new String[5]; // take 5 cells, that makes indexing 1..4 easier + + private final class UserDataListener implements EndTextElementListener { + private int index; + + public UserDataListener(int index) { + this.index = index; + } + + @Override + public void end(String user) { + userData[index] = validate(user); + } + } private static final class CacheAttributeTranslator { // List of cache attributes matching IDs used in GPX files. @@ -243,19 +259,25 @@ public abstract class GPXParser extends FileParser { cache.reason = listId; cache.detailed = true; + if (StringUtils.isBlank(cache.personalNote)) { + StringBuilder buffer = new StringBuilder(); + for (int i = 0; i < userData.length; i++) { + if (StringUtils.isNotBlank(userData[i])) { + buffer.append(' ').append(userData[i]); + } + } + String note = buffer.toString().trim(); + if (StringUtils.isNotBlank(note)) { + cache.personalNote = note; + } + } + app.addCacheToSearch(search, cache); } showFinishedMessage(handler, search); - type = null; - sym = null; - name = null; - desc = null; - cmt = null; - - cache = null; - cache = new cgCache(); + resetCache(); } }); @@ -353,6 +375,24 @@ public abstract class GPXParser extends FileParser { // for GPX 1.1 from extensions node final Element cacheParent = getCacheParent(waypoint); + + // GSAK extensions + final Element gsak = cacheParent.getChild(GSAK_NS, "wptExtension"); + gsak.getChild(GSAK_NS, "Watch").setEndTextElementListener(new EndTextElementListener() { + + @Override + public void end(String watchList) { + cache.onWatchlist = Boolean.valueOf(watchList.trim()); + } + }); + + gsak.getChild(GSAK_NS, "UserData").setEndTextElementListener(new UserDataListener(1)); + + for (int i = 2; i <= 4; i++) { + gsak.getChild(GSAK_NS, "User" + i).setEndTextElementListener(new UserDataListener(i)); + } + + // 3 different versions of the GC schema for (String nsGC : nsGCList) { // waypoints.cache final Element gcCache = cacheParent.getChild(nsGC, "cache"); @@ -720,6 +760,19 @@ public abstract class GPXParser extends FileParser { } } + private void resetCache() { + type = null; + sym = null; + name = null; + desc = null; + cmt = null; + + cache = new cgCache(); + for (int i = 0; i < userData.length; i++) { + userData[i] = null; + } + } + public static UUID parseGPX(cgeoapplication app, File file, int listId, Handler handler) { final cgSearch search = new cgSearch(); UUID searchId = null; diff --git a/src/cgeo/geocaching/geopoint/Geopoint.java b/src/cgeo/geocaching/geopoint/Geopoint.java index 85149be..44aadcc 100644 --- a/src/cgeo/geocaching/geopoint/Geopoint.java +++ b/src/cgeo/geocaching/geopoint/Geopoint.java @@ -14,15 +14,6 @@ public class Geopoint private double longitude; /** - * Creates new Geopoint with latitude and longitude set to 0. - */ - public Geopoint() - { - setLatitude(0); - setLongitude(0); - } - - /** * Creates new Geopoint with given latitude and longitude (both degree). * * @param lat latitude @@ -33,6 +24,18 @@ public class Geopoint setLatitude(lat); setLongitude(lon); } + + /** + * Creates new Geopoint with given latitude and longitude (both microdegree). + * + * @param lat latitude + * @param lon longitude + */ + public Geopoint(final int lat, final int lon) + { + setLatitude(lat * 1E-6); + setLongitude(lon * 1E-6); + } /** * Creates new Geopoint with latitude and longitude parsed from string. @@ -63,7 +66,7 @@ public class Geopoint * @return this * @throws MalformedCoordinateException if not -90 <= lat <= 90 */ - public Geopoint setLatitude(final double lat) + private void setLatitude(final double lat) { if (lat <= 90 && lat >= -90) { @@ -73,35 +76,6 @@ public class Geopoint { throw new MalformedCoordinateException("malformed latitude: " + lat); } - - return this; - } - - /** - * Set latitude in microdegree. - * - * @param lat latitude - * @return this - * @see setLatitude(final double lat) - * @throws MalformedCoordinateException if not -90 <= (lat * 1E-6) <= 90 - */ - public Geopoint setLatitudeE6(final int lat) - { - return setLatitude(lat * 1E-6); - } - - /** - * Set latitude by parsing string. - * - * @param lat latitude - * @return this - * @see setLatitude(final double lat) - * @throws ParseException if lat could not be parsed - * @throws MalformedCoordinateException if not -90 <= lat <= 90 - */ - public Geopoint setLatitude(final String lat) - { - return setLatitude(GeopointParser.parseLatitude(lat)); } /** @@ -131,7 +105,7 @@ public class Geopoint * @return this * @throws MalformedCoordinateException if not -180 <= lon <= 180 */ - public Geopoint setLongitude(final double lon) + private void setLongitude(final double lon) { if (lon <= 180 && lon >=-180) { @@ -141,35 +115,6 @@ public class Geopoint { throw new MalformedCoordinateException("malformed longitude: " + lon); } - - return this; - } - - /** - * Set longitude in microdegree. - * - * @param lon longitude - * @return this - * @see setLongitude(final double lon) - * @throws MalformedCoordinateException if not -180 <= (lon * 1E-6) <= 180 - */ - public Geopoint setLongitudeE6(final int lon) - { - return setLongitude(lon * 1E-6); - } - - /** - * Set longitude by parsing string. - * - * @param lon longitude - * @return this - * @see setLongitude(final double lon) - * @throws ParseException if lon could not be parsed - * @throws MalformedCoordinateException if not -180 <= lon <= 180 - */ - public Geopoint setLongitude(final String lon) - { - return setLongitude(GeopointParser.parseLongitude(lon)); } /** @@ -243,25 +188,11 @@ public class Geopoint } else if (ilat1 == ilat2) { - if (ilon1 > ilon2) - { - return 270; - } - else - { - return 90; - } + return (ilon1 > ilon2) ? 270 : 90; } else if (ilon1 == ilon2) { - if (ilat1 > ilat2) - { - return 180; - } - else - { - return 0; - } + return (ilat1 > ilat2) ? 180: 0; } else { diff --git a/src/cgeo/geocaching/mapcommon/cgMapOverlay.java b/src/cgeo/geocaching/mapcommon/cgMapOverlay.java index 475ded2..19c47a3 100644 --- a/src/cgeo/geocaching/mapcommon/cgMapOverlay.java +++ b/src/cgeo/geocaching/mapcommon/cgMapOverlay.java @@ -202,7 +202,7 @@ public class cgMapOverlay extends ItemizedOverlayBase implements OverlayBase { cgCoord coordinate = item.getCoord(); - if (coordinate.type != null && coordinate.type.equalsIgnoreCase("cache") && StringUtils.isNotBlank(coordinate.geocode)) { + if (StringUtils.isNotBlank(coordinate.type) && coordinate.type.equalsIgnoreCase("cache") && StringUtils.isNotBlank(coordinate.geocode)) { Intent popupIntent = new Intent(context, cgeopopup.class); popupIntent.putExtra("fromdetail", fromDetail); diff --git a/src/cgeo/geocaching/mapcommon/cgeomap.java b/src/cgeo/geocaching/mapcommon/cgeomap.java index c9fb60b..2798a61 100644 --- a/src/cgeo/geocaching/mapcommon/cgeomap.java +++ b/src/cgeo/geocaching/mapcommon/cgeomap.java @@ -7,6 +7,8 @@ import java.util.Locale; import java.util.Map; import java.util.UUID; +import org.apache.commons.lang3.StringUtils; + import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; @@ -24,7 +26,11 @@ import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import android.view.WindowManager; +import android.view.ViewGroup.LayoutParams; +import android.widget.ImageSwitcher; import android.widget.ImageView; +import android.widget.ImageView.ScaleType; +import android.widget.ViewSwitcher.ViewFactory; import cgeo.geocaching.R; import cgeo.geocaching.cgBase; import cgeo.geocaching.cgCache; @@ -32,13 +38,13 @@ import cgeo.geocaching.cgCoord; import cgeo.geocaching.cgDirection; import cgeo.geocaching.cgGeo; import cgeo.geocaching.cgSettings; -import cgeo.geocaching.cgSettings.mapSourceEnum; import cgeo.geocaching.cgUpdateDir; import cgeo.geocaching.cgUpdateLoc; import cgeo.geocaching.cgUser; import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.activity.ActivityMixin; +import cgeo.geocaching.cgSettings.mapSourceEnum; import cgeo.geocaching.mapinterfaces.ActivityImpl; import cgeo.geocaching.mapinterfaces.CacheOverlayItemImpl; import cgeo.geocaching.mapinterfaces.GeoPointImpl; @@ -48,7 +54,7 @@ import cgeo.geocaching.mapinterfaces.MapViewImpl; import cgeo.geocaching.mapinterfaces.OnDragListener; import cgeo.geocaching.mapinterfaces.UserOverlayItemImpl; -public class cgeomap extends MapBase implements OnDragListener { +public class cgeomap extends MapBase implements OnDragListener, ViewFactory { private static final int MENU_SELECT_MAPVIEW = 1; private static final int MENU_MAP_LIVE = 2; @@ -126,7 +132,7 @@ public class cgeomap extends MapBase implements OnDragListener { private int detailProgress = 0; private Long detailProgressTime = 0L; // views - private ImageView myLocSwitch = null; + private ImageSwitcher myLocSwitch = null; // other things private boolean live = true; // live map (live, dead) or rest (displaying caches on map) private boolean liveChanged = false; // previous state for loadTimer @@ -344,8 +350,14 @@ public class cgeomap extends MapBase implements OnDragListener { centerMap(geocodeIntent, searchIdIntent, latitudeIntent, longitudeIntent, mapStateIntent); } + // prepare my location button + myLocSwitch = (ImageSwitcher) activity.findViewById(R.id.my_position); + myLocSwitch.setFactory(this); + myLocSwitch.setInAnimation(activity, android.R.anim.fade_in); + myLocSwitch.setOutAnimation(activity, android.R.anim.fade_out); + myLocSwitch.setOnClickListener(new MyLocationListener()); + switchMyLocationButton(); - setMyLoc(null); startTimer(); } @@ -1064,7 +1076,7 @@ public class cgeomap extends MapBase implements OnDragListener { // stage 1 - pull and render from the DB only - if (fromDetailIntent) { + if (fromDetailIntent || StringUtils.isNotEmpty(searchIdIntent)) { searchId = UUID.fromString(searchIdIntent); } else { if (!live || settings.maplive == 0) { @@ -1657,7 +1669,7 @@ public class cgeomap extends MapBase implements OnDragListener { if (geocodeCenter != null) { viewport = app.getBounds(geocodeCenter); } else { - viewport = app.getBounds(searchIdCenter); + viewport = app.getBounds(UUID.fromString(searchIdCenter)); } if (viewport == null) return; @@ -1724,46 +1736,20 @@ public class cgeomap extends MapBase implements OnDragListener { } // switch My Location button image - private void setMyLoc(Boolean status) { - if (myLocSwitch == null) { - myLocSwitch = (ImageView) activity.findViewById(R.id.my_position); - } - - if (status == null) { - if (followMyLocation) { - myLocSwitch.setImageResource(R.drawable.actionbar_mylocation_on); - } else { - myLocSwitch.setImageResource(R.drawable.actionbar_mylocation_off); - } + private void switchMyLocationButton() { + if (followMyLocation) { + myLocSwitch.setImageResource(R.drawable.actionbar_mylocation_on); + myLocationInMiddle(); } else { - if (status) { - myLocSwitch.setImageResource(R.drawable.actionbar_mylocation_on); - } else { - myLocSwitch.setImageResource(R.drawable.actionbar_mylocation_off); - } + myLocSwitch.setImageResource(R.drawable.actionbar_mylocation_off); } - - myLocSwitch.setOnClickListener(new MyLocationListener()); } // set my location listener private class MyLocationListener implements View.OnClickListener { - public void onClick(View view) { - if (myLocSwitch == null) { - myLocSwitch = (ImageView) activity.findViewById(R.id.my_position); - } - - if (followMyLocation) { - followMyLocation = false; - - myLocSwitch.setImageResource(R.drawable.actionbar_mylocation_off); - } else { - followMyLocation = true; - myLocationInMiddle(); - - myLocSwitch.setImageResource(R.drawable.actionbar_mylocation_on); - } + followMyLocation = !followMyLocation; + switchMyLocationButton(); } } @@ -1771,7 +1757,7 @@ public class cgeomap extends MapBase implements OnDragListener { public void onDrag() { if (followMyLocation) { followMyLocation = false; - myLocSwitch.setImageResource(R.drawable.actionbar_mylocation_off); + switchMyLocationButton(); } } @@ -1789,4 +1775,12 @@ public class cgeomap extends MapBase implements OnDragListener { public void goManual(View view) { ActivityMixin.goManual(activity, "c:geo-live-map"); } + + @Override + public View makeView() { + ImageView imageView = new ImageView(activity); + imageView.setScaleType(ScaleType.CENTER); + imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); + return imageView; + } } |
