Skip to content

Commit b5fe4f1

Browse files
authored
fix(android): stop unnecessary hidden location puck rendering (#4432)
* fix(android): stop unnecessary hidden location puck rendering * test(android): cover hidden puck transitions in existing JVM suite * chore(android): keep CI unchanged for hidden puck fix * fix(android): simplify hidden puck handling after review
1 parent ab9322d commit b5fe4f1

4 files changed

Lines changed: 107 additions & 13 deletions

File tree

‎.github/workflows/android-actions.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
- run: yarn postinstall
7979
working-directory: example
8080

81-
- run: ./gradlew assemble
81+
- run: ./gradlew assemble :rnmapbox_maps:testDebugUnitTest
8282
working-directory: example/android
8383
env:
8484
CI_MAP_IMPL: ${{ inputs.MAP_IMPL }}

‎android/src/main/java/com/rnmapbox/rnmbx/components/location/LocationComponentManager.kt‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ import com.rnmapbox.rnmbx.v11compat.location.PuckBearing
3131
* LocationComponentManager attempts to separate that, so that Camera can ask for location tracking independent of display of user current location.
3232
* And NativeUserLocation can ask for display of user's current location - independent of Camera's user tracking.
3333
*/
34-
class LocationComponentManager(mapView: RNMBXMapView, context: Context) {
34+
class LocationComponentManager(
35+
mapView: RNMBXMapView,
36+
context: Context,
37+
private val locationManager: LocationManager = LocationManager.getInstance(context),
38+
) {
3539
private val MAPBOX_BLUE_COLOR = Color.parseColor("#4A90E2")
3640

3741
var mMapView = mapView
@@ -47,8 +51,6 @@ class LocationComponentManager(mapView: RNMBXMapView, context: Context) {
4751
scale = 1.0,
4852
)
4953

50-
private var mLocationManager: LocationManager = LocationManager.getInstance(context)
51-
5254
private var mNeedsFullUpdate = true
5355

5456
private var mLocationManagerStarted = false
@@ -187,29 +189,29 @@ class LocationComponentManager(mapView: RNMBXMapView, context: Context) {
187189

188190
private fun startLocationManager() {
189191
if (!mLocationManagerStarted) {
190-
mLocationManager.startCounted()
192+
locationManager.startCounted()
191193
mLocationManagerStarted = true
192194
}
193195
}
194196

195197
private fun stopLocationManager() {
196198
if (mLocationManagerStarted) {
197-
mLocationManager.stopCounted()
199+
locationManager.stopCounted()
198200
mLocationManagerStarted = false
199201
}
200202
}
201203

202204
private fun useMapLocationProvider(mapView: RNMBXMapView) {
203205
val provider = mapView.mapPlugins.location.getLocationProvider()
204206
if (provider != null) {
205-
mLocationManager.provider = provider
207+
locationManager.provider = provider
206208
}
207209
}
208210

209211

210-
fun showNativeUserLocation(showUserLocation: Boolean) {
212+
fun showNativeUserLocation(showUserLocation: Boolean, visible: Boolean) {
211213
update {
212-
it.copy(showUserLocation = showUserLocation, nativeUserLocation= showUserLocation)
214+
it.copy(showUserLocation = showUserLocation && visible, nativeUserLocation = showUserLocation)
213215
}
214216
}
215217

‎android/src/main/java/com/rnmapbox/rnmbx/components/location/RNMBXNativeUserLocation.kt‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
135135
}
136136

137137
private fun _apply(mapView: MapView) {
138+
mMapView?.locationComponentManager?.showNativeUserLocation(mEnabled, visible)
138139
val location2 = mapView.location2;
139140

140141
val locationPuck3D = model?.let { makeLocationPuck3D(it) }
@@ -202,6 +203,9 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
202203
}
203204
}
204205
}
206+
if (!visible) {
207+
location2.pulsingEnabled = false
208+
}
205209
}
206210

207211
private fun makeLocationPuck3D(model: ReadableMap): LocationPuck3D? {
@@ -227,15 +231,14 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
227231
mEnabled = true
228232
mapView.getMapboxMap()
229233
mapView.getMapAsync(this)
230-
mMapView?.locationComponentManager?.showNativeUserLocation(true)
231234
mMBXMapView = mapView
232235
_fetchImages(mapView)
233236
_apply()
234237
}
235238

236239
override fun removeFromMap(mapView: RNMBXMapView, reason: RemovalReason): Boolean {
237240
mEnabled = false
238-
mMapView?.locationComponentManager?.showNativeUserLocation(false)
241+
mMapView?.locationComponentManager?.showNativeUserLocation(false, visible)
239242
mMap?.getStyle(this)
240243
mMBXMapView = null
241244
return super.removeFromMap(mapView, reason)
@@ -256,7 +259,7 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
256259
}
257260

258261
mMapView?.locationComponentManager?.update()
259-
mMapView?.locationComponentManager?.showNativeUserLocation(mEnabled)
262+
mMapView?.locationComponentManager?.showNativeUserLocation(mEnabled, visible)
260263
}
261264

262265
// region fetch images and subscribe on updates
@@ -338,4 +341,4 @@ fun makeDefaultLocationPuck2D(context: Context, renderMode: RenderMode): Locatio
338341
LR.drawable.mapbox_user_icon_shadow
339342
)
340343
);
341-
}
344+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.rnmapbox.rnmbx.components.location
2+
3+
import android.content.Context
4+
import android.graphics.Color
5+
import android.os.Handler
6+
import android.os.Looper
7+
import androidx.lifecycle.Lifecycle
8+
import com.mapbox.maps.plugin.LocationPuck2D
9+
import com.mapbox.maps.plugin.Plugin
10+
import com.mapbox.maps.plugin.delegates.MapPluginProviderDelegate
11+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin
12+
import com.mapbox.maps.plugin.locationcomponent.generated.LocationComponentSettings
13+
import com.rnmapbox.rnmbx.components.mapview.RNMBXMapView
14+
import com.rnmapbox.rnmbx.location.LocationManager
15+
import org.junit.jupiter.api.Assertions.*
16+
import org.junit.jupiter.api.Test
17+
import org.mockito.Mockito
18+
import org.mockito.kotlin.*
19+
20+
class LocationComponentManagerTest {
21+
private class Fixture {
22+
val location: LocationComponentPlugin = mock()
23+
val provider: MapPluginProviderDelegate = mock()
24+
val map: RNMBXMapView = mock()
25+
val locationManager: LocationManager = mock()
26+
var settings = LocationComponentSettings.Builder(LocationPuck2D()).build()
27+
val manager: LocationComponentManager
28+
29+
init {
30+
whenever(provider.getPlugin<LocationComponentPlugin>(Plugin.MAPBOX_LOCATION_COMPONENT_PLUGIN_ID))
31+
.thenReturn(location)
32+
whenever(map.mapPlugins).thenReturn(provider)
33+
whenever(map.getLifecycleState()).thenReturn(Lifecycle.State.STARTED)
34+
doAnswer { invocation ->
35+
val update = invocation.getArgument<LocationComponentSettings.Builder.() -> Unit>(0)
36+
settings = settings.toBuilder().apply(update).build()
37+
null
38+
}.whenever(location).updateSettings(any())
39+
manager = LocationComponentManager(map, mock<Context>(), locationManager)
40+
}
41+
}
42+
43+
private fun withManager(test: Fixture.() -> Unit) {
44+
Mockito.mockStatic(Color::class.java).use {
45+
Mockito.mockStatic(Looper::class.java).use {
46+
Mockito.mockConstruction(Handler::class.java).use {
47+
Fixture().test()
48+
}
49+
}
50+
}
51+
}
52+
53+
@Test
54+
fun `hidden puck stays disabled until following starts and stops again`() = withManager {
55+
manager.showNativeUserLocation(true, visible = false)
56+
assertFalse(settings.enabled)
57+
verify(locationManager, never()).startCounted()
58+
59+
manager.setFollowLocation(true)
60+
assertTrue(settings.enabled)
61+
verify(locationManager).startCounted()
62+
63+
manager.setFollowLocation(false)
64+
assertFalse(settings.enabled)
65+
verify(locationManager).stopCounted()
66+
}
67+
68+
@Test
69+
fun `hiding a puck preserves active following`() = withManager {
70+
manager.showNativeUserLocation(true, visible = true)
71+
manager.setFollowLocation(true)
72+
manager.showNativeUserLocation(true, visible = false)
73+
assertTrue(settings.enabled)
74+
verify(locationManager, never()).stopCounted()
75+
}
76+
77+
@Test
78+
fun `repeated hide show cycles balance location subscriptions`() = withManager {
79+
repeat(20) {
80+
manager.showNativeUserLocation(true, visible = true)
81+
assertTrue(settings.enabled)
82+
manager.showNativeUserLocation(true, visible = false)
83+
assertFalse(settings.enabled)
84+
}
85+
verify(locationManager, times(20)).startCounted()
86+
verify(locationManager, times(20)).stopCounted()
87+
}
88+
89+
}

0 commit comments

Comments
 (0)