1 |
/**
|
2 |
* ZANavi, Zoff Android Navigation system.
|
3 |
* Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
/** @file vehicle_android.c
|
21 |
* @brief android uses dbus signals
|
22 |
*
|
23 |
* Navit, a modular navigation system.
|
24 |
* Copyright (C) 2005-2008 Navit Team
|
25 |
*
|
26 |
* This program is free software; you can redistribute it and/or
|
27 |
* modify it under the terms of the GNU General Public License
|
28 |
* version 2 as published by the Free Software Foundation.
|
29 |
*
|
30 |
* This program is distributed in the hope that it will be useful,
|
31 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
32 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
33 |
* GNU General Public License for more details.
|
34 |
*
|
35 |
* You should have received a copy of the GNU General Public License
|
36 |
* along with this program; if not, write to the
|
37 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
38 |
* Boston, MA 02110-1301, USA.
|
39 |
*
|
40 |
* @Author Tim Niemeyer <reddog@mastersword.de>
|
41 |
* @date 2008-2009
|
42 |
*/
|
43 |
|
44 |
#include <config.h>
|
45 |
#include <string.h>
|
46 |
#include <glib.h>
|
47 |
#include <math.h>
|
48 |
#include <time.h>
|
49 |
#include "debug.h"
|
50 |
#include "callback.h"
|
51 |
#include "plugin.h"
|
52 |
#include "coord.h"
|
53 |
#include "item.h"
|
54 |
#include "android.h"
|
55 |
#include "vehicle.h"
|
56 |
|
57 |
struct vehicle_priv
|
58 |
{
|
59 |
struct callback_list *cbl;
|
60 |
struct coord_geo geo;
|
61 |
double speed;
|
62 |
double direction;
|
63 |
double height;
|
64 |
double radius;
|
65 |
int fix_type;
|
66 |
time_t fix_time;
|
67 |
char fixiso8601[128];
|
68 |
int sats;
|
69 |
int sats_used;
|
70 |
int have_coords;
|
71 |
struct attr ** attrs;
|
72 |
struct callback *cb;
|
73 |
jclass NavitVehicleClass;
|
74 |
jobject NavitVehicle;
|
75 |
jclass LocationClass;
|
76 |
jmethodID Location_getLatitude, Location_getLongitude, Location_getSpeed, Location_getBearing, Location_getAltitude, Location_getTime, Location_getAccuracy;
|
77 |
};
|
78 |
|
79 |
// global vars
|
80 |
struct vehicle_priv *priv_global_android = NULL;
|
81 |
|
82 |
jclass NavitClass3 = NULL;
|
83 |
jmethodID Navit_get_vehicle;
|
84 |
// global vars
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
static int find_static_method(jclass class, char *name, char *args, jmethodID *ret)
|
90 |
{
|
91 |
JNIEnv *jnienv2;
|
92 |
jnienv2 = jni_getenv();
|
93 |
|
94 |
//DBG dbg(0,"EEnter\n");
|
95 |
*ret = (*jnienv2)->GetStaticMethodID(jnienv2, class, name, args);
|
96 |
if (*ret == NULL)
|
97 |
{
|
98 |
//DBG dbg(0, "Failed to get static Method %s with signature %s\n", name, args);
|
99 |
return 0;
|
100 |
}
|
101 |
return 1;
|
102 |
}
|
103 |
|
104 |
|
105 |
/**
|
106 |
* @brief Free the android_vehicle
|
107 |
*
|
108 |
* @param priv
|
109 |
* @returns nothing
|
110 |
*/
|
111 |
static void vehicle_android_destroy(struct vehicle_priv *priv)
|
112 |
{
|
113 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
114 |
dbg(0,"+#+:enter\n");
|
115 |
#endif
|
116 |
// //DBG dbg(0,"enter\n");
|
117 |
priv_global_android = NULL;
|
118 |
g_free(priv);
|
119 |
}
|
120 |
|
121 |
/**
|
122 |
* @brief Provide the outside with information
|
123 |
*
|
124 |
* @param priv
|
125 |
* @param type TODO: What can this be?
|
126 |
* @param attr
|
127 |
* @returns true/false
|
128 |
*/
|
129 |
static int vehicle_android_position_attr_get(struct vehicle_priv *priv, enum attr_type type, struct attr *attr)
|
130 |
{
|
131 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
132 |
dbg(0,"+#+:enter\n");
|
133 |
#endif
|
134 |
//dbg(1,"enter %s\n",attr_to_name(type));
|
135 |
switch (type)
|
136 |
{
|
137 |
#if 0
|
138 |
case attr_position_fix_type:
|
139 |
attr->u.num = priv->fix_type;
|
140 |
break;
|
141 |
#endif
|
142 |
case attr_position_height:
|
143 |
attr->u.numd = &priv->height;
|
144 |
break;
|
145 |
case attr_position_speed:
|
146 |
attr->u.numd = &priv->speed;
|
147 |
break;
|
148 |
case attr_position_direction:
|
149 |
attr->u.numd = &priv->direction;
|
150 |
break;
|
151 |
case attr_position_radius:
|
152 |
attr->u.numd = &priv->radius;
|
153 |
break;
|
154 |
|
155 |
#if 0
|
156 |
case attr_position_qual:
|
157 |
attr->u.num = priv->sats;
|
158 |
break;
|
159 |
case attr_position_sats_used:
|
160 |
attr->u.num = priv->sats_used;
|
161 |
break;
|
162 |
#endif
|
163 |
case attr_position_coord_geo:
|
164 |
attr->u.coord_geo = &priv->geo;
|
165 |
if (!priv->have_coords)
|
166 |
return 0;
|
167 |
break;
|
168 |
case attr_position_time_iso8601:
|
169 |
attr->u.str = priv->fixiso8601;
|
170 |
break;
|
171 |
default:
|
172 |
return 0;
|
173 |
}
|
174 |
//dbg(1,"ok\n");
|
175 |
attr->type = type;
|
176 |
|
177 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
178 |
dbg(0,"+#+:leave\n");
|
179 |
#endif
|
180 |
|
181 |
return 1;
|
182 |
}
|
183 |
|
184 |
static void vehicle_android_update_location_direct(jobject location)
|
185 |
{
|
186 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
187 |
dbg(0,"+#+:enter\n");
|
188 |
#endif
|
189 |
|
190 |
#ifdef NAVIT_MEASURE_TIME_DEBUG
|
191 |
clock_t s_ = debug_measure_start();
|
192 |
#endif
|
193 |
|
194 |
time_t tnow;
|
195 |
struct tm *tm;
|
196 |
|
197 |
struct vehicle_priv *v = priv_global_android;
|
198 |
|
199 |
JNIEnv *jnienv2;
|
200 |
jnienv2 = jni_getenv();
|
201 |
|
202 |
//dbg(0,"jnienv=%p\n", jnienv);
|
203 |
//dbg(0,"priv_global_android=%p\n", priv_global_android);
|
204 |
//dbg(0,"v=%p\n", v);
|
205 |
//dbg(0,"location=%p\n", location);
|
206 |
|
207 |
// this seems to slow and stupid, try to give those values directly (instead of calling those functions every time!!)
|
208 |
// this seems to slow and stupid, try to give those values directly (instead of calling those functions every time!!)
|
209 |
// this seems to slow and stupid, try to give those values directly (instead of calling those functions every time!!)
|
210 |
v->geo.lat = (*jnienv2)->CallDoubleMethod(jnienv2, location, v->Location_getLatitude);
|
211 |
v->geo.lng = (*jnienv2)->CallDoubleMethod(jnienv2, location, v->Location_getLongitude);
|
212 |
v->speed = (*jnienv2)->CallFloatMethod(jnienv2, location, v->Location_getSpeed) * 3.6; // convert from m/s -> km/h
|
213 |
v->direction = (*jnienv2)->CallFloatMethod(jnienv2, location, v->Location_getBearing);
|
214 |
v->height = (*jnienv2)->CallDoubleMethod(jnienv2, location, v->Location_getAltitude);
|
215 |
v->radius = (*jnienv2)->CallFloatMethod(jnienv2, location, v->Location_getAccuracy);
|
216 |
tnow = (*jnienv2)->CallLongMethod(jnienv2, location, v->Location_getTime) / 1000;
|
217 |
// this seems to slow and stupid, try to give those values directly (instead of calling those functions every time!!)
|
218 |
// this seems to slow and stupid, try to give those values directly (instead of calling those functions every time!!)
|
219 |
// this seems to slow and stupid, try to give those values directly (instead of calling those functions every time!!)
|
220 |
|
221 |
|
222 |
tm = gmtime(&tnow);
|
223 |
strftime(v->fixiso8601, sizeof(v->fixiso8601), "%Y-%m-%dT%TZ", tm);
|
224 |
// //DBG dbg(0,"lat %f lon %f\n",v->geo.lat,v->geo.lng);
|
225 |
v->have_coords = 1;
|
226 |
|
227 |
// remove globalref again
|
228 |
(*jnienv2)->DeleteGlobalRef(jnienv2, location);
|
229 |
|
230 |
// ***** calls: navit.c -> navit_vehicle_update
|
231 |
// xxx stupid callback stuff -> remove me!! xxx
|
232 |
callback_list_call_attr_0(v->cbl, attr_position_coord_geo);
|
233 |
|
234 |
#ifdef NAVIT_MEASURE_TIME_DEBUG
|
235 |
debug_mrp(__PRETTY_FUNCTION__, debug_measure_end(s_));
|
236 |
#endif
|
237 |
|
238 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
239 |
dbg(0,"+#+:leave\n");
|
240 |
#endif
|
241 |
|
242 |
}
|
243 |
|
244 |
struct vehicle_methods vehicle_android_methods =
|
245 |
{ vehicle_android_destroy, vehicle_android_position_attr_get, NULL, vehicle_android_update_location_direct};
|
246 |
|
247 |
static int vehicle_android_init(struct vehicle_priv *ret)
|
248 |
{
|
249 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
250 |
dbg(0,"+#+:enter\n");
|
251 |
#endif
|
252 |
|
253 |
int thread_id = gettid();
|
254 |
dbg(0, "THREAD ID=%d\n", thread_id);
|
255 |
|
256 |
JNIEnv *jnienv2;
|
257 |
jnienv2 = jni_getenv();
|
258 |
|
259 |
jmethodID cid;
|
260 |
|
261 |
//dbg(0,"priv_global_android=%p\n", priv_global_android);
|
262 |
|
263 |
if (!android_find_class_global("android/location/Location", &ret->LocationClass))
|
264 |
return 0;
|
265 |
if (!android_find_method(ret->LocationClass, "getLatitude", "()D", &ret->Location_getLatitude))
|
266 |
return 0;
|
267 |
if (!android_find_method(ret->LocationClass, "getLongitude", "()D", &ret->Location_getLongitude))
|
268 |
return 0;
|
269 |
if (!android_find_method(ret->LocationClass, "getSpeed", "()F", &ret->Location_getSpeed))
|
270 |
return 0;
|
271 |
if (!android_find_method(ret->LocationClass, "getBearing", "()F", &ret->Location_getBearing))
|
272 |
return 0;
|
273 |
if (!android_find_method(ret->LocationClass, "getAltitude", "()D", &ret->Location_getAltitude))
|
274 |
return 0;
|
275 |
if (!android_find_method(ret->LocationClass, "getTime", "()J", &ret->Location_getTime))
|
276 |
return 0;
|
277 |
if (!android_find_method(ret->LocationClass, "getAccuracy", "()F", &ret->Location_getAccuracy))
|
278 |
return 0;
|
279 |
if (!android_find_class_global("com/zoffcc/applications/zanavi/NavitVehicle", &ret->NavitVehicleClass))
|
280 |
{
|
281 |
return 0;
|
282 |
}
|
283 |
|
284 |
//dbg(0,"jnienv2=%p\n", jnienv2);
|
285 |
|
286 |
//DBG dbg(0,"at 3\n");
|
287 |
//cid = (*jnienv2)->GetMethodID(jnienv2, ret->NavitVehicleClass, "<init>", "(Landroid/content/Context;I)V");
|
288 |
//if (cid == NULL)
|
289 |
//{
|
290 |
// //DBG dbg(0,"no method found\n");
|
291 |
// return 0;
|
292 |
//}
|
293 |
|
294 |
// --------------- Init the new Vehicle Object here -----------------
|
295 |
// --------------- Init the new Vehicle Object here -----------------
|
296 |
// --------------- Init the new Vehicle Object here -----------------
|
297 |
dbg(0,"Init the new Vehicle Object here\n");
|
298 |
|
299 |
if (NavitClass3 == NULL)
|
300 |
{
|
301 |
if (!android_find_class_global("com/zoffcc/applications/zanavi/Navit", &NavitClass3))
|
302 |
{
|
303 |
NavitClass3 = NULL;
|
304 |
return 0;
|
305 |
}
|
306 |
}
|
307 |
|
308 |
if (!find_static_method(NavitClass3, "get_vehicle_object", "()Lcom/zoffcc/applications/zanavi/NavitVehicle;", &Navit_get_vehicle))
|
309 |
{
|
310 |
return 0;
|
311 |
}
|
312 |
|
313 |
/// --old-- ret->NavitVehicle = (*jnienv2)->NewObject(jnienv2, ret->NavitVehicleClass, cid, android_activity, (int) ret->cb);
|
314 |
/// --new--
|
315 |
ret->NavitVehicle = (*jnienv2)->CallStaticObjectMethod(jnienv2, NavitClass3, Navit_get_vehicle);
|
316 |
/// --new--
|
317 |
// --------------- Init the new Vehicle Object here -----------------
|
318 |
// --------------- Init the new Vehicle Object here -----------------
|
319 |
// --------------- Init the new Vehicle Object here -----------------
|
320 |
|
321 |
if (!ret->NavitVehicle)
|
322 |
{
|
323 |
return 0;
|
324 |
}
|
325 |
|
326 |
if (ret->NavitVehicle)
|
327 |
{
|
328 |
ret->NavitVehicle = (*jnienv2)->NewGlobalRef(jnienv2, ret->NavitVehicle);
|
329 |
}
|
330 |
|
331 |
dbg(0,"leave\n");
|
332 |
|
333 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
334 |
dbg(0,"+#+:leave\n");
|
335 |
#endif
|
336 |
|
337 |
return 1;
|
338 |
}
|
339 |
|
340 |
/**
|
341 |
* @brief Create android_vehicle
|
342 |
*
|
343 |
* @param meth
|
344 |
* @param cbl
|
345 |
* @param attrs
|
346 |
* @returns vehicle_priv
|
347 |
*/
|
348 |
static struct vehicle_priv *
|
349 |
vehicle_android_new_android(struct vehicle_methods *meth, struct callback_list *cbl, struct attr **attrs)
|
350 |
{
|
351 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
352 |
dbg(0,"+#+:enter\n");
|
353 |
#endif
|
354 |
struct vehicle_priv *ret;
|
355 |
|
356 |
//DBG dbg(0, "enter\n");
|
357 |
ret = g_new0(struct vehicle_priv, 1);
|
358 |
ret->cbl = cbl;
|
359 |
// *********** // ret->cb = callback_new_1(callback_cast(vehicle_android_callback), ret);
|
360 |
*meth = vehicle_android_methods;
|
361 |
priv_global_android = ret;
|
362 |
//dbg(0,"priv_global_android=%p\n", priv_global_android);
|
363 |
vehicle_android_init(ret);
|
364 |
//DBG dbg(0, "return\n");
|
365 |
|
366 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
367 |
dbg(0,"+#+:leave\n");
|
368 |
#endif
|
369 |
|
370 |
return ret;
|
371 |
}
|
372 |
|
373 |
/**
|
374 |
* @brief register vehicle_android
|
375 |
*
|
376 |
* @returns nothing
|
377 |
*/
|
378 |
void plugin_init(void)
|
379 |
{
|
380 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
381 |
dbg(0,"+#+:enter\n");
|
382 |
#endif
|
383 |
//DBG dbg(0, "enter\n");
|
384 |
plugin_register_vehicle_type("android", vehicle_android_new_android);
|
385 |
|
386 |
#ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
|
387 |
dbg(0,"+#+:leave\n");
|
388 |
#endif
|
389 |
|
390 |
}
|
391 |
|