diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java index 9d004a7e165..2f8d2e3acbe 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java @@ -415,6 +415,14 @@ public MapboxNavigation retrieveMapboxNavigation() { return navigationViewModel.retrieveNavigation(); } + public void initializeOfflineData(String tileFilePath, String translationsDirPath) { + navigationViewModel.initializeOfflineData(tileFilePath, translationsDirPath); + } + + public void setOffline(boolean isOffline) { + navigationViewModel.setOffline(isOffline); + } + private void initializeView() { inflate(getContext(), R.layout.navigation_view_layout, this); bind(); diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewModel.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewModel.java index 59253edbaa8..08a4b8414d2 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewModel.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewModel.java @@ -48,6 +48,7 @@ import com.mapbox.services.android.navigation.v5.utils.LocaleUtils; import com.mapbox.services.android.navigation.v5.utils.RouteUtils; +import java.util.ArrayList; import java.util.List; public class NavigationViewModel extends AndroidViewModel { @@ -65,7 +66,7 @@ public class NavigationViewModel extends AndroidViewModel { final MutableLiveData shouldRecordScreenshot = new MutableLiveData<>(); private MapboxNavigation navigation; - private ViewRouteFetcher navigationViewRouteEngine; + private ViewRouteFetcher viewRouteFetcher; private LocationEngineConductor locationEngineConductor; private NavigationViewEventDispatcher navigationViewEventDispatcher; private SpeechPlayer speechPlayer; @@ -81,6 +82,7 @@ public class NavigationViewModel extends AndroidViewModel { private LocaleUtils localeUtils; private String accessToken; private DistanceFormatter distanceFormatter; + private boolean isOffline; public NavigationViewModel(Application application) { super(application); @@ -164,6 +166,14 @@ public MapboxNavigation retrieveNavigation() { return navigation; } + public void initializeOfflineData(String tileFilePath, String translationsDirPath) { + navigation.initializeOfflineData(tileFilePath, translationsDirPath); + } + + public void setOffline(boolean isOffline) { + this.isOffline = isOffline; + } + void initializeEventDispatcher(NavigationViewEventDispatcher navigationViewEventDispatcher) { this.navigationViewEventDispatcher = navigationViewEventDispatcher; } @@ -186,7 +196,7 @@ MapboxNavigation initialize(NavigationViewOptions options) { initializeNavigation(getApplication(), navigationOptions); addMilestones(options); } - navigationViewRouteEngine.extractRouteOptions(options); + viewRouteFetcher.extractRouteOptions(options); return navigation; } @@ -210,7 +220,7 @@ private void initializeConnectivityManager(Application application) { } private void initializeNavigationRouteEngine() { - navigationViewRouteEngine = new ViewRouteFetcher(getApplication(), accessToken, routeEngineListener); + viewRouteFetcher = new ViewRouteFetcher(getApplication(), accessToken, routeEngineListener); } private void initializeNavigationLocationEngine() { @@ -301,8 +311,7 @@ public void onProgressChange(Location location, RouteProgress routeProgress) { public void userOffRoute(Location location) { if (hasNetworkConnection()) { speechPlayer.onOffRoute(); - Point newOrigin = Point.fromLngLat(location.getLongitude(), location.getLatitude()); - handleOffRouteEvent(newOrigin); + handleOffRouteEvent(location); } } }; @@ -354,7 +363,7 @@ public void onDestinationSet(Point destination) { private LocationEngineConductorListener locationEngineCallback = new LocationEngineConductorListener() { @Override public void onLocationUpdate(Location location) { - navigationViewRouteEngine.updateRawLocation(location); + viewRouteFetcher.updateRawLocation(location); } }; @@ -443,11 +452,19 @@ private void sendEventArrival(RouteProgress routeProgress, Milestone milestone) } } - private void handleOffRouteEvent(Point newOrigin) { + private void handleOffRouteEvent(Location location) { + Point newOrigin = Point.fromLngLat(location.getLongitude(), location.getLatitude()); if (navigationViewEventDispatcher != null && navigationViewEventDispatcher.allowRerouteFrom(newOrigin)) { navigationViewEventDispatcher.onOffRoute(newOrigin); OffRouteEvent event = new OffRouteEvent(newOrigin, routeProgress); - navigationViewRouteEngine.fetchRouteFromOffRouteEvent(event); + + if (isOffline) { + ArrayList locations = viewRouteFetcher.calculateRemainingCoordinates(event); + navigation.findOfflineRouteFor(location, locations); + } else { + viewRouteFetcher.fetchRouteFromOffRouteEvent(event); + } + isOffRoute.setValue(true); } } diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/ViewRouteFetcher.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/ViewRouteFetcher.java index 1177b643d1c..25f0624a7ba 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/ViewRouteFetcher.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/ViewRouteFetcher.java @@ -15,6 +15,7 @@ import com.mapbox.services.android.navigation.v5.route.RouteListener; import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress; +import java.util.ArrayList; import java.util.List; public class ViewRouteFetcher extends RouteFetcher implements RouteListener { @@ -66,6 +67,22 @@ public void fetchRouteFromOffRouteEvent(OffRouteEvent event) { } } + /** + * Calculates the remaining coordinates in the given OffRouteEvent + * + * @param event from which the progress is extracted + * @return List of remaining coordinates + */ + public ArrayList calculateRemainingCoordinates(OffRouteEvent event) { + if (OffRouteEvent.isValid(event)) { + RouteProgress routeProgress = event.getRouteProgress(); + List list = routeUtils.calculateRemainingWaypoints(routeProgress); + return new ArrayList<>(list); + } else { + return new ArrayList<>(); + } + } + /** * Updates this object's awareness of the raw location * diff --git a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigation.java b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigation.java index 42f4ee8a610..eeb0389f29e 100644 --- a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigation.java +++ b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigation.java @@ -35,6 +35,7 @@ import com.mapbox.services.android.navigation.v5.route.FasterRouteListener; import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener; import com.mapbox.services.android.navigation.v5.snap.Snap; +import com.mapbox.services.android.navigation.v5.utils.ValidationUtils; import java.util.ArrayList; import java.util.Date; @@ -477,6 +478,10 @@ private FixLocation buildFixLocationFrom(Point point) { ); } + public void setOffline(String tileFilePath, String translationsDirPath) { + initializeOfflineData(tileFilePath, translationsDirPath); + } + /** * Call this when the navigation session needs to end before the user reaches their final * destination. There isn't a need to manually end the navigation session using this API when the diff --git a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/route/RouteFetcher.java b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/route/RouteFetcher.java index 82a61b8dca7..60cfa81e485 100644 --- a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/route/RouteFetcher.java +++ b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/route/RouteFetcher.java @@ -34,7 +34,7 @@ public class RouteFetcher { private final WeakReference contextWeakReference; private RouteProgress routeProgress; - private RouteUtils routeUtils; + protected RouteUtils routeUtils; public RouteFetcher(Context context, String accessToken) { this.accessToken = accessToken;