Skip to content

Commit d099d83

Browse files
natiginfogithub-actions[bot]
authored andcommitted
[maps-android] add delay before calling resetThreadServiceType + handle onResume (#4975)
GitOrigin-RevId: 855abf51f8b147193cbff5280a8839cc83decc99
1 parent 1d811f2 commit d099d83

File tree

18 files changed

+88
-4
lines changed

18 files changed

+88
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Mapbox welcomes participation and contributions from everyone.
1010
# 11.14.0-rc.1
1111
## Features ✨ and improvements 🏁
1212
* Added `setContentDescription()` method to `AttributionPlugin` and `AttributionView` interfaces to programmatically set accessibility content description for the attribution button.
13+
* Added `MapView.onResume()` which should be called in `onResume()` of the host activity or fragment to resume the map view if `plugin-lifecycle` is not used.
1314

1415
## Bug fixes 🐞
1516
* Fix exception when accessing enum properties in annotations.

extension-compose/src/main/java/com/mapbox/maps/extension/compose/internal/MapViewLifecycle.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ internal fun MapViewLifecycle(mapView: MapView) {
5353
private fun MapView.lifecycleEventObserver(): LifecycleEventObserver =
5454
LifecycleEventObserver { _, event ->
5555
when (event) {
56-
Lifecycle.Event.ON_CREATE, Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_RESUME, Lifecycle.Event.ON_DESTROY -> {
56+
Lifecycle.Event.ON_CREATE, Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_DESTROY -> {
5757
// no-ops, onDestroy handled in DisposeEffect.onDispose
5858
}
5959
Lifecycle.Event.ON_START -> this.onStart()
6060
Lifecycle.Event.ON_STOP -> this.onStop()
61+
Lifecycle.Event.ON_RESUME -> this.onResume()
6162

6263
else -> throw IllegalStateException()
6364
}

maps-sdk/api/Release/metalava.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ package com.mapbox.maps {
9090
method public void onDestroy();
9191
method public boolean onGenericMotionEvent(android.view.MotionEvent event);
9292
method public void onLowMemory();
93+
method public void onResume();
9394
method public void onSizeChanged(int w, int h);
9495
method public void onStart();
9596
method public void onStop();
@@ -126,6 +127,7 @@ package com.mapbox.maps {
126127
method public static final boolean isTerrainRenderingSupported();
127128
method public void onDestroy();
128129
method public void onLowMemory();
130+
method public void onResume();
129131
method public void onSizeChanged(int w, int h, int oldw, int oldh);
130132
method public void onSizeChanged(int w, int h);
131133
method public void onStart();

maps-sdk/api/maps-sdk.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public final class com/mapbox/maps/MapSurface : com/mapbox/maps/MapControllable,
8686
public fun onDestroy ()V
8787
public fun onGenericMotionEvent (Landroid/view/MotionEvent;)Z
8888
public fun onLowMemory ()V
89+
public fun onResume ()V
8990
public fun onSizeChanged (II)V
9091
public fun onStart ()V
9192
public fun onStop ()V
@@ -126,6 +127,7 @@ public class com/mapbox/maps/MapView : android/widget/FrameLayout, com/mapbox/ma
126127
public fun onGenericMotionEvent (Landroid/view/MotionEvent;)Z
127128
public fun onInterceptTouchEvent (Landroid/view/MotionEvent;)Z
128129
public fun onLowMemory ()V
130+
public fun onResume ()V
129131
public fun onSizeChanged (II)V
130132
public fun onSizeChanged (IIII)V
131133
public fun onStart ()V

maps-sdk/src/main/java/com/mapbox/maps/MapController.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ internal class MapController : MapPluginProviderDelegate, MapControllable {
198198
MapProvider.flushPendingEvents()
199199
}
200200

201+
override fun onResume() {
202+
renderer.onResume()
203+
}
204+
201205
override fun onDestroy() {
202206
if (lifecycleState == LifecycleState.STATE_DESTROYED) {
203207
return

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ class MapSurface : MapPluginProviderDelegate, MapControllable {
229229
mapController.onStop()
230230
}
231231

232+
/**
233+
* Called when the activity is resumed
234+
*/
235+
override fun onResume() {
236+
mapController.onResume()
237+
}
238+
232239
/**
233240
* Called to dispose the renderer
234241
*/

maps-sdk/src/main/java/com/mapbox/maps/MapView.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ open class MapView : FrameLayout, MapPluginProviderDelegate, MapControllable {
249249
}
250250
}
251251

252+
/**
253+
* You must call this method from the parent's Activity#onResume() or Fragment#onResume()
254+
* @see android.app.Activity.onResume
255+
* @see android.app.Fragment.onResume
256+
*/
257+
override fun onResume() {
258+
mapController.onResume()
259+
}
260+
252261
/**
253262
* You must call this method from the parent's Activity#onLowMemory() or Fragment#onLowMemory()
254263
* @see android.app.Activity.onLowMemory

maps-sdk/src/main/java/com/mapbox/maps/renderer/MapboxRenderThread.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import com.mapbox.maps.renderer.gl.TextureRenderer
2424
import com.mapbox.maps.renderer.widget.Widget
2525
import com.mapbox.maps.viewannotation.ViewAnnotationManager
2626
import com.mapbox.maps.viewannotation.ViewAnnotationUpdateMode
27+
import kotlinx.coroutines.DelicateCoroutinesApi
28+
import kotlinx.coroutines.GlobalScope
29+
import kotlinx.coroutines.delay
30+
import kotlinx.coroutines.launch
2731
import java.util.concurrent.ConcurrentLinkedQueue
2832
import java.util.concurrent.locks.Condition
2933
import java.util.concurrent.locks.ReentrantLock
@@ -713,11 +717,15 @@ internal class MapboxRenderThread : Choreographer.FrameCallback {
713717
logI(TAG, "Renderer paused")
714718
}
715719

720+
@OptIn(DelicateCoroutinesApi::class)
716721
@UiThread
717722
fun resume() {
718723
paused = false
719724
logI(TAG, "Renderer resumed, renderThreadPrepared=$renderThreadPrepared, surface.isValid=${surface?.isValid}")
720-
mapboxRenderer.resetThreadServiceType()
725+
GlobalScope.launch {
726+
delay(RESET_THREAD_SERVICE_TYPE_DELAY_MS)
727+
mapboxRenderer.resetThreadServiceType()
728+
}
721729
// schedule render if we resume not after first create (e.g. bring map back to front)
722730
renderPreparedGuardedRun(::postPrepareRenderFrame)
723731
}
@@ -787,5 +795,11 @@ internal class MapboxRenderThread : Choreographer.FrameCallback {
787795
* rescheduling configuration with that delay in order not to overflood handler thread message queue.
788796
*/
789797
internal const val RETRY_DELAY_MS = 50L
798+
/**
799+
* Delay before calling resetThreadServiceType() on resume to ensure CPU affinity is properly set.
800+
* This delay helps address timing issues where CPU affinity might not be set immediately
801+
* when coming back from background.
802+
*/
803+
internal const val RESET_THREAD_SERVICE_TYPE_DELAY_MS = 300L
790804
}
791805
}

maps-sdk/src/main/java/com/mapbox/maps/renderer/MapboxRenderer.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ internal abstract class MapboxRenderer(mapName: String) : DelegatingMapClient {
145145
renderFrameCancelable = map?.subscribe(renderFrameFinishedCallback)
146146
}
147147

148+
@UiThread
149+
fun onResume() {
150+
renderThread.resume()
151+
}
152+
148153
@RenderThread
149154
fun snapshot(): Bitmap? {
150155
if (!readyForSnapshot.get()) {

maps-sdk/src/test/java/com/mapbox/maps/MapControllerTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ class MapControllerTest {
144144
}
145145
}
146146

147+
@Test
148+
fun onResume() {
149+
every { mockRenderer.onResume() } just Runs
150+
151+
testMapController.onResume()
152+
153+
verifySequence {
154+
mockRenderer.onResume()
155+
}
156+
}
157+
147158
@OptIn(com.mapbox.annotation.MapboxExperimental::class)
148159
@Test
149160
fun onStartDeliversUpdatedStyle() {

0 commit comments

Comments
 (0)