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

Rewire gesture handling and telemetry event push #9494

Merged
merged 1 commit into from
Jul 17, 2017
Merged
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 @@ -50,13 +50,12 @@ final class MapGestureDetector {

private PointF focalPoint;

private boolean twoTap = false;
private boolean zoomStarted = false;
private boolean dragStarted = false;
private boolean quickZoom = false;
private boolean scrollInProgress = false;
private boolean scaleGestureOccurred = false;
private boolean recentScaleGestureOccurred = false;
private boolean twoTap;
private boolean quickZoom;
private boolean tiltGestureOccurred;
private boolean scrollGestureOccurred;
private boolean scaleGestureOccurred;
private boolean recentScaleGestureOccurred;

MapGestureDetector(Context context, Transform transform, Projection projection, UiSettings uiSettings,
TrackingSettings trackingSettings, AnnotationManager annotationManager,
Expand Down Expand Up @@ -193,10 +192,10 @@ boolean onTouchEvent(MotionEvent event) {
}

// Scroll / Pan Has Stopped
if (scrollInProgress) {
if (scrollGestureOccurred) {
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapDragEndEvent(
getLocationFromGesture(event.getX(), event.getY()), transform));
scrollInProgress = false;
scrollGestureOccurred = false;
cameraChangeDispatcher.onCameraIdle();
}

Expand Down Expand Up @@ -393,20 +392,18 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
return false;
}

if (dragStarted) {
if (tiltGestureOccurred) {
return false;
}

if (scaleGestureOccurred) {
return false;
}

if (!scrollInProgress) {
scrollInProgress = true;
if (!scrollGestureOccurred) {
scrollGestureOccurred = true;

// Cancel any animation
transform.cancelTransitions();
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);
if (!scaleGestureOccurred) {
transform.cancelTransitions();
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);
}

MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
getLocationFromGesture(e1.getX(), e1.getY()),
Expand Down Expand Up @@ -456,7 +453,6 @@ public void onScaleEnd(ScaleGestureDetector detector) {
scaleGestureOccurred = false;
beginTime = 0;
scaleFactor = 1.0f;
zoomStarted = false;
cameraChangeDispatcher.onCameraIdle();
}

Expand All @@ -468,27 +464,27 @@ public boolean onScale(ScaleGestureDetector detector) {
return super.onScale(detector);
}

// If scale is large enough ignore a tap
scaleFactor *= detector.getScaleFactor();
if ((scaleFactor > 1.05f) || (scaleFactor < 0.95f)) {
// notify camera change listener
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);
zoomStarted = true;
if (tiltGestureOccurred) {
return false;
}

// Ignore short touches in case it is a tap
// Also ignore small scales
long time = detector.getEventTime();
long interval = time - beginTime;
if (!zoomStarted && (interval <= ViewConfiguration.getTapTimeout())) {
if (!scaleGestureOccurred && (interval <= ViewConfiguration.getTapTimeout())) {
return false;
}

if (!zoomStarted) {
return false;
// If scale is large enough ignore a tap
scaleFactor *= detector.getScaleFactor();
if ((scaleFactor > 1.05f) || (scaleFactor < 0.95f)) {
// notify camera change listener
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);
scaleGestureOccurred = true;
}

if (dragStarted) {
if (!scaleGestureOccurred) {
return false;
}

Expand All @@ -506,18 +502,20 @@ public boolean onScale(ScaleGestureDetector detector) {
// Scale the map
if (focalPoint != null) {
// arround user provided focal point
transform.zoomBy(Math.log(detector.getScaleFactor()) / Math.log(2), focalPoint.x, focalPoint.y);
transform.zoomBy(Math.log(detector.getScaleFactor()) / Math.log(Math.PI / 2), focalPoint.x, focalPoint.y);
} else if (quickZoom) {
cameraChangeDispatcher.onCameraMove();
// clamp scale factors we feed to core #7514
float scaleFactor = MathUtils.clamp(detector.getScaleFactor(),
MapboxConstants.MINIMUM_SCALE_FACTOR_CLAMP,
MapboxConstants.MAXIMUM_SCALE_FACTOR_CLAMP);
// around center map
transform.zoomBy(Math.log(scaleFactor) / Math.log(2), uiSettings.getWidth() / 2, uiSettings.getHeight() / 2);
transform.zoomBy(Math.log(scaleFactor) / Math.log(Math.PI / 2),
uiSettings.getWidth() / 2, uiSettings.getHeight() / 2);
} else {
// around gesture
transform.zoomBy(Math.log(detector.getScaleFactor()) / Math.log(2), detector.getFocusX(), detector.getFocusY());
transform.zoomBy(Math.log(detector.getScaleFactor()) / Math.log(Math.PI / 2),
detector.getFocusX(), detector.getFocusY());
}

return true;
Expand All @@ -544,9 +542,6 @@ public boolean onRotateBegin(RotateGestureDetector detector) {
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);

beginTime = detector.getEventTime();
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
getLocationFromGesture(detector.getFocusX(), detector.getFocusY()),
MapboxEvent.GESTURE_ROTATION_START, transform));
return true;
}

Expand All @@ -563,17 +558,10 @@ public void onRotateEnd(RotateGestureDetector detector) {
// Called for rotation
@Override
public boolean onRotate(RotateGestureDetector detector) {
if (!trackingSettings.isRotateGestureCurrentlyEnabled() || dragStarted) {
if (!trackingSettings.isRotateGestureCurrentlyEnabled() || tiltGestureOccurred) {
return false;
}

// If rotate is large enough ignore a tap
// Also is zoom already started, don't rotate
totalAngle += detector.getRotationDegreesDelta();
if (totalAngle > 20.0f || totalAngle < -20.0f) {
started = true;
}

// Ignore short touches in case it is a tap
// Also ignore small rotate
long time = detector.getEventTime();
Expand All @@ -582,6 +570,16 @@ public boolean onRotate(RotateGestureDetector detector) {
return false;
}

// If rotate is large enough ignore a tap
// Also is zoom already started, don't rotate
totalAngle += detector.getRotationDegreesDelta();
if (totalAngle > 35.0f || totalAngle < -35.0f) {
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
getLocationFromGesture(detector.getFocusX(), detector.getFocusY()),
MapboxEvent.GESTURE_ROTATION_START, transform));
started = true;
}

if (!started) {
return false;
}
Expand Down Expand Up @@ -610,9 +608,8 @@ public boolean onRotate(RotateGestureDetector detector) {
*/
private class ShoveGestureListener implements ShoveGestureDetector.OnShoveGestureListener {

long beginTime = 0;
float totalDelta = 0.0f;
boolean started = false;
private long beginTime = 0;
private float totalDelta = 0.0f;

@Override
public boolean onShoveBegin(ShoveGestureDetector detector) {
Expand All @@ -622,19 +619,14 @@ public boolean onShoveBegin(ShoveGestureDetector detector) {

// notify camera change listener
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);
beginTime = detector.getEventTime();
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
getLocationFromGesture(detector.getFocusX(), detector.getFocusY()),
MapboxEvent.GESTURE_PITCH_START, transform));
return true;
}

@Override
public void onShoveEnd(ShoveGestureDetector detector) {
beginTime = 0;
totalDelta = 0.0f;
started = false;
dragStarted = false;
tiltGestureOccurred = false;
}

@Override
Expand All @@ -643,22 +635,26 @@ public boolean onShove(ShoveGestureDetector detector) {
return false;
}

// If tilt is large enough ignore a tap
// Also if zoom already started, don't tilt
totalDelta += detector.getShovePixelsDelta();
if (!zoomStarted && ((totalDelta > 10.0f) || (totalDelta < -10.0f))) {
started = true;
}

// Ignore short touches in case it is a tap
// Also ignore small tilt
long time = detector.getEventTime();
long interval = time - beginTime;
if (!started && (interval <= ViewConfiguration.getTapTimeout())) {
if (!tiltGestureOccurred && (interval <= ViewConfiguration.getTapTimeout())) {
return false;
}

if (!started) {
// If tilt is large enough ignore a tap
// Also if zoom already started, don't tilt
totalDelta += detector.getShovePixelsDelta();
if (!tiltGestureOccurred && ((totalDelta > 10.0f) || (totalDelta < -10.0f))) {
tiltGestureOccurred = true;
beginTime = detector.getEventTime();
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
getLocationFromGesture(detector.getFocusX(), detector.getFocusY()),
MapboxEvent.GESTURE_PITCH_START, transform));
}

if (!tiltGestureOccurred) {
return false;
}

Expand All @@ -669,9 +665,6 @@ public boolean onShove(ShoveGestureDetector detector) {

// Tilt the map
transform.setTilt(pitch);

dragStarted = true;

return true;
}
}
Expand Down