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
|
/**
* 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 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;
import java.util.List;
/**
* @brief The TraFF receiver implementation.
*
* This class registers the broadcast receiver for TraFF feeds, polls all registered sources once on creation, receives
* TraFF feeds and forwards them to the traffic module for processing.
*/
public class NavitTraff extends BroadcastReceiver {
public static String ACTION_TRAFF_FEED = "org.traffxml.traff.FEED";
public static String ACTION_TRAFF_POLL = "org.traffxml.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 Forwards a newly received TraFF feed to the traffic module for processing.
*
* This is 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.
*
* Creating a new {@code NavitTraff} instance registers a broadcast receiver for TraFF broadcasts and polls all
* registered sources once to ensure we have messages which were received by these sources before we started up.
*
* @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);
}
}
}
|