/[zanavi_public1]/navit/navit/vehicle/android/vehicle_android.c
ZANavi

Contents of /navit/navit/vehicle/android/vehicle_android.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations) (download)
Mon Apr 9 21:27:36 2012 UTC (12 years ago) by zoff99
File MIME type: text/plain
File size: 7409 byte(s)
lots of new stuff, tranlsations, bug fixes ...
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 struct callback_list *cbl;
59 struct coord_geo geo;
60 double speed;
61 double direction;
62 double height;
63 double radius;
64 int fix_type;
65 time_t fix_time;
66 char fixiso8601[128];
67 int sats;
68 int sats_used;
69 int have_coords;
70 struct attr ** attrs;
71 struct callback *cb;
72 jclass NavitVehicleClass;
73 jobject NavitVehicle;
74 jclass LocationClass;
75 jmethodID Location_getLatitude, Location_getLongitude, Location_getSpeed, Location_getBearing, Location_getAltitude, Location_getTime, Location_getAccuracy;
76 };
77
78 /**
79 * @brief Free the android_vehicle
80 *
81 * @param priv
82 * @returns nothing
83 */
84 static void
85 vehicle_android_destroy(struct vehicle_priv *priv)
86 {
87 // //DBG dbg(0,"enter\n");
88 g_free(priv);
89 }
90
91 /**
92 * @brief Provide the outside with information
93 *
94 * @param priv
95 * @param type TODO: What can this be?
96 * @param attr
97 * @returns true/false
98 */
99 static int
100 vehicle_android_position_attr_get(struct vehicle_priv *priv,
101 enum attr_type type, struct attr *attr)
102 {
103 //dbg(1,"enter %s\n",attr_to_name(type));
104 switch (type)
105 {
106 #if 0
107 case attr_position_fix_type:
108 attr->u.num = priv->fix_type;
109 break;
110 #endif
111 case attr_position_height:
112 attr->u.numd = &priv->height;
113 break;
114 case attr_position_speed:
115 attr->u.numd = &priv->speed;
116 break;
117 case attr_position_direction:
118 attr->u.numd = &priv->direction;
119 break;
120 case attr_position_radius:
121 attr->u.numd = &priv->radius;
122 break;
123
124 #if 0
125 case attr_position_qual:
126 attr->u.num = priv->sats;
127 break;
128 case attr_position_sats_used:
129 attr->u.num = priv->sats_used;
130 break;
131 #endif
132 case attr_position_coord_geo:
133 attr->u.coord_geo = &priv->geo;
134 if (!priv->have_coords)
135 return 0;
136 break;
137 case attr_position_time_iso8601:
138 attr->u.str=priv->fixiso8601;
139 break;
140 default:
141 return 0;
142 }
143 //dbg(1,"ok\n");
144 attr->type = type;
145 return 1;
146 }
147
148 struct vehicle_methods vehicle_android_methods = {
149 vehicle_android_destroy,
150 vehicle_android_position_attr_get,
151 };
152
153 static void
154 vehicle_android_callback(struct vehicle_priv *v, jobject location)
155 {
156 time_t tnow;
157 struct tm *tm;
158
159 v->geo.lat = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getLatitude);
160 v->geo.lng = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getLongitude);
161 v->speed = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getSpeed)*3.6; // convert from m/s -> km/h
162 v->direction = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getBearing);
163 v->height = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getAltitude);
164 v->radius = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getAccuracy);
165 tnow=(*jnienv)->CallLongMethod(jnienv, location, v->Location_getTime)/1000;
166 tm = gmtime(&tnow);
167 strftime(v->fixiso8601, sizeof(v->fixiso8601), "%Y-%m-%dT%TZ", tm);
168 // //DBG dbg(0,"lat %f lon %f\n",v->geo.lat,v->geo.lng);
169 v->have_coords=1;
170
171 // ***** calls: navit.c -> navit_vehicle_update
172 callback_list_call_attr_0(v->cbl, attr_position_coord_geo);
173 }
174
175 static int
176 vehicle_android_init(struct vehicle_priv *ret)
177 {
178 jmethodID cid;
179
180 if (!android_find_class_global("android/location/Location", &ret->LocationClass))
181 return 0;
182 if (!android_find_method(ret->LocationClass, "getLatitude", "()D", &ret->Location_getLatitude))
183 return 0;
184 if (!android_find_method(ret->LocationClass, "getLongitude", "()D", &ret->Location_getLongitude))
185 return 0;
186 if (!android_find_method(ret->LocationClass, "getSpeed", "()F", &ret->Location_getSpeed))
187 return 0;
188 if (!android_find_method(ret->LocationClass, "getBearing", "()F", &ret->Location_getBearing))
189 return 0;
190 if (!android_find_method(ret->LocationClass, "getAltitude", "()D", &ret->Location_getAltitude))
191 return 0;
192 if (!android_find_method(ret->LocationClass, "getTime", "()J", &ret->Location_getTime))
193 return 0;
194 if (!android_find_method(ret->LocationClass, "getAccuracy", "()F", &ret->Location_getAccuracy))
195 return 0;
196 if (!android_find_class_global("com/zoffcc/applications/zanavi/NavitVehicle", &ret->NavitVehicleClass))
197 {
198 return 0;
199 }
200 //DBG dbg(0,"at 3\n");
201 cid = (*jnienv)->GetMethodID(jnienv, ret->NavitVehicleClass, "<init>", "(Landroid/content/Context;I)V");
202 if (cid == NULL)
203 {
204 //DBG dbg(0,"no method found\n");
205 return 0; /* exception thrown */
206 }
207 //DBG dbg(0,"at 4 android_activity=%p\n",android_activity);
208 ret->NavitVehicle=(*jnienv)->NewObject(jnienv, ret->NavitVehicleClass, cid, android_activity, (int) ret->cb);
209 //DBG dbg(0,"result=%p\n",ret->NavitVehicle);
210 if (!ret->NavitVehicle)
211 {
212 return 0;
213 }
214
215 if (ret->NavitVehicle)
216 {
217 ret->NavitVehicle = (*jnienv)->NewGlobalRef(jnienv, ret->NavitVehicle);
218 }
219
220 return 1;
221 }
222
223 /**
224 * @brief Create android_vehicle
225 *
226 * @param meth
227 * @param cbl
228 * @param attrs
229 * @returns vehicle_priv
230 */
231 static struct vehicle_priv *
232 vehicle_android_new_android(struct vehicle_methods *meth,
233 struct callback_list *cbl,
234 struct attr **attrs)
235 {
236 struct vehicle_priv *ret;
237
238 //DBG dbg(0, "enter\n");
239 ret = g_new0(struct vehicle_priv, 1);
240 ret->cbl = cbl;
241 ret->cb=callback_new_1(callback_cast(vehicle_android_callback), ret);
242 *meth = vehicle_android_methods;
243 vehicle_android_init(ret);
244 //DBG dbg(0, "return\n");
245 return ret;
246 }
247
248 /**
249 * @brief register vehicle_android
250 *
251 * @returns nothing
252 */
253 void
254 plugin_init(void)
255 {
256 //DBG dbg(0, "enter\n");
257 plugin_register_vehicle_type("android", vehicle_android_new_android);
258 }

   
Visit the ZANavi Wiki