Skip to content

Commit bb848c7

Browse files
author
Cameron Mace
authored
Navigation fixes (#367)
* changed start and end navigation positions to origin and destination * allow user to disable traffic when getting route * added exceptions to getRoute method and javadoc * added javadoc to direction methods in MapboxNavigation * fixed issue if alert listener wasn't set application would crash * fixed some navigation issues * fixed a memory leak with navigation service * fixed checkstyle issue * added offroute listener * checkstyle fixes * added test to PR
1 parent f847dd7 commit bb848c7

16 files changed

Lines changed: 1638 additions & 154 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ libandroid/app/src/main/res/values/developer-config.xml
2323

2424
# Do not track signing file
2525
secring.gpg
26+
27+
# Documentation files
28+
node_modules/

mapbox/dependencies.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ ext {
5353

5454
// testing
5555
junit : 'junit:junit:4.12',
56+
mockito : 'org.mockito:mockito-core:2.7.13',
5657
hamcrestJunit : 'org.hamcrest:hamcrest-junit:2.0.0.0',
5758
supportEspresso : 'com.android.support.test.espresso:espresso-core:2.2.2'
5859
]

mapbox/libandroid-services/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ android {
2424
consumerProguardFiles 'proguard-rules.pro'
2525
}
2626
}
27+
28+
testOptions {
29+
unitTests.returnDefaultValues = true
30+
}
2731
}
2832

2933
dependencies {
@@ -42,7 +46,11 @@ dependencies {
4246
compile rootProject.ext.dep.lost
4347

4448
// Testing
49+
testCompile rootProject.ext.dep.mockito
50+
testCompile rootProject.ext.dep.okhttp3Mockwebserver
51+
testCompile rootProject.ext.dep.hamcrestJunit
4552
testCompile rootProject.ext.dep.junit
53+
4654
androidTestCompile(rootProject.ext.dep.supportEspresso, {
4755
exclude group: 'com.android.support', module: 'support-annotations'
4856
})

mapbox/libandroid-services/src/androidTest/java/com/mapbox/services/android/ExampleInstrumentedTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
23
package="com.mapbox.services.android">
34

4-
<application android:allowBackup="true"
5-
android:label="@string/app_name"
6-
android:supportsRtl="true"
7-
>
5+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
6+
<uses-permission android:name="android.permission.INTERNET"/>
87

8+
<application
9+
android:allowBackup="true"
10+
android:label="@string/app_name"
11+
android:supportsRtl="true">
12+
13+
<service android:name="com.mapbox.services.android.navigation.v5.NavigationService"/>
914
</application>
1015

11-
</manifest>
16+
</manifest>

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,27 @@
44
import com.mapbox.services.api.navigation.v5.RouteProgress;
55

66
/**
7-
* This is an experimental API. Experimental APIs are quickly evolving and
8-
* might change or be removed in minor versions.
7+
* Listening in to the alertLevelChange is useful for correctly getting the timing of user notifications while the user
8+
* is traversing along the route. The listener's invoked only when the user's reached a specific point along the
9+
* current step they are on. The alert thresholds can be adjusted within the constants file while developing and are
10+
* based on time (in seconds) till the user reaches the next maneuver.
11+
* <p>
12+
* This is an experimental API. Experimental APIs are quickly evolving and might change or be removed in minor versions.
13+
*
14+
* @see <a href="https://www.mapbox.com/mapbox-java/#alertlevelchange">AlertLevelChange documentation</a>
15+
* @since 2.0.0
916
*/
1017
@Experimental
1118
public interface AlertLevelChangeListener {
19+
20+
/**
21+
* Method's invoked when the alert level has changed during the current navigation session.
22+
*
23+
* @param alertLevel One of the alert level constants found in {@link com.mapbox.services.android.Constants}.
24+
* @param routeProgress Provides a {@link RouteProgress} object which will contain information about the users
25+
* current position along the route.
26+
* @see <a href="https://www.mapbox.com/mapbox-java/#alertlevelchange">AlertLevelChange documentation</a>
27+
* @since 2.0.0
28+
*/
1229
void onAlertLevelChange(int alertLevel, RouteProgress routeProgress);
1330
}

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.os.Handler;
55
import android.os.HandlerThread;
66
import android.os.Message;
7+
import android.support.annotation.VisibleForTesting;
78

89
import com.mapbox.services.Experimental;
910
import com.mapbox.services.android.Constants;
@@ -28,7 +29,7 @@
2829
* might change or be removed in minor versions.
2930
*/
3031
@Experimental
31-
public class LocationUpdatedThread extends HandlerThread {
32+
class LocationUpdatedThread extends HandlerThread {
3233

3334
private static final String TAG = "LocationUpdatedThread";
3435
private static final int MESSAGE_LOCATION_UPDATED = 0;
@@ -41,6 +42,8 @@ public class LocationUpdatedThread extends HandlerThread {
4142
private AlertLevelChangeListener alertLevelChangeListener;
4243
private ProgressChangeListener progressChangeListener;
4344
private double userDistanceToManeuverLocation;
45+
private OffRouteListener offRouteListener;
46+
private boolean userStillOnRoute = true;
4447
private boolean snapToRoute;
4548
private Location location;
4649

@@ -64,7 +67,7 @@ void updateLocation(RouteProgress target, Location location) {
6467
requestHandler.obtainMessage(MESSAGE_LOCATION_UPDATED, target).sendToTarget();
6568
}
6669

67-
private void handleRequest(final RouteProgress target, Location location) {
70+
private void handleRequest(final RouteProgress target, final Location location) {
6871
if (location == null) {
6972
return;
7073
}
@@ -85,7 +88,6 @@ private void handleRequest(final RouteProgress target, Location location) {
8588
List<StepIntersection> intersections = getNextIntersections(target, snappedPosition);
8689

8790
// Test the closest intersection to the user only.
88-
boolean userStillOnRoute = true;
8991
if (intersections.size() > 0) {
9092
userStillOnRoute = isUserStillOnRoute(intersections.get(0), location.getBearing());
9193
}
@@ -100,11 +102,19 @@ private void handleRequest(final RouteProgress target, Location location) {
100102
// Post back to the UI Thread.
101103
responseHandler.post(new Runnable() {
102104
public void run() {
105+
if (offRouteListener != null && !userStillOnRoute) {
106+
offRouteListener.userOffRoute(location);
107+
}
108+
103109
if (target.getPreviousAlertLevel() != alertLevel) {
104-
target.setAlertUserLevel(alertLevel);
105-
alertLevelChangeListener.onAlertLevelChange(alertLevel, target);
110+
if (alertLevelChangeListener != null) {
111+
target.setAlertUserLevel(alertLevel);
112+
alertLevelChangeListener.onAlertLevelChange(alertLevel, target);
113+
}
114+
}
115+
if (progressChangeListener != null) {
116+
progressChangeListener.onProgressChange(LocationUpdatedThread.this.location, target);
106117
}
107-
progressChangeListener.onProgressChange(LocationUpdatedThread.this.location, target);
108118
}
109119
});
110120

@@ -121,6 +131,10 @@ void setProgressChangeListener(ProgressChangeListener progressChangeListener) {
121131
this.progressChangeListener = progressChangeListener;
122132
}
123133

134+
void setOffRouteListener(OffRouteListener offRouteListener) {
135+
this.offRouteListener = offRouteListener;
136+
}
137+
124138
void setSnapToRoute(boolean snapToRoute) {
125139
this.snapToRoute = snapToRoute;
126140
}
@@ -209,7 +223,8 @@ private int monitorStepProgress(final RouteProgress routeProgress, Location loca
209223
* @throws TurfException Thrown if turf calculation error occurs.
210224
* @since 2.0.0
211225
*/
212-
private List<StepIntersection> getNextIntersections(RouteProgress routeProgress, Position userPosition)
226+
@VisibleForTesting
227+
public List<StepIntersection> getNextIntersections(RouteProgress routeProgress, Position userPosition)
213228
throws TurfException {
214229
List<StepIntersection> intersectionsWithinRange = new ArrayList<>();
215230
List<StepIntersection> stepIntersections = new ArrayList<>();
@@ -263,6 +278,7 @@ private List<StepIntersection> getNextIntersections(RouteProgress routeProgress,
263278
* @return boolean true if the user remains on the route through the intersection, else false.
264279
* @since 2.0.0
265280
*/
281+
@VisibleForTesting
266282
private boolean isUserStillOnRoute(StepIntersection intersection, double userHeading) {
267283
// We start off assuming the user is on route.
268284
boolean isOnRoute = true;

0 commit comments

Comments
 (0)