forked from cyclestreets/android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoute.kt
261 lines (222 loc) · 7.25 KB
/
Route.kt
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
259
260
261
package net.cyclestreets.routing
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import android.widget.Toast
import net.cyclestreets.CycleStreetsPreferences
import net.cyclestreets.content.RouteData
import net.cyclestreets.content.RouteDatabase
import net.cyclestreets.content.RouteSummary
import net.cyclestreets.routing.Journey.Companion.NULL_JOURNEY
import net.cyclestreets.routing.Journey.Companion.loadFromJson
import net.cyclestreets.util.Logging
import net.cyclestreets.view.R
import java.util.*
object Route {
private val TAG = Logging.getTag(Route::class.java)
private val listeners_ = Listeners()
@JvmStatic
fun registerListener(l: Listener) {
listeners_.register(l)
}
fun softRegisterListener(l: Listener) {
listeners_.softRegister(l)
}
@JvmStatic
fun unregisterListener(l: Listener) {
listeners_.unregister(l)
}
@JvmStatic
fun PlotRoute(plan: String,
speed: Int,
context: Context,
waypoints: Waypoints) {
val query = CycleStreetsRoutingTask(plan, speed, context)
query.execute(waypoints)
}
@JvmStatic
fun plotCircularRoute(plan: String,
distance: Int?,
duration: Int?,
pois: String?,
context: Context) {
val query = CycleStreetsRoutingTask(plan, 0, context, distance, duration, pois)
query.execute(waypoints_)
}
fun LiveReplanRoute(plan: String,
speed: Int,
context: Context,
waypoints: Waypoints) {
val query = LiveRideReplanRoutingTask(plan, speed, context)
query.execute(waypoints)
}
@JvmStatic
fun FetchRoute(plan: String,
itinerary: Long,
speed: Int,
context: Context) {
val query = FetchCycleStreetsRouteTask(plan, speed, context)
query.execute(itinerary)
}
fun RePlotRoute(plan: String,
context: Context) {
val query = ReplanRoutingTask(plan, db_, context)
query.execute(plannedRoute_)
}
@JvmStatic
fun PlotStoredRoute(localId: Int,
context: Context) {
val query = StoredRoutingTask(db_, context)
query.execute(localId)
}
@JvmStatic
fun RenameRoute(localId: Int, newName: String) {
db_.renameRoute(localId, newName)
}
@JvmStatic
fun DeleteRoute(localId: Int) {
db_.deleteRoute(localId)
}
/////////////////////////////////////////
private var plannedRoute_ = NULL_JOURNEY
private var waypoints_ = plannedRoute_.waypoints
private lateinit var db_: RouteDatabase
private lateinit var context_: Context
@JvmStatic
fun initialise(context: Context) {
context_ = context
db_ = RouteDatabase(context)
if (isLoaded)
loadLastJourney()
else
restoreWaypoints()
}
fun resetJourney() {
onNewJourney(null)
}
fun onPause(waypoints: Waypoints) {
if (!isLoaded) {
waypoints_ = waypoints
stashWaypoints()
}
}
fun onResume() {
Segment.formatter = DistanceFormatter.formatter(CycleStreetsPreferences.units())
}
/////////////////////////////////////
fun storedCount(): Int {
return db_.routeCount()
}
@JvmStatic
fun storedRoutes(): List<RouteSummary> {
return db_.savedRoutes()
}
/////////////////////////////////////
fun onNewJourney(route: RouteData?): Boolean {
try {
doOnNewJourney(route)
return true
} catch (e: Exception) {
Log.w(TAG, "Route finding failed", e)
Toast.makeText(context_, R.string.route_finding_failed, Toast.LENGTH_LONG).show()
}
return false
}
private fun doOnNewJourney(route: RouteData?) {
if (route == null) {
plannedRoute_ = NULL_JOURNEY
waypoints_ = NULL_WAYPOINTS
listeners_.onReset()
clearRouteLoaded()
return
}
plannedRoute_ = loadFromJson(route.json(), route.points(), route.name())
db_.saveRoute(plannedRoute_, route.json())
waypoints_ = plannedRoute_.waypoints
listeners_.onNewJourney(plannedRoute_, waypoints_)
setRouteLoaded()
}
fun waypoints(): Waypoints {
return waypoints_
}
@JvmStatic
fun routeAvailable(): Boolean {
return plannedRoute_ != NULL_JOURNEY
}
@JvmStatic
fun journey(): Journey {
return plannedRoute_
}
private fun loadLastJourney() {
val routeSummaries = storedRoutes()
if (!storedRoutes().isEmpty()) {
val lastRoute = routeSummaries[0]
val route = db_.route(lastRoute.localId())
onNewJourney(route)
}
}
private fun setRouteLoaded() {
prefs()
.edit()
.putBoolean(routeLoadedPref, true)
.remove(waypointsInProgressPref)
.apply()
}
private fun clearRouteLoaded() {
prefs()
.edit()
.remove(routeLoadedPref)
.apply()
}
private fun stashWaypoints() {
val stash = RouteDatabase.serializeWaypoints(waypoints_)
prefs()
.edit()
.putString(waypointsInProgressPref, stash)
.apply()
}
private fun restoreWaypoints() {
val stash = prefs().getString(waypointsInProgressPref, "")
if (stash != null && stash.isNotEmpty()) {
waypoints_ = Waypoints(RouteDatabase.deserializeWaypoints(stash))
}
}
private val isLoaded: Boolean
get() = prefs().getBoolean(routeLoadedPref, false)
private const val routeLoadedPref = "route"
private const val waypointsInProgressPref = "waypoints-in-progress"
private fun prefs(): SharedPreferences {
return context_.getSharedPreferences("net.cyclestreets.CycleStreets", Context.MODE_PRIVATE)
}
interface Listener {
fun onNewJourney(journey: Journey, waypoints: Waypoints)
fun onResetJourney()
}
private class Listeners {
private val listeners_: MutableList<Listener> = ArrayList()
fun register(listener: Listener) {
if (!doRegister(listener)) return
if (journey() != NULL_JOURNEY || waypoints() != NULL_WAYPOINTS)
listener.onNewJourney(journey(), waypoints())
else
listener.onResetJourney()
}
fun softRegister(listener: Listener) {
doRegister(listener)
}
private fun doRegister(listener: Listener): Boolean {
if (listeners_.contains(listener)) return false
listeners_.add(listener)
return true
}
fun unregister(listener: Listener) {
listeners_.remove(listener)
}
fun onNewJourney(journey: Journey, waypoints: Waypoints) {
listeners_.forEach { it.onNewJourney(journey, waypoints) }
}
fun onReset() {
listeners_.forEach { it.onResetJourney() }
}
}
}