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

Map touch listeners API based on lists (cherry pick #10696) #10749

Merged
merged 1 commit into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.mapbox.services.android.telemetry.utils.MathUtils;
import com.mapbox.services.android.telemetry.utils.TelemetryUtils;

import java.util.concurrent.CopyOnWriteArrayList;

import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveStartedListener.REASON_API_GESTURE;

/**
Expand All @@ -43,16 +45,30 @@ final class MapGestureDetector {
private final AnnotationManager annotationManager;
private final CameraChangeDispatcher cameraChangeDispatcher;

private final GestureDetectorCompat gestureDetector;
private final ScaleGestureDetector scaleGestureDetector;
private final RotateGestureDetector rotateGestureDetector;
private final ShoveGestureDetector shoveGestureDetector;
private GestureDetectorCompat gestureDetector;
private ScaleGestureDetector scaleGestureDetector;
private RotateGestureDetector rotateGestureDetector;
private ShoveGestureDetector shoveGestureDetector;

// deprecated map touch API
private MapboxMap.OnMapClickListener onMapClickListener;
private MapboxMap.OnMapLongClickListener onMapLongClickListener;
private MapboxMap.OnFlingListener onFlingListener;
private MapboxMap.OnScrollListener onScrollListener;

// new map touch API
private final CopyOnWriteArrayList<MapboxMap.OnMapClickListener> onMapClickListenerList
= new CopyOnWriteArrayList<>();

private final CopyOnWriteArrayList<MapboxMap.OnMapLongClickListener> onMapLongClickListenerList
= new CopyOnWriteArrayList<>();

private final CopyOnWriteArrayList<MapboxMap.OnFlingListener> onFlingListenerList
= new CopyOnWriteArrayList<>();

private final CopyOnWriteArrayList<MapboxMap.OnScrollListener> onScrollListenerList
= new CopyOnWriteArrayList<>();

private PointF focalPoint;

private boolean twoTap;
Expand Down Expand Up @@ -81,12 +97,14 @@ final class MapGestureDetector {
this.cameraChangeDispatcher = cameraChangeDispatcher;

// Touch gesture detectors
gestureDetector = new GestureDetectorCompat(context, new GestureListener());
gestureDetector.setIsLongpressEnabled(true);
scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureListener());
ScaleGestureDetectorCompat.setQuickScaleEnabled(scaleGestureDetector, true);
rotateGestureDetector = new RotateGestureDetector(context, new RotateGestureListener());
shoveGestureDetector = new ShoveGestureDetector(context, new ShoveGestureListener());
if (context != null) {
gestureDetector = new GestureDetectorCompat(context, new GestureListener());
gestureDetector.setIsLongpressEnabled(true);
scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureListener());
ScaleGestureDetectorCompat.setQuickScaleEnabled(scaleGestureDetector, true);
rotateGestureDetector = new RotateGestureDetector(context, new RotateGestureListener());
shoveGestureDetector = new ShoveGestureDetector(context, new ShoveGestureListener());
}
}

/**
Expand Down Expand Up @@ -287,7 +305,6 @@ boolean onGenericMotionEvent(MotionEvent event) {
return false;
}


/**
* Responsible for handling one finger gestures.
*/
Expand Down Expand Up @@ -355,10 +372,7 @@ public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
annotationManager.deselectMarkers();
}

// notify app of map click
if (onMapClickListener != null) {
onMapClickListener.onMapClick(projection.fromScreenLocation(tapPoint));
}
notifyOnMapClickListeners(tapPoint);
}

MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
Expand All @@ -370,9 +384,10 @@ public boolean onSingleTapConfirmed(MotionEvent motionEvent) {

@Override
public void onLongPress(MotionEvent motionEvent) {
if (onMapLongClickListener != null && !quickZoom) {
onMapLongClickListener.onMapLongClick(
projection.fromScreenLocation(new PointF(motionEvent.getX(), motionEvent.getY())));
PointF longClickPoint = new PointF(motionEvent.getX(), motionEvent.getY());

if (!quickZoom) {
notifyOnMapLongClickListeners(longClickPoint);
}
}

Expand Down Expand Up @@ -412,9 +427,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
// update transformation
transform.moveBy(offsetX, offsetY, animationTime);

if (onFlingListener != null) {
onFlingListener.onFling();
}
notifyOnFlingListeners();
return true;
}

Expand Down Expand Up @@ -449,13 +462,59 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
// Scroll the map
transform.moveBy(-distanceX, -distanceY, 0 /*no duration*/);

if (onScrollListener != null) {
onScrollListener.onScroll();
}
notifyOnScrollListeners();
return true;
}
}

void notifyOnMapClickListeners(PointF tapPoint) {
// deprecated API
if (onMapClickListener != null) {
onMapClickListener.onMapClick(projection.fromScreenLocation(tapPoint));
}

// new API
for (MapboxMap.OnMapClickListener listener : onMapClickListenerList) {
listener.onMapClick(projection.fromScreenLocation(tapPoint));
}
}

void notifyOnMapLongClickListeners(PointF longClickPoint) {
// deprecated API
if (onMapLongClickListener != null) {
onMapLongClickListener.onMapLongClick(projection.fromScreenLocation(longClickPoint));
}

// new API
for (MapboxMap.OnMapLongClickListener listener : onMapLongClickListenerList) {
listener.onMapLongClick(projection.fromScreenLocation(longClickPoint));
}
}

void notifyOnFlingListeners() {
// deprecated API
if (onFlingListener != null) {
onFlingListener.onFling();
}

// new API
for (MapboxMap.OnFlingListener listener : onFlingListenerList) {
listener.onFling();
}
}

void notifyOnScrollListeners() {
//deprecated API
if (onScrollListener != null) {
onScrollListener.onScroll();
}

// new API
for (MapboxMap.OnScrollListener listener : onScrollListenerList) {
listener.onScroll();
}
}

/**
* Responsible for handling two finger gestures and double-tap drag gestures.
*/
Expand Down Expand Up @@ -844,4 +903,36 @@ void setOnFlingListener(MapboxMap.OnFlingListener onFlingListener) {
void setOnScrollListener(MapboxMap.OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}

void addOnMapClickListener(MapboxMap.OnMapClickListener onMapClickListener) {
onMapClickListenerList.add(onMapClickListener);
}

void removeOnMapClickListener(MapboxMap.OnMapClickListener onMapClickListener) {
onMapClickListenerList.remove(onMapClickListener);
}

void addOnMapLongClickListener(MapboxMap.OnMapLongClickListener onMapLongClickListener) {
onMapLongClickListenerList.add(onMapLongClickListener);
}

void removeOnMapLongClickListener(MapboxMap.OnMapLongClickListener onMapLongClickListener) {
onMapLongClickListenerList.remove(onMapLongClickListener);
}

void addOnFlingListener(MapboxMap.OnFlingListener onFlingListener) {
onFlingListenerList.add(onFlingListener);
}

void removeOnFlingListener(MapboxMap.OnFlingListener onFlingListener) {
onFlingListenerList.remove(onFlingListener);
}

void addOnScrollListener(MapboxMap.OnScrollListener onScrollListener) {
onScrollListenerList.add(onScrollListener);
}

void removeOnScrollListener(MapboxMap.OnScrollListener onScrollListener) {
onScrollListenerList.remove(onScrollListener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -908,24 +908,64 @@ public void onFocalPointChanged(PointF pointF) {
private class RegisterTouchListener implements MapboxMap.OnRegisterTouchListener {

@Override
public void onRegisterMapClickListener(MapboxMap.OnMapClickListener listener) {
public void onSetMapClickListener(MapboxMap.OnMapClickListener listener) {
mapGestureDetector.setOnMapClickListener(listener);
}

@Override
public void onRegisterMapLongClickListener(MapboxMap.OnMapLongClickListener listener) {
public void onAddMapClickListener(MapboxMap.OnMapClickListener listener) {
mapGestureDetector.addOnMapClickListener(listener);
}

@Override
public void onRemoveMapClickListener(MapboxMap.OnMapClickListener listener) {
mapGestureDetector.removeOnMapClickListener(listener);
}

@Override
public void onSetMapLongClickListener(MapboxMap.OnMapLongClickListener listener) {
mapGestureDetector.setOnMapLongClickListener(listener);
}

@Override
public void onRegisterScrollListener(MapboxMap.OnScrollListener listener) {
public void onAddMapLongClickListener(MapboxMap.OnMapLongClickListener listener) {
mapGestureDetector.addOnMapLongClickListener(listener);
}

@Override
public void onRemoveMapLongClickListener(MapboxMap.OnMapLongClickListener listener) {
mapGestureDetector.removeOnMapLongClickListener(listener);
}

@Override
public void onSetScrollListener(MapboxMap.OnScrollListener listener) {
mapGestureDetector.setOnScrollListener(listener);
}

@Override
public void onRegisterFlingListener(MapboxMap.OnFlingListener listener) {
public void onAddScrollListener(MapboxMap.OnScrollListener listener) {
mapGestureDetector.addOnScrollListener(listener);
}

@Override
public void onRemoveScrollListener(MapboxMap.OnScrollListener listener) {
mapGestureDetector.removeOnScrollListener(listener);
}

@Override
public void onSetFlingListener(MapboxMap.OnFlingListener listener) {
mapGestureDetector.setOnFlingListener(listener);
}

@Override
public void onAddFlingListener(MapboxMap.OnFlingListener listener) {
mapGestureDetector.addOnFlingListener(listener);
}

@Override
public void onRemoveFlingListener(MapboxMap.OnFlingListener listener) {
mapGestureDetector.removeOnFlingListener(listener);
}
}

private static class MapZoomControllerListener implements ZoomButtonsController.OnZoomListener {
Expand Down
Loading