-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] add periodic refresh (every 5 minutes) functionality to update …
…traffic and etas on a regular basis when in navigation
- Loading branch information
1 parent
8dd2fae
commit 4c5b556
Showing
9 changed files
with
292 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...on/src/main/java/com/mapbox/services/android/navigation/v5/navigation/RouteRefresher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.mapbox.services.android.navigation.v5.navigation; | ||
|
||
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress; | ||
|
||
import java.util.Date; | ||
|
||
class RouteRefresher { | ||
private final RouteRefresh routeRefresh; | ||
private final MapboxNavigation mapboxNavigation; | ||
private final long refreshIntervalInMilliseconds; | ||
private Date lastRefreshedDate; | ||
private boolean isChecking; | ||
|
||
RouteRefresher(MapboxNavigation mapboxNavigation, RouteRefresh routeRefresh, long refreshIntervalInMilliseconds) { | ||
this.mapboxNavigation = mapboxNavigation; | ||
this.routeRefresh = routeRefresh; | ||
this.refreshIntervalInMilliseconds = refreshIntervalInMilliseconds; | ||
this.lastRefreshedDate = new Date(); | ||
} | ||
|
||
boolean check(Date currentDate) { | ||
if (isChecking) { | ||
return false; | ||
} | ||
long millisSinceLastRefresh = currentDate.getTime() - lastRefreshedDate.getTime(); | ||
return millisSinceLastRefresh > refreshIntervalInMilliseconds; | ||
} | ||
|
||
void refresh(RouteProgress routeProgress) { | ||
updateIsChecking(true); | ||
routeRefresh.refresh(routeProgress, new RouteRefresherCallback(mapboxNavigation, this)); | ||
} | ||
|
||
void updateLastRefresh(Date date) { | ||
lastRefreshedDate = date; | ||
} | ||
|
||
void updateIsChecking(boolean isChecking) { | ||
this.isChecking = isChecking; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ain/java/com/mapbox/services/android/navigation/v5/navigation/RouteRefresherCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.mapbox.services.android.navigation.v5.navigation; | ||
|
||
import com.mapbox.api.directions.v5.models.DirectionsRoute; | ||
|
||
import java.util.Date; | ||
|
||
import timber.log.Timber; | ||
|
||
class RouteRefresherCallback implements RefreshCallback { | ||
private final MapboxNavigation mapboxNavigation; | ||
private final RouteRefresher routeRefresher; | ||
|
||
RouteRefresherCallback(MapboxNavigation mapboxNavigation, RouteRefresher routeRefresher) { | ||
this.mapboxNavigation = mapboxNavigation; | ||
this.routeRefresher = routeRefresher; | ||
} | ||
|
||
@Override | ||
public void onRefresh(DirectionsRoute directionsRoute) { | ||
mapboxNavigation.startNavigation(directionsRoute, DirectionsRouteType.FRESH_ROUTE); | ||
routeRefresher.updateLastRefresh(new Date()); | ||
routeRefresher.updateIsChecking(false); | ||
} | ||
|
||
@Override | ||
public void onError(RefreshError error) { | ||
Timber.w(error.getMessage()); | ||
routeRefresher.updateIsChecking(false); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...java/com/mapbox/services/android/navigation/v5/navigation/RouteRefresherCallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.mapbox.services.android.navigation.v5.navigation; | ||
|
||
import com.mapbox.api.directions.v5.models.DirectionsRoute; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Date; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class RouteRefresherCallbackTest { | ||
|
||
@Test | ||
public void checksStartNavigationWithRefreshedRouteIsCalledWhenOnRefresh() { | ||
MapboxNavigation mockedMapboxNavigation = mock(MapboxNavigation.class); | ||
RouteRefresher mockedRouteRefresher = mock(RouteRefresher.class); | ||
RouteRefresherCallback theRouteRefresherCallback = new RouteRefresherCallback(mockedMapboxNavigation, | ||
mockedRouteRefresher); | ||
DirectionsRoute anyRoute = mock(DirectionsRoute.class); | ||
|
||
theRouteRefresherCallback.onRefresh(anyRoute); | ||
|
||
verify(mockedMapboxNavigation).startNavigation(eq(anyRoute), eq(DirectionsRouteType.FRESH_ROUTE)); | ||
} | ||
|
||
@Test | ||
public void checksUpdateLastRefreshDateIsCalledWhenOnRefresh() { | ||
MapboxNavigation mockedMapboxNavigation = mock(MapboxNavigation.class); | ||
RouteRefresher mockedRouteRefresher = mock(RouteRefresher.class); | ||
RouteRefresherCallback theRouteRefresherCallback = new RouteRefresherCallback(mockedMapboxNavigation, | ||
mockedRouteRefresher); | ||
DirectionsRoute anyRoute = mock(DirectionsRoute.class); | ||
|
||
theRouteRefresherCallback.onRefresh(anyRoute); | ||
|
||
verify(mockedRouteRefresher).updateLastRefresh(any(Date.class)); | ||
} | ||
|
||
@Test | ||
public void checksUpdateIsNotCheckingAfterOnRefresh() { | ||
MapboxNavigation mockedMapboxNavigation = mock(MapboxNavigation.class); | ||
RouteRefresher mockedRouteRefresher = mock(RouteRefresher.class); | ||
RouteRefresherCallback theRouteRefresherCallback = new RouteRefresherCallback(mockedMapboxNavigation, | ||
mockedRouteRefresher); | ||
DirectionsRoute anyRoute = mock(DirectionsRoute.class); | ||
|
||
theRouteRefresherCallback.onRefresh(anyRoute); | ||
|
||
verify(mockedRouteRefresher).updateIsChecking(eq(false)); | ||
} | ||
|
||
@Test | ||
public void checksUpdateIsNotCheckingIfOnError() { | ||
MapboxNavigation mockedMapboxNavigation = mock(MapboxNavigation.class); | ||
RouteRefresher mockedRouteRefresher = mock(RouteRefresher.class); | ||
RouteRefresherCallback theRouteRefresherCallback = new RouteRefresherCallback(mockedMapboxNavigation, | ||
mockedRouteRefresher); | ||
RefreshError anyRefreshError = mock(RefreshError.class); | ||
|
||
theRouteRefresherCallback.onError(anyRefreshError); | ||
|
||
verify(mockedRouteRefresher).updateIsChecking(eq(false)); | ||
} | ||
} |
Oops, something went wrong.