Skip to content

Commit

Permalink
Fix annotation update missing issue. (#441)
Browse files Browse the repository at this point in the history
* Make workrHandler in GeojsonSource not static

* Fix test cases

* Format generated codes

* Add test  cases to verify annotations update
  • Loading branch information
Kevin Li authored Jun 29, 2021
1 parent 1128836 commit fdc298c
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package com.mapbox.maps.testapp.annotation

import android.graphics.Color
import android.os.Handler
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import com.google.gson.JsonPrimitive
import com.mapbox.geojson.Point
import com.mapbox.maps.Style
import com.mapbox.maps.extension.style.utils.ColorUtils
import com.mapbox.maps.plugin.annotation.annotations
import com.mapbox.maps.plugin.annotation.generated.*
import com.mapbox.maps.testapp.BaseMapTest
import junit.framework.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

/**
* Instrumented test for verifying annotation updating while changing style
*/
@RunWith(AndroidJUnit4::class)
@LargeTest
class UpdateAnnotationWithMultiManagersTest : BaseMapTest() {
private val latch = CountDownLatch(4)
private lateinit var pointAnnotationManager: PointAnnotationManager
private lateinit var circleAnnotationManager: CircleAnnotationManager
private lateinit var polygonAnnotationManager: PolygonAnnotationManager
private lateinit var polylineAnnotationManager: PolylineAnnotationManager
private lateinit var pointAnnotation: PointAnnotation
private lateinit var circleAnnotation: CircleAnnotation
private lateinit var polylineAnnotation: PolylineAnnotation
private lateinit var polygonAnnotation: PolygonAnnotation
private lateinit var handler: Handler
private val updateTimes = 100
private val updateSteps = 0.1
private val delta = 0.0001
private val points = mutableListOf(
mutableListOf(
Point.fromLngLat(-3.363937, -10.733102),
Point.fromLngLat(1.754703, -19.716317),
Point.fromLngLat(-15.747196, -21.085074),
Point.fromLngLat(-3.363937, -10.733102)
)
)
private val polylinePoints = mutableListOf(
Point.fromLngLat(-4.375974, -2.178992),
Point.fromLngLat(-7.639772, -4.107888),
Point.fromLngLat(-11.439207, 2.798737),
)

@Test
fun testUpdateAnnotation() {
rule.scenario.onActivity {
handler = Handler(it.mainLooper)
it.runOnUiThread {
mapboxMap.loadStyleUri(Style.MAPBOX_STREETS) {
pointAnnotationManager = mapView.annotations.createPointAnnotationManager(mapView)
pointAnnotationManager.textFont = listOf("Open Sans Regular")
pointAnnotation = pointAnnotationManager.create(
PointAnnotationOptions()
.withIconColor(ColorUtils.colorToRgbaString(Color.RED))
.withIconImage("car-15")
.withPoint(Point.fromLngLat(0.0, 0.0))
)
circleAnnotationManager = mapView.annotations.createCircleAnnotationManager(mapView)
circleAnnotation = circleAnnotationManager.create(
CircleAnnotationOptions()
.withCircleColor(Color.RED)
.withCircleRadius(5.0)
.withPoint(Point.fromLngLat(0.0, 0.0))
)

polygonAnnotationManager = mapView.annotations.createPolygonAnnotationManager(mapView)
polygonAnnotation = polygonAnnotationManager.create(
PolygonAnnotationOptions()
.withPoints(points)
.withData(JsonPrimitive("Foobar"))
.withFillColor(Color.RED)
)

polylineAnnotationManager = mapView.annotations.createPolylineAnnotationManager(mapView)
polylineAnnotation = polylineAnnotationManager.create(
PolylineAnnotationOptions()
.withPoints(polylinePoints)
.withLineColor(Color.RED)
.withLineWidth(5.0)
)

Thread {
for (i in 1..updateTimes) {
pointAnnotation.point = Point.fromLngLat(
pointAnnotation.point.longitude() + updateSteps,
pointAnnotation.point.latitude() + updateSteps
)
pointAnnotationManager.update(pointAnnotation)
Thread.sleep(10)
}
latch.countDown()
}.start()

Thread {
for (i in 1..updateTimes) {
circleAnnotation.point = Point.fromLngLat(
circleAnnotation.point.longitude() + updateSteps,
circleAnnotation.point.latitude() + updateSteps
)
circleAnnotationManager.update(circleAnnotation)
Thread.sleep(10)
}
latch.countDown()
}.start()

Thread {
for (i in 1..updateTimes) {
points[0][0] = Point.fromLngLat(
points[0][0].longitude() + updateSteps,
points[0][0].latitude() + updateSteps
)
polygonAnnotation.points = points
polygonAnnotationManager.update(polygonAnnotation)
Thread.sleep(10)
}
latch.countDown()
}.start()
Thread {
for (i in 1..updateTimes) {
polylinePoints[0] = Point.fromLngLat(
polylinePoints[0].longitude() + updateSteps,
polylinePoints[0].latitude() + updateSteps
)
polylineAnnotation.points = polylinePoints
polylineAnnotationManager.update(polylineAnnotation)
Thread.sleep(10)
}
latch.countDown()
}.start()
}
}
}
if (!latch.await(3000, TimeUnit.MILLISECONDS)) {
throw TimeoutException()
}
assertEquals(pointAnnotation.point.latitude(), updateSteps * updateTimes, delta)
assertEquals(pointAnnotation.point.longitude(), updateSteps * updateTimes, delta)
assertEquals(circleAnnotation.point.longitude(), updateSteps * updateTimes, delta)
assertEquals(circleAnnotation.point.latitude(), updateSteps * updateTimes, delta)
assertEquals(polylineAnnotation.points[0].longitude(), polylinePoints[0].longitude(), delta)
assertEquals(polylineAnnotation.points[0].latitude(), polylinePoints[0].latitude(), delta)
assertEquals(polygonAnnotation.points[0][0].longitude(), points[0][0].longitude(), delta)
assertEquals(polygonAnnotation.points[0][0].latitude(), points[0][0].latitude(), delta)
}
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@
android:value=".ExampleOverviewActivity" />
</activity>
<activity
android:name=".examples.annotation.PolyfillAnnotationActivity"
android:name=".examples.annotation.PolygoneAnnotationActivity"
android:description="@string/description_annotation_fill"
android:exported="true"
android:label="@string/activity_fill">
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import kotlinx.android.synthetic.main.activity_annotation.*
import java.util.*

/**
* Example showing how to add Fill annotations
* Example showing how to add Polygone annotations
*/
class PolyfillAnnotationActivity : AppCompatActivity() {
class PolygoneAnnotationActivity : AppCompatActivity() {
private val random = Random()
private var polygonAnnotationManager: PolygonAnnotationManager? = null
private var index: Int = 0
Expand All @@ -37,7 +37,7 @@ class PolyfillAnnotationActivity : AppCompatActivity() {
polygonAnnotationManager = annotationPlugin.createPolygonAnnotationManager(mapView).apply {
addClickListener(
OnPolygonAnnotationClickListener {
Toast.makeText(this@PolyfillAnnotationActivity, "click ${it.id}", Toast.LENGTH_SHORT)
Toast.makeText(this@PolygoneAnnotationActivity, "click ${it.id}", Toast.LENGTH_SHORT)
.show()
false
}
Expand All @@ -46,15 +46,15 @@ class PolyfillAnnotationActivity : AppCompatActivity() {
addInteractionListener(object : OnPolygonAnnotationInteractionListener {
override fun onSelectAnnotation(annotation: PolygonAnnotation) {
Toast.makeText(
this@PolyfillAnnotationActivity,
this@PolygoneAnnotationActivity,
"onSelectAnnotation ${annotation.id}",
Toast.LENGTH_SHORT
).show()
}

override fun onDeselectAnnotation(annotation: PolygonAnnotation) {
Toast.makeText(
this@PolyfillAnnotationActivity,
this@PolygoneAnnotationActivity,
"onDeselectAnnotation ${annotation.id}",
Toast.LENGTH_SHORT
).show()
Expand Down Expand Up @@ -89,7 +89,7 @@ class PolyfillAnnotationActivity : AppCompatActivity() {
create(polygonAnnotationOptionsList)

AnnotationUtils.loadStringFromAssets(
this@PolyfillAnnotationActivity,
this@PolygoneAnnotationActivity,
"annotations.json"
)?.let {
create(FeatureCollection.fromJson(it))
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ class AnnotationPluginImpl : AnnotationPlugin {
// no-ops
}

/**
* Called when the map is destroyed. Should be used to cleanup plugin resources for that map.
*/
override fun cleanup() {
managerList.forEach {
it.get()?.onDestroy()
}
}

/**
* Provides all map delegate instances.
*/
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fdc298c

Please sign in to comment.