diff --git a/library/src/main/java/com/google/maps/android/clustering/view/ClusterRendererMultipleItems.java b/library/src/main/java/com/google/maps/android/clustering/view/ClusterRendererMultipleItems.java index 9b1c5adf2..88e45f9e4 100644 --- a/library/src/main/java/com/google/maps/android/clustering/view/ClusterRendererMultipleItems.java +++ b/library/src/main/java/com/google/maps/android/clustering/view/ClusterRendererMultipleItems.java @@ -107,31 +107,19 @@ public enum AnimationType { } public void setAnimationType(AnimationType type) { - switch (type) { - case EASE_IN, ACCELERATE: - animationInterp = new AccelerateInterpolator(); - break; - case EASE_OUT: - animationInterp = new DecelerateInterpolator(); - break; - case EASE_IN_OUT: - animationInterp = new AccelerateDecelerateInterpolator(); - break; - case FAST_OUT_SLOW_IN: - animationInterp = new FastOutSlowInInterpolator(); - break; - case BOUNCE: - animationInterp = new BounceInterpolator(); - break; - case DECELERATE: - animationInterp = new DecelerateInterpolator(); - break; - default: - animationInterp = new LinearInterpolator(); - break; - } + animationInterp = switch (type) { + case LINEAR -> new LinearInterpolator(); + case EASE_IN, ACCELERATE -> new AccelerateInterpolator(); + case EASE_OUT, DECELERATE -> new DecelerateInterpolator(); + case EASE_IN_OUT -> new AccelerateDecelerateInterpolator(); + case FAST_OUT_SLOW_IN -> new FastOutSlowInInterpolator(); + case BOUNCE -> new BounceInterpolator(); + }; } + public void setAnimationInterpolator(TimeInterpolator interpolator) { + animationInterp = interpolator; + } /** * Markers that are currently on the map. @@ -459,6 +447,7 @@ public void setMapZoom(float zoom) { } @SuppressLint("NewApi") + @Override public void run() { final MarkerModifier markerModifier = new MarkerModifier(); final float zoom = mMapZoom; @@ -687,13 +676,16 @@ private MarkerModifier() { */ public void add(boolean priority, CreateMarkerTask c) { lock.lock(); - sendEmptyMessage(BLANK); - if (priority) { - mOnScreenCreateMarkerTasks.add(c); - } else { - mCreateMarkerTasks.add(c); + try { + sendEmptyMessage(BLANK); + if (priority) { + mOnScreenCreateMarkerTasks.add(c); + } else { + mCreateMarkerTasks.add(c); + } + } finally { + lock.unlock(); } - lock.unlock(); } /** @@ -704,13 +696,16 @@ public void add(boolean priority, CreateMarkerTask c) { */ public void remove(boolean priority, Marker m) { lock.lock(); - sendEmptyMessage(BLANK); - if (priority) { - mOnScreenRemoveMarkerTasks.add(m); - } else { - mRemoveMarkerTasks.add(m); + try { + sendEmptyMessage(BLANK); + if (priority) { + mOnScreenRemoveMarkerTasks.add(m); + } else { + mRemoveMarkerTasks.add(m); + } + } finally { + lock.unlock(); } - lock.unlock(); } /** @@ -722,18 +717,21 @@ public void remove(boolean priority, Marker m) { */ public void animate(MarkerWithPosition marker, LatLng from, LatLng to) { lock.lock(); - AnimationTask task = new AnimationTask(marker, from, to, lock); + try { + AnimationTask task = new AnimationTask(marker, from, to, lock); - for (AnimationTask existingTask : ongoingAnimations) { - if (existingTask.marker.getId().equals(task.marker.getId())) { - existingTask.cancel(); - break; + for (AnimationTask existingTask : ongoingAnimations) { + if (existingTask.marker.getId().equals(task.marker.getId())) { + existingTask.cancel(); + break; + } } - } - mAnimationTasks.add(task); - ongoingAnimations.add(task); - lock.unlock(); + mAnimationTasks.add(task); + ongoingAnimations.add(task); + } finally { + lock.unlock(); + } } /** @@ -746,18 +744,21 @@ public void animate(MarkerWithPosition marker, LatLng from, LatLng to) { */ public void animateThenRemove(MarkerWithPosition marker, LatLng from, LatLng to) { lock.lock(); - AnimationTask animationTask = new AnimationTask(marker, from, to, lock); - for (AnimationTask existingTask : ongoingAnimations) { - if (existingTask.marker.getId().equals(animationTask.marker.getId())) { - existingTask.cancel(); - break; + try { + AnimationTask animationTask = new AnimationTask(marker, from, to, lock); + for (AnimationTask existingTask : ongoingAnimations) { + if (existingTask.marker.getId().equals(animationTask.marker.getId())) { + existingTask.cancel(); + break; + } } - } - ongoingAnimations.add(animationTask); - animationTask.removeOnAnimationComplete(mClusterManager.getMarkerManager()); - mAnimationTasks.add(animationTask); - lock.unlock(); + ongoingAnimations.add(animationTask); + animationTask.removeOnAnimationComplete(mClusterManager.getMarkerManager()); + mAnimationTasks.add(animationTask); + } finally { + lock.unlock(); + } } @Override @@ -821,9 +822,13 @@ private void removeMarker(Marker m) { * @return true if there is still work to be processed. */ public boolean isBusy() { + lock.lock(); try { - lock.lock(); - return !(mCreateMarkerTasks.isEmpty() && mOnScreenCreateMarkerTasks.isEmpty() && mOnScreenRemoveMarkerTasks.isEmpty() && mRemoveMarkerTasks.isEmpty() && mAnimationTasks.isEmpty()); + return !(mCreateMarkerTasks.isEmpty() + && mOnScreenCreateMarkerTasks.isEmpty() + && mOnScreenRemoveMarkerTasks.isEmpty() + && mRemoveMarkerTasks.isEmpty() + && mAnimationTasks.isEmpty()); } finally { lock.unlock(); } @@ -1257,11 +1262,11 @@ public void cancel() { new Handler(Looper.getMainLooper()).post(this::cancel); return; } + lock.lock(); try { markerWithPosition.position = to; mRemoveOnComplete = false; valueAnimator.cancel(); - lock.lock(); ongoingAnimations.remove(this); } finally { lock.unlock(); @@ -1279,8 +1284,11 @@ public void onAnimationEnd(Animator animation) { // Remove the task from the queue lock.lock(); - ongoingAnimations.remove(this); - lock.unlock(); + try { + ongoingAnimations.remove(this); + } finally { + lock.unlock(); + } } public void removeOnAnimationComplete(MarkerManager markerManager) {