/
AprsService.scala
259 lines (219 loc) · 7.78 KB
/
AprsService.scala
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package de.duenndns.aprsdroid
import _root_.android.app.Service
import _root_.android.content.{Context, Intent}
import _root_.android.location._
import _root_.android.os.{Bundle, IBinder, Handler}
import _root_.android.preference.PreferenceManager
import _root_.android.util.Log
import _root_.android.widget.Toast
object AprsService {
val SERVICE = "de.duenndns.aprsdroid.SERVICE"
val SERVICE_ONCE = "de.duenndns.aprsdroid.ONCE"
val UPDATE = "de.duenndns.aprsdroid.UPDATE"
val LOCATION = "de.duenndns.aprsdroid.LOCATION"
val STATUS = "de.duenndns.aprsdroid.STATUS"
val PACKET = "de.duenndns.aprsdroid.PACKET"
def intent(ctx : Context, action : String) : Intent = {
new Intent(action, null, ctx, classOf[AprsService])
}
var running = false
implicit def block2runnable(block: => Unit) =
new Runnable() {
def run() { block }
}
}
class AprsService extends Service with LocationListener {
import AprsService._
val TAG = "AprsService"
lazy val prefs = PreferenceManager.getDefaultSharedPreferences(this)
lazy val locMan = getSystemService(Context.LOCATION_SERVICE).asInstanceOf[LocationManager]
val handler = new Handler()
lazy val db = StorageDatabase.open(this)
var poster : AprsIsUploader = null
var singleShot = false
var lastLoc : Location = null
var awaitingSpdCourse : Location = null
override def onStart(i : Intent, startId : Int) {
Log.d(TAG, "onStart: " + i + ", " + startId);
super.onStart(i, startId)
handleStart(i)
}
override def onStartCommand(i : Intent, flags : Int, startId : Int) : Int = {
Log.d(TAG, "onStartCommand: " + i + ", " + flags + ", " + startId);
handleStart(i)
Service.START_REDELIVER_INTENT
}
def handleStart(i : Intent) {
running = true
// get update interval and distance
val upd_int = prefs.getString("interval", "10").toInt
val upd_dist = prefs.getString("distance", "10").toInt
// display notification (even though we are not actually started yet,
// but we need this to prevent error message reordering)
awaitingSpdCourse = null
if (i.getAction() == SERVICE_ONCE) {
lastLoc = null // for singleshot mode, ignore last post
singleShot = true
showToast(getString(R.string.service_once))
} else
showToast(getString(R.string.service_start).format(upd_int, upd_dist))
// the poster needs to be running before location updates come in
startPoster()
if (prefs.getBoolean("netloc", false)) {
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
upd_int * 60000, upd_dist * 1000, this)
}
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,
upd_int * 60000, upd_dist * 1000, this)
val callssid = AprsPacket.formatCallSsid(prefs.getString("callsign", null), prefs.getString("ssid", ""))
val message = "%s: %d min, %d km".format(callssid, upd_int, upd_dist)
ServiceNotifier.instance.start(this, message)
}
def startPoster() {
var hostname = prefs.getString("host", null)
if (hostname == null || hostname == "")
hostname = getString(R.string.aprs_server);
val login = AprsPacket.formatLogin(prefs.getString("callsign", null).trim(),
prefs.getString("ssid", null), prefs.getString("passcode", null))
val filterdist = prefs.getString("filterdist", "10").toInt
prefs.getString("conntype", "http") match {
case "udp" =>
poster = new UdpUploader(hostname, login)
case "http" =>
poster = new HttpPostUploader(hostname, login)
case "tcp" =>
poster = new TcpUploader(this, hostname, login, " filter m/%d".format(filterdist))
case "afsk" =>
poster = new AfskUploader(hostname, login)
case _ =>
stopSelf()
}
poster.start()
}
override def onBind(i : Intent) : IBinder = null
override def onUnbind(i : Intent) : Boolean = false
def showToast(msg : String) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
addPost(StorageDatabase.Post.TYPE_INFO, null, msg)
}
override def onDestroy() {
locMan.removeUpdates(this);
// catch FC when service is killed from outside
if (poster != null) {
poster.stop()
showToast(getString(R.string.service_stop))
}
ServiceNotifier.instance.stop(this)
running = false
}
def speedBearingStart() {
Log.d(TAG, "switching to fast lane");
// request fast update rate
locMan.removeUpdates(this);
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, this)
handler.postDelayed({ speedBearingEnd(true) }, 30000)
}
def speedBearingEnd(post : Boolean) {
if (!running)
return;
Log.d(TAG, "switching to slow lane");
if (post && awaitingSpdCourse != null) {
Log.d(TAG, "speedBearingEnd: posting " + awaitingSpdCourse);
postLocation(awaitingSpdCourse)
}
awaitingSpdCourse = null
// reset update speed
locMan.removeUpdates(this);
val upd_int = prefs.getString("interval", "10").toInt
val upd_dist = prefs.getString("distance", "10").toInt
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,
upd_int, upd_dist, this)
}
def checkSpeedBearing(location : Location) : Boolean = {
val hasSpdBrg = (location.hasBearing && location.hasSpeed)
if (!hasSpdBrg) {
if (awaitingSpdCourse == null)
speedBearingStart()
awaitingSpdCourse = location
return false
} else if (awaitingSpdCourse != null && hasSpdBrg) {
speedBearingEnd(false)
}
true
}
// LocationListener interface
override def onLocationChanged(location : Location) {
Log.d(TAG, "onLocationChanged: " + location)
val upd_int = prefs.getString("interval", "10").toInt * 60000
val upd_dist = prefs.getString("distance", "10").toInt * 1000
if (lastLoc != null &&
(location.getTime - lastLoc.getTime < upd_int ||
location.distanceTo(lastLoc) < upd_dist)) {
Log.d(TAG, "onLocationChanged: ignoring premature location")
return
}
// check if we have speed and course
val speedbrg = prefs.getBoolean("speedbrg", false)
if (speedbrg && location.getProvider == LocationManager.GPS_PROVIDER) {
if (!checkSpeedBearing(location))
return
}
postLocation(location)
}
def postLocation(location : Location) {
lastLoc = location
val i = new Intent(UPDATE)
i.putExtra(LOCATION, location)
val callsign = prefs.getString("callsign", null).trim()
val callssid = AprsPacket.formatCallSsid(callsign, prefs.getString("ssid", ""))
var symbol = prefs.getString("symbol", "")
if (symbol.length != 2)
symbol = getString(R.string.default_symbol)
val status = prefs.getString("status", getString(R.string.default_status))
val packet = AprsPacket.formatLoc(callssid, symbol, status, location)
Log.d(TAG, "packet: " + packet)
val result = try {
val status = poster.update(packet)
i.putExtra(STATUS, status)
i.putExtra(PACKET, packet)
val prec_status = "%s (±%dm)".format(status, location.getAccuracy.asInstanceOf[Int])
addPost(StorageDatabase.Post.TYPE_POST, prec_status, packet)
prec_status
} catch {
case e : Exception =>
i.putExtra(PACKET, e.getMessage())
addPost(StorageDatabase.Post.TYPE_ERROR, "Error", e.getMessage())
e.getMessage()
}
if (singleShot) {
singleShot = false
stopSelf()
} else {
val message = "%s: %s".format(callssid, result)
ServiceNotifier.instance.start(this, message)
}
}
override def onProviderDisabled(provider : String) {
Log.d(TAG, "onProviderDisabled: " + provider)
}
override def onProviderEnabled(provider : String) {
Log.d(TAG, "onProviderEnabled: " + provider)
}
override def onStatusChanged(provider : String, st: Int, extras : Bundle) {
Log.d(TAG, "onStatusChanged: " + provider)
}
def addPost(t : Int, status : String, message : String) {
db.addPost(System.currentTimeMillis(), t, status, message)
sendBroadcast(new Intent(UPDATE).putExtra(STATUS, message))
}
def postSubmit(post : String) {
handler.post { addPost(StorageDatabase.Post.TYPE_INCMG, "incoming", post) }
}
def postAbort(post : String) {
handler.post {
addPost(StorageDatabase.Post.TYPE_ERROR, "Error", post)
stopSelf()
}
}
}