aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/apps/cache/navi/LocusApp.java
blob: 41b989691baafb297106a8f33465e0562add5a22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package cgeo.geocaching.apps.cache.navi;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;

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;

class LocusApp extends AbstractLocusApp implements NavigationApp {

	LocusApp(Resources res) {
		super(res);
	}

	@Override
	public boolean invoke(cgGeo geo, Activity activity, Resources res,
			cgCache cache,
			Long searchId, cgWaypoint waypoint, Double latitude, Double longitude) {
		if (cache == null && waypoint == null && latitude == null
				&& longitude == null) {
			return false;
		}
		try {
			if (isInstalled(activity)) {
				final ArrayList<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.latitude != null && wp.longitude != 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 && cache.name != null
						&& cache.name.length() > 0) {
					dos.writeUTF(cache.name);
				} else if (waypoint != null && waypoint.name != null
						&& waypoint.name.length() > 0) {
					dos.writeUTF(waypoint.name);
				} else {
					dos.writeUTF("");
				}

				// description
				if (cache != null && cache.geocode != null
						&& cache.geocode.length() > 0) {
					dos.writeUTF(cache.geocode.toUpperCase());
				} else if (waypoint != null && waypoint.lookup != null
						&& waypoint.lookup.length() > 0) {
					dos.writeUTF(waypoint.lookup.toUpperCase());
				} else {
					dos.writeUTF("");
				}

				// additional data :: keyword, button title, package, activity,
				// data name, data content
				if (cache != null && cache.geocode != null
						&& cache.geocode.length() > 0) {
					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.latitude != null
						&& cache.longitude != null) {
					dos.writeDouble(cache.latitude); // latitude
					dos.writeDouble(cache.longitude); // longitude
				} else if (waypoint != null && waypoint.latitude != null
						&& waypoint.longitude != null) {
					dos.writeDouble(waypoint.latitude); // latitude
					dos.writeDouble(waypoint.longitude); // longitude
				} else {
					dos.writeDouble(latitude); // latitude
					dos.writeDouble(longitude); // longitude
				}

				// cache waypoints
				if (waypoints != null && waypoints.isEmpty() == false) {
					for (cgWaypoint wp : waypoints) {
						if (wp == null || wp.latitude == null
								|| wp.longitude == null) {
							continue;
						}

						final int wpIcon = cgBase.getMarkerIcon(false, wp.type, false,
								false, false);

						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 (wp.lookup != null && wp.lookup.length() > 0) {
							dos.writeUTF(wp.lookup.toUpperCase());
						} else {
							dos.writeUTF("");
						}

						// description
						if (wp.name != null && wp.name.length() > 0) {
							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.latitude); // latitude
						dos.writeDouble(wp.longitude); // 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);

				return true;
			}
		} catch (Exception e) {
			// nothing
		}
		return false;
	}

}