-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add periodic refresh #1855
Merged
Merged
Add periodic refresh #1855
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
43 changes: 43 additions & 0 deletions
43
...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,43 @@ | ||
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 MapboxNavigation mapboxNavigation; | ||
private final RouteRefresh routeRefresh; | ||
private final long refreshIntervalInMilliseconds; | ||
private Date lastRefreshedDate; | ||
private boolean isChecking; | ||
private boolean isRefreshRouteEnabled; | ||
|
||
RouteRefresher(MapboxNavigation mapboxNavigation, RouteRefresh routeRefresh) { | ||
this.mapboxNavigation = mapboxNavigation; | ||
this.routeRefresh = routeRefresh; | ||
this.refreshIntervalInMilliseconds = mapboxNavigation.options().refreshIntervalInMilliseconds(); | ||
this.lastRefreshedDate = new Date(); | ||
this.isRefreshRouteEnabled = mapboxNavigation.options().enableRefreshRoute(); | ||
} | ||
|
||
boolean check(Date currentDate) { | ||
if (isChecking || !isRefreshRouteEnabled) { | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enabling refresh by default