summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormvglasow <michael -at- vonglasow.com>2018-07-19 22:58:07 +0200
committermvglasow <michael -at- vonglasow.com>2018-07-20 22:19:36 +0200
commit328801a3bbfbeec61a7772e7f3ec14fc7e71ed90 (patch)
tree749ffa01b3cb73d5c81b65b50230a5c10356c581
parent289f53510a5a98ac3eb4ddc7dd005c71ca588729 (diff)
downloadnavit-328801a3bbfbeec61a7772e7f3ec14fc7e71ed90.tar.gz
Add:traffic/traff_android:Complete TraFF feed processing functionality
Signed-off-by: mvglasow <michael -at- vonglasow.com>
-rw-r--r--navit/android.c11
-rw-r--r--navit/android/src/org/navitproject/navit/NavitTraff.java101
-rw-r--r--navit/traffic/traff_android/traffic_traff_android.c75
3 files changed, 186 insertions, 1 deletions
diff --git a/navit/android.c b/navit/android.c
index d595cd43c..9b06ebb44 100644
--- a/navit/android.c
+++ b/navit/android.c
@@ -172,6 +172,17 @@ JNIEXPORT void JNICALL Java_org_navitproject_navit_NavitSensors_SensorCallback(
callback_call_4((struct callback *)id, sensor, &x, &y, &z);
}
+JNIEXPORT void JNICALL Java_org_navitproject_navit_NavitTraff_onFeedReceived(JNIEnv * env, jobject thiz, int id,
+ jstring feed) {
+ const char *s;
+ s = (*env)->GetStringUTFChars(env, feed, NULL);
+ if (id)
+ callback_call_1((struct callback *) id, s);
+ (*env)->ReleaseStringUTFChars(env, feed, s);
+}
+
+
+
// type: 0=town, 1=street, 2=House#
void android_return_search_result(struct jni_object *jni_o, int type, struct pcoord *location, const char *address) {
struct coord_geo geo_location;
diff --git a/navit/android/src/org/navitproject/navit/NavitTraff.java b/navit/android/src/org/navitproject/navit/NavitTraff.java
new file mode 100644
index 000000000..d7e94a78b
--- /dev/null
+++ b/navit/android/src/org/navitproject/navit/NavitTraff.java
@@ -0,0 +1,101 @@
+/**
+ * Navit, a modular navigation system.
+ * Copyright (C) 2005-2018 Navit Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+package org.navitproject.navit;
+
+import java.util.List;
+
+import android.Manifest;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.util.Log;
+
+/**
+ * @brief The TraFF receiver implementation
+ */
+public class NavitTraff extends BroadcastReceiver {
+ /* TODO change names to their final values (officially registered non-personal FQDN) */
+ public static String ACTION_TRAFF_FEED = "com.vonglasow.michael.traff.FEED";
+
+ public static String ACTION_TRAFF_POLL = "com.vonglasow.michael.traff.POLL";
+
+ public static String EXTRA_FEED = "feed";
+
+ /** Identifier for the callback function */
+ private int cbid;
+
+ private Context context = null;
+
+ /** An intent filter for TraFF events */
+ private IntentFilter traffFilter = new IntentFilter();
+
+ /**
+ * @brief Called when a TraFF feed is received.
+ *
+ * @param id The identifier for the native callback implementation
+ * @param feed The TraFF feed
+ */
+ public native void onFeedReceived(int id, String feed);
+
+ /**
+ * @brief Creates a new {@code NavitTraff} instance.
+ *
+ * @param context The context
+ * @param cbid The callback identifier for the native method to call upon receiving a feed
+ */
+ NavitTraff(Context context, int cbid) {
+ this.context = context;
+ this.cbid = cbid;
+
+ traffFilter.addAction(ACTION_TRAFF_FEED);
+ traffFilter.addAction(ACTION_TRAFF_POLL);
+
+ context.registerReceiver(this, traffFilter);
+ /* TODO unregister receiver on exit */
+
+ /* Broadcast a poll intent */
+ Intent outIntent = new Intent(ACTION_TRAFF_POLL);
+ PackageManager pm = context.getPackageManager();
+ List<ResolveInfo> receivers = pm.queryBroadcastReceivers(outIntent, 0);
+ if (receivers != null)
+ for (ResolveInfo receiver : receivers) {
+ ComponentName cn = new ComponentName(receiver.activityInfo.applicationInfo.packageName,
+ receiver.activityInfo.name);
+ outIntent = new Intent(ACTION_TRAFF_POLL);
+ outIntent.setComponent(cn);
+ context.sendBroadcast(outIntent, Manifest.permission.ACCESS_COARSE_LOCATION);
+ }
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if ((intent != null) && (intent.getAction().equals(ACTION_TRAFF_FEED))) {
+ String feed = intent.getStringExtra(EXTRA_FEED);
+ if (feed == null)
+ Log.w(this.getClass().getSimpleName(), "empty feed, ignoring");
+ else
+ onFeedReceived(cbid, feed);
+ }
+ }
+}
diff --git a/navit/traffic/traff_android/traffic_traff_android.c b/navit/traffic/traff_android/traffic_traff_android.c
index 71392076e..7e99af3a5 100644
--- a/navit/traffic/traff_android/traffic_traff_android.c
+++ b/navit/traffic/traff_android/traffic_traff_android.c
@@ -33,9 +33,11 @@
#endif
#include "glib_slice.h"
#include "config.h"
-#include "coord.h"
#include "item.h"
+#include "attr.h"
+#include "coord.h"
#include "xmlconfig.h"
+#include "android.h"
#include "traffic.h"
#include "plugin.h"
#include "debug.h"
@@ -45,6 +47,9 @@
*/
struct traffic_priv {
struct navit * nav; /**< The navit instance */
+ struct callback * cbid; /**< The callback function for TraFF feeds **/
+ jclass NavitTraffClass; /**< The `NavitTraff` class */
+ jobject NavitTraff; /**< An instance of `NavitTraff` */
};
struct traffic_message ** traffic_traff_android_get_messages(struct traffic_priv * this_);
@@ -65,6 +70,69 @@ static struct traffic_methods traffic_traff_android_meth = {
traffic_traff_android_get_messages,
};
+
+/**
+ * @brief Called when a new TraFF feed is received.
+ *
+ * @param this_ Private data for the module instance
+ * @param feed Feed data in string form
+ */
+static void traffic_traff_android_on_feed_received(struct traffic_priv * this_, char * feed) {
+ struct attr * attr;
+ struct attr_iter * a_iter;
+ struct traffic * traffic = NULL;
+ struct traffic_message ** messages;
+
+ dbg(lvl_debug, "enter");
+ attr = g_new0(struct attr, 1);
+ a_iter = navit_attr_iter_new();
+ if (navit_get_attr(this_->nav, attr_traffic, attr, a_iter))
+ traffic = (struct traffic *) attr->u.navit_object;
+ navit_attr_iter_destroy(a_iter);
+ g_free(attr);
+
+ if (!traffic) {
+ dbg(lvl_error, "failed to obtain traffic instance");
+ return;
+ }
+
+ dbg(lvl_debug, "processing traffic feed");
+ messages = traffic_get_messages_from_xml_string(traffic, feed);
+ if (messages) {
+ dbg(lvl_debug, "got messages from feed, processing");
+ traffic_process_messages(traffic, messages);
+ g_free(messages);
+ }
+}
+
+
+/**
+ * @brief Initializes a traff_android plugin
+ *
+ * @return True on success, false on failure
+ */
+static int traffic_traff_android_init(struct traffic_priv * this_) {
+ jmethodID cid;
+
+ if (!android_find_class_global("org/navitproject/navit/NavitTraff", &this_->NavitTraffClass))
+ return 0;
+ cid = (*jnienv)->GetMethodID(jnienv, this_->NavitTraffClass, "<init>", "(Landroid/content/Context;I)V");
+ if (cid == NULL) {
+ dbg(lvl_error,"no method found");
+ return 0; /* exception thrown */
+ }
+ this_->NavitTraff=(*jnienv)->NewObject(jnienv, this_->NavitTraffClass, cid, android_activity,
+ (int) this_->cbid);
+ dbg(lvl_debug,"result=%p", this_->NavitTraff);
+ if (!this_->NavitTraff)
+ return 0;
+ if (this_->NavitTraff)
+ this_->NavitTraff = (*jnienv)->NewGlobalRef(jnienv, this_->NavitTraff);
+
+ return 1;
+}
+
+
/**
* @brief Registers a new traff_android traffic plugin
*
@@ -82,8 +150,13 @@ static struct traffic_priv * traffic_traff_android_new(struct navit *nav, struct
dbg(lvl_debug, "enter");
ret = g_new0(struct traffic_priv, 1);
+ ret->nav = nav;
+ ret->cbid = callback_new_1(callback_cast(traffic_traff_android_on_feed_received), ret);
+ /* TODO populate members, if any */
*meth = traffic_traff_android_meth;
+ traffic_traff_android_init(ret);
+
return ret;
}