aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/apps
diff options
context:
space:
mode:
Diffstat (limited to 'src/cgeo/geocaching/apps')
-rw-r--r--src/cgeo/geocaching/apps/AbstractLocusApp.java155
-rwxr-xr-xsrc/cgeo/geocaching/apps/LocusDataStorageProvider.java68
-rw-r--r--src/cgeo/geocaching/apps/cache/navi/LocusApp.java193
-rw-r--r--src/cgeo/geocaching/apps/cachelist/CacheListAppFactory.java3
-rw-r--r--src/cgeo/geocaching/apps/cachelist/LocusCacheListApp.java91
5 files changed, 254 insertions, 256 deletions
diff --git a/src/cgeo/geocaching/apps/AbstractLocusApp.java b/src/cgeo/geocaching/apps/AbstractLocusApp.java
index b0e9249..1b21b8e 100644
--- a/src/cgeo/geocaching/apps/AbstractLocusApp.java
+++ b/src/cgeo/geocaching/apps/AbstractLocusApp.java
@@ -1,24 +1,167 @@
package cgeo.geocaching.apps;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+import menion.android.locus.addon.publiclib.DisplayData;
+import menion.android.locus.addon.publiclib.LocusUtils;
+import menion.android.locus.addon.publiclib.geoData.Point;
+import menion.android.locus.addon.publiclib.geoData.PointGeocachingData;
+import menion.android.locus.addon.publiclib.geoData.PointGeocachingDataWaypoint;
+import menion.android.locus.addon.publiclib.geoData.PointsData;
+import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
-import android.net.Uri;
+import android.location.Location;
import cgeo.geocaching.R;
+import cgeo.geocaching.cgCache;
+import cgeo.geocaching.cgSettings;
+import cgeo.geocaching.cgWaypoint;
+import cgeo.geocaching.enumerations.CacheSize;
+import cgeo.geocaching.enumerations.CacheType;
+import cgeo.geocaching.enumerations.WaypointType;
+/**
+ * @see http://forum.asamm.cz/viewtopic.php?f=29&t=767
+ */
public abstract class AbstractLocusApp extends AbstractApp {
private static final String INTENT = Intent.ACTION_VIEW;
-
+ private static final SimpleDateFormat ISO8601DATE = new SimpleDateFormat("yyyy-MM-dd'T'");
+
protected AbstractLocusApp(final Resources res) {
super(res.getString(R.string.caches_map_locus), INTENT);
}
@Override
- public boolean isInstalled(final Context context) {
- final Intent intentTest = new Intent(INTENT);
- intentTest.setData(Uri.parse("menion.points:x"));
- return isIntentAvailable(context, intentTest);
+ public boolean isInstalled(Context context) {
+ return LocusUtils.isLocusAvailable(context);
+ }
+
+ /**
+ * Display a list of caches / waypoints in Locus
+ *
+ * @param objectsToShow which caches/waypoints to show
+ * @param withCacheWaypoints wether to give waypoints of caches to Locus or not
+ * @param activity
+ * @author koem
+ */
+ protected void showInLocus(List<? extends Object> objectsToShow, boolean withCacheWaypoints,
+ Activity activity) {
+ if (objectsToShow == null) return;
+
+ int pc = 0; // counter for points
+ PointsData pd = new PointsData("c:geo");
+ for (Object o : objectsToShow) {
+ // get icon and Point
+ Point p = null;
+ if (o instanceof cgCache) {
+ p = this.getPoint((cgCache) o, withCacheWaypoints);
+ } else if (o instanceof cgWaypoint) {
+ p = this.getPoint((cgWaypoint) o);
+ } else {
+ continue; // no cache, no waypoint => ignore
+ }
+ if (p == null) continue;
+
+ pd.addPoint(p);
+ ++pc;
+ }
+
+ if (pc <= 1000) {
+ DisplayData.sendData(activity, pd, false);
+ } else {
+ ArrayList<PointsData> data = new ArrayList<PointsData>();
+ data.add(pd);
+ DisplayData.sendDataCursor(activity, data,
+ "content://" + LocusDataStorageProvider.class.getCanonicalName().toLowerCase(),
+ false);
+ }
+ }
+
+ /**
+ * This method constructs a <code>Point</code> for displaying in Locus
+ *
+ * @param cache
+ * @param withWaypoints wether to give waypoints to Locus or not
+ * @return null, when the <code>Point</code> could not be constructed
+ * @author koem
+ */
+ private Point getPoint(cgCache cache, boolean withWaypoints) {
+ if (cache == null || cache.coords == null) return null;
+
+ // create one simple point with location
+ Location loc = new Location(cgSettings.tag);
+ loc.setLatitude(cache.coords.getLatitude());
+ loc.setLongitude(cache.coords.getLongitude());
+
+ Point p = new Point(cache.name, loc);
+ PointGeocachingData pg = new PointGeocachingData();
+ p.setGeocachingData(pg);
+
+ // set data in Locus' cache
+ pg.cacheID = cache.geocode;
+ pg.available = ! cache.disabled;
+ pg.archived = cache.archived;
+ pg.premiumOnly = cache.members;
+ pg.name = cache.name;
+ pg.placedBy = cache.owner;
+ if (cache.hidden != null) pg.hidden = ISO8601DATE.format(cache.hidden.getTime());
+ CacheType ct = CacheType.findByCgeoId(cache.type);
+ if (ct != null && ct.locusId != CacheType.NO_LOCUS_ID) pg.type = ct.locusId;
+ CacheSize cs = CacheSize.findByCgeoId(cache.size);
+ if (cs != null) pg.container = cs.locusId;
+ if (cache.difficulty != null) pg.difficulty = cache.difficulty;
+ if (cache.terrain != null) pg.terrain = cache.terrain;
+ pg.found = cache.found;
+
+ if (withWaypoints && cache.waypoints != null) {
+ pg.waypoints = new ArrayList<PointGeocachingDataWaypoint>();
+ for (cgWaypoint waypoint : cache.waypoints) {
+ if (waypoint == null || waypoint.coords == null) continue;
+ PointGeocachingDataWaypoint wp = new PointGeocachingDataWaypoint();
+ wp.code = waypoint.geocode;
+ wp.name = waypoint.name;
+ WaypointType wt = WaypointType.findByCgeoId(waypoint.type);
+ if (wt != null) wp.type = wt.locusId;
+ wp.lat = waypoint.coords.getLatitude();
+ wp.lon = waypoint.coords.getLongitude();
+ pg.waypoints.add(wp);
+ }
+ }
+
+ // Other properties of caches, not used yet. When there are many caches to be displayed
+ // in Locus, using these properties can lead to Exceptions in Locus.
+ // Examination necessary when to display and when not. E. g.: > 200 caches: don't display
+ // these properties.
+
+ //pg.shortDescription = cache.shortdesc;
+ //pg.longDescription = cache.description;
+ //pg.encodedHints = cache.hint;
+
+ return p;
}
+ /**
+ * This method constructs a <code>Point</code> for displaying in Locus
+ *
+ * @param waypoint
+ * @return null, when the <code>Point</code> could not be constructed
+ * @author koem
+ */
+ private Point getPoint(cgWaypoint waypoint) {
+ if (waypoint == null || waypoint.coords == null) return null;
+
+ // create one simple point with location
+ Location loc = new Location(cgSettings.tag);
+ loc.setLatitude(waypoint.coords.getLatitude());
+ loc.setLongitude(waypoint.coords.getLongitude());
+
+ Point p = new Point(waypoint.name, loc);
+ p.setDescription("<a href=\"http://coord.info/" + waypoint.geocode + "\">"
+ + waypoint.geocode + "</a>");
+ return p;
+ }
}
diff --git a/src/cgeo/geocaching/apps/LocusDataStorageProvider.java b/src/cgeo/geocaching/apps/LocusDataStorageProvider.java
new file mode 100755
index 0000000..8da7160
--- /dev/null
+++ b/src/cgeo/geocaching/apps/LocusDataStorageProvider.java
@@ -0,0 +1,68 @@
+package cgeo.geocaching.apps;
+
+import java.util.ArrayList;
+
+import menion.android.locus.addon.publiclib.geoData.PointsData;
+import menion.android.locus.addon.publiclib.utils.DataCursor;
+import menion.android.locus.addon.publiclib.utils.DataStorage;
+import android.content.ContentProvider;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Parcel;
+
+public class LocusDataStorageProvider extends ContentProvider {
+
+// private final static String TAG = "DataStorageProvider";
+
+ @Override
+ public Cursor query(Uri aUri, String[] aProjection, String aSelection,
+ String[] aSelectionArgs, String aSortOrder) {
+
+ DataCursor cursor = new DataCursor(new String[] {"data"});
+
+ ArrayList<PointsData> data = DataStorage.getData();
+ if (data == null || data.size() == 0)
+ return cursor;
+
+ for (int i = 0; i < data.size(); i++) {
+ // get byte array
+ Parcel par = Parcel.obtain();
+ data.get(i).writeToParcel(par, 0);
+ byte[] byteData = par.marshall();
+ // add to row
+ cursor.addRow(new Object[] {byteData});
+ }
+ // data filled to cursor, clear reference to prevent some memory issue
+ DataStorage.clearData();
+ // now finally return filled cursor
+ return cursor;
+ }
+
+ @Override
+ public int delete(Uri uri, String selection, String[] selectionArgs) {
+ return 0;
+ }
+
+ @Override
+ public String getType(Uri uri) {
+ return null;
+ }
+
+ @Override
+ public Uri insert(Uri uri, ContentValues values) {
+ return null;
+ }
+
+ @Override
+ public boolean onCreate() {
+ return false;
+ }
+
+ @Override
+ public int update(Uri uri, ContentValues values, String selection,
+ String[] selectionArgs) {
+ return 0;
+ }
+
+}
diff --git a/src/cgeo/geocaching/apps/cache/navi/LocusApp.java b/src/cgeo/geocaching/apps/cache/navi/LocusApp.java
index 3c9928e..557c298 100644
--- a/src/cgeo/geocaching/apps/cache/navi/LocusApp.java
+++ b/src/cgeo/geocaching/apps/cache/navi/LocusApp.java
@@ -1,26 +1,15 @@
package cgeo.geocaching.apps.cache.navi;
-import java.io.ByteArrayOutputStream;
-import java.io.DataOutputStream;
import java.util.ArrayList;
-import java.util.List;
import java.util.UUID;
-import org.apache.commons.lang3.StringUtils;
-
import android.app.Activity;
-import android.content.Intent;
import android.content.res.Resources;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.net.Uri;
-import cgeo.geocaching.cgBase;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgGeo;
import cgeo.geocaching.cgWaypoint;
import cgeo.geocaching.apps.AbstractLocusApp;
import cgeo.geocaching.geopoint.Geopoint;
-import cgeo.geocaching.utils.CollectionUtils;
class LocusApp extends AbstractLocusApp implements NavigationApp {
@@ -28,173 +17,41 @@ class LocusApp extends AbstractLocusApp implements NavigationApp {
super(res);
}
+ /**
+ * Show a single cache with waypoints or a single waypoint in Locus.
+ * This method constructs a list of cache and waypoints only.
+ *
+ * @see AbstractLocusApp#showInLocus
+ * @author koem
+ */
@Override
- public boolean invoke(cgGeo geo, Activity activity, Resources res,
- cgCache cache,
+ public boolean invoke(cgGeo geo, Activity activity, Resources res, cgCache cache,
final UUID searchId, cgWaypoint waypoint, final Geopoint coords) {
+
if (cache == null && waypoint == null && coords == null) {
return false;
}
- try {
- if (isInstalled(activity)) {
- final List<cgWaypoint> waypoints = new ArrayList<cgWaypoint>();
- // get only waypoints with coordinates
- if (cache != null && cache.waypoints != null
- && cache.waypoints.isEmpty() == false) {
- for (cgWaypoint wp : cache.waypoints) {
- if (wp.coords != null) {
- waypoints.add(wp);
- }
- }
- }
-
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final DataOutputStream dos = new DataOutputStream(baos);
-
- dos.writeInt(1); // not used
- if (cache != null) {
- if (waypoints == null || waypoints.isEmpty()) {
- dos.writeInt(1); // cache only
- } else {
- dos.writeInt((1 + waypoints.size())); // cache and
- // waypoints
- }
- } else {
- dos.writeInt(1); // one waypoint
- }
-
- int icon = -1;
- if (cache != null) {
- icon = cgBase.getMarkerIcon(true, cache.type, cache.own, cache.found,
- cache.disabled || cache.archived);
- } else if (waypoint != null) {
- icon = cgBase.getMarkerIcon(false, waypoint.type, false, false, false);
- } else {
- icon = cgBase.getMarkerIcon(false, "waypoint", false, false, false);
- }
-
- if (icon > 0) {
- // load icon
- Bitmap bitmap = BitmapFactory.decodeResource(res, icon);
- ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos2);
- byte[] image = baos2.toByteArray();
-
- dos.writeInt(image.length);
- dos.write(image);
- } else {
- // no icon
- dos.writeInt(0); // no image
- }
-
- // name
- if (cache != null && StringUtils.isNotBlank(cache.name)) {
- dos.writeUTF(cache.name);
- } else if (waypoint != null && StringUtils.isNotBlank(waypoint.name)) {
- dos.writeUTF(waypoint.name);
- } else {
- dos.writeUTF("");
- }
-
- // description
- if (cache != null && StringUtils.isNotBlank(cache.geocode)) {
- dos.writeUTF(cache.geocode.toUpperCase());
- } else if (waypoint != null && StringUtils.isNotBlank(waypoint.lookup)) {
- dos.writeUTF(waypoint.lookup.toUpperCase());
- } else {
- dos.writeUTF("");
- }
-
- // additional data :: keyword, button title, package, activity,
- // data name, data content
- if (cache != null && StringUtils.isNotBlank(cache.geocode)) {
- dos.writeUTF("intent;c:geo;cgeo.geocaching;cgeo.geocaching.cgeodetail;geocode;"
- + cache.geocode);
- } else if (waypoint != null && waypoint.id != null
- && waypoint.id > 0) {
- dos.writeUTF("intent;c:geo;cgeo.geocaching;cgeo.geocaching.cgeowaypoint;id;"
- + waypoint.id);
- } else {
- dos.writeUTF("");
- }
-
- if (cache != null && cache.coords != null) {
- dos.writeDouble(cache.coords.getLatitude()); // latitude
- dos.writeDouble(cache.coords.getLongitude()); // longitude
- } else if (waypoint != null && waypoint.coords != null) {
- dos.writeDouble(waypoint.coords.getLatitude()); // latitude
- dos.writeDouble(waypoint.coords.getLongitude()); // longitude
- } else {
- dos.writeDouble(coords.getLatitude()); // latitude
- dos.writeDouble(coords.getLongitude()); // longitude
- }
-
- // cache waypoints
- if (CollectionUtils.isNotEmpty(waypoints)) {
- for (cgWaypoint wp : waypoints) {
- if (wp == null || wp.coords == null) {
- continue;
- }
- final int wpIcon = cgBase.getMarkerIcon(false, wp.type, false,
- false, false);
+ if (isInstalled(activity)) { // TODO: is this if-statement really necessary?
+ final ArrayList<Object> points = new ArrayList<Object>();
- if (wpIcon > 0) {
- // load icon
- Bitmap bitmap = BitmapFactory.decodeResource(res,
- wpIcon);
- ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100,
- baos2);
- byte[] image = baos2.toByteArray();
-
- dos.writeInt(image.length);
- dos.write(image);
- } else {
- // no icon
- dos.writeInt(0); // no image
- }
-
- // name
- if (StringUtils.isNotBlank(wp.lookup)) {
- dos.writeUTF(wp.lookup.toUpperCase());
- } else {
- dos.writeUTF("");
- }
-
- // description
- if (StringUtils.isNotBlank(wp.name)) {
- dos.writeUTF(wp.name);
- } else {
- dos.writeUTF("");
- }
-
- // additional data :: keyword, button title, package,
- // activity, data name, data content
- if (wp.id != null && wp.id > 0) {
- dos.writeUTF("intent;c:geo;cgeo.geocaching;cgeo.geocaching.cgeowaypoint;id;"
- + wp.id);
- } else {
- dos.writeUTF("");
- }
-
- dos.writeDouble(wp.coords.getLatitude()); // latitude
- dos.writeDouble(wp.coords.getLongitude()); // longitude
- }
- }
-
- final Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(Uri.parse("menion.points:data"));
- intent.putExtra("data", baos.toByteArray());
-
- activity.startActivity(intent);
+ // add cache if present
+ if (cache != null) {
+ if (cache.coords == null) cache.coords = coords;
+ if (cache.coords != null) points.add(cache);
+ }
- return true;
+ // add waypoint if present
+ if (waypoint != null) {
+ if (waypoint.coords == null) waypoint.coords = coords;
+ if (waypoint.coords != null) points.add(waypoint);
}
- } catch (Exception e) {
- // nothing
+
+ this.showInLocus(points, true, activity);
+
+ return true;
}
+
return false;
}
diff --git a/src/cgeo/geocaching/apps/cachelist/CacheListAppFactory.java b/src/cgeo/geocaching/apps/cachelist/CacheListAppFactory.java
index cb69e17..91e547a 100644
--- a/src/cgeo/geocaching/apps/cachelist/CacheListAppFactory.java
+++ b/src/cgeo/geocaching/apps/cachelist/CacheListAppFactory.java
@@ -64,8 +64,7 @@ public final class CacheListAppFactory extends AbstractAppFactory {
public static boolean onMenuItemSelected(final MenuItem item,
final cgGeo geo, final List<cgCache> caches, final Activity activity, final Resources res,
final UUID searchId) {
- CacheListApp app = (CacheListApp) getAppFromMenuItem(
- item, apps);
+ CacheListApp app = (CacheListApp) getAppFromMenuItem(item, apps);
if (app != null) {
try {
return app.invoke(geo, caches, activity, res, searchId);
diff --git a/src/cgeo/geocaching/apps/cachelist/LocusCacheListApp.java b/src/cgeo/geocaching/apps/cachelist/LocusCacheListApp.java
index 41a1cdd..a18010a 100644
--- a/src/cgeo/geocaching/apps/cachelist/LocusCacheListApp.java
+++ b/src/cgeo/geocaching/apps/cachelist/LocusCacheListApp.java
@@ -1,20 +1,10 @@
package cgeo.geocaching.apps.cachelist;
-import java.io.ByteArrayOutputStream;
-import java.io.DataOutputStream;
-import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
-import org.apache.commons.lang3.StringUtils;
-
import android.app.Activity;
-import android.content.Intent;
import android.content.res.Resources;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.net.Uri;
-import cgeo.geocaching.cgBase;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgGeo;
import cgeo.geocaching.apps.AbstractLocusApp;
@@ -25,78 +15,19 @@ class LocusCacheListApp extends AbstractLocusApp implements CacheListApp {
super(res);
}
+ /**
+ * show caches in Locus
+ *
+ * @see AbstractLocusApp#showInLocus
+ * @author koem
+ */
@Override
- public boolean invoke(cgGeo geo, List<cgCache> cacheList, Activity activity, Resources res, final UUID searchId) {
- if (cacheList == null || cacheList.isEmpty()) {
- return false;
- }
-
- try {
- final List<cgCache> cacheListCoord = new ArrayList<cgCache>();
- for (cgCache cache : cacheList) {
- if (cache.coords != null) {
- cacheListCoord.add(cache);
- }
- }
-
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final DataOutputStream dos = new DataOutputStream(baos);
-
- dos.writeInt(1); // not used
- dos.writeInt(cacheListCoord.size()); // cache and waypoints
-
- // cache waypoints
- for (cgCache cache : cacheListCoord) {
- final int wpIcon = cgBase.getMarkerIcon(true, cache.type, cache.own, cache.found, cache.disabled);
-
- if (wpIcon > 0) {
- // load icon
- Bitmap bitmap = BitmapFactory.decodeResource(res, wpIcon);
- ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos2);
- byte[] image = baos2.toByteArray();
-
- dos.writeInt(image.length);
- dos.write(image);
- } else {
- // no icon
- dos.writeInt(0); // no image
- }
-
- // name
- if (StringUtils.isNotBlank(cache.geocode)) {
- dos.writeUTF(cache.geocode.toUpperCase());
- } else {
- dos.writeUTF("");
- }
-
- // description
- if (StringUtils.isNotBlank(cache.name)) {
- dos.writeUTF(cache.name);
- } else {
- dos.writeUTF("");
- }
-
- // additional data :: keyword, button title, package, activity, data name, data content
- if (StringUtils.isNotBlank(cache.geocode)) {
- dos.writeUTF("intent;c:geo;cgeo.geocaching;cgeo.geocaching.cgeodetail;geocode;" + cache.geocode);
- } else {
- dos.writeUTF("");
- }
-
- dos.writeDouble(cache.coords.getLatitude()); // latitude
- dos.writeDouble(cache.coords.getLongitude()); // longitude
- }
-
- final Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(Uri.parse("menion.points:data"));
- intent.putExtra("data", baos.toByteArray());
+ public boolean invoke(cgGeo geo, List<cgCache> cacheList, Activity activity, Resources res,
+ final UUID searchId) {
+ if (cacheList == null || cacheList.isEmpty()) return false;
+
+ this.showInLocus((List<? extends Object>) cacheList, false, activity);
- activity.startActivity(intent);
- } catch (Exception e) {
- // nothing
- }
return true;
}