Skip to content

Commit

Permalink
fix callback to animating camera pos was sometimes done on wrong thread
Browse files Browse the repository at this point in the history
  • Loading branch information
westnordost committed Mar 3, 2020
1 parent 6a868ec commit e9a22fa
Showing 1 changed file with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import android.provider.Settings
import android.util.Property
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Interpolator
import androidx.annotation.AnyThread
import androidx.annotation.UiThread
import androidx.core.animation.addListener
import com.mapzen.tangram.CameraUpdateFactory
import com.mapzen.tangram.MapController
Expand Down Expand Up @@ -39,10 +41,12 @@ class CameraManager(private val c: MapController, private val contentResolver: C
private var lastAnimator: ValueAnimator? = null
set(value) {
if (field == value) return
if (field == null) {
listener?.onAnimationsStarted()
} else if(value == null) {
listener?.onAnimationsEnded()
mainHandler.post {
if (field == null) {
listener?.onAnimationsStarted()
} else if(value == null) {
listener?.onAnimationsEnded()
}
}
field = value
}
Expand All @@ -60,32 +64,36 @@ class CameraManager(private val c: MapController, private val contentResolver: C
val isAnimating: Boolean get() = lastAnimator != null

interface AnimationsListener {
fun onAnimationsStarted()
fun onAnimating()
fun onAnimationsEnded()
@UiThread fun onAnimationsStarted()
@UiThread fun onAnimating()
@UiThread fun onAnimationsEnded()
}
var listener: AnimationsListener? = null

fun updateCamera(duration: Long = 0, interpolator: Interpolator = defaultInterpolator, update: CameraUpdate) {
@AnyThread fun updateCamera(duration: Long = 0, interpolator: Interpolator = defaultInterpolator, update: CameraUpdate) {
synchronized(_tangramCamera) {
pullCameraPositionFromController()
update.resolveDeltas(_tangramCamera)
cancelCameraAnimations(update)
if (duration == 0L || isAnimationsOff) {
applyCameraUpdate(update)
// workaround https://github.com/tangrams/tangram-es/issues/2129
listener?.onAnimating()
listener?.onAnimationsEnded()
mainHandler.post {
listener?.onAnimating()
listener?.onAnimationsEnded()
}
} else {
animateCameraUpdate(update, duration, interpolator)
}
}
}

fun cancelAllCameraAnimations() {
@AnyThread fun cancelAllCameraAnimations() {
synchronized(currentAnimations) {
for (animator in currentAnimations.values.toSet()) {
animator.cancel()
mainHandler.post {
for (animator in currentAnimations.values.toSet()) {
animator.cancel()
}
}
currentAnimations.clear()
}
Expand All @@ -94,7 +102,7 @@ class CameraManager(private val c: MapController, private val contentResolver: C
private val isAnimationsOff get() =
Settings.Global.getFloat(contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f) == 0f

private fun cancelCameraAnimations(update: CameraUpdate) {
@AnyThread private fun cancelCameraAnimations(update: CameraUpdate) {
if(update.rotation != null) cancelAnimation("rotation")
if(update.tilt != null) cancelAnimation("tilt")
if(update.zoom != null) cancelAnimation("zoom")
Expand All @@ -112,7 +120,7 @@ class CameraManager(private val c: MapController, private val contentResolver: C
pushCameraPositionToController()
}

private fun animateCameraUpdate(update: CameraUpdate, duration: Long, interpolator: Interpolator) {
@AnyThread private fun animateCameraUpdate(update: CameraUpdate, duration: Long, interpolator: Interpolator) {
val animator = ObjectAnimator()
val propValues = mutableListOf<PropertyValuesHolder>()
update.rotation?.let {
Expand Down Expand Up @@ -141,7 +149,7 @@ class CameraManager(private val c: MapController, private val contentResolver: C
animator.setValues(*propValues.toTypedArray())
animator.duration = duration
animator.interpolator = interpolator
animator.addListener(onEnd = this::unassignAnimator)
animator.addListener(onEnd = this::unassignAnimation)

val endTime = System.currentTimeMillis() + duration
if (lastAnimatorEndTime < endTime) {
Expand All @@ -161,23 +169,23 @@ class CameraManager(private val c: MapController, private val contentResolver: C
c.updateCameraPosition(CameraUpdateFactory.newCameraPosition(_tangramCamera))
}

private fun animate(animator: ValueAnimator) {
@UiThread private fun animate(animator: ValueAnimator) {
synchronized(_tangramCamera) {
pushCameraPositionToController()
}
listener?.onAnimating()
}

private fun cancelAnimation(key: String) {
@AnyThread private fun cancelAnimation(key: String) {
var animator: Animator?
synchronized(currentAnimations) {
animator = currentAnimations[key]
animator?.let { unassignAnimator(it) }
animator?.let { unassignAnimation(it) }
}
mainHandler.post { animator?.cancel() }
}

private fun unassignAnimator(animator: Animator) {
@AnyThread private fun unassignAnimation(animator: Animator) {
synchronized(currentAnimations) {
currentAnimations.entries.removeAll { (_, anim) -> animator == anim }
}
Expand All @@ -186,7 +194,7 @@ class CameraManager(private val c: MapController, private val contentResolver: C
}
}

private fun assignAnimation(key: String, animator: ObjectAnimator) {
@AnyThread private fun assignAnimation(key: String, animator: ObjectAnimator) {
synchronized(currentAnimations) {
currentAnimations[key] = animator
}
Expand Down

0 comments on commit e9a22fa

Please sign in to comment.