Skip to content

Commit 41e6f77

Browse files
author
Cameron Mace
authored
Route progress immutable (#415)
* rafactored monitor location method * fixed routeProgress object not updating in NavigationService * finished route progress and added test * reset navigation example
1 parent fc5259c commit 41e6f77

13 files changed

Lines changed: 1135 additions & 510 deletions

File tree

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ dex-count:
6565
cd mapbox; ./gradlew countDebugDexMethods
6666
cd mapbox; ./gradlew countReleaseDexMethods
6767

68+
navigation-fixtures:
69+
# Navigation: Taylor street to Page street
70+
curl "https://api.mapbox.com/directions/v5/mapbox/driving/-122.413165,37.795042;-122.433378,37.7727?geometries=polyline6&overview=full&steps=true&access_token=$(MAPBOX_ACCESS_TOKEN)" \
71+
-o mapbox/libandroid-services/src/test/resources/navigation.json
72+
6873
geocoding-fixtures:
6974
# Geocoding: 1600 Pennsylvania Ave NW
7075
curl "https://api.mapbox.com/geocoding/v5/mapbox.places/1600+pennsylvania+ave+nw.json?access_token=$(MAPBOX_ACCESS_TOKEN)" \

mapbox/app/src/main/java/com/mapbox/services/android/testapp/MasApplication.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.mapbox.mapboxsdk.Mapbox;
88
import com.squareup.leakcanary.LeakCanary;
99

10+
import timber.log.Timber;
11+
1012
public class MasApplication extends Application {
1113

1214
private static final String LOG_TAG = MasApplication.class.getSimpleName();
@@ -21,6 +23,10 @@ public void onCreate() {
2123
}
2224
LeakCanary.install(this);
2325

26+
if (BuildConfig.DEBUG) {
27+
Timber.plant(new Timber.DebugTree());
28+
}
29+
2430
// Access token
2531
String mapboxAccessToken = Utils.getMapboxAccessToken(getApplicationContext());
2632
if (TextUtils.isEmpty(mapboxAccessToken)) {

mapbox/app/src/main/java/com/mapbox/services/android/testapp/nav/NavigationActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void onRunning(boolean running) {
197197

198198
@Override
199199
public void onProgressChange(Location location, RouteProgress routeProgress) {
200-
Timber.d("onProgressChange: fraction of route traveled: %d", routeProgress.getFractionTraveledOnRoute());
200+
Timber.d("onProgressChange: fraction of route traveled: %d", routeProgress.getFractionTraveled());
201201
}
202202

203203
@Override

mapbox/libandroid-services/src/main/java/com/mapbox/services/android/navigation/v5/LocationUpdatedThread.java

Lines changed: 111 additions & 92 deletions
Large diffs are not rendered by default.

mapbox/libandroid-services/src/main/java/com/mapbox/services/android/navigation/v5/NavigationService.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
import android.support.v4.app.NotificationCompat;
1414

1515
import com.mapbox.services.Experimental;
16+
import com.mapbox.services.android.Constants;
1617
import com.mapbox.services.android.R;
1718
import com.mapbox.services.android.telemetry.location.LocationEngine;
1819
import com.mapbox.services.android.telemetry.location.LocationEngineListener;
1920
import com.mapbox.services.api.directions.v5.models.DirectionsRoute;
2021
import com.mapbox.services.api.navigation.v5.RouteProgress;
22+
import com.mapbox.services.commons.models.Position;
2123

2224
import timber.log.Timber;
2325

@@ -42,6 +44,7 @@ public class NavigationService extends Service implements LocationEngineListener
4244
private LocationUpdatedThread locationUpdatedThread;
4345

4446
private RouteProgress routeProgress;
47+
private DirectionsRoute directionsRoute;
4548

4649
@Override
4750
public void onCreate() {
@@ -66,7 +69,8 @@ public void setupNotification(Activity activity) {
6669
// Sets up the top bar notification
6770
notifyBuilder = new NotificationCompat.Builder(this)
6871
.setContentTitle("Mapbox Navigation")
69-
.setContentText("Distance: " + routeProgress.getDistanceRemainingOnStep())
72+
.setContentText("Distance: " + routeProgress.getCurrentLegProgress().getCurrentStepProgress()
73+
.getDistanceRemaining())
7074
.setSmallIcon(R.drawable.ic_navigation_black)
7175
.setContentIntent(PendingIntent.getActivity(this, 0,
7276
new Intent(this, activity.getClass()), 0));
@@ -116,18 +120,27 @@ public void setSnapToRoute(boolean snapToRoute) {
116120

117121
private void startNavigation() {
118122
Timber.d("Navigation session started.");
119-
routeProgress = new RouteProgress();
123+
if (navigationEventListener != null) {
124+
navigationEventListener.onRunning(true);
125+
}
120126

121127
Handler responseHandler = new Handler();
122128
locationUpdatedThread = new LocationUpdatedThread(responseHandler);
123129
locationUpdatedThread.start();
124130
locationUpdatedThread.getLooper();
125131
Timber.d("Background thread started");
132+
133+
locationUpdatedThread.setNewRouteProgressListener(new NewRouteProgressListener() {
134+
@Override
135+
public void onRouteProgressChange(RouteProgress routeProgress) {
136+
NavigationService.this.routeProgress = routeProgress;
137+
}
138+
});
126139
}
127140

128-
public void startRoute(DirectionsRoute route) {
141+
public void startRoute(DirectionsRoute directionsRoute) {
129142
Timber.d("Start Route called.");
130-
routeProgress.setRoute(route);
143+
this.directionsRoute = directionsRoute;
131144

132145
if (locationEngine != null) {
133146
// Begin listening into location at its highest accuracy and add navigation location listener
@@ -191,18 +204,35 @@ public void setLocationEngine(LocationEngine locationEngine) {
191204
@Override
192205
public void onConnected() {
193206
Timber.d("NavigationService now connected to location listener");
194-
locationEngine.requestLocationUpdates();
195207
Location lastLocation = locationEngine.getLastLocation();
208+
if (routeProgress == null) {
209+
Timber.d("Create new routeProgress object");
210+
routeProgress = new RouteProgress(
211+
directionsRoute,
212+
Position.fromCoordinates(lastLocation.getLongitude(), lastLocation.getLatitude()),
213+
0, 0,
214+
Constants.NONE_ALERT_LEVEL
215+
);
216+
}
196217
if (locationUpdatedThread != null && lastLocation != null) {
197-
locationUpdatedThread.updateLocation(routeProgress, lastLocation);
218+
locationUpdatedThread.updateLocation(directionsRoute, routeProgress, lastLocation);
198219
}
199220
}
200221

201222
@Override
202223
public void onLocationChanged(Location location) {
203224
Timber.d("LocationChange occurred");
225+
if (routeProgress == null && location != null) {
226+
Timber.d("Create new routeProgress object");
227+
routeProgress = new RouteProgress(
228+
directionsRoute,
229+
Position.fromCoordinates(location.getLongitude(), location.getLatitude()),
230+
0, 0,
231+
Constants.NONE_ALERT_LEVEL
232+
);
233+
}
204234
if (locationUpdatedThread != null && location != null) {
205-
locationUpdatedThread.updateLocation(routeProgress, location);
235+
locationUpdatedThread.updateLocation(directionsRoute, routeProgress, location);
206236
}
207237
}
208238
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.mapbox.services.android.navigation.v5;
2+
3+
import com.mapbox.services.api.navigation.v5.RouteProgress;
4+
5+
public interface NewRouteProgressListener {
6+
void onRouteProgressChange(RouteProgress routeProgress);
7+
}

mapbox/libandroid-services/src/test/java/com/mapbox/services/android/navigation/v5/BaseNavigationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
public class BaseNavigationTest {
99

10+
protected static final double DELTA = 1E-10;
11+
1012
static String convertStreamToString(InputStream is) throws IOException {
1113
final int bufferSize = 1024;
1214
final char[] buffer = new char[bufferSize];
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.mapbox.services.android.navigation.v5;
2+
3+
import com.google.gson.Gson;
4+
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
5+
import com.mapbox.services.api.directions.v5.models.DirectionsRoute;
6+
import com.mapbox.services.api.navigation.v5.RouteProgress;
7+
import com.mapbox.services.commons.models.Position;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
public class RouteProgressTest extends BaseNavigationTest {
18+
19+
private static final int NONE_ALERT_LEVEL = 0;
20+
private static final int LOW_ALERT_LEVEL = 2;
21+
private static final String NAVIGATION_FIXTURE = "navigation.json";
22+
23+
private DirectionsRoute route;
24+
private RouteProgress routeProgress;
25+
26+
@Before
27+
public void setUp() throws IOException {
28+
Gson gson = new Gson();
29+
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(NAVIGATION_FIXTURE);
30+
String body = convertStreamToString(inputStream);
31+
DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
32+
route = response.getRoutes().get(0);
33+
34+
Position userSnappedPosition = Position.fromCoordinates(-122.413165, 37.795042);
35+
routeProgress = new RouteProgress(route, userSnappedPosition, 0, 0, NONE_ALERT_LEVEL);
36+
}
37+
38+
@Test
39+
public void routeProgressTest() {
40+
assertEquals(routeProgress.getCurrentLeg(), route.getLegs().get(0));
41+
assertEquals(routeProgress.getAlertUserLevel(), NONE_ALERT_LEVEL);
42+
assertEquals(routeProgress.getLegIndex(), 0);
43+
assertEquals(routeProgress.getFractionTraveled(), 0, DELTA);
44+
assertEquals(routeProgress.getDistanceRemaining(), 4317.5, 0.1);
45+
assertEquals(routeProgress.getDistanceTraveled(), 0, DELTA);
46+
assertEquals(routeProgress.getDurationRemaining(), 916, DELTA);
47+
}
48+
49+
@Test
50+
public void routeLegProgressTest() {
51+
assertEquals(routeProgress.getCurrentLegProgress().getUpComingStep(), route.getLegs().get(0).getSteps().get(1));
52+
assertEquals(routeProgress.getCurrentLegProgress().getDistanceRemaining(), 4317.5, 0.1);
53+
assertEquals(routeProgress.getCurrentLegProgress().getDurationRemaining(), 916);
54+
assertEquals(routeProgress.getCurrentLegProgress().getFractionTraveled(), 0, DELTA);
55+
assertEquals(routeProgress.getCurrentLegProgress().getCurrentStep(), route.getLegs().get(0).getSteps().get(0));
56+
assertEquals(routeProgress.getCurrentLegProgress().getDistanceTraveled(), 0, DELTA);
57+
assertEquals(routeProgress.getCurrentLegProgress().getPreviousStep(), null);
58+
assertEquals(routeProgress.getCurrentLegProgress().getStepIndex(), 0);
59+
}
60+
61+
@Test
62+
public void routeStepProgressTest() {
63+
assertEquals(routeProgress.getCurrentLegProgress().getCurrentStepProgress().getDistanceRemaining(), 279.8, 0.1);
64+
assertEquals(routeProgress.getCurrentLegProgress().getCurrentStepProgress().getFractionTraveled(), 0, DELTA);
65+
assertEquals(routeProgress.getCurrentLegProgress().getCurrentStepProgress().getDistanceTraveled(), 0, DELTA);
66+
assertEquals(routeProgress.getCurrentLegProgress().getCurrentStepProgress().getDurationRemaining(), 69, DELTA);
67+
}
68+
69+
70+
@Test
71+
public void nextRouteStepProgressTest() {
72+
Position userNextStepSnappedPosition = route.getLegs().get(0).getSteps().get(1).getManeuver().asPosition();
73+
RouteProgress nextRouteProgress = new RouteProgress(route, userNextStepSnappedPosition, 0, 1, LOW_ALERT_LEVEL);
74+
75+
assertEquals(nextRouteProgress.getCurrentLeg(), route.getLegs().get(0));
76+
assertEquals(nextRouteProgress.getAlertUserLevel(), LOW_ALERT_LEVEL);
77+
assertEquals(nextRouteProgress.getLegIndex(), 0);
78+
assertEquals(nextRouteProgress.getDistanceRemaining(), 4037.6, 0.1);
79+
assertEquals(nextRouteProgress.getDistanceTraveled(), 279.8, 0.1);
80+
assertEquals(nextRouteProgress.getDurationRemaining(), 856, DELTA);
81+
assertEquals(nextRouteProgress.getFractionTraveled(), 0.0648, 0.0001);
82+
}
83+
}

0 commit comments

Comments
 (0)