diff options
Diffstat (limited to 'navit/vehicle/android/vehicle_android.c')
-rw-r--r-- | navit/vehicle/android/vehicle_android.c | 320 |
1 files changed, 158 insertions, 162 deletions
diff --git a/navit/vehicle/android/vehicle_android.c b/navit/vehicle/android/vehicle_android.c index 722943294..5eb9a24b7 100644 --- a/navit/vehicle/android/vehicle_android.c +++ b/navit/vehicle/android/vehicle_android.c @@ -41,39 +41,39 @@ */ struct vehicle_priv { - struct callback_list *cbl; - struct coord_geo geo; /**< The last known position of the vehicle **/ - double speed; /**< Speed in km/h **/ - double direction; /**< Bearing in degrees **/ - double height; /**< Elevation in meters **/ - double radius; /**< Position accuracy in meters **/ - int fix_type; /**< Type of last fix (1 = valid, 0 = invalid) **/ - time_t fix_time; /**< Timestamp of last fix (not used) **/ - char fixiso8601[128]; /**< Timestamp of last fix in ISO 8601 format **/ - int sats; /**< Number of satellites in view **/ - int sats_used; /**< Number of satellites used in fix **/ - int valid; /**< Whether the vehicle coordinates in {@code geo} are valid **/ - struct attr ** attrs; - struct callback *pcb; /**< The callback function for position updates **/ - struct callback *scb; /**< The callback function for status updates **/ - struct callback *fcb; /**< The callback function for fix status updates **/ - jclass NavitVehicleClass; /**< The {@code NavitVehicle} class **/ - jobject NavitVehicle; /**< An instance of {@code NavitVehicle} **/ - jclass LocationClass; /**< Android's {@code Location} class **/ - jmethodID Location_getLatitude, Location_getLongitude, Location_getSpeed, Location_getBearing, Location_getAltitude, Location_getTime, Location_getAccuracy; + struct callback_list *cbl; + struct coord_geo geo; /**< The last known position of the vehicle **/ + double speed; /**< Speed in km/h **/ + double direction; /**< Bearing in degrees **/ + double height; /**< Elevation in meters **/ + double radius; /**< Position accuracy in meters **/ + int fix_type; /**< Type of last fix (1 = valid, 0 = invalid) **/ + time_t fix_time; /**< Timestamp of last fix (not used) **/ + char fixiso8601[128]; /**< Timestamp of last fix in ISO 8601 format **/ + int sats; /**< Number of satellites in view **/ + int sats_used; /**< Number of satellites used in fix **/ + int valid; /**< Whether the vehicle coordinates in {@code geo} are valid **/ + struct attr ** attrs; + struct callback *pcb; /**< The callback function for position updates **/ + struct callback *scb; /**< The callback function for status updates **/ + struct callback *fcb; /**< The callback function for fix status updates **/ + jclass NavitVehicleClass; /**< The {@code NavitVehicle} class **/ + jobject NavitVehicle; /**< An instance of {@code NavitVehicle} **/ + jclass LocationClass; /**< Android's {@code Location} class **/ + jmethodID Location_getLatitude, Location_getLongitude, Location_getSpeed, Location_getBearing, Location_getAltitude, + Location_getTime, Location_getAccuracy; }; /** * @brief Free the android_vehicle - * + * * @param priv vehicle_priv structure for the vehicle * @returns nothing */ static void -vehicle_android_destroy(struct vehicle_priv *priv) -{ - dbg(lvl_debug,"enter"); - g_free(priv); +vehicle_android_destroy(struct vehicle_priv *priv) { + dbg(lvl_debug,"enter"); + g_free(priv); } /** @@ -86,53 +86,52 @@ vehicle_android_destroy(struct vehicle_priv *priv) */ static int vehicle_android_position_attr_get(struct vehicle_priv *priv, - enum attr_type type, struct attr *attr) -{ - dbg(lvl_debug,"enter %s",attr_to_name(type)); - switch (type) { - case attr_position_fix_type: - attr->u.num = priv->fix_type; - break; - case attr_position_height: - attr->u.numd = &priv->height; - break; - case attr_position_speed: - attr->u.numd = &priv->speed; - break; - case attr_position_direction: - attr->u.numd = &priv->direction; - break; - case attr_position_radius: - attr->u.numd = &priv->radius; - break; - case attr_position_qual: - attr->u.num = priv->sats; - break; - case attr_position_sats_used: - attr->u.num = priv->sats_used; - break; - case attr_position_coord_geo: - attr->u.coord_geo = &priv->geo; - if (priv->valid == attr_position_valid_invalid) - return 0; - break; - case attr_position_time_iso8601: - attr->u.str=priv->fixiso8601; - break; - case attr_position_valid: - attr->u.num = priv->valid; - break; - default: - return 0; - } - dbg(lvl_debug,"ok"); - attr->type = type; - return 1; + enum attr_type type, struct attr *attr) { + dbg(lvl_debug,"enter %s",attr_to_name(type)); + switch (type) { + case attr_position_fix_type: + attr->u.num = priv->fix_type; + break; + case attr_position_height: + attr->u.numd = &priv->height; + break; + case attr_position_speed: + attr->u.numd = &priv->speed; + break; + case attr_position_direction: + attr->u.numd = &priv->direction; + break; + case attr_position_radius: + attr->u.numd = &priv->radius; + break; + case attr_position_qual: + attr->u.num = priv->sats; + break; + case attr_position_sats_used: + attr->u.num = priv->sats_used; + break; + case attr_position_coord_geo: + attr->u.coord_geo = &priv->geo; + if (priv->valid == attr_position_valid_invalid) + return 0; + break; + case attr_position_time_iso8601: + attr->u.str=priv->fixiso8601; + break; + case attr_position_valid: + attr->u.num = priv->valid; + break; + default: + return 0; + } + dbg(lvl_debug,"ok"); + attr->type = type; + return 1; } struct vehicle_methods vehicle_android_methods = { - vehicle_android_destroy, - vehicle_android_position_attr_get, + vehicle_android_destroy, + vehicle_android_position_attr_get, }; /** @@ -145,25 +144,25 @@ struct vehicle_methods vehicle_android_methods = { */ static void vehicle_android_position_callback(struct vehicle_priv *v, jobject location) { - time_t tnow; - struct tm *tm; - dbg(lvl_debug,"enter"); + time_t tnow; + struct tm *tm; + dbg(lvl_debug,"enter"); - v->geo.lat = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getLatitude); - v->geo.lng = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getLongitude); - v->speed = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getSpeed)*3.6; - v->direction = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getBearing); - v->height = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getAltitude); - v->radius = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getAccuracy); - tnow=(*jnienv)->CallLongMethod(jnienv, location, v->Location_getTime)/1000; - tm = gmtime(&tnow); - strftime(v->fixiso8601, sizeof(v->fixiso8601), "%Y-%m-%dT%TZ", tm); - dbg(lvl_debug,"lat %f lon %f time %s",v->geo.lat,v->geo.lng,v->fixiso8601); - if (v->valid != attr_position_valid_valid) { - v->valid = attr_position_valid_valid; - callback_list_call_attr_0(v->cbl, attr_position_valid); - } - callback_list_call_attr_0(v->cbl, attr_position_coord_geo); + v->geo.lat = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getLatitude); + v->geo.lng = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getLongitude); + v->speed = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getSpeed)*3.6; + v->direction = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getBearing); + v->height = (*jnienv)->CallDoubleMethod(jnienv, location, v->Location_getAltitude); + v->radius = (*jnienv)->CallFloatMethod(jnienv, location, v->Location_getAccuracy); + tnow=(*jnienv)->CallLongMethod(jnienv, location, v->Location_getTime)/1000; + tm = gmtime(&tnow); + strftime(v->fixiso8601, sizeof(v->fixiso8601), "%Y-%m-%dT%TZ", tm); + dbg(lvl_debug,"lat %f lon %f time %s",v->geo.lat,v->geo.lng,v->fixiso8601); + if (v->valid != attr_position_valid_valid) { + v->valid = attr_position_valid_valid; + callback_list_call_attr_0(v->cbl, attr_position_valid); + } + callback_list_call_attr_0(v->cbl, attr_position_coord_geo); } /** @@ -182,14 +181,14 @@ vehicle_android_position_callback(struct vehicle_priv *v, jobject location) { */ static void vehicle_android_status_callback(struct vehicle_priv *v, int sats_in_view, int sats_used) { - if (v->sats != sats_in_view) { - v->sats = sats_in_view; - callback_list_call_attr_0(v->cbl, attr_position_qual); - } - if (v->sats_used != sats_used) { - v->sats_used = sats_used; - callback_list_call_attr_0(v->cbl, attr_position_sats_used); - } + if (v->sats != sats_in_view) { + v->sats = sats_in_view; + callback_list_call_attr_0(v->cbl, attr_position_qual); + } + if (v->sats_used != sats_used) { + v->sats_used = sats_used; + callback_list_call_attr_0(v->cbl, attr_position_sats_used); + } } /** @@ -202,14 +201,14 @@ vehicle_android_status_callback(struct vehicle_priv *v, int sats_in_view, int sa */ static void vehicle_android_fix_callback(struct vehicle_priv *v, int fix_type) { - if (v->fix_type != fix_type) { - v->fix_type = fix_type; - callback_list_call_attr_0(v->cbl, attr_position_fix_type); - if (!fix_type && (v->valid == attr_position_valid_valid)) { - v->valid = attr_position_valid_extrapolated_time; - callback_list_call_attr_0(v->cbl, attr_position_valid); - } - } + if (v->fix_type != fix_type) { + v->fix_type = fix_type; + callback_list_call_attr_0(v->cbl, attr_position_fix_type); + if (!fix_type && (v->valid == attr_position_valid_valid)) { + v->valid = attr_position_valid_extrapolated_time; + callback_list_call_attr_0(v->cbl, attr_position_valid); + } + } } /** @@ -218,49 +217,48 @@ vehicle_android_fix_callback(struct vehicle_priv *v, int fix_type) { * @return True on success, false on failure */ static int -vehicle_android_init(struct vehicle_priv *ret) -{ - jmethodID cid; +vehicle_android_init(struct vehicle_priv *ret) { + jmethodID cid; - if (!android_find_class_global("android/location/Location", &ret->LocationClass)) - return 0; - if (!android_find_method(ret->LocationClass, "getLatitude", "()D", &ret->Location_getLatitude)) - return 0; - if (!android_find_method(ret->LocationClass, "getLongitude", "()D", &ret->Location_getLongitude)) - return 0; - if (!android_find_method(ret->LocationClass, "getSpeed", "()F", &ret->Location_getSpeed)) - return 0; - if (!android_find_method(ret->LocationClass, "getBearing", "()F", &ret->Location_getBearing)) - return 0; - if (!android_find_method(ret->LocationClass, "getAltitude", "()D", &ret->Location_getAltitude)) - return 0; - if (!android_find_method(ret->LocationClass, "getTime", "()J", &ret->Location_getTime)) - return 0; - if (!android_find_method(ret->LocationClass, "getAccuracy", "()F", &ret->Location_getAccuracy)) - return 0; - if (!android_find_class_global("org/navitproject/navit/NavitVehicle", &ret->NavitVehicleClass)) - return 0; - dbg(lvl_debug,"at 3"); - cid = (*jnienv)->GetMethodID(jnienv, ret->NavitVehicleClass, "<init>", "(Landroid/content/Context;III)V"); - if (cid == NULL) { - dbg(lvl_error,"no method found"); - return 0; /* exception thrown */ - } - dbg(lvl_debug, "at 4 android_activity=%p", android_activity); - ret->NavitVehicle=(*jnienv)->NewObject(jnienv, ret->NavitVehicleClass, cid, android_activity, - (int) ret->pcb, (int) ret->scb, (int) ret->fcb); - dbg(lvl_debug,"result=%p",ret->NavitVehicle); - if (!ret->NavitVehicle) - return 0; - if (ret->NavitVehicle) - ret->NavitVehicle = (*jnienv)->NewGlobalRef(jnienv, ret->NavitVehicle); + if (!android_find_class_global("android/location/Location", &ret->LocationClass)) + return 0; + if (!android_find_method(ret->LocationClass, "getLatitude", "()D", &ret->Location_getLatitude)) + return 0; + if (!android_find_method(ret->LocationClass, "getLongitude", "()D", &ret->Location_getLongitude)) + return 0; + if (!android_find_method(ret->LocationClass, "getSpeed", "()F", &ret->Location_getSpeed)) + return 0; + if (!android_find_method(ret->LocationClass, "getBearing", "()F", &ret->Location_getBearing)) + return 0; + if (!android_find_method(ret->LocationClass, "getAltitude", "()D", &ret->Location_getAltitude)) + return 0; + if (!android_find_method(ret->LocationClass, "getTime", "()J", &ret->Location_getTime)) + return 0; + if (!android_find_method(ret->LocationClass, "getAccuracy", "()F", &ret->Location_getAccuracy)) + return 0; + if (!android_find_class_global("org/navitproject/navit/NavitVehicle", &ret->NavitVehicleClass)) + return 0; + dbg(lvl_debug,"at 3"); + cid = (*jnienv)->GetMethodID(jnienv, ret->NavitVehicleClass, "<init>", "(Landroid/content/Context;III)V"); + if (cid == NULL) { + dbg(lvl_error,"no method found"); + return 0; /* exception thrown */ + } + dbg(lvl_debug, "at 4 android_activity=%p", android_activity); + ret->NavitVehicle=(*jnienv)->NewObject(jnienv, ret->NavitVehicleClass, cid, android_activity, + (int) ret->pcb, (int) ret->scb, (int) ret->fcb); + dbg(lvl_debug,"result=%p",ret->NavitVehicle); + if (!ret->NavitVehicle) + return 0; + if (ret->NavitVehicle) + ret->NavitVehicle = (*jnienv)->NewGlobalRef(jnienv, ret->NavitVehicle); - return 1; + return 1; } /** * @brief Create android_vehicle - * + * * @param meth * @param cbl * @param attrs @@ -268,36 +266,34 @@ vehicle_android_init(struct vehicle_priv *ret) */ static struct vehicle_priv * vehicle_android_new_android(struct vehicle_methods *meth, - struct callback_list *cbl, - struct attr **attrs) -{ - struct vehicle_priv *ret; + struct callback_list *cbl, + struct attr **attrs) { + struct vehicle_priv *ret; - dbg(lvl_debug, "enter"); - ret = g_new0(struct vehicle_priv, 1); - ret->cbl = cbl; - ret->pcb = callback_new_1(callback_cast(vehicle_android_position_callback), ret); - ret->scb = callback_new_1(callback_cast(vehicle_android_status_callback), ret); - ret->fcb = callback_new_1(callback_cast(vehicle_android_fix_callback), ret); - ret->valid = attr_position_valid_invalid; - ret->sats = 0; - ret->sats_used = 0; - *meth = vehicle_android_methods; - vehicle_android_init(ret); - dbg(lvl_debug, "return"); - return ret; + dbg(lvl_debug, "enter"); + ret = g_new0(struct vehicle_priv, 1); + ret->cbl = cbl; + ret->pcb = callback_new_1(callback_cast(vehicle_android_position_callback), ret); + ret->scb = callback_new_1(callback_cast(vehicle_android_status_callback), ret); + ret->fcb = callback_new_1(callback_cast(vehicle_android_fix_callback), ret); + ret->valid = attr_position_valid_invalid; + ret->sats = 0; + ret->sats_used = 0; + *meth = vehicle_android_methods; + vehicle_android_init(ret); + dbg(lvl_debug, "return"); + return ret; } /** * @brief register vehicle_android - * + * * @returns nothing */ void -plugin_init(void) -{ - dbg(lvl_debug, "enter"); - plugin_register_category_vehicle("android", vehicle_android_new_android); +plugin_init(void) { + dbg(lvl_debug, "enter"); + plugin_register_category_vehicle("android", vehicle_android_new_android); } /** @} */ |