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 13, 2018
1 parent d6a4294 commit f8d8833
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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,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;
}
Expand All @@ -186,7 +196,7 @@ MapboxNavigation initialize(NavigationViewOptions options) {
initializeNavigation(getApplication(), navigationOptions);
addMilestones(options);
}
navigationViewRouteEngine.extractRouteOptions(options);
viewRouteFetcher.extractRouteOptions(options);
return navigation;
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
}
};
Expand Down Expand Up @@ -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);
}
};

Expand Down Expand Up @@ -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<Point> locations = viewRouteFetcher.calculateRemainingCoordinates(event);
navigation.findOfflineRouteFor(location, locations);
} 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 @@ -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;
Expand Down Expand Up @@ -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
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 f8d8833

Please sign in to comment.