/[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 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (12 years, 5 months ago) by zoff99
File MIME type: text/plain
File size: 6598 byte(s)
import files
1 /** @file vehicle_android.c
2 * @brief android uses dbus signals
3 *
4 * Navit, a modular navigation system.
5 * Copyright (C) 2005-2008 Navit Team
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 * @Author Tim Niemeyer <reddog@mastersword.de>
22 * @date 2008-2009
23 */
24
25 #include <config.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <math.h>
29 #include <time.h>
30 #include "debug.h"
31 #include "callback.h"
32 #include "plugin.h"
33 #include "coord.h"
34 #include "item.h"
35 #include "android.h"
36 #include "vehicle.h"
37
38 struct vehicle_priv {
39 struct callback_list *cbl;
40 struct coord_geo geo;
41 double speed;
42 double direction;
43 double height;
44 double radius;
45 int fix_type;
46 time_t fix_time;
47 char fixiso8601[128];
48 int sats;
49 int sats_used;
50 int have_coords;
51 struct attr ** attrs;
52 struct callback *cb;
53 jclass NavitVehicleClass;
54 jobject NavitVehicle;
55 jclass LocationClass;
56 jmethodID Location_getLatitude, Location_getLongitude, Location_getSpeed, Location_getBearing, Location_getAltitude, Location_getTime, Location_getAccuracy;
57 };
58
59 /**
60 * @brief Free the android_vehicle
61 *
62 * @param priv
63 * @returns nothing
64 */
65 static void
66 vehicle_android_destroy(struct vehicle_priv *priv)
67 {
68 // dbg(0,"enter\n");
69 g_free(priv);
70 }
71
72 /**
73 * @brief Provide the outside with information
74 *
75 * @param priv
76 * @param type TODO: What can this be?
77 * @param attr
78 * @returns true/false
79 */
80 static int
81 vehicle_android_position_attr_get(struct vehicle_priv *priv,
82 enum attr_type type, struct attr *attr)
83 {
84 dbg(1,"enter %s\n",attr_to_name(type));
85 switch (type) {
86 #if 0
87 case attr_position_fix_type:
88 attr->u.num = priv->fix_type;
89 break;
90 #endif
91 case attr_position_height:
92 attr->u.numd = &priv->height;
93 break;
94 case attr_position_speed:
95 attr->u.numd = &priv->speed;
96 break;
97 case attr_position_direction:
98 attr->u.numd = &priv->direction;
99 break;
100 case attr_position_radius:
101 attr->u.numd = &priv->radius;
102 break;
103
104 #if 0
105 case attr_position_qual:
106 attr->u.num = priv->sats;
107 break;
108 case attr_position_sats_used:
109 attr->u.num = priv->sats_used;
110 break;
111 #endif
112 case attr_position_coord_geo:
113 attr->u.coord_geo = &priv->geo;
114 if (!priv->have_coords)
115 return 0;
116 break;
117 case attr_position_time_iso8601:
118 attr->u.str=priv->fixiso8601;
119 break;
120 default:
121 return 0;
122 }
123 dbg(1,"ok\n");
124 attr->type = type;
125 return 1;
126 }
127
128 struct vehicle_methods vehicle_android_methods = {
129 vehicle_android_destroy,
130 vehicle_android_position_attr_get,
131 };
132
133 static void
134 vehicle_android_callback(struct vehicle_priv *v, jobject location)
135 {
136 time_t tnow;
137 struct tm *tm;
138
139 v->geo.lat = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getLatitude);
140 v->geo.lng = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getLongitude);
141 v->speed = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getSpeed)*3.6; // convert from m/s -> km/h
142 v->direction = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getBearing);
143 v->height = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getAltitude);
144 v->radius = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getAccuracy);
145 tnow=(*jnienv)->CallLongMethod(jnienv, location, v->Location_getTime)/1000;
146 tm = gmtime(&tnow);
147 strftime(v->fixiso8601, sizeof(v->fixiso8601), "%Y-%m-%dT%TZ", tm);
148 // dbg(0,"lat %f lon %f\n",v->geo.lat,v->geo.lng);
149 v->have_coords=1;
150
151 // ***** calls: navit.c -> navit_vehicle_update
152 callback_list_call_attr_0(v->cbl, attr_position_coord_geo);
153 }
154
155 static int
156 vehicle_android_init(struct vehicle_priv *ret)
157 {
158 jmethodID cid;
159
160 if (!android_find_class_global("android/location/Location", &ret->LocationClass))
161 return 0;
162 if (!android_find_method(ret->LocationClass, "getLatitude", "()D", &ret->Location_getLatitude))
163 return 0;
164 if (!android_find_method(ret->LocationClass, "getLongitude", "()D", &ret->Location_getLongitude))
165 return 0;
166 if (!android_find_method(ret->LocationClass, "getSpeed", "()F", &ret->Location_getSpeed))
167 return 0;
168 if (!android_find_method(ret->LocationClass, "getBearing", "()F", &ret->Location_getBearing))
169 return 0;
170 if (!android_find_method(ret->LocationClass, "getAltitude", "()D", &ret->Location_getAltitude))
171 return 0;
172 if (!android_find_method(ret->LocationClass, "getTime", "()J", &ret->Location_getTime))
173 return 0;
174 if (!android_find_method(ret->LocationClass, "getAccuracy", "()F", &ret->Location_getAccuracy))
175 return 0;
176 if (!android_find_class_global("com/zoffcc/applications/zanavi/NavitVehicle", &ret->NavitVehicleClass))
177 return 0;
178 dbg(0,"at 3\n");
179 cid = (*jnienv)->GetMethodID(jnienv, ret->NavitVehicleClass, "<init>", "(Landroid/content/Context;I)V");
180 if (cid == NULL) {
181 dbg(0,"no method found\n");
182 return 0; /* exception thrown */
183 }
184 dbg(0,"at 4 android_activity=%p\n",android_activity);
185 ret->NavitVehicle=(*jnienv)->NewObject(jnienv, ret->NavitVehicleClass, cid, android_activity, (int) ret->cb);
186 dbg(0,"result=%p\n",ret->NavitVehicle);
187 if (!ret->NavitVehicle)
188 return 0;
189 if (ret->NavitVehicle)
190 (*jnienv)->NewGlobalRef(jnienv, ret->NavitVehicle);
191
192 return 1;
193 }
194
195 /**
196 * @brief Create android_vehicle
197 *
198 * @param meth
199 * @param cbl
200 * @param attrs
201 * @returns vehicle_priv
202 */
203 static struct vehicle_priv *
204 vehicle_android_new_android(struct vehicle_methods *meth,
205 struct callback_list *cbl,
206 struct attr **attrs)
207 {
208 struct vehicle_priv *ret;
209
210 dbg(0, "enter\n");
211 ret = g_new0(struct vehicle_priv, 1);
212 ret->cbl = cbl;
213 ret->cb=callback_new_1(callback_cast(vehicle_android_callback), ret);
214 *meth = vehicle_android_methods;
215 vehicle_android_init(ret);
216 dbg(0, "return\n");
217 return ret;
218 }
219
220 /**
221 * @brief register vehicle_android
222 *
223 * @returns nothing
224 */
225 void
226 plugin_init(void)
227 {
228 // dbg(0, "enter\n");
229 plugin_register_vehicle_type("android", vehicle_android_new_android);
230 }

   
Visit the ZANavi Wiki