-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapboxNavigationViewManager.kt
More file actions
214 lines (183 loc) · 7.31 KB
/
MapboxNavigationViewManager.kt
File metadata and controls
214 lines (183 loc) · 7.31 KB
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
package ch.jls.reactnative.mapboxnavigation
import android.content.pm.PackageManager
import android.util.Log
import android.view.Choreographer
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.FragmentActivity
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.annotations.ReactProp
import com.mapbox.common.MapboxOptions
import com.mapbox.geojson.Point
class MapboxNavigationViewManager(private var reactContext: ReactApplicationContext) :
ViewGroupManager<FrameLayout>() {
private var accessToken: String? = null
private var mapboxNavigationFragment: MapboxNavigationFragment? = null
/**
* Navigation destination point
*/
private var destination: Point? = null
/**
* Navigation origin point
*/
private var origin: Point? = null
/**
* List of waypoints for the navigation to follow
*/
private var waypoints: List<Point> = ArrayList()
/**
* Whether navigation should be simulated
*/
private var shouldSimulateRoute = false
/**
* Whether feedback should be shown by mapbox when navigation is finished
*/
private var shouldShowEndOfRouteFeedback = false
/**
* Whether the voice should be muted
*/
private var isVoiceInstructionsMuted = false
companion object {
private const val REACT_CLASS = "MapboxNavigation"
private const val COMMAND_CREATE = 1
}
init {
reactContext.runOnUiQueueThread {
try {
val app = reactContext.packageManager.getApplicationInfo(
reactContext.packageName,
PackageManager.GET_META_DATA
)
val bundle = app.metaData
val accessToken = bundle.getString("MAPBOX_ACCESS_TOKEN")
this.accessToken = accessToken
if (accessToken != null) {
MapboxOptions.accessToken = accessToken
}
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
}
}
override fun getName() = REACT_CLASS
override fun createViewInstance(reactContext: ThemedReactContext): FrameLayout {
return FrameLayout(reactContext)
}
override fun onDropViewInstance(view: FrameLayout) {
super.onDropViewInstance(view)
Log.d("MapboxNavigation", "Dropping view instance")
if (reactContext.currentActivity == null) {
Log.d("MapboxNavigation", "Activity is already null, no need to remove fragment")
return
}
val activity = reactContext.currentActivity as FragmentActivity
activity.supportFragmentManager
.beginTransaction()
.remove(this.mapboxNavigationFragment!!)
.commit()
Log.d("MapboxNavigation", "Fragment removed")
}
override fun getCommandsMap(): Map<String, Int> {
return mapOf("create" to COMMAND_CREATE)
}
override fun receiveCommand(root: FrameLayout, commandId: String?, args: ReadableArray?) {
super.receiveCommand(root, commandId, args)
val reactNativeViewId = requireNotNull(args).getInt(0)
Log.d("MapboxNavigation", "Received command: $commandId with viewId: $reactNativeViewId")
when (requireNotNull(commandId).toInt()) {
COMMAND_CREATE -> createFragment(root, reactNativeViewId)
}
}
private fun createFragment(root: FrameLayout, reactNativeViewId: Int) {
val parentView = root.findViewById<ViewGroup>(reactNativeViewId)
setupLayout(parentView)
this.mapboxNavigationFragment = MapboxNavigationFragment(this.reactContext)
this.mapboxNavigationFragment!!.setOrigin(this.origin)
this.mapboxNavigationFragment!!.setDestination(this.destination)
this.mapboxNavigationFragment!!.resetWaypoints()
this.waypoints.forEach { this.mapboxNavigationFragment!!.addWaypoint(it) }
this.mapboxNavigationFragment!!.setShouldSimulateRoute(this.shouldSimulateRoute)
this.mapboxNavigationFragment!!.setShouldShowEndOfRouteFeedback(this.shouldShowEndOfRouteFeedback)
this.mapboxNavigationFragment!!.setMute(this.isVoiceInstructionsMuted)
val activity = reactContext.currentActivity as FragmentActivity
activity.supportFragmentManager
.beginTransaction()
.replace(reactNativeViewId, this.mapboxNavigationFragment!!, reactNativeViewId.toString())
.commit()
Log.d("MapboxNavigation", "Fragment created: ${this.mapboxNavigationFragment}")
}
private fun setupLayout(parentView: View) {
Choreographer.getInstance().postFrameCallback(object : Choreographer.FrameCallback {
override fun doFrame(frameTimeNanos: Long) {
layoutChildView(parentView)
parentView.viewTreeObserver.dispatchOnGlobalLayout()
Choreographer.getInstance().postFrameCallback(this)
}
})
}
private fun layoutChildView(parentView: View) {
val fragmentView = this.mapboxNavigationFragment?.view ?: return
val parentWidth = parentView.width
val parentHeight = parentView.height
// When using this fragment approach, this is the only way to ensure that the ConstraintLayout
// used at the root of navigation_view.xml is properly sized.
fragmentView.measure(
View.MeasureSpec.makeMeasureSpec(parentWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(parentHeight, View.MeasureSpec.EXACTLY)
)
fragmentView.layout(0, 0, parentWidth, parentHeight)
}
@ReactProp(name = "destination")
fun setDestination(view: FrameLayout, destination: ReadableArray?) {
if (destination == null) {
this.destination = null
this.mapboxNavigationFragment?.setDestination(null)
return
}
this.destination = Point.fromLngLat(destination!!.getDouble(0), destination!!.getDouble(1))
this.mapboxNavigationFragment?.setDestination(this.destination)
}
@ReactProp(name = "origin")
fun setOrigin(view: FrameLayout, origin: ReadableArray?) {
if (origin == null) {
this.origin = null
this.mapboxNavigationFragment?.setOrigin(null)
return
}
this.origin = Point.fromLngLat(origin!!.getDouble(0), origin!!.getDouble(1))
this.mapboxNavigationFragment?.setOrigin(this.origin)
}
@ReactProp(name = "waypoints")
fun setWaypoints(view: FrameLayout, waypoints: ReadableArray?) {
this.mapboxNavigationFragment?.resetWaypoints()
if (waypoints == null) {
this.waypoints = ArrayList()
return
}
for (i in 0 until waypoints.size()) {
val entry = waypoints.getArray(i)
val point = Point.fromLngLat(entry!!.getDouble(0), entry!!.getDouble(1))
this.waypoints += point
this.mapboxNavigationFragment?.addWaypoint(point)
}
}
@ReactProp(name = "shouldSimulateRoute")
fun setShouldSimulateRoute(view: FrameLayout, shouldSimulateRoute: Boolean) {
this.shouldSimulateRoute = shouldSimulateRoute
this.mapboxNavigationFragment?.setShouldSimulateRoute(shouldSimulateRoute)
}
@ReactProp(name = "shouldShowEndOfRouteFeedback")
fun setShowsEndOfRouteFeedback(view: FrameLayout, shouldShowEndOfRouteFeedback: Boolean) {
this.shouldShowEndOfRouteFeedback = shouldShowEndOfRouteFeedback
this.mapboxNavigationFragment?.setShouldShowEndOfRouteFeedback(shouldShowEndOfRouteFeedback)
}
@ReactProp(name = "mute")
fun setMute(view: FrameLayout, mute: Boolean) {
this.isVoiceInstructionsMuted = mute
this.mapboxNavigationFragment?.setMute(mute)
}
}