Skip to content

Commit 92fc7a2

Browse files
committed
remove context from RoadLabelSurfaceLayer constructor
1 parent ac8d25e commit 92fc7a2

6 files changed

Lines changed: 39 additions & 29 deletions

File tree

libnavui-androidauto/api/current.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,12 @@ package com.mapbox.androidauto.navigation.roadlabel {
499499
}
500500

501501
public final class RoadLabelRenderer {
502-
ctor public RoadLabelRenderer(android.content.res.Resources resources);
503-
method public android.graphics.Bitmap? render(java.util.List<com.mapbox.navigation.base.road.model.RoadComponent> road, java.util.List<? extends com.mapbox.navigation.ui.shield.model.RouteShield> shields, com.mapbox.androidauto.navigation.roadlabel.RoadLabelOptions options = RoadLabelOptions.default);
502+
ctor public RoadLabelRenderer();
503+
method public android.graphics.Bitmap? render(android.content.res.Resources resources, java.util.List<com.mapbox.navigation.base.road.model.RoadComponent> road, java.util.List<? extends com.mapbox.navigation.ui.shield.model.RouteShield> shields, com.mapbox.androidauto.navigation.roadlabel.RoadLabelOptions options = RoadLabelOptions.default);
504504
}
505505

506506
public final class RoadLabelSurfaceLayer implements com.mapbox.maps.extension.androidauto.MapboxCarMapObserver {
507-
ctor public RoadLabelSurfaceLayer(androidx.car.app.CarContext carContext);
507+
ctor public RoadLabelSurfaceLayer();
508508
}
509509

510510
public abstract class RoadNameObserver implements com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver {

libnavui-androidauto/src/androidTest/java/com/mapbox/androidauto/car/navigation/roadlabel/RoadLabelRendererTest.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ class RoadLabelRendererTest {
4040
"test_road_label_images"
4141
)
4242

43-
private val roadLabelBitmapRenderer =
44-
RoadLabelRenderer(InstrumentationRegistry.getInstrumentation().context.resources)
43+
private val roadLabelBitmapRenderer = RoadLabelRenderer()
44+
private val resources = InstrumentationRegistry.getInstrumentation().context.resources
4545

4646
@Test
4747
fun street_with_name() {
4848
val bitmap = roadLabelBitmapRenderer.render(
49+
resources,
4950
createRoad("Pennsylvania Avenue"),
5051
emptyList(),
5152
RoadLabelOptions.Builder()
@@ -59,6 +60,7 @@ class RoadLabelRendererTest {
5960
@Test
6061
fun street_with_numbers() {
6162
val bitmap = roadLabelBitmapRenderer.render(
63+
resources,
6264
createRoad("11th Street"),
6365
emptyList(),
6466
RoadLabelOptions.Builder()
@@ -72,6 +74,7 @@ class RoadLabelRendererTest {
7274
@Test
7375
fun very_long_street_name() {
7476
val bitmap = roadLabelBitmapRenderer.render(
77+
resources,
7578
createRoad(
7679
"Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhen" +
7780
"uakitanatahu"
@@ -88,6 +91,7 @@ class RoadLabelRendererTest {
8891
@Test
8992
fun blue_label_without_shadow() {
9093
val bitmap = roadLabelBitmapRenderer.render(
94+
resources,
9195
createRoad("Eu Tong Sen Street"),
9296
emptyList(),
9397
RoadLabelOptions.Builder()
@@ -106,6 +110,7 @@ class RoadLabelRendererTest {
106110
val byteArray = context.assets.open("shield.svg").use { it.readBytes() }
107111
val mapboxShield = mockk<MapboxShield>()
108112
val bitmap = roadLabelBitmapRenderer.render(
113+
resources,
109114
listOf(
110115
createComponent("Clarksburg Road"),
111116
createComponent("/"),

libnavui-androidauto/src/main/java/com/mapbox/androidauto/freedrive/FreeDriveCarScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class FreeDriveCarScreen @UiThread constructor(
3333
initialCarCameraMode = CarCameraMode.FOLLOWING,
3434
alternativeCarCameraMode = null,
3535
)
36-
private val roadLabelSurfaceLayer = RoadLabelSurfaceLayer(carContext)
36+
private val roadLabelSurfaceLayer = RoadLabelSurfaceLayer()
3737
private val mapActionStripBuilder = MapboxMapActionStrip(this, carNavigationCamera)
3838

3939
init {

libnavui-androidauto/src/main/java/com/mapbox/androidauto/navigation/ActiveGuidanceScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal class ActiveGuidanceScreen constructor(
3737
initialCarCameraMode = CarCameraMode.FOLLOWING,
3838
alternativeCarCameraMode = CarCameraMode.OVERVIEW,
3939
)
40-
private val roadLabelSurfaceLayer = RoadLabelSurfaceLayer(carContext)
40+
private val roadLabelSurfaceLayer = RoadLabelSurfaceLayer()
4141
private val navigationInfoProvider = CarNavigationInfoProvider()
4242
.invalidateOnChange(this)
4343
private val carActiveGuidanceMarkers = CarActiveGuidanceMarkers()

libnavui-androidauto/src/main/java/com/mapbox/androidauto/navigation/roadlabel/RoadLabelRenderer.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ import kotlin.math.roundToInt
1212
/**
1313
* This class will a road name and create a bitmap that fits the text.
1414
*/
15-
class RoadLabelRenderer(private val resources: Resources) {
15+
class RoadLabelRenderer {
1616

1717
/**
1818
* Render [road] and [shields] to a [Bitmap]
1919
*/
2020
fun render(
21+
resources: Resources,
2122
road: List<RoadComponent>,
2223
shields: List<RouteShield>,
2324
options: RoadLabelOptions = RoadLabelOptions.default
2425
): Bitmap? {
2526
if (road.isEmpty()) return null
2627
textPaint.color = options.textColor
27-
val components = measureRoadLabel(road, shields)
28+
val components = measureRoadLabel(resources, road, shields)
2829
val spaceWidth = textPaint.measureText(" ").roundToInt()
2930
val width = components.sumOf { component ->
3031
when (component) {
@@ -54,17 +55,22 @@ class RoadLabelRenderer(private val resources: Resources) {
5455
}
5556

5657
private fun measureRoadLabel(
58+
resources: Resources,
5759
road: List<RoadComponent>,
5860
shields: List<RouteShield>
5961
): List<Component> {
6062
return road.map { component ->
61-
getShieldBitmap(component, shields)
63+
getShieldBitmap(resources, component, shields)
6264
?.let { Component.Shield(it) }
6365
?: Component.Text(component.text, getTextBounds(component.text))
6466
}
6567
}
6668

67-
private fun getShieldBitmap(component: RoadComponent, shields: List<RouteShield>): Bitmap? {
69+
private fun getShieldBitmap(
70+
resources: Resources,
71+
component: RoadComponent,
72+
shields: List<RouteShield>,
73+
): Bitmap? {
6874
val shield = component.shield?.let { shield ->
6975
shields.find { it is RouteShield.MapboxDesignedShield && it.compareWith(shield) }
7076
} ?: component.imageBaseUrl?.let { baseUrl ->

libnavui-androidauto/src/main/java/com/mapbox/androidauto/navigation/roadlabel/RoadLabelSurfaceLayer.kt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class RoadLabelSurfaceLayer internal constructor(
3737
private val delegate: RoadLabelSurfaceLayerDelegate,
3838
) : MapboxCarMapObserver {
3939

40-
constructor(carContext: CarContext) : this(RoadLabelSurfaceLayerDelegate(carContext))
40+
constructor() : this(RoadLabelSurfaceLayerDelegate())
4141

4242
override fun onAttached(mapboxCarMapSurface: MapboxCarMapSurface) {
4343
delegate.onAttached(mapboxCarMapSurface)
@@ -57,25 +57,14 @@ class RoadLabelSurfaceLayer internal constructor(
5757
}
5858

5959
@OptIn(MapboxExperimental::class)
60-
internal class RoadLabelSurfaceLayerDelegate(
61-
private val carContext: CarContext,
62-
) : CarSurfaceLayer() {
60+
internal class RoadLabelSurfaceLayerDelegate : CarSurfaceLayer() {
6361

64-
private val roadLabelRenderer = RoadLabelRenderer(carContext.resources)
62+
private val roadLabelRenderer = RoadLabelRenderer()
6563
private val carTextLayerHost = CarTextLayerHost()
6664
private val routeShieldApi = MapboxRouteShieldApi()
6765
private val mapUserStyleObserver = MapUserStyleObserver()
6866
private var styleLoadedListener: OnStyleLoadedListener? = null
69-
70-
private val roadNameObserver = object : RoadNameObserver(
71-
routeShieldApi,
72-
mapUserStyleObserver
73-
) {
74-
override fun onRoadUpdate(road: List<RoadComponent>, shields: List<RouteShield>) {
75-
val bitmap = roadLabelRenderer.render(road, shields, roadLabelOptions())
76-
carTextLayerHost.offerBitmap(bitmap)
77-
}
78-
}
67+
private var roadNameObserver: RoadNameObserver? = null
7968

8069
override fun children(): List<MapboxCarMapObserver> = listOf(carTextLayerHost.mapScene)
8170

@@ -94,11 +83,20 @@ internal class RoadLabelSurfaceLayerDelegate(
9483
logAndroidAutoFailure("Add custom layer exception $it")
9584
}
9685
}
86+
val carContext = mapboxCarMapSurface.carContext
87+
val roadNameObserver = object : RoadNameObserver(routeShieldApi, mapUserStyleObserver) {
88+
override fun onRoadUpdate(road: List<RoadComponent>, shields: List<RouteShield>) {
89+
val options = roadLabelOptions(carContext)
90+
val bitmap = roadLabelRenderer.render(carContext.resources, road, shields, options)
91+
carTextLayerHost.offerBitmap(bitmap)
92+
}
93+
}.also { roadNameObserver = it }
9794
styleLoadedListener = mapboxCarMapSurface.handleStyleOnAttached {
9895
val bitmap = roadLabelRenderer.render(
96+
carContext.resources,
9997
roadNameObserver.currentRoad,
10098
roadNameObserver.currentShields,
101-
roadLabelOptions()
99+
roadLabelOptions(carContext)
102100
)
103101
carTextLayerHost.offerBitmap(bitmap)
104102
}
@@ -111,13 +109,14 @@ internal class RoadLabelSurfaceLayerDelegate(
111109
logAndroidAuto("RoadLabelSurfaceLayer carMapSurface detached")
112110
mapboxCarMapSurface.handleStyleOnDetached(styleLoadedListener)
113111
?.removeStyleLayer(CAR_NAVIGATION_VIEW_LAYER_ID)
114-
MapboxNavigationApp.unregisterObserver(roadNameObserver)
112+
roadNameObserver?.let { MapboxNavigationApp.unregisterObserver(it) }
113+
roadNameObserver = null
115114
routeShieldApi.cancel()
116115
mapUserStyleObserver.onDetached(mapboxCarMapSurface)
117116
super.onDetached(mapboxCarMapSurface)
118117
}
119118

120-
private fun roadLabelOptions(): RoadLabelOptions =
119+
private fun roadLabelOptions(carContext: CarContext): RoadLabelOptions =
121120
if (carContext.isDarkMode) {
122121
DARK_OPTIONS
123122
} else {

0 commit comments

Comments
 (0)