Skip to content

Commit 19eec76

Browse files
stanis-kgithub-actions[bot]
authored andcommitted
Extend MapSurface with createPlugin and removePlugin methods (#15000)
I'm making Android Auto integration great again. The last update was made in 2023. I'm working on adding support for the new road camera module. Road Cameras are working as a [Map Plugin](https://github.com/mapbox/mapbox-sdk/blob/6ad1970e3b9147cc4a5e28d080c430ee0a31d1bc/projects/navigation-android/roadcam-ui/src/main/java/com/mapbox/roadcam/ui/internal/RoadCamerasMapControllerImpl.kt#L425). In Android Auto, we don't use MapView, but instead use MapSurface. This PR adds the missing `createPlugin` and `removePlugin` methods to the MapSurface class. cc @mapbox/maps-android cc @mapbox/navnative GitOrigin-RevId: 3a5c56bb64d87e92644217991b9daaae121f1cf8
1 parent 8fa81e8 commit 19eec76

5 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎maps-sdk/api/Release/metalava.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ package com.mapbox.maps {
8585
method public void addRendererSetupErrorListener(com.mapbox.maps.renderer.RendererSetupErrorListener rendererSetupErrorListener);
8686
method @com.mapbox.maps.MapboxExperimental public void addWidget(com.mapbox.maps.renderer.widget.Widget widget);
8787
method @MainThread @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX) public void clearMaximumFps();
88+
method public void createPlugin(com.mapbox.maps.plugin.Plugin plugin);
8889
method public android.content.Context getContext();
8990
method public com.mapbox.maps.MapboxMap getMapboxMap();
9091
method @Deprecated @kotlin.jvm.JvmSynthetic public com.mapbox.maps.MapboxMap! getMapboxMap();
@@ -101,6 +102,7 @@ package com.mapbox.maps {
101102
method public boolean onTouchEvent(android.view.MotionEvent event);
102103
method public void queueEvent(Runnable event, boolean needRender);
103104
method @MainThread @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX) public void removeOnMaximumFpsChangedListener(com.mapbox.maps.renderer.OnMaximumFpsChangedListener listener);
105+
method public void removePlugin(String id);
104106
method public void removeRendererSetupErrorListener(com.mapbox.maps.renderer.RendererSetupErrorListener rendererSetupErrorListener);
105107
method @com.mapbox.maps.MapboxExperimental public boolean removeWidget(com.mapbox.maps.renderer.widget.Widget widget);
106108
method @MainThread public void setMaximumFps(int fps);

‎maps-sdk/api/maps-sdk.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public final class com/mapbox/maps/MapSurface : com/mapbox/maps/MapControllable,
8282
public fun addRendererSetupErrorListener (Lcom/mapbox/maps/renderer/RendererSetupErrorListener;)V
8383
public fun addWidget (Lcom/mapbox/maps/renderer/widget/Widget;)V
8484
public final fun clearMaximumFps ()V
85+
public final fun createPlugin (Lcom/mapbox/maps/plugin/Plugin;)V
8586
public final fun getContext ()Landroid/content/Context;
8687
public fun getMapboxMap ()Lcom/mapbox/maps/MapboxMap;
8788
public final synthetic fun getMapboxMapDeprecated ()Lcom/mapbox/maps/MapboxMap;
@@ -98,6 +99,7 @@ public final class com/mapbox/maps/MapSurface : com/mapbox/maps/MapControllable,
9899
public fun onTouchEvent (Landroid/view/MotionEvent;)Z
99100
public fun queueEvent (Ljava/lang/Runnable;Z)V
100101
public final fun removeOnMaximumFpsChangedListener (Lcom/mapbox/maps/renderer/OnMaximumFpsChangedListener;)V
102+
public final fun removePlugin (Ljava/lang/String;)V
101103
public fun removeRendererSetupErrorListener (Lcom/mapbox/maps/renderer/RendererSetupErrorListener;)V
102104
public fun removeWidget (Lcom/mapbox/maps/renderer/widget/Widget;)Z
103105
public fun setMaximumFps (I)V

‎maps-sdk/src/main/java/com/mapbox/maps/MapSurface.kt‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.annotation.RestrictTo
1111
import androidx.annotation.VisibleForTesting
1212
import androidx.annotation.VisibleForTesting.Companion.PRIVATE
1313
import com.mapbox.maps.plugin.MapPlugin
14+
import com.mapbox.maps.plugin.Plugin
1415
import com.mapbox.maps.plugin.delegates.MapPluginProviderDelegate
1516
import com.mapbox.maps.renderer.*
1617
import com.mapbox.maps.renderer.widget.Widget
@@ -346,7 +347,7 @@ class MapSurface : MapPluginProviderDelegate, MapControllable {
346347
/**
347348
* Add an instance of [RendererSetupErrorListener].
348349
*
349-
* Please note that errors could be already reported from the renderer during [MapView] creation
350+
* Please note that errors could be already reported from the renderer during [MapSurface] creation
350351
* before this method will be called - all accumulated renderer errors will be delivered.
351352
*/
352353
override fun addRendererSetupErrorListener(rendererSetupErrorListener: RendererSetupErrorListener) {
@@ -360,6 +361,25 @@ class MapSurface : MapPluginProviderDelegate, MapControllable {
360361
mapController.removeRendererSetupErrorListener(rendererSetupErrorListener)
361362
}
362363

364+
/**
365+
* Create a new plugin instance that will be added to the map.
366+
* Only one instance of [Plugin.instance] with given [Plugin.id] can exist for given [MapSurface].
367+
*
368+
* @param plugin instance of [Plugin] that will be added to the [MapSurface].
369+
*/
370+
fun createPlugin(
371+
plugin: Plugin
372+
) = mapController.createPlugin(null, plugin)
373+
374+
/**
375+
* Remove the plugin instance that will be removed from the map.
376+
*
377+
* Removing the plugin from [MapSurface] will result in [MapPlugin.cleanup] being called and existing
378+
* events subscription cancelled.
379+
*
380+
* @param id the id of the plugin to be removed
381+
*/
382+
fun removePlugin(id: String) = mapController.removePlugin(id)
363383
/**
364384
* Get the plugin instance.
365385
*

‎maps-sdk/src/test/java/com/mapbox/maps/MapSurfaceTest.kt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.view.MotionEvent
88
import android.view.Surface
99
import android.view.WindowManager
1010
import com.mapbox.maps.plugin.MapPlugin
11+
import com.mapbox.maps.plugin.Plugin
1112
import com.mapbox.maps.renderer.MapboxSurfaceRenderer
1213
import com.mapbox.maps.renderer.OnFpsChangedListener
1314
import com.mapbox.maps.renderer.RendererSetupErrorListener
@@ -264,6 +265,20 @@ class MapSurfaceTest {
264265
verifyOnce { mapController.removeRendererSetupErrorListener(listener) }
265266
}
266267

268+
@Test
269+
fun createPlugin() {
270+
every { mapController.createPlugin(any(), any()) } returns mockk()
271+
val plugin = Plugin.Custom("id", mockk())
272+
mapSurface.createPlugin(plugin)
273+
verify { mapController.createPlugin(null, plugin) }
274+
}
275+
276+
@Test
277+
fun removePlugin() {
278+
mapSurface.removePlugin("id")
279+
verify { mapController.removePlugin("id") }
280+
}
281+
267282
@Test
268283
fun getPluginTest() {
269284
val pluginId = "no_such_plugin"

‎maps-sdk/src/test/java/com/mapbox/maps/MapViewTest.kt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ class MapViewTest {
218218
verify { mapController.createPlugin(mapView, plugin) }
219219
}
220220

221+
@Test
222+
fun removePlugin() {
223+
mapView.removePlugin("id")
224+
verify { mapController.removePlugin("id") }
225+
}
226+
221227
@Test
222228
fun getPlugin() {
223229
every { mapController.getPlugin<MapPlugin>(any()) } returns mockk()

0 commit comments

Comments
 (0)