summaryrefslogtreecommitdiff
path: root/navit/traffic/traff_android/traffic_traff_android.c
blob: 91d4087776e71802767c5a2dba72b9deb9bc1d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * 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.
 */

/**
 * @file traffic_traff_android.c
 *
 * @brief The TraFF plugin for Android
 *
 * This plugin receives TraFF feeds via Android broadcasts.
 */

#include <string.h>
#include <time.h>

#ifdef _POSIX_C_SOURCE
#include <sys/types.h>
#endif
#include "glib_slice.h"
#include "config.h"
#include "item.h"
#include "attr.h"
#include "coord.h"
#include "xmlconfig.h"
#include "android.h"
#include "traffic.h"
#include "plugin.h"
#include "callback.h"
#include "debug.h"

/**
 * @brief Stores information about the plugin instance.
 */
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_);

/**
 * @brief Returns an empty traffic report.
 *
 * @return Always `NULL`
 */
struct traffic_message ** traffic_traff_android_get_messages(struct traffic_priv * this_) {
    return NULL;
}

/**
 * @brief The methods implemented by this plugin
 */
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:\n%s", 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_application,
                                           (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
 *
 * @param nav The navit instance
 * @param meth Receives the traffic methods
 * @param attrs The attributes for the map
 * @param cbl
 *
 * @return A pointer to a `traffic_priv` structure for the plugin instance
 */
static struct traffic_priv * traffic_traff_android_new(struct navit *nav, struct traffic_methods *meth,
        struct attr **attrs, struct callback_list *cbl) {
    struct traffic_priv *ret;

    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;
}

/**
 * @brief Initializes the traffic plugin.
 *
 * This function is called once on startup.
 */
void plugin_init(void) {
    dbg(lvl_debug, "enter");

    plugin_register_category_traffic("traff_android", traffic_traff_android_new);
}