Skip to content

Commit

Permalink
Stop workthread while destroying Annotation plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Li committed Jun 21, 2021
1 parent 89b2b07 commit ac66525
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.mapbox.maps.extension.style.expressions.generated.Expression.Companio
import com.mapbox.maps.extension.style.expressions.generated.Expression.Companion.toNumber
import com.mapbox.maps.extension.style.layers.properties.generated.IconAnchor
import com.mapbox.maps.extension.style.layers.properties.generated.TextAnchor
import com.mapbox.maps.plugin.annotation.Annotation
import com.mapbox.maps.plugin.annotation.AnnotationPlugin
import com.mapbox.maps.plugin.annotation.annotations
import com.mapbox.maps.plugin.annotation.generated.*
Expand All @@ -33,7 +34,9 @@ import java.util.*
*/
class PointAnnotationActivity : AppCompatActivity() {
private var pointAnnotationManager: PointAnnotationManager? = null
private var circleAnnotationManager: CircleAnnotationManager? = null
private var pointAnnotation: PointAnnotation? = null
private var circleAnnotation: CircleAnnotation? = null
private val animators: MutableList<ValueAnimator> = mutableListOf()
private var index: Int = 0
private val nextStyle: String
Expand All @@ -47,9 +50,29 @@ class PointAnnotationActivity : AppCompatActivity() {
setContentView(R.layout.activity_annotation)
mapView.getMapboxMap().loadStyleUri(nextStyle) {
annotationPlugin = mapView.annotations
circleAnnotationManager = annotationPlugin.createCircleAnnotationManager(mapView).apply {
val circleAnnotationOptions: CircleAnnotationOptions = CircleAnnotationOptions()
.withPoint(Point.fromLngLat(AIRPORT_LONGITUDE, AIRPORT_LATITUDE))
.withCircleColor(Color.YELLOW)
.withCircleRadius(12.0)
.withDraggable(false)
circleAnnotation = create(circleAnnotationOptions)
}
pointAnnotationManager = annotationPlugin.createPointAnnotationManager(mapView).apply {
textFont = listOf("Arial Unicode MS Bold", "Open Sans Regular")

addDragListener(object : OnPointAnnotationDragListener {
override fun onAnnotationDragStarted(annotation: Annotation<*>) {}

override fun onAnnotationDrag(annotation: Annotation<*>) {
circleAnnotation?.let {
it.point = (annotation as PointAnnotation).point
circleAnnotationManager?.update(it)
}
}

override fun onAnnotationDragFinished(annotation: Annotation<*>) {}
})
addClickListener(
OnPointAnnotationClickListener {
Toast.makeText(this@PointAnnotationActivity, "Click: ${it.id}", Toast.LENGTH_SHORT)
Expand Down Expand Up @@ -118,7 +141,7 @@ class PointAnnotationActivity : AppCompatActivity() {
}
// random add symbols across the globe
val pointAnnotationOptionsList: MutableList<PointAnnotationOptions> = ArrayList()
for (i in 0..20) {
for (i in 0..2000) {
pointAnnotationOptionsList.add(
PointAnnotationOptions()
.withPoint(AnnotationUtils.createRandomPoint())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
private val mapMoveResolver = MapMove()
private var draggedAnnotation: T? = null
private val annotationMap = ConcurrentHashMap<Long, T>()
private val lock = ReentrantLock()
private val waitCondition = lock.newCondition()
protected var touchAreaShiftX: Int = mapView.scrollX
protected var touchAreaShiftY: Int = mapView.scrollY
protected abstract val layerId: String
Expand Down Expand Up @@ -125,6 +127,13 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
gesturesPlugin.addOnMapClickListener(mapClickResolver)
gesturesPlugin.addOnMapLongClickListener(mapLongClickResolver)
gesturesPlugin.addOnMoveListener(mapMoveResolver)
if (workerThread == null) {
workerThread = HandlerThread("ANNOTATION_WORKER").apply {
priority = Thread.MAX_PRIORITY
start()
workerHandler = Handler(looper)
}
}
}

/**
Expand Down Expand Up @@ -343,9 +352,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
Logger.e(TAG, "Can't update source: source has not been added to style.")
return
}
workerHandler.post {
val lock = ReentrantLock()
val waitCondition = lock.newCondition()
workerHandler?.post {
lock.withLock {
annotations
.filter { it.getType() == AnnotationType.PointAnnotation }
Expand Down Expand Up @@ -675,14 +682,17 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
private const val CLUSTER_TEXT_LAYER_ID = "mapbox-android-cluster-text-layer"
private val DEFAULT_TEXT_FIELD = get("point_count")

private val workerThread by lazy {
HandlerThread("ANNOTATION_WORKER").apply {
priority = Thread.MAX_PRIORITY
start()
}
}
private val workerHandler by lazy {
Handler(workerThread.looper)
private var workerThread: HandlerThread? = null
private var workerHandler: Handler? = null

/**
* Clean and stop the workerThread for updating source
*/
fun cleanupWorkerThread() {
workerHandler?.removeCallbacksAndMessages(null)
workerThread?.quit()
workerHandler = null
workerThread = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.mapbox.maps.plugin.annotation

import android.view.View
import androidx.annotation.VisibleForTesting
import com.mapbox.maps.extension.style.StyleInterface
import com.mapbox.maps.plugin.annotation.generated.*
import com.mapbox.maps.plugin.annotation.generated.CircleAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PolygonAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PolylineAnnotationManager
import com.mapbox.maps.plugin.delegates.MapDelegateProvider
import com.mapbox.maps.plugin.delegates.MapPluginProviderDelegate
import java.lang.ref.WeakReference
Expand Down Expand Up @@ -74,6 +76,9 @@ class AnnotationPluginImpl : AnnotationPlugin {
}
}
}
if (managerList.isEmpty()) {
AnnotationManagerImpl.cleanupWorkerThread()
}
}

/**
Expand All @@ -88,12 +93,10 @@ class AnnotationPluginImpl : AnnotationPlugin {
}

/**
* Called when a new Style is loaded.
*
* @param styleDelegate
* Called when the map is destroyed. Should be used to cleanup plugin resources for that map.
*/
override fun onStyleChanged(styleDelegate: StyleInterface) {
// no-ops
override fun cleanup() {
AnnotationManagerImpl.cleanupWorkerThread()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package com.mapbox.maps.plugin.annotation
import android.view.View
import com.mapbox.maps.plugin.MapPlugin
import com.mapbox.maps.plugin.MapSizePlugin
import com.mapbox.maps.plugin.MapStyleObserverPlugin

/**
* Plugin interface for the annotation.
*/
interface AnnotationPlugin : MapPlugin, MapSizePlugin, MapStyleObserverPlugin {
interface AnnotationPlugin : MapPlugin, MapSizePlugin {
/**
* Get an annotation manger
*
Expand Down

0 comments on commit ac66525

Please sign in to comment.