Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Post camera listener invocations (#10690)
Browse files Browse the repository at this point in the history
* [android] - post camera listener invocations

* remove jvm unit test
  • Loading branch information
tobrun authored Dec 14, 2017
1 parent 7012c1b commit 4525ff7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.mapboxsdk.maps;

import android.os.Handler;
import android.support.annotation.NonNull;

import java.util.concurrent.CopyOnWriteArrayList;
Expand All @@ -9,10 +10,16 @@
import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveListener;
import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveStartedListener;

/**
* Class responsible for dispatching camera change events to registered listeners.
*/
class CameraChangeDispatcher implements MapboxMap.OnCameraMoveStartedListener, MapboxMap.OnCameraMoveListener,
MapboxMap.OnCameraMoveCanceledListener, OnCameraIdleListener {

private final Handler handler = new Handler();

private boolean idle = true;
private int moveStartedReason;

private final CopyOnWriteArrayList<OnCameraMoveStartedListener> onCameraMoveStarted = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<OnCameraMoveCanceledListener> onCameraMoveCanceled = new CopyOnWriteArrayList<>();
Expand All @@ -24,6 +31,74 @@ class CameraChangeDispatcher implements MapboxMap.OnCameraMoveStartedListener, M
private OnCameraMoveListener onCameraMoveListener;
private OnCameraIdleListener onCameraIdleListener;

private final Runnable onCameraMoveStartedRunnable = new Runnable() {
@Override
public void run() {
// deprecated API
if (onCameraMoveStartedListener != null) {
onCameraMoveStartedListener.onCameraMoveStarted(moveStartedReason);
}

// new API
if (!onCameraMoveStarted.isEmpty()) {
for (OnCameraMoveStartedListener cameraMoveStartedListener : onCameraMoveStarted) {
cameraMoveStartedListener.onCameraMoveStarted(moveStartedReason);
}
}
}
};

private final Runnable onCameraMoveRunnable = new Runnable() {
@Override
public void run() {
// deprecated API
if (onCameraMoveListener != null && !idle) {
onCameraMoveListener.onCameraMove();
}

// new API
if (!onCameraMove.isEmpty() && !idle) {
for (OnCameraMoveListener cameraMoveListener : onCameraMove) {
cameraMoveListener.onCameraMove();
}
}
}
};

private final Runnable onCameraMoveCancelRunnable = new Runnable() {
@Override
public void run() {
// deprecated API
if (onCameraMoveCanceledListener != null && !idle) {
onCameraMoveCanceledListener.onCameraMoveCanceled();
}

// new API
if (!onCameraMoveCanceled.isEmpty() && !idle) {
for (OnCameraMoveCanceledListener cameraMoveCanceledListener : onCameraMoveCanceled) {
cameraMoveCanceledListener.onCameraMoveCanceled();
}
}
}
};

private final Runnable onCameraIdleRunnable = new Runnable() {
@Override
public void run() {
// deprecated API
if (onCameraIdleListener != null) {
onCameraIdleListener.onCameraIdle();
}

// new API
if (!onCameraIdle.isEmpty()) {
for (OnCameraIdleListener cameraIdleListener : onCameraIdle) {
cameraIdleListener.onCameraIdle();
}
}
}
};

@Deprecated
void setOnCameraMoveStartedListener(OnCameraMoveStartedListener onCameraMoveStartedListener) {
this.onCameraMoveStartedListener = onCameraMoveStartedListener;
Expand All @@ -45,70 +120,30 @@ void setOnCameraIdleListener(OnCameraIdleListener onCameraIdleListener) {
}

@Override
public void onCameraMoveStarted(int reason) {
public void onCameraMoveStarted(final int reason) {
if (!idle) {
return;
}
idle = false;

// deprecated API
if (onCameraMoveStartedListener != null) {
onCameraMoveStartedListener.onCameraMoveStarted(reason);
}

// new API
if (!onCameraMoveStarted.isEmpty()) {
for (OnCameraMoveStartedListener cameraMoveStartedListener : onCameraMoveStarted) {
cameraMoveStartedListener.onCameraMoveStarted(reason);
}
}
moveStartedReason = reason;
handler.post(onCameraMoveStartedRunnable);
}

@Override
public void onCameraMove() {
// deprecated API
if (onCameraMoveListener != null && !idle) {
onCameraMoveListener.onCameraMove();
}

// new API
if (!onCameraMove.isEmpty() && !idle) {
for (OnCameraMoveListener cameraMoveListener : onCameraMove) {
cameraMoveListener.onCameraMove();
}
}
handler.post(onCameraMoveRunnable);
}

@Override
public void onCameraMoveCanceled() {
// deprecated API
if (onCameraMoveCanceledListener != null && !idle) {
onCameraMoveCanceledListener.onCameraMoveCanceled();
}

// new API
if (!onCameraMoveCanceled.isEmpty() && !idle) {
for (OnCameraMoveCanceledListener cameraMoveCanceledListener : onCameraMoveCanceled) {
cameraMoveCanceledListener.onCameraMoveCanceled();
}
}
handler.post(onCameraMoveCancelRunnable);
}

@Override
public void onCameraIdle() {
if (!idle) {
idle = true;
// deprecated API
if (onCameraIdleListener != null) {
onCameraIdleListener.onCameraIdle();
}

// new API
if (!onCameraIdle.isEmpty()) {
for (OnCameraIdleListener cameraIdleListener : onCameraIdle) {
cameraIdleListener.onCameraIdle();
}
}
handler.post(onCameraIdleRunnable);
}
}

Expand Down

This file was deleted.

0 comments on commit 4525ff7

Please sign in to comment.