Skip to content

Commit

Permalink
Always provide DirectionsRoute in NavigationActivity (#980)
Browse files Browse the repository at this point in the history
* Always provide DirectionsRoute in NavigationActivity

* Add check for empty queue
  • Loading branch information
danesfeder authored May 31, 2018
1 parent 7a3a704 commit 1c808c6
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.v7.app.AppCompatActivity;

import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigationOptions;
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants;
Expand All @@ -19,7 +20,6 @@
public class NavigationActivity extends AppCompatActivity implements OnNavigationReadyCallback, NavigationListener {

private NavigationView navigationView;
private boolean isRunning;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -60,15 +60,13 @@ public void onBackPressed() {
@Override
protected void onSaveInstanceState(Bundle outState) {
navigationView.onSaveInstanceState(outState);
outState.putBoolean(NavigationConstants.NAVIGATION_VIEW_RUNNING, isRunning);
super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
navigationView.onRestoreInstanceState(savedInstanceState);
isRunning = savedInstanceState.getBoolean(NavigationConstants.NAVIGATION_VIEW_RUNNING);
}

@Override
Expand All @@ -94,14 +92,10 @@ public void onNavigationReady() {
MapboxNavigationOptions.Builder navigationOptions = MapboxNavigationOptions.builder();
NavigationViewOptions.Builder options = NavigationViewOptions.builder();
options.navigationListener(this);
if (!isRunning) {
extractRoute(options);
}
extractRoute(options);
extractConfiguration(options, navigationOptions);

options.navigationOptions(navigationOptions.build());
navigationView.startNavigation(options.build());
isRunning = true;
}

@Override
Expand All @@ -122,7 +116,8 @@ public void onNavigationRunning() {
}

private void extractRoute(NavigationViewOptions.Builder options) {
options.directionsRoute(NavigationLauncher.extractRoute(this));
DirectionsRoute route = NavigationLauncher.extractRoute(this);
options.directionsRoute(route);
}

private void extractConfiguration(NavigationViewOptions.Builder options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@
import com.google.gson.GsonBuilder;
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.Point;
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants;

/**
* Use this class to launch the navigation UI
* <p>
* You can launch the UI with either a route you have already retrieved from
* {@link com.mapbox.services.android.navigation.v5.navigation.NavigationRoute} or you can pass a
* {@link Point} origin and {@link Point} destination and the UI will request the {@link DirectionsRoute}
* while initializing.
* </p><p>
* You have an option to include a AWS Cognito Pool ID, which will initialize the UI with AWS Polly Voice instructions
* You can launch the UI a route you have already retrieved from
* {@link com.mapbox.services.android.navigation.v5.navigation.NavigationRoute}.
* </p><p>
* For testing, you can launch with simulation, in which our
* {@link com.mapbox.services.android.navigation.v5.location.MockLocationEngine} will begin
* following the given {@link DirectionsRoute} once the UI is initialized
* following the given {@link DirectionsRoute} once the UI is initialized.
* </p>
*/
public class NavigationLauncher {
Expand All @@ -41,7 +36,7 @@ public static void startNavigation(Activity activity, NavigationLauncherOptions
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
SharedPreferences.Editor editor = preferences.edit();

storeRouteOptions(options, editor);
storeDirectionsRouteValue(options, editor);
storeConfiguration(options, editor);

storeThemePreferences(options, editor);
Expand All @@ -67,13 +62,9 @@ static DirectionsRoute extractRoute(Context context) {
return DirectionsRoute.fromJson(directionsRouteJson);
}

private static void storeRouteOptions(NavigationLauncherOptions options, SharedPreferences.Editor editor) {
if (options.directionsRoute() != null) {
storeDirectionsRouteValue(options, editor);
} else {
throw new RuntimeException("A valid DirectionsRoute or origin and "
+ "destination must be provided in NavigationViewOptions");
}
private static void storeDirectionsRouteValue(NavigationLauncherOptions options, SharedPreferences.Editor editor) {
editor.putString(NavigationConstants.NAVIGATION_VIEW_ROUTE_KEY, new GsonBuilder()
.registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create().toJson(options.directionsRoute()));
}

private static void storeConfiguration(NavigationLauncherOptions options, SharedPreferences.Editor editor) {
Expand All @@ -96,9 +87,4 @@ private static void storeThemePreferences(NavigationLauncherOptions options, Sha
}
}
}

private static void storeDirectionsRouteValue(NavigationLauncherOptions options, SharedPreferences.Editor editor) {
editor.putString(NavigationConstants.NAVIGATION_VIEW_ROUTE_KEY, new GsonBuilder()
.registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create().toJson(options.directionsRoute()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ public void onProgressChange(Location location, RouteProgress routeProgress) {
@Override
public void userOffRoute(Location location) {
if (hasNetworkConnection()) {
instructionPlayer.onOffRoute();
Point newOrigin = Point.fromLngLat(location.getLongitude(), location.getLatitude());
sendEventOffRoute(newOrigin);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ public void updateRawLocation(@NonNull Location rawLocation) {

private void extractRouteFromOptions(NavigationViewOptions options) {
DirectionsRoute route = options.directionsRoute();
if (route != null) {
cacheRouteInformation(options, route);
updateCurrentRoute(route);
}
cacheRouteInformation(options, route);
updateCurrentRoute(route);
}

private void cacheRouteInformation(NavigationViewOptions options, DirectionsRoute route) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void onDestroy() {
@Override
public void onStart() {
requestAudioFocus();
instructionQueue.remove();
instructionQueue.poll();
}

@Override
Expand All @@ -69,7 +69,7 @@ public void onError(boolean isMapboxPlayer) {
if (isMapboxPlayer) {
androidSpeechPlayer.play(instructionQueue.peek().getAnnouncement());
} else {
instructionQueue.remove();
instructionQueue.poll();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,6 @@ private NavigationConstants() {
*/
public static final int NAVIGATION_HIGH_ALERT_DURATION = 15;

/**
* Constant used to store running state in navigation view.
*
* @since 0.10.0
*/
public static final String NAVIGATION_VIEW_RUNNING = "navigation_view_running";

/**
* Default location acceptable accuracy threshold
* used in {@link com.mapbox.services.android.navigation.v5.location.LocationValidator}.
Expand Down

0 comments on commit 1c808c6

Please sign in to comment.