diff options
author | Guardiola31337 <pablo.guardiola@mapbox.com> | 2018-01-29 18:30:29 +0100 |
---|---|---|
committer | Guardiola31337 <pablo.guardiola@mapbox.com> | 2018-01-29 18:30:29 +0100 |
commit | 0dd19374e53fc64fad841770337566f164d8447b (patch) | |
tree | 30dd56a36cca58690473e3a27532f9d546ba5dd4 | |
parent | a30a81b53e3a985ca87eda6495b38957e7568795 (diff) | |
download | qtlocation-mapboxgl-upstream/pg-fix-events-lib-integration-comments.tar.gz |
fix #10999 commentsupstream/pg-fix-events-lib-integration-comments
6 files changed, 115 insertions, 78 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/build.gradle b/platform/android/MapboxGLAndroidSDK/build.gradle index a1028aa1d0..8305a0cf87 100644 --- a/platform/android/MapboxGLAndroidSDK/build.gradle +++ b/platform/android/MapboxGLAndroidSDK/build.gradle @@ -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) } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java index 245424d769..5356242622 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java @@ -11,13 +11,10 @@ import android.text.TextUtils; 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> @@ -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. @@ -57,13 +52,6 @@ public final class Mapbox { 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; @@ -151,11 +139,8 @@ public final class Mapbox { * * @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; - } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java index 53472eb870..2bcbd5ce40 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java @@ -11,7 +11,6 @@ import android.view.View; 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; @@ -88,7 +87,7 @@ public class AttributionDialogManager implements View.OnClickListener, DialogInt 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(); } }); @@ -102,7 +101,7 @@ public class AttributionDialogManager implements View.OnClickListener, DialogInt 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(); } }); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Events.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Events.java new file mode 100644 index 0000000000..a68d4763ac --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Events.java @@ -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; + } +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java index 788ae48e8e..8047e19809 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java @@ -21,7 +21,6 @@ import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector; 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; @@ -189,13 +188,13 @@ final class MapGestureDetector { && 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; @@ -224,12 +223,12 @@ final class MapGestureDetector { // 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) { @@ -345,13 +344,13 @@ final class MapGestureDetector { // 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; } @@ -379,13 +378,13 @@ final class MapGestureDetector { 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; } @@ -459,13 +458,13 @@ final class MapGestureDetector { 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 @@ -548,13 +547,13 @@ final class MapGestureDetector { 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; } @@ -735,13 +734,13 @@ final class MapGestureDetector { // 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; } @@ -910,13 +909,13 @@ final class MapGestureDetector { 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) { @@ -981,4 +980,15 @@ final class MapGestureDetector { 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; + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 9bd28a2baa..cc25497f37 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -23,9 +23,11 @@ import android.widget.FrameLayout; 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; @@ -287,9 +289,12 @@ public class MapView extends FrameLayout { @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; } |