Skip to content

Commit

Permalink
Added API to set offline in NavigationView
Browse files Browse the repository at this point in the history
  • Loading branch information
Devota Aabel committed Sep 17, 2018
1 parent 4509e0a commit d3d0156
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import com.mapbox.services.android.navigation.v5.utils.DistanceFormatter;
import com.mapbox.services.android.navigation.v5.utils.LocaleUtils;

import java.util.ArrayList;

/**
* View that creates the drop-in UI.
* <p>
Expand Down Expand Up @@ -415,6 +417,28 @@ public MapboxNavigation retrieveMapboxNavigation() {
return navigationViewModel.retrieveNavigation();
}

/**
* Initializes the offline data used for fetching offline routes.
* <p>
* This method must be called before {@link MapboxNavigation#findOfflineRouteFor(Location, ArrayList)}.
*
* @param tileFilePath path to directory containing tile data
* @param translationsDirPath path to directory containing OSRMTI translations
*/
public void initializeOfflineData(String tileFilePath, String translationsDirPath) {
navigationViewModel.initializeOfflineData(tileFilePath, translationsDirPath);
}

/**
* Sets the NavigationView to use or not use offline data. This call should be followed by a call
* to initializeOfflineData.
*
* @param isOffline whether the map should load offline or not
*/
public void setOffline(boolean isOffline) {
navigationViewModel.setOffline(isOffline);
}

private void initializeView() {
inflate(getContext(), R.layout.navigation_view_layout, this);
bind();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -65,7 +66,7 @@ public class NavigationViewModel extends AndroidViewModel {
final MutableLiveData<Boolean> shouldRecordScreenshot = new MutableLiveData<>();

private MapboxNavigation navigation;
private ViewRouteFetcher navigationViewRouteEngine;
private ViewRouteFetcher viewRouteFetcher;
private LocationEngineConductor locationEngineConductor;
private NavigationViewEventDispatcher navigationViewEventDispatcher;
private SpeechPlayer speechPlayer;
Expand All @@ -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);
Expand Down Expand Up @@ -164,6 +166,22 @@ public MapboxNavigation retrieveNavigation() {
return navigation;
}

/**
* Initializes the offline data used for fetching offline routes.
* <p>
* This method must be called before {@link MapboxNavigation#findOfflineRouteFor(Location, ArrayList)}.
*
* @param tileFilePath path to directory containing tile data
* @param translationsDirPath path to directory containing OSRMTI translations
*/
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;
}
Expand All @@ -186,7 +204,7 @@ MapboxNavigation initialize(NavigationViewOptions options) {
initializeNavigation(getApplication(), navigationOptions);
addMilestones(options);
}
navigationViewRouteEngine.extractRouteOptions(options);
viewRouteFetcher.extractRouteOptions(options);
return navigation;
}

Expand All @@ -210,7 +228,7 @@ private void initializeConnectivityManager(Application application) {
}

private void initializeNavigationRouteEngine() {
navigationViewRouteEngine = new ViewRouteFetcher(getApplication(), accessToken, routeEngineListener);
viewRouteFetcher = new ViewRouteFetcher(getApplication(), accessToken, viewRouteListener);
}

private void initializeNavigationLocationEngine() {
Expand Down Expand Up @@ -301,8 +319,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);
}
}
};
Expand Down Expand Up @@ -331,7 +348,7 @@ public void fasterRouteFound(DirectionsRoute directionsRoute) {
}
};

private ViewRouteListener routeEngineListener = new ViewRouteListener() {
private ViewRouteListener viewRouteListener = new ViewRouteListener() {
@Override
public void onRouteUpdate(DirectionsRoute directionsRoute) {
updateRoute(directionsRoute);
Expand All @@ -354,7 +371,7 @@ public void onDestinationSet(Point destination) {
private LocationEngineConductorListener locationEngineCallback = new LocationEngineConductorListener() {
@Override
public void onLocationUpdate(Location location) {
navigationViewRouteEngine.updateRawLocation(location);
viewRouteFetcher.updateRawLocation(location);
}
};

Expand Down Expand Up @@ -443,11 +460,20 @@ 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<Point> locations = viewRouteFetcher.calculateRemainingCoordinates(event);
DirectionsRoute directionsRoute = navigation.findOfflineRouteFor(location, locations);
updateRoute(directionsRoute);
} else {
viewRouteFetcher.fetchRouteFromOffRouteEvent(event);
}

isOffRoute.setValue(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Point> calculateRemainingCoordinates(OffRouteEvent event) {
if (OffRouteEvent.isValid(event)) {
RouteProgress routeProgress = event.getRouteProgress();
List<Point> list = routeUtils.calculateRemainingWaypoints(routeProgress);
return new ArrayList<>(list);
} else {
return new ArrayList<>();
}
}

/**
* Updates this object's awareness of the raw location
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class RouteFetcher {
private final WeakReference<Context> contextWeakReference;

private RouteProgress routeProgress;
private RouteUtils routeUtils;
protected RouteUtils routeUtils;

public RouteFetcher(Context context, String accessToken) {
this.accessToken = accessToken;
Expand Down

0 comments on commit d3d0156

Please sign in to comment.