Skip to content

Commit

Permalink
Use runnable Date for snapped location / off route
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Sep 17, 2018
1 parent 0b49379 commit 26995b6
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 509 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public void userOffRoute(Location location) {
if (hasNetworkConnection()) {
speechPlayer.onOffRoute();
Point newOrigin = Point.fromLngLat(location.getLongitude(), location.getLatitude());
sendEventOffRoute(newOrigin);
handleOffRouteEvent(newOrigin);
}
}
};
Expand Down Expand Up @@ -446,7 +446,7 @@ private void sendEventArrival(RouteProgress routeProgress, Milestone milestone)
}
}

private void sendEventOffRoute(Point newOrigin) {
private void handleOffRouteEvent(Point newOrigin) {
if (navigationViewEventDispatcher != null && navigationViewEventDispatcher.allowRerouteFrom(newOrigin)) {
navigationViewEventDispatcher.onOffRoute(newOrigin);
OffRouteEvent event = new OffRouteEvent(newOrigin, routeProgress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public MapboxNavigation(@NonNull Context context, @NonNull String accessToken,
this.options = options;
this.navigationTelemetry = navigationTelemetry;
this.locationEngine = locationEngine;
initialize();
initializeForTest();
}

// Package private (no modifier) for testing purposes
Expand All @@ -130,7 +130,22 @@ public MapboxNavigation(@NonNull Context context, @NonNull String accessToken,
this.options = MapboxNavigationOptions.builder().build();
this.navigationTelemetry = navigationTelemetry;
this.locationEngine = locationEngine;
initialize();
initializeForTest();
}

private void initializeForTest() {
// Initialize event dispatcher and add internal listeners
navigationEventDispatcher = new NavigationEventDispatcher();
navigationEngineFactory = new NavigationEngineFactory();
initializeDefaultLocationEngine();
initializeTelemetry();

// Create and add default milestones if enabled.
milestones = new HashSet<>();
if (options.defaultMilestonesEnabled()) {
addMilestone(new VoiceInstructionMilestone.Builder().setIdentifier(VOICE_INSTRUCTION_MILESTONE_ID).build());
addMilestone(new BannerInstructionMilestone.Builder().setIdentifier(BANNER_INSTRUCTION_MILESTONE_ID).build());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.mapbox.services.android.navigation.v5.navigation;

import android.location.Location;

import com.mapbox.navigator.Navigator;
import com.mapbox.services.android.navigation.v5.navigation.camera.Camera;
import com.mapbox.services.android.navigation.v5.navigation.camera.SimpleCamera;
import com.mapbox.services.android.navigation.v5.offroute.OffRoute;
import com.mapbox.services.android.navigation.v5.offroute.OffRouteDetector;
import com.mapbox.services.android.navigation.v5.route.FasterRoute;
import com.mapbox.services.android.navigation.v5.route.FasterRouteDetector;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
import com.mapbox.services.android.navigation.v5.snap.Snap;
import com.mapbox.services.android.navigation.v5.snap.SnapToRoute;

Expand All @@ -21,6 +24,24 @@ class NavigationEngineFactory {
initializeDefaultEngines(navigator);
}

// For testing purposes only
NavigationEngineFactory() {
cameraEngine = new SimpleCamera();
fasterRouteEngine = new FasterRouteDetector();
snapEngine = new Snap() {
@Override
public Location getSnappedLocation(Location location, RouteProgress routeProgress) {
return location;
}
};
offRouteEngine = new OffRoute() {
@Override
public boolean isUserOffRoute(Location location, RouteProgress routeProgress, MapboxNavigationOptions options) {
return false;
}
};
}

OffRoute retrieveOffRouteEngine() {
return offRouteEngine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public void onConnected() {
public void onLocationChanged(Location location) {
navigator.updateLocation(buildFixLocationFrom(location));
thread.updateLocation(location);
if (!thread.isAlive()) {
thread.start();
}
}

boolean isValidLocationUpdate(Location location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import java.util.List;

import timber.log.Timber;

/**
* This class extends handler thread to run most of the navigation calculations on a separate
* background thread.
Expand All @@ -37,7 +35,6 @@ class RouteProcessorBackgroundThread extends HandlerThread {
@Override
public synchronized void start() {
super.start();
Timber.d("NAV_DEBUG *background thread running*");
if (workerHandler == null) {
workerHandler = new Handler(getLooper());
}
Expand All @@ -50,8 +47,10 @@ public synchronized void start() {
}

void updateLocation(Location location) {
Timber.d("NAV_DEBUG background thread Location updated");
unfilteredLocation = location;
if (!isAlive()) {
start();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.services.android.navigation.v5.milestone.Milestone;
import com.mapbox.services.android.navigation.v5.offroute.OffRoute;
import com.mapbox.services.android.navigation.v5.offroute.OffRouteDetector;
import com.mapbox.services.android.navigation.v5.route.FasterRoute;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
import com.mapbox.services.android.navigation.v5.snap.Snap;
import com.mapbox.services.android.navigation.v5.snap.SnapToRoute;

import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -49,27 +51,34 @@ private void process() {
MapboxNavigationOptions options = mapboxNavigation.options();
Location rawLocation = locationUpdate.location();
DirectionsRoute route = mapboxNavigation.getRoute();
RouteProgress routeProgress = routeProcessor.buildNewRouteProgress(new Date(), route);
Date currentDate = new Date();
RouteProgress routeProgress = routeProcessor.buildNewRouteProgress(currentDate, route);

NavigationEngineFactory engineFactory = mapboxNavigation.retrieveEngineFactory();
final boolean userOffRoute = isUserOffRoute(options, rawLocation, routeProgress, engineFactory);
final Location snappedLocation = findSnappedLocation(rawLocation, routeProgress, engineFactory);
final boolean userOffRoute = isUserOffRoute(options, currentDate, rawLocation, routeProgress, engineFactory);
final Location snappedLocation = findSnappedLocation(currentDate, rawLocation, routeProgress, engineFactory);
final boolean checkFasterRoute = checkFasterRoute(options, rawLocation, routeProgress, engineFactory, userOffRoute);
final List<Milestone> milestones = findTriggeredMilestones(mapboxNavigation, routeProgress);

workerHandler.postDelayed(this, ONE_SECOND);
sendUpdateToResponseHandler(userOffRoute, milestones, snappedLocation, checkFasterRoute, routeProgress);
}

private boolean isUserOffRoute(MapboxNavigationOptions options, Location rawLocation,
private boolean isUserOffRoute(MapboxNavigationOptions options, Date date, Location rawLocation,
RouteProgress routeProgress, NavigationEngineFactory engineFactory) {
OffRoute offRoute = engineFactory.retrieveOffRouteEngine();
if (offRoute instanceof OffRouteDetector) {
return ((OffRouteDetector) offRoute).isUserOffRouteWith(date);
}
return offRoute.isUserOffRoute(rawLocation, routeProgress, options);
}

private Location findSnappedLocation(Location rawLocation, RouteProgress routeProgress,
private Location findSnappedLocation(Date date, Location rawLocation, RouteProgress routeProgress,
NavigationEngineFactory engineFactory) {
Snap snap = engineFactory.retrieveSnapEngine();
if (snap instanceof SnapToRoute) {
return ((SnapToRoute) snap).getSnappedLocationWith(rawLocation, date);
}
return snap.getSnappedLocation(rawLocation, routeProgress);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ public OffRouteDetector(Navigator navigator) {

@Override
public boolean isUserOffRoute(Location location, RouteProgress routeProgress, MapboxNavigationOptions options) {
return determineIsUserOffRoute(location);
// No impl
return false;
}

private boolean determineIsUserOffRoute(Location location) {
Date locationDate = new Date(location.getTime());
NavigationStatus status = navigator.getStatus(locationDate);
public boolean isUserOffRouteWith(Date date) {
return determineIsUserOffRoute(date);
}

private boolean determineIsUserOffRoute(Date date) {
NavigationStatus status = navigator.getStatus(date);
return status.getRouteState() == RouteState.OFFROUTE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public int remainingWaypoints() {
@Nullable
public abstract List<Point> upcomingStepPoints();

/**
* Returns whether or not the location updates are
* considered in a tunnel along the route.
*
* @return true if in a tunnel, false otherwise
* @since 0.19.0
*/
public abstract boolean inTunnel();

public abstract RouteProgress.Builder toBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.services.android.navigation.v5.snap;

import android.location.Location;
import android.support.annotation.NonNull;

import com.mapbox.navigator.NavigationStatus;
import com.mapbox.navigator.Navigator;
Expand All @@ -18,12 +19,17 @@ public SnapToRoute(Navigator navigator) {

@Override
public Location getSnappedLocation(Location location, RouteProgress routeProgress) {
return buildSnappedLocation(location);
// No impl
return location;
}

private Location buildSnappedLocation(Location location) {
Date locationDate = new Date(location.getTime());
NavigationStatus status = navigator.getStatus(locationDate);
public Location getSnappedLocationWith(Location location, Date date) {
return buildSnappedLocation(location, date);
}

@NonNull
private Location buildSnappedLocation(Location location, Date date) {
NavigationStatus status = navigator.getStatus(date);
Location snappedLocation = new Location(location);
snappedLocation.setLatitude(status.getLocation().latitude());
snappedLocation.setLongitude(status.getLocation().longitude());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.mapbox.services.android.navigation.v5.snap.Snap;
import com.mapbox.services.android.navigation.v5.snap.SnapToRoute;

import org.junit.Ignore;
import org.junit.Test;

import java.util.ArrayList;
Expand Down Expand Up @@ -218,6 +219,7 @@ public void getLocationEngine_returnsCorrectLocationEngine() throws Exception {
}

@Test
@Ignore
public void startNavigation_doesSendTrueToNavigationEvent() throws Exception {
MapboxNavigation navigation = buildMapboxNavigation();
NavigationEventListener navigationEventListener = mock(NavigationEventListener.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ public class NavigationEngineFactoryTest {

@Test
public void onInitialization_defaultCameraEngineIsCreated() {
NavigationEngineFactory provider = new NavigationEngineFactory();
NavigationEngineFactory provider = buildNavigationEngineFactory();

assertNotNull(provider.retrieveCameraEngine());
}

@Test
public void onInitialization_defaultOffRouteEngineIsCreated() {
NavigationEngineFactory provider = new NavigationEngineFactory();
NavigationEngineFactory provider = buildNavigationEngineFactory();

assertNotNull(provider.retrieveOffRouteEngine());
}

@Test
public void onInitialization_defaultSnapEngineIsCreated() {
NavigationEngineFactory provider = new NavigationEngineFactory();
NavigationEngineFactory provider = buildNavigationEngineFactory();

assertNotNull(provider.retrieveSnapEngine());
}

@Test
public void onInitialization_defaultFasterRouteEngineIsCreated() {
NavigationEngineFactory provider = new NavigationEngineFactory();
NavigationEngineFactory provider = buildNavigationEngineFactory();

assertNotNull(provider.retrieveFasterRouteEngine());
}

@Test
public void updateFasterRouteEngine_ignoresNull() {
NavigationEngineFactory provider = new NavigationEngineFactory();
NavigationEngineFactory provider = buildNavigationEngineFactory();

provider.updateFasterRouteEngine(null);

Expand All @@ -45,7 +45,7 @@ public void updateFasterRouteEngine_ignoresNull() {

@Test
public void updateOffRouteEngine_ignoresNull() {
NavigationEngineFactory provider = new NavigationEngineFactory();
NavigationEngineFactory provider = buildNavigationEngineFactory();

provider.updateOffRouteEngine(null);

Expand All @@ -54,7 +54,7 @@ public void updateOffRouteEngine_ignoresNull() {

@Test
public void updateCameraEngine_ignoresNull() {
NavigationEngineFactory provider = new NavigationEngineFactory();
NavigationEngineFactory provider = buildNavigationEngineFactory();

provider.updateCameraEngine(null);

Expand All @@ -63,10 +63,14 @@ public void updateCameraEngine_ignoresNull() {

@Test
public void updateSnapEngine_ignoresNull() {
NavigationEngineFactory provider = new NavigationEngineFactory();
NavigationEngineFactory provider = buildNavigationEngineFactory();

provider.updateSnapEngine(null);

assertNotNull(provider.retrieveSnapEngine());
}

private NavigationEngineFactory buildNavigationEngineFactory() {
return new NavigationEngineFactory();
}
}
Loading

0 comments on commit 26995b6

Please sign in to comment.