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

Commit

Permalink
fix #10999 comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Guardiola31337 committed Jan 29, 2018
1 parent a30a81b commit 0dd1937
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 78 deletions.
2 changes: 2 additions & 0 deletions platform/android/MapboxGLAndroidSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ android {
minSdkVersion androidVersions.minSdkVersion
targetSdkVersion androidVersions.targetSdkVersion
buildConfigField "String", "GIT_REVISION_SHORT", String.format("\"%s\"", getGitRevision())
// TODO Review sdkIdentifier and sdkVersion values (used in AppUserTurnstile)
buildConfigField "String", "MAPBOX_SDK_IDENTIFIER", String.format("\"%s\"", "MapboxMapsAndroid")
buildConfigField "String", "MAPBOX_VERSION_STRING", String.format("\"Mapbox/%s\"", project.VERSION_NAME)
buildConfigField "String", "MAPBOX_EVENTS_USER_AGENT", String.format("\"MapboxEventsAndroid/%s\"", project.VERSION_NAME)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEnginePriority;
import com.mapbox.android.core.location.LocationEngineProvider;
import com.mapbox.android.telemetry.MapboxTelemetry;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;

import timber.log.Timber;

/**
* The entry point to initialize the Mapbox Android SDK.
* <p>
Expand All @@ -35,8 +32,6 @@ public final class Mapbox {
private String accessToken;
private Boolean connected;
private LocationEngine locationEngine;
@SuppressLint("StaticFieldLeak")
private static MapboxTelemetry telemetry;

/**
* Get an instance of Mapbox.
Expand All @@ -57,13 +52,6 @@ public static synchronized Mapbox getInstance(@NonNull Context context, @NonNull
INSTANCE = new Mapbox(appContext, accessToken, locationEngine);
locationEngine.setPriority(LocationEnginePriority.NO_POWER);

try {
telemetry = new MapboxTelemetry(appContext, accessToken, BuildConfig.MAPBOX_EVENTS_USER_AGENT);
telemetry.enable();
} catch (Exception exception) {
Timber.e(exception, "Unable to instantiate Mapbox telemetry");
}

ConnectivityReceiver.instance(appContext);
}
return INSTANCE;
Expand Down Expand Up @@ -151,11 +139,8 @@ public static synchronized Boolean isConnected() {
*
* @return the location engine configured
*/
// TODO Do we need to expose this?
public static LocationEngine getLocationEngine() {
return INSTANCE.locationEngine;
}

public static MapboxTelemetry obtainTelemetry() {
return INSTANCE.telemetry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.attribution.Attribution;
import com.mapbox.mapboxsdk.attribution.AttributionParser;
Expand Down Expand Up @@ -88,7 +87,7 @@ private void showTelemetryDialog() {
builder.setPositiveButton(R.string.mapbox_attributionTelemetryPositive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Mapbox.obtainTelemetry().enable();
Events.obtainTelemetry().enable();
dialog.cancel();
}
});
Expand All @@ -102,7 +101,7 @@ public void onClick(DialogInterface dialog, int which) {
builder.setNegativeButton(R.string.mapbox_attributionTelemetryNegative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Mapbox.obtainTelemetry().disable();
Events.obtainTelemetry().disable();
dialog.cancel();
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mapbox.mapboxsdk.maps;


import com.mapbox.android.telemetry.MapboxTelemetry;
import com.mapbox.android.telemetry.TelemetryEnabler;
import com.mapbox.mapboxsdk.BuildConfig;
import com.mapbox.mapboxsdk.Mapbox;

class Events {
static final String TWO_FINGER_TAP = "TwoFingerTap";
static final String DOUBLE_TAP = "DoubleTap";
static final String SINGLE_TAP = "SingleTap";
static final String PAN = "Pan";
static final String PINCH = "Pinch";
static final String ROTATION = "Rotation";
static final String PITCH = "Pitch";
private MapboxTelemetry telemetry;

private Events() {
telemetry = new MapboxTelemetry(Mapbox.getApplicationContext(), Mapbox.getAccessToken(),
BuildConfig.MAPBOX_EVENTS_USER_AGENT);
TelemetryEnabler.State telemetryState = TelemetryEnabler.retrieveTelemetryStateFromPreferences();
if (TelemetryEnabler.State.NOT_INITIALIZED.equals(telemetryState)
|| TelemetryEnabler.State.ENABLED.equals(telemetryState)) {
telemetry.enable();
}
}

private static class EventsHolder {
private static final Events INSTANCE = new Events();
}

static MapboxTelemetry obtainTelemetry() {
return EventsHolder.INSTANCE.telemetry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.mapbox.android.telemetry.Event;
import com.mapbox.android.telemetry.MapEventFactory;
import com.mapbox.android.telemetry.MapState;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.MathUtils;
Expand Down Expand Up @@ -189,13 +188,13 @@ boolean onTouchEvent(MotionEvent event) {
&& uiSettings.isZoomGesturesEnabled();
if (twoTap) {
// Confirmed 2nd Finger Down
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(event.getX(), event.getY()));
// TODO transform.getZoom() may cause a NullPointerException
MapState twoFingerTap = new MapState((float) latLng.getLatitude(), (float) latLng.getLongitude(), (float)
transform.getZoom());
twoFingerTap.setGesture("TwoFingerTap");
Mapbox.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, twoFingerTap));
if (isZoomValid(transform)) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(event.getX(), event.getY()));
MapState twoFingerTap = new MapState(latLng.getLatitude(), latLng.getLongitude(), transform.getZoom());
twoFingerTap.setGesture(Events.TWO_FINGER_TAP);
Events.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, twoFingerTap));
}
}
break;

Expand Down Expand Up @@ -224,12 +223,12 @@ boolean onTouchEvent(MotionEvent event) {

// Scroll / Pan Has Stopped
if (scrollGestureOccurred) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(event.getX(), event.getY()));
// TODO transform.getZoom() may cause a NullPointerException
MapState dragend = new MapState((float) latLng.getLatitude(), (float) latLng.getLongitude(), (float)
transform.getZoom());
Mapbox.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_DRAGEND, dragend));
if (isZoomValid(transform)) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(event.getX(), event.getY()));
MapState dragend = new MapState(latLng.getLatitude(), latLng.getLongitude(), transform.getZoom());
Events.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_DRAGEND, dragend));
}
scrollGestureOccurred = false;

if (!scaleAnimating && !rotateAnimating) {
Expand Down Expand Up @@ -345,13 +344,13 @@ public boolean onDoubleTapEvent(MotionEvent e) {
// Zoom in on gesture
transform.zoom(true, new PointF(e.getX(), e.getY()));
}
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(e.getX(), e.getY()));
// TODO transform.getZoom() may cause a NullPointerException
MapState doubleTap = new MapState((float) latLng.getLatitude(), (float) latLng.getLongitude(), (float)
transform.getZoom());
doubleTap.setGesture("DoubleTap");
Mapbox.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, doubleTap));
if (isZoomValid(transform)) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(e.getX(), e.getY()));
MapState doubleTap = new MapState(latLng.getLatitude(), latLng.getLongitude(), transform.getZoom());
doubleTap.setGesture(Events.DOUBLE_TAP);
Events.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, doubleTap));
}
break;
}

Expand Down Expand Up @@ -379,13 +378,13 @@ public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
notifyOnMapClickListeners(tapPoint);
}

MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(motionEvent.getX(), motionEvent.getY()));
// TODO transform.getZoom() may cause a NullPointerException
MapState singleTap = new MapState((float) latLng.getLatitude(), (float) latLng.getLongitude(), (float)
transform.getZoom());
singleTap.setGesture("SingleTap");
Mapbox.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, singleTap));
if (isZoomValid(transform)) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(motionEvent.getX(), motionEvent.getY()));
MapState singleTap = new MapState(latLng.getLatitude(), latLng.getLongitude(), transform.getZoom());
singleTap.setGesture(Events.SINGLE_TAP);
Events.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, singleTap));
}

return true;
}
Expand Down Expand Up @@ -459,13 +458,13 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);
}

MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(e1.getX(), e1.getY()));
// TODO transform.getZoom() may cause a NullPointerException
MapState pan = new MapState((float) latLng.getLatitude(), (float) latLng.getLongitude(), (float) transform
.getZoom());
pan.setGesture("Pan");
Mapbox.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, pan));
if (isZoomValid(transform)) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(e1.getX(), e1.getY()));
MapState pan = new MapState(latLng.getLatitude(), latLng.getLongitude(), transform.getZoom());
pan.setGesture(Events.PAN);
Events.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, pan));
}
}

// reset tracking if needed
Expand Down Expand Up @@ -548,13 +547,13 @@ public boolean onScaleBegin(ScaleGestureDetector detector) {
recentScaleGestureOccurred = true;
scalePointBegin = new PointF(detector.getFocusX(), detector.getFocusY());
scaleBeginTime = detector.getEventTime();
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(detector.getFocusX(), detector.getFocusY()));
// TODO transform.getZoom() may cause a NullPointerException
MapState pinch = new MapState((float) latLng.getLatitude(), (float) latLng.getLongitude(), (float) transform
.getZoom());
pinch.setGesture("Pinch");
Mapbox.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, pinch));
if (isZoomValid(transform)) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(detector.getFocusX(), detector.getFocusY()));
MapState pinch = new MapState(latLng.getLatitude(), latLng.getLongitude(), transform.getZoom());
pinch.setGesture(Events.PINCH);
Events.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, pinch));
}
return true;
}

Expand Down Expand Up @@ -735,13 +734,13 @@ public boolean onRotate(RotateGestureDetector detector) {
// Also is zoom already started, don't rotate
float angle = detector.getRotationDegreesDelta();
if (Math.abs(angle) >= ROTATE_INVOKE_ANGLE) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(detector.getFocusX(), detector.getFocusY()));
// TODO transform.getZoom() may cause a NullPointerException
MapState rotation = new MapState((float) latLng.getLatitude(), (float) latLng.getLongitude(), (float)
transform.getZoom());
rotation.setGesture("Rotation");
Mapbox.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, rotation));
if (isZoomValid(transform)) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(detector.getFocusX(), detector.getFocusY()));
MapState rotation = new MapState(latLng.getLatitude(), latLng.getLongitude(), transform.getZoom());
rotation.setGesture(Events.ROTATION);
Events.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, rotation));
}
started = true;
}

Expand Down Expand Up @@ -910,13 +909,13 @@ public boolean onShove(ShoveGestureDetector detector) {
if (!tiltGestureOccurred && ((totalDelta > 10.0f) || (totalDelta < -10.0f))) {
tiltGestureOccurred = true;
beginTime = detector.getEventTime();
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(detector.getFocusX(), detector.getFocusY()));
// TODO transform.getZoom() may cause a NullPointerException
MapState pitch = new MapState((float) latLng.getLatitude(), (float) latLng.getLongitude(), (float) transform
.getZoom());
pitch.setGesture("Pitch");
Mapbox.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, pitch));
if (isZoomValid(transform)) {
MapEventFactory mapEventFactory = new MapEventFactory();
LatLng latLng = projection.fromScreenLocation(new PointF(detector.getFocusX(), detector.getFocusY()));
MapState pitch = new MapState(latLng.getLatitude(), latLng.getLongitude(), transform.getZoom());
pitch.setGesture(Events.PITCH);
Events.obtainTelemetry().push(mapEventFactory.createMapGestureEvent(Event.Type.MAP_CLICK, pitch));
}
}

if (!tiltGestureOccurred) {
Expand Down Expand Up @@ -981,4 +980,15 @@ void addOnScrollListener(MapboxMap.OnScrollListener onScrollListener) {
void removeOnScrollListener(MapboxMap.OnScrollListener onScrollListener) {
onScrollListenerList.remove(onScrollListener);
}

private boolean isZoomValid(Transform transform) {
if (transform == null) {
return false;
}
double mapZoom = transform.getZoom();
if (mapZoom < MapboxConstants.MINIMUM_ZOOM || mapZoom > MapboxConstants.MAXIMUM_ZOOM) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import android.widget.ImageView;
import android.widget.ZoomButtonsController;

import com.mapbox.android.telemetry.AppUserTurnstile;
import com.mapbox.android.telemetry.Event;
import com.mapbox.android.telemetry.MapEventFactory;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.android.telemetry.MapboxTelemetry;
import com.mapbox.mapboxsdk.BuildConfig;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.MarkerViewManager;
Expand Down Expand Up @@ -287,9 +289,12 @@ public void onClick(View v) {
@UiThread
public void onCreate(@Nullable Bundle savedInstanceState) {
if (savedInstanceState == null) {
// TODO Push AppUserTurnstile event
MapboxTelemetry telemetry = Events.obtainTelemetry();
AppUserTurnstile turnstileEvent = new AppUserTurnstile(BuildConfig.MAPBOX_SDK_IDENTIFIER,
BuildConfig.MAPBOX_VERSION_STRING);
telemetry.push(turnstileEvent);
MapEventFactory mapEventFactory = new MapEventFactory();
Mapbox.obtainTelemetry().push(mapEventFactory.createMapLoadEvent(Event.Type.MAP_LOAD));
telemetry.push(mapEventFactory.createMapLoadEvent(Event.Type.MAP_LOAD));
} else if (savedInstanceState.getBoolean(MapboxConstants.STATE_HAS_SAVED_STATE)) {
this.savedInstanceState = savedInstanceState;
}
Expand Down

0 comments on commit 0dd1937

Please sign in to comment.