Skip to content
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

Always provide DirectionsRoute in NavigationActivity #980

Merged
merged 2 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove null check?

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 @@ -77,10 +77,8 @@ public void updateRawLocation(@NonNull Location rawLocation) {

private void extractRouteFromOptions(NavigationViewOptions options) {
DirectionsRoute route = options.directionsRoute();
if (route != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove null check?

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 @@ -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